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