Deprecated controller fine-grained sharding APIs
[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.util.concurrent.CheckedFuture;
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.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
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  * This implementation does not support cancellation of commit,
34  *
35  * In order to advance to next phase of three phase commit all subtasks of
36  * previous step must be finish.
37  *
38  * This executor does not have an upper bound on subtask timeout.
39  */
40 public class SerializedDOMDataBroker extends AbstractDOMDataBroker {
41     private static final Logger LOG = LoggerFactory.getLogger(SerializedDOMDataBroker.class);
42     private final DurationStatisticsTracker commitStatsTracker = DurationStatisticsTracker.createConcurrent();
43     private final ListeningExecutorService executor;
44
45     /**
46      *
47      * Construct DOMDataCommitCoordinator which uses supplied executor to
48      * process commit coordinations.
49      *
50      * @param executor
51      */
52     public SerializedDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores, final ListeningExecutorService executor) {
53         super(datastores);
54         this.executor = Preconditions.checkNotNull(executor, "executor must not be null.");
55     }
56
57     public DurationStatisticsTracker getCommitStatsTracker() {
58         return commitStatsTracker;
59     }
60
61     @Override
62     protected CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
63             final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
64         Preconditions.checkArgument(transaction != null, "Transaction must not be null.");
65         Preconditions.checkArgument(cohorts != null, "Cohorts must not be null.");
66         LOG.debug("Tx: {} is submitted for execution.", transaction.getIdentifier());
67
68         ListenableFuture<Void> commitFuture = null;
69         try {
70             commitFuture = executor.submit(new CommitCoordinationTask(transaction, cohorts,
71                     commitStatsTracker));
72         } catch(RejectedExecutionException e) {
73             LOG.error("The commit executor's queue is full - submit task was rejected. \n" +
74                       executor, e);
75             return Futures.immediateFailedCheckedFuture(
76                     new TransactionCommitFailedException(
77                         "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
78         }
79
80         return MappingCheckedFuture.create(commitFuture,
81                 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
82     }
83 }