BUG-650: Split out CommitCoordinationTask
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMDataCommitCoordinatorImpl.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.concurrent.RejectedExecutionException;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
19 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  *
25  * Implementation of blocking three phase commit coordinator, which which
26  * supports coordination on multiple {@link DOMStoreThreePhaseCommitCohort}.
27  *
28  * This implementation does not support cancelation of commit,
29  *
30  * In order to advance to next phase of three phase commit all subtasks of
31  * previous step must be finish.
32  *
33  * This executor does not have an upper bound on subtask timeout.
34  *
35  *
36  */
37 public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
38
39     private static final Logger LOG = LoggerFactory.getLogger(DOMDataCommitCoordinatorImpl.class);
40     private final DurationStatisticsTracker commitStatsTracker = DurationStatisticsTracker.createConcurrent();
41     private final ListeningExecutorService executor;
42
43     /**
44      *
45      * Construct DOMDataCommitCoordinator which uses supplied executor to
46      * process commit coordinations.
47      *
48      * @param executor
49      */
50     public DOMDataCommitCoordinatorImpl(final ListeningExecutorService executor) {
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     public 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 }