854ccfaaebd4179a1a2e35cf13ea6e3d66675e63
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / SerializedDOMDataBroker.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.mdsal.dom.broker;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Map;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.RejectedExecutionException;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.broker.CommitCoordinationTask.WithTracker;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
26 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Implementation of blocking three phase commit coordinator, which which supports coordination on multiple
32  * {@link DOMStoreThreePhaseCommitCohort}. This implementation does not support cancellation of commit.
33  *
34  * <p>
35  * In order to advance to next phase of three phase commit all subtasks of previous step must be finish.
36  *
37  * <p>
38  * This executor does not have an upper bound on subtask timeout.
39  */
40 public final class SerializedDOMDataBroker extends AbstractDOMDataBroker {
41     private static final Logger LOG = LoggerFactory.getLogger(SerializedDOMDataBroker.class);
42
43     private final DurationStatisticsTracker commitStatsTracker = DurationStatisticsTracker.createConcurrent();
44     private final Executor executor;
45
46     /**
47      * Construct DOMDataCommitCoordinator which uses supplied executor to
48      * process commit coordinations.
49      *
50      * @param datastores the Map of backing DOMStore instances
51      * @param executor the Executor to use
52      */
53     public SerializedDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores, final Executor executor) {
54         super(datastores);
55         this.executor = requireNonNull(executor, "executor must not be null.");
56     }
57
58     public DurationStatisticsTracker getCommitStatsTracker() {
59         return commitStatsTracker;
60     }
61
62     @Override
63     protected FluentFuture<CommitInfo> commit(final DOMDataTreeWriteTransaction transaction,
64             final DOMStoreThreePhaseCommitCohort cohort) {
65         LOG.debug("Tx: {} is submitted for execution.", transaction.getIdentifier());
66
67         final ListenableFuture<CommitInfo> future;
68         try {
69             // FIXME: use FluentFutures.submit() once it is available
70             future = Futures.submit(new WithTracker(transaction, cohort, commitStatsTracker), executor);
71         } catch (RejectedExecutionException e) {
72             LOG.error("The commit executor's queue is full - submit task was rejected. \n{}", executor, e);
73             return FluentFutures.immediateFailedFluentFuture(new TransactionCommitFailedException(
74                 "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
75         }
76
77         return FluentFuture.from(future);
78     }
79 }