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.controller.md.sal.dom.store.impl;
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;
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;
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;
47 private static void warnDebugContext(final AbstractDOMStoreTransaction<?> transaction) {
48 final Throwable ctx = transaction.getDebugContext();
50 LOG.warn("Transaction {} has been allocated in the following context", transaction.getIdentifier(), ctx);
55 public final ListenableFuture<Boolean> canCommit() {
56 if (operationError != null) {
57 return Futures.immediateFailedFuture(operationError);
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(),
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(),
72 warnDebugContext(getTransaction());
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);
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);
86 public final ListenableFuture<Void> preCommit() {
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);
97 public final ListenableFuture<Void> abort() {
99 return SUCCESSFUL_FUTURE;
102 protected final SnapshotBackedWriteTransaction<String> getTransaction() {
107 public ListenableFuture<Void> commit() {
108 checkState(candidate != null, "Proposed subtree must be computed");
111 * The commit has to occur atomically with regard to listener
114 store.commit(candidate);
115 return SUCCESSFUL_FUTURE;