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