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