import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction.TransactionClosePrototype;
import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
*/
@Beta
public abstract class AbstractSnapshotBackedTransactionChain<T> extends TransactionReadyPrototype<T>
- implements DOMStoreTransactionChain {
+ implements DOMStoreTransactionChain, TransactionClosePrototype<T> {
private abstract static class State {
/**
* Allocate a new snapshot.
protected DOMStoreReadTransaction newReadOnlyTransaction(T transactionId) {
final Entry<State, DataTreeSnapshot> entry = getSnapshot(transactionId);
- return SnapshotBackedTransactions.newReadTransaction(transactionId, getDebugTransactions(), entry.getValue());
+ return SnapshotBackedTransactions.newReadTransaction(transactionId, getDebugTransactions(), entry.getValue(),
+ this);
+ }
+
+ @Override
+ public void transactionClosed(final SnapshotBackedReadTransaction<T> tx) {
+ // Defaults to no-op
}
@Override
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
public final class SnapshotBackedReadTransaction<T> extends AbstractDOMStoreTransaction<T>
implements DOMStoreReadTransaction {
private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
+
+ @SuppressWarnings("rawtypes")
+ private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
+ "stableSnapshot");
+
+ // Guarded by stableSnapshot CAS, hence it does not need to be volatile
+ private TransactionClosePrototype<T> closeImpl;
private volatile DataTreeSnapshot stableSnapshot;
/**
* @param debug Enable transaction debugging
* @param snapshot Snapshot which will be modified.
*/
- SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
+ SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
+ final TransactionClosePrototype<T> closeImpl) {
super(identifier, debug);
this.stableSnapshot = Preconditions.checkNotNull(snapshot);
+ this.closeImpl = closeImpl;
LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
}
@Override
public void close() {
+ final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
+ if (prev == null) {
+ LOG.debug("Store transaction: {} : previously closed", getIdentifier());
+ return;
+ }
+
LOG.debug("Store transaction: {} : Closed", getIdentifier());
- stableSnapshot = null;
+ if (closeImpl != null) {
+ closeImpl.transactionClosed(this);
+ closeImpl = null;
+ }
}
@Override
return Futures.immediateFailedCheckedFuture(e);
}
}
+
+ /**
+ * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
+ *
+ * <p>
+ * This class is intended to be implemented by Transaction factories responsible for allocation
+ * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
+ * providing underlying logic for applying implementation.
+ *
+ * @param <T> identifier type
+ */
+ public interface TransactionClosePrototype<T> {
+ /**
+ * Called when a transaction is closed. This is not invoked at most once for every transaction.
+ *
+ * @param tx Transaction which got closed.
+ */
+ void transactionClosed(SnapshotBackedReadTransaction<T> tx);
+ }
}
*/
package org.opendaylight.controller.sal.core.spi.data;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
+import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction.TransactionClosePrototype;
import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
* @param identifier Transaction Identifier
* @param debug Enable transaction debugging
* @param snapshot Snapshot which will be modified.
+ * @return A new read-only transaction
*/
public static <T> SnapshotBackedReadTransaction<T> newReadTransaction(final T identifier, final boolean debug,
final DataTreeSnapshot snapshot) {
- return new SnapshotBackedReadTransaction<>(identifier, debug, snapshot);
+ return new SnapshotBackedReadTransaction<>(identifier, debug, snapshot, null);
+ }
+
+ /**
+ * Creates a new read-only transaction.
+ *
+ * @param identifier Transaction Identifier
+ * @param debug Enable transaction debugging
+ * @param snapshot Snapshot which will be modified.
+ * @param closeImpl Implementation of close method
+ * @return A new read-only transaction
+ */
+ public static <T> SnapshotBackedReadTransaction<T> newReadTransaction(final T identifier,
+ final boolean debug, final DataTreeSnapshot snapshot, final TransactionClosePrototype<T> closeImpl) {
+ return new SnapshotBackedReadTransaction<>(identifier, debug, snapshot, requireNonNull(closeImpl));
}
/**
* @param debug Enable transaction debugging
* @param snapshot Snapshot which will be modified.
* @param readyImpl Implementation of ready method.
+ * @return A new read-write transaction
*/
public static <T> SnapshotBackedReadWriteTransaction<T> newReadWriteTransaction(final T identifier,
final boolean debug, final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {
* @param debug Enable transaction debugging
* @param snapshot Snapshot which will be modified.
* @param readyImpl Implementation of ready method.
+ * @return A new write transaction
*/
public static <T> SnapshotBackedWriteTransaction<T> newWriteTransaction(final T identifier, final boolean debug,
final DataTreeSnapshot snapshot, final TransactionReadyPrototype<T> readyImpl) {