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