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