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
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
24 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class);
25 private static final ListenableFuture<Boolean> TRUE_FUTURE = Futures.immediateFuture(Boolean.TRUE);
26 private static final ListenableFuture<Void> VOID_FUTURE = Futures.immediateFuture(null);
27 private final DataTreeModification transaction;
28 private final ShardDataTree dataTree;
29 private final String transactionId;
30 private DataTreeCandidateTip candidate;
32 SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction,
33 final String transactionId) {
34 this.dataTree = Preconditions.checkNotNull(dataTree);
35 this.transaction = Preconditions.checkNotNull(transaction);
36 this.transactionId = transactionId;
40 DataTreeCandidateTip getCandidate() {
45 public ListenableFuture<Boolean> canCommit() {
46 DataTreeModification modification = getDataTreeModification();
48 dataTree.getDataTree().validate(modification);
49 LOG.trace("Transaction {} validated", transaction);
52 catch (ConflictingModificationAppliedException e) {
53 LOG.warn("Store Tx {}: Conflicting modification for path {}.", transactionId, e.getPath());
54 return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
55 } catch (DataValidationFailedException e) {
56 LOG.warn("Store Tx {}: Data validation failed for path {}.", transactionId, e.getPath(), e);
58 // For debugging purposes, allow dumping of the modification. Coupled with the above
59 // precondition log, it should allow us to understand what went on.
60 LOG.debug("Store Tx {}: modifications: {} tree: {}", transactionId, modification, dataTree.getDataTree());
62 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
63 } catch (Exception e) {
64 LOG.warn("Unexpected failure in validation phase", e);
65 return Futures.immediateFailedFuture(e);
70 public ListenableFuture<Void> preCommit() {
72 candidate = dataTree.getDataTree().prepare(getDataTreeModification());
74 * FIXME: this is the place where we should be interacting with persistence, specifically by invoking
75 * persist on the candidate (which gives us a Future).
77 LOG.trace("Transaction {} prepared candidate {}", transaction, candidate);
79 } catch (Exception e) {
80 if(LOG.isTraceEnabled()) {
81 LOG.trace("Transaction {} failed to prepare", transaction, e);
83 LOG.error("Transaction failed to prepare", e);
85 return Futures.immediateFailedFuture(e);
90 DataTreeModification getDataTreeModification() {
91 DataTreeModification dataTreeModification = transaction;
92 if(transaction instanceof PruningDataTreeModification){
93 dataTreeModification = ((PruningDataTreeModification) transaction).getResultingModification();
95 return dataTreeModification;
99 public ListenableFuture<Void> abort() {
105 public ListenableFuture<Void> commit() {
107 dataTree.getDataTree().commit(candidate);
108 } catch (Exception e) {
109 if(LOG.isTraceEnabled()) {
110 LOG.trace("Transaction {} failed to commit", transaction, e);
112 LOG.error("Transaction failed to commit", e);
114 return Futures.immediateFailedFuture(e);
117 LOG.trace("Transaction {} committed, proceeding to notify", transaction);
118 dataTree.notifyListeners(candidate);