X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2FAbstractDOMBrokerTransaction.java;h=f75707ccbf4082876e4802cc81a5a9abaa720f9c;hb=18991f44b807ab6f06fcec76216b7f70b900b0f4;hp=98fea88f632285b3e7d5d6f1f900d6b716d3bb89;hpb=6fdcecdc2ecc9f8489de7c72f0709d28b6a1da87;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerTransaction.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerTransaction.java index 98fea88f63..f75707ccbf 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerTransaction.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerTransaction.java @@ -5,103 +5,125 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.databroker; -import com.google.common.base.Preconditions; -import java.util.Collection; -import java.util.EnumMap; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.VarHandle; import java.util.Map; -import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; - -public abstract class AbstractDOMBrokerTransaction implements - AsyncTransaction> { - - private Map backingTxs; - private final Object identifier; +import java.util.Map.Entry; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.mdsal.common.api.TransactionDatastoreMismatchException; +import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionFactory; + +public abstract class AbstractDOMBrokerTransaction implements DOMDataTreeTransaction { + + private static final VarHandle BACKING_TX; + + static { + try { + BACKING_TX = MethodHandles.lookup() + .findVarHandle(AbstractDOMBrokerTransaction.class, "backingTx", Entry.class); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new ExceptionInInitializerError(e); + } + } + + private final @NonNull Object identifier; private final Map storeTxFactories; + private volatile Entry backingTx; + /** + * Creates new transaction. * - * Creates new composite Transactions. - * - * @param identifier - * Identifier of transaction. + * @param identifier Identifier of transaction. */ - protected AbstractDOMBrokerTransaction(final Object identifier, Map storeTxFactories) { - this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null"); - this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories, "Store Transaction Factories should not be null"); - this.backingTxs = new EnumMap(LogicalDatastoreType.class); + protected AbstractDOMBrokerTransaction(final Object identifier, + Map storeTxFactories) { + this.identifier = requireNonNull(identifier, "Identifier should not be null"); + this.storeTxFactories = requireNonNull(storeTxFactories, "Store Transaction Factories should not be null"); + checkArgument(!storeTxFactories.isEmpty(), "Store Transaction Factories should not be empty"); } /** - * Returns subtransaction associated with supplied key. + * Returns sub-transaction associated with supplied key. * - * @param key - * @return - * @throws NullPointerException - * if key is null - * @throws IllegalArgumentException - * if no subtransaction is associated with key. + * @param datastoreType the data store type + * @return the sub-transaction + * @throws NullPointerException if datastoreType is null + * @throws IllegalArgumentException if no sub-transaction is associated with datastoreType. + * @throws TransactionDatastoreMismatchException if datastoreType mismatches the one used at first access */ - protected final T getSubtransaction(final K key) { - Preconditions.checkNotNull(key, "key must not be null."); + protected final T getSubtransaction(final LogicalDatastoreType datastoreType) { + requireNonNull(datastoreType, "datastoreType must not be null."); - T ret = backingTxs.get(key); - if(ret == null){ - ret = createTransaction(key); - backingTxs.put(key, ret); + var entry = backingTx; + if (entry == null) { + if (!storeTxFactories.containsKey(datastoreType)) { + throw new IllegalArgumentException(datastoreType + " is not supported"); + } + final var tx = createTransaction(datastoreType); + final var newEntry = Map.entry(datastoreType, tx); + final var witness = (Entry) BACKING_TX.compareAndExchange(this, null, newEntry); + if (witness != null) { + tx.close(); + entry = witness; + } else { + entry = newEntry; + } } - Preconditions.checkArgument(ret != null, "No subtransaction associated with %s", key); - return ret; - } - protected abstract T createTransaction(final K key); + final var expected = entry.getKey(); + if (expected != datastoreType) { + throw new TransactionDatastoreMismatchException(expected, datastoreType); + } + return entry.getValue(); + } /** - * Returns immutable Iterable of all subtransactions. - * + * Returns sub-transaction if initialized. */ - protected Collection getSubtransactions() { - return backingTxs.values(); + protected T getSubtransaction() { + final Entry entry; + return (entry = backingTx) == null ? null : entry.getValue(); } + protected abstract T createTransaction(LogicalDatastoreType datastoreType); + @Override public Object getIdentifier() { return identifier; } - protected void closeSubtransactions() { - /* - * We share one exception for all failures, which are added - * as supressedExceptions to it. - */ - IllegalStateException failure = null; - for (T subtransaction : backingTxs.values()) { + @SuppressWarnings("checkstyle:IllegalCatch") + protected void closeSubtransaction() { + if (backingTx != null) { try { - subtransaction.close(); + backingTx.getValue().close(); } catch (Exception e) { - // If we did not allocated failure we allocate it - if (failure == null) { - failure = new IllegalStateException("Uncaught exception occured during closing transaction", e); - } else { - // We update it with additional exceptions, which occurred during error. - failure.addSuppressed(e); - } + throw new IllegalStateException("Uncaught exception occurred during closing transaction", e); } } - // If we have failure, we throw it at after all attempts to close. - if (failure != null) { - throw failure; - } } - protected DOMStoreTransactionFactory getTxFactory(K type){ + protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) { return storeTxFactories.get(type); } + + @Override + public final String toString() { + return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString(); + } + + protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { + return toStringHelper.add("identifier", identifier); + } }