checkStyleViolationSeverity=error implemented for mdsal-dom-broker
[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 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.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
23 import org.opendaylight.mdsal.dom.spi.store.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  *<p>
34  * This implementation does not support cancellation of commit,
35  *
36  *<p>
37  * In order to advance to next phase of three phase commit all subtasks of
38  * previous step must be finish.
39  *
40  *<p>
41  * This executor does not have an upper bound on subtask timeout.
42  */
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 the Map of backing DOMStore instances
53      * @param executor the ListeningExecutorService to use
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 CheckedFuture<Void,TransactionCommitFailedException> submit(
67             final DOMDataTreeWriteTransaction transaction,
68             final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
69         Preconditions.checkArgument(transaction != null, "Transaction must not be null.");
70         Preconditions.checkArgument(cohorts != null, "Cohorts must not be null.");
71         LOG.debug("Tx: {} is submitted for execution.", transaction.getIdentifier());
72
73         ListenableFuture<Void> commitFuture = null;
74         try {
75             commitFuture = executor.submit(new CommitCoordinationTask(transaction, cohorts,
76                     commitStatsTracker));
77         } catch (RejectedExecutionException e) {
78             LOG.error("The commit executor's queue is full - submit task was rejected. \n"
79                     + executor, e);
80             return Futures.immediateFailedCheckedFuture(
81                     new TransactionCommitFailedException(
82                         "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
83         }
84
85         return MappingCheckedFuture.create(commitFuture,
86                 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
87     }
88 }