1d5912cc6a23b917f1844026f650efb1237e56d8
[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  *
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.controller.md.sal.dom.broker.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Supplier;
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.Collection;
17 import java.util.Map;
18 import java.util.concurrent.RejectedExecutionException;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Implementation of blocking three phase commit coordinator, which which
30  * supports coordination on multiple {@link DOMStoreThreePhaseCommitCohort}.
31  *
32  * <p>
33  * 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
37  * previous step must be finish.
38  *
39  * <p>
40  * This executor does not have an upper bound on subtask timeout.
41  */
42 @Deprecated
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 data stores
53      * @param executor executor service
54      */
55     public SerializedDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores,
56                                    final ListeningExecutorService executor) {
57         super(datastores);
58         this.executor = Preconditions.checkNotNull(executor, "executor must not be null.");
59     }
60
61     public DurationStatisticsTracker getCommitStatsTracker() {
62         return commitStatsTracker;
63     }
64
65     @Override
66     protected <T> ListenableFuture<T> commit(final DOMDataWriteTransaction transaction,
67             final Collection<DOMStoreThreePhaseCommitCohort> cohorts, final Supplier<T> futureValueSupplier) {
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         ListenableFuture<T> commitFuture;
73         try {
74             commitFuture = executor.submit(new CommitCoordinationTask<>(transaction, cohorts, commitStatsTracker,
75                     futureValueSupplier));
76         } catch (RejectedExecutionException e) {
77             LOG.error("The commit executor {} queue is full - submit task was rejected. \n", executor, e);
78             commitFuture = Futures.immediateFailedFuture(new TransactionCommitFailedException(
79                     "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
80         }
81
82         return commitFuture;
83     }
84 }