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