Make ShardCoordinationTasks public
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ShardCanCommitCoordinationTask.java
1 /*
2  * Copyright (c) 2016 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.store.inmemory;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collection;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Task that coordinates the CanCommit phase of the provided {@link DOMStoreThreePhaseCommitCohort}'s
25  */
26 @Beta
27 public class ShardCanCommitCoordinationTask implements Callable<Boolean> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(ShardCanCommitCoordinationTask.class);
30
31     private final DOMDataTreeIdentifier rootShardPrefix;
32     private final Collection<DOMStoreThreePhaseCommitCohort> cohorts;
33
34     public ShardCanCommitCoordinationTask(final DOMDataTreeIdentifier rootShardPrefix,
35                                        final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
36         this.rootShardPrefix = Preconditions.checkNotNull(rootShardPrefix);
37         this.cohorts = Preconditions.checkNotNull(cohorts);
38     }
39
40     @Override
41     public Boolean call() throws TransactionCommitFailedException {
42
43         try {
44             LOG.debug("Shard {}, canCommit started", rootShardPrefix);
45             canCommitBlocking();
46
47             return true;
48         } catch (final TransactionCommitFailedException e) {
49             LOG.warn("Shard: {} Submit Error during phase CanCommit, starting Abort", rootShardPrefix, e);
50             //FIXME abort here
51             throw e;
52         }
53     }
54
55     void canCommitBlocking() throws TransactionCommitFailedException {
56         for (final ListenableFuture<?> canCommit : canCommitAll()) {
57             try {
58                 final Boolean result = (Boolean)canCommit.get();
59                 if (result == null || !result) {
60                     throw new TransactionCommitFailedException("CanCommit failed, no detailed cause available.");
61                 }
62             } catch (InterruptedException | ExecutionException e) {
63                 throw new TransactionCommitFailedException("CanCommit failed", e);
64             }
65         }
66     }
67
68     private ListenableFuture<?>[] canCommitAll() {
69         final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
70         int i = 0;
71         for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
72             ops[i++] = cohort.canCommit();
73         }
74         return ops;
75     }
76 }