Merge "Pass down a collection of cohorts"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / SerializedDOMDataBroker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.controller.md.sal.dom.broker.impl;
8
9 import com.google.common.base.Preconditions;
10 import com.google.common.util.concurrent.CheckedFuture;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.concurrent.RejectedExecutionException;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
23 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Implementation of blocking three phase commit coordinator, which which
29  * supports coordination on multiple {@link DOMStoreThreePhaseCommitCohort}.
30  *
31  * This implementation does not support cancellation of commit,
32  *
33  * In order to advance to next phase of three phase commit all subtasks of
34  * previous step must be finish.
35  *
36  * This executor does not have an upper bound on subtask timeout.
37  */
38 public class SerializedDOMDataBroker extends AbstractDOMDataBroker {
39     private static final Logger LOG = LoggerFactory.getLogger(SerializedDOMDataBroker.class);
40     private final DurationStatisticsTracker commitStatsTracker = DurationStatisticsTracker.createConcurrent();
41     private final ListeningExecutorService executor;
42
43     /**
44      *
45      * Construct DOMDataCommitCoordinator which uses supplied executor to
46      * process commit coordinations.
47      *
48      * @param executor
49      */
50     public SerializedDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores, final ListeningExecutorService executor) {
51         super(datastores);
52         this.executor = Preconditions.checkNotNull(executor, "executor must not be null.");
53     }
54
55     public DurationStatisticsTracker getCommitStatsTracker() {
56         return commitStatsTracker;
57     }
58
59     @Override
60     protected CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
61             final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
62         Preconditions.checkArgument(transaction != null, "Transaction must not be null.");
63         Preconditions.checkArgument(cohorts != null, "Cohorts must not be null.");
64         LOG.debug("Tx: {} is submitted for execution.", transaction.getIdentifier());
65
66         ListenableFuture<Void> commitFuture = null;
67         try {
68             commitFuture = executor.submit(new CommitCoordinationTask(transaction, cohorts,
69                     commitStatsTracker));
70         } catch(RejectedExecutionException e) {
71             LOG.error("The commit executor's queue is full - submit task was rejected. \n" +
72                       executor, e);
73             return Futures.immediateFailedCheckedFuture(
74                     new TransactionCommitFailedException(
75                         "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
76         }
77
78         return MappingCheckedFuture.create(commitFuture,
79                 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
80     }
81 }