Delay snapshot backed transaction ready error
[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
13 import com.google.common.base.Preconditions;
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;
27
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;
37
38     InMemoryDOMStoreThreePhaseCommitCohort(final InMemoryDOMDataStore store,
39                                            final SnapshotBackedWriteTransaction<String> writeTransaction,
40                                            final DataTreeModification modification,
41                                            final Exception operationError) {
42         this.transaction = Preconditions.checkNotNull(writeTransaction);
43         this.modification = Preconditions.checkNotNull(modification);
44         this.store = Preconditions.checkNotNull(store);
45         this.operationError = operationError;
46     }
47
48     private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
49         final Throwable ctx = transaction.getDebugContext();
50         if (ctx != null) {
51             LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
52         }
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     @Override
57     public final ListenableFuture<Boolean> canCommit() {
58         if (operationError != null) {
59             return Futures.immediateFailedFuture(operationError);
60         }
61
62         try {
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(),
68                     e.getPath());
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(),
73                     e.getPath(), e);
74             warnDebugContext(getTransaction());
75
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(),
79                     modification, store);
80
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);
86         }
87     }
88
89     @SuppressWarnings("checkstyle:IllegalCatch")
90     @Override
91     public final ListenableFuture<Void> preCommit() {
92         try {
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);
98         }
99     }
100
101     @Override
102     public final ListenableFuture<Void> abort() {
103         candidate = null;
104         return SUCCESSFUL_FUTURE;
105     }
106
107     protected final SnapshotBackedWriteTransaction<String> getTransaction() {
108         return transaction;
109     }
110
111     @Override
112     public ListenableFuture<Void> commit() {
113         checkState(candidate != null, "Proposed subtree must be computed");
114
115         /*
116          * The commit has to occur atomically with regard to listener
117          * registrations.
118          */
119         store.commit(candidate);
120         return SUCCESSFUL_FUTURE;
121     }
122 }
123