X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2FDOMBrokerWriteOnlyTransaction.java;h=65f11c227401d6ef49cd69df2482f1e93cc51ab9;hp=6d00210629a9392524f6b56bbdb2260c4c123bd3;hb=28e9832cc97a345d5ceb69262784e5c8fef77e37;hpb=2b2517144e4eb9c17d9b41e9d9ec20d0264f5e12 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerWriteOnlyTransaction.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerWriteOnlyTransaction.java index 6d00210629..65f11c2274 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerWriteOnlyTransaction.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerWriteOnlyTransaction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015 Huawei Technologies Co. Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -8,134 +8,28 @@ package org.opendaylight.controller.cluster.databroker; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.CheckedFuture; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import java.util.ArrayList; -import java.util.Collection; import java.util.Map; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; -import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction; -import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; +import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory; import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class DOMBrokerWriteOnlyTransaction - extends AbstractDOMBrokerTransaction implements DOMDataWriteTransaction { - - private static final AtomicReferenceFieldUpdater IMPL_UPDATER = - AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl"); - @SuppressWarnings("rawtypes") - private static final AtomicReferenceFieldUpdater FUTURE_UPDATER = - AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, Future.class, "commitFuture"); - private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerWriteOnlyTransaction.class); - private static final Future CANCELLED_FUTURE = Futures.immediateCancelledFuture(); - - /** - * Implementation of real commit. It also acts as an indication that - * the transaction is running -- which we flip atomically using - * {@link #IMPL_UPDATER}. - */ - private volatile AbstractDOMTransactionFactory commitImpl; +public class DOMBrokerWriteOnlyTransaction extends AbstractDOMBrokerWriteTransaction { /** - * Future task of transaction commit. It starts off as null, but is - * set appropriately on {@link #submit()} and {@link #cancel()} via - * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}. + * Constructs an instance. * - * Lazy set is safe for use because it is only referenced to in the - * {@link #cancel()} slow path, where we will busy-wait for it. The - * fast path gets the benefit of a store-store barrier instead of the - * usual store-load barrier. + * @param identifier identifier of transaction. + * @param storeTxFactories the backing transaction store factories */ - private volatile Future commitFuture; - - protected DOMBrokerWriteOnlyTransaction(final Object identifier, - Map storeTxFactories, final AbstractDOMTransactionFactory commitImpl) { - super(identifier, storeTxFactories); - this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null."); - } - - @Override - protected T createTransaction(LogicalDatastoreType key) { - // FIXME : Casting shouldn't be necessary here - return (T) getTxFactory(key).newWriteOnlyTransaction(); - } - - @Override - public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) { - checkRunning(commitImpl); - getSubtransaction(store).write(path, data); - } - - @Override - public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) { - checkRunning(commitImpl); - getSubtransaction(store).delete(path); - } - - @Override - public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) { - checkRunning(commitImpl); - getSubtransaction(store).merge(path, data); - } - - @Override - public boolean cancel() { - final AbstractDOMTransactionFactory impl = IMPL_UPDATER.getAndSet(this, null); - if (impl != null) { - LOG.trace("Transaction {} cancelled before submit", getIdentifier()); - FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE); - closeSubtransactions(); - return true; - } - - // The transaction is in process of being submitted or cancelled. Busy-wait - // for the corresponding future. - Future future; - do { - future = commitFuture; - } while (future == null); - - return future.cancel(false); + public DOMBrokerWriteOnlyTransaction(Object identifier, + Map storeTxFactories, + AbstractDOMTransactionFactory commitImpl) { + super(identifier, storeTxFactories, commitImpl); } - @Deprecated @Override - public ListenableFuture> commit() { - return AbstractDataTransaction.convertToLegacyCommitFuture(submit()); - } - - @Override - public CheckedFuture submit() { - final AbstractDOMTransactionFactory impl = IMPL_UPDATER.getAndSet(this, null); - checkRunning(impl); - - final Collection txns = getSubtransactions(); - final Collection cohorts = new ArrayList<>(txns.size()); - - // FIXME: deal with errors thrown by backed (ready and submit can fail in theory) - for (DOMStoreWriteTransaction txn : txns) { - cohorts.add(txn.ready()); - } - - final CheckedFuture ret = impl.submit(this, cohorts); - FUTURE_UPDATER.lazySet(this, ret); - return ret; - } - - private void checkRunning(final AbstractDOMTransactionFactory impl) { - Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier()); + protected DOMStoreWriteTransaction createTransaction(LogicalDatastoreType key) { + return getTxFactory(key).newWriteOnlyTransaction(); } }