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