Update DOMStoreThreePhaseCommitCohort design
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMStoreThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
19 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
20 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
23 import org.opendaylight.yangtools.yang.common.Empty;
24 import org.opendaylight.yangtools.yang.data.tree.api.ConflictingModificationAppliedException;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 class InMemoryDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
32     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreThreePhaseCommitCohort.class);
33     private static final ListenableFuture<Empty> SUCCESSFUL_FUTURE = Futures.immediateFuture(Empty.value());
34     private static final ListenableFuture<Boolean> CAN_COMMIT_FUTURE = Futures.immediateFuture(Boolean.TRUE);
35
36     private final SnapshotBackedWriteTransaction<String> transaction;
37     private final DataTreeModification modification;
38     private final InMemoryDOMDataStore store;
39     private final Exception operationError;
40
41     @VisibleForTesting
42     DataTreeCandidate candidate;
43
44     InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
45             final SnapshotBackedWriteTransaction<String> transaction, final DataTreeModification modification,
46             final Exception operationError) {
47         this.transaction = requireNonNull(transaction);
48         this.modification = requireNonNull(modification);
49         this.store = requireNonNull(store);
50         this.operationError = operationError;
51     }
52
53     private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
54         final Throwable ctx = transaction.getDebugContext();
55         if (ctx != null) {
56             LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
57         }
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     @Override
62     public final ListenableFuture<Boolean> canCommit() {
63         if (operationError != null) {
64             return Futures.immediateFailedFuture(operationError);
65         }
66
67         try {
68             store.validate(modification);
69             LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
70             return CAN_COMMIT_FUTURE;
71         } catch (ConflictingModificationAppliedException e) {
72             LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
73                     e.getPath());
74             warnDebugContext(getTransaction());
75             return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
76         } catch (DataValidationFailedException e) {
77             LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(),
78                     e.getPath(), e);
79             warnDebugContext(getTransaction());
80
81             // For debugging purposes, allow dumping of the modification. Coupled with the above
82             // precondition log, it should allow us to understand what went on.
83             LOG.trace("Store Tx: {} modifications: {} tree: {}", getTransaction().getIdentifier(),
84                     modification, store);
85
86             return Futures.immediateFailedFuture(
87                     new TransactionCommitFailedException("Data did not pass validation.", e));
88         } catch (Exception e) {
89             LOG.warn("Unexpected failure in validation phase", e);
90             return Futures.immediateFailedFuture(e);
91         }
92     }
93
94     @SuppressWarnings("checkstyle:IllegalCatch")
95     @Override
96     public final ListenableFuture<Empty> preCommit() {
97         try {
98             candidate = store.prepare(modification);
99             return SUCCESSFUL_FUTURE;
100         } catch (Exception e) {
101             LOG.warn("Unexpected failure in pre-commit phase", e);
102             return Futures.immediateFailedFuture(e);
103         }
104     }
105
106     @Override
107     public final ListenableFuture<Empty> abort() {
108         candidate = null;
109         return SUCCESSFUL_FUTURE;
110     }
111
112     protected final SnapshotBackedWriteTransaction<String> getTransaction() {
113         return transaction;
114     }
115
116     @Override
117     public ListenableFuture<CommitInfo> commit() {
118         checkState(candidate != null, "Proposed subtree must be computed");
119
120         // The commit has to occur atomically with regard to listener registrations.
121         store.commit(candidate);
122         return CommitInfo.emptyFluentFuture();
123     }
124 }
125