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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.