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