2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.dom.store.inmemory;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
17 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
18 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTransaction;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
29 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
30 private static final ListenableFuture<Void> SUCCESSFUL_FUTURE = Futures.immediateFuture(null);
31 private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
32 private final SnapshotBackedWriteTransaction<String> transaction;
33 private final DataTreeModification modification;
34 private final InMemoryDOMDataStore store;
35 private DataTreeCandidate candidate;
36 private final Exception operationError;
38 InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
39 final SnapshotBackedWriteTransaction<String> writeTransaction,
40 final DataTreeModification modification,
41 final Exception operationError) {
42 this.transaction = requireNonNull(writeTransaction);
43 this.modification = requireNonNull(modification);
44 this.store = requireNonNull(store);
45 this.operationError = operationError;
48 private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
49 final Throwable ctx = transaction.getDebugContext();
51 LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
55 @SuppressWarnings("checkstyle:IllegalCatch")
57 public final ListenableFuture<Boolean> canCommit() {
58 if (operationError != null) {
59 return Futures.immediateFailedFuture(operationError);
63 store.validate(modification);
64 LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
65 return CAN_COMMIT_FUTURE;
66 } catch (ConflictingModificationAppliedException e) {
67 LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
69 warnDebugContext(getTransaction());
70 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
71 } catch (DataValidationFailedException e) {
72 LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
74 warnDebugContext(getTransaction());
76 // For debugging purposes, allow dumping of the modification. Coupled with the above
77 // precondition log, it should allow us to understand what went on.
78 LOG.trace("Store Tx: {} modifications: {} tree: {}", getTransaction().getIdentifier(),
81 return Futures.immediateFailedFuture(
82 new TransactionCommitFailedException("Data did not pass validation.", e));
83 } catch (Exception e) {
84 LOG.warn("Unexpected failure in validation phase", e);
85 return Futures.immediateFailedFuture(e);
89 @SuppressWarnings("checkstyle:IllegalCatch")
91 public final ListenableFuture<Void> preCommit() {
93 candidate = store.prepare(modification);
94 return SUCCESSFUL_FUTURE;
95 } catch (Exception e) {
96 LOG.warn("Unexpected failure in pre-commit phase", e);
97 return Futures.immediateFailedFuture(e);
102 public final ListenableFuture<Void> abort() {
104 return SUCCESSFUL_FUTURE;
107 protected final SnapshotBackedWriteTransaction<String> getTransaction() {
112 public ListenableFuture<Void> commit() {
113 checkState(candidate != null, "Proposed subtree must be computed");
116 * The commit has to occur atomically with regard to listener
119 store.commit(candidate);
120 return SUCCESSFUL_FUTURE;