Fix InMemory shard transaction chaining.
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ShardCommitCoordinationTask.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 Commit phase of the provided {@link DOMStoreThreePhaseCommitCohort}'s.
25  */
26 @Beta
27 public class ShardCommitCoordinationTask implements Callable<Void> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(ShardCommitCoordinationTask.class);
30
31     private final DOMDataTreeIdentifier rootShardPrefix;
32     private final Collection<DOMStoreThreePhaseCommitCohort> cohorts;
33     private InmemoryDOMDataTreeShardWriteTransaction transaction;
34
35     public ShardCommitCoordinationTask(final DOMDataTreeIdentifier rootShardPrefix,
36                                        final Collection<DOMStoreThreePhaseCommitCohort> cohorts,
37                                        final InmemoryDOMDataTreeShardWriteTransaction transaction) {
38         this.rootShardPrefix = Preconditions.checkNotNull(rootShardPrefix);
39         this.cohorts = Preconditions.checkNotNull(cohorts);
40         this.transaction = Preconditions.checkNotNull(transaction);
41     }
42
43     @Override
44     public Void call() throws TransactionCommitFailedException {
45
46         try {
47             LOG.debug("Shard {}, commit started", rootShardPrefix);
48             commitBlocking();
49             transaction.transactionCommited(transaction);
50
51             return null;
52         } catch (final TransactionCommitFailedException e) {
53             LOG.warn("Shard: {} Submit Error during phase Commit, starting Abort", rootShardPrefix, e);
54             //FIXME abort here
55             throw e;
56         }
57     }
58
59     void commitBlocking() throws TransactionCommitFailedException {
60         for (final ListenableFuture<?> commit : commitAll()) {
61             try {
62                 commit.get();
63             } catch (InterruptedException | ExecutionException e) {
64                 throw new TransactionCommitFailedException("Commit failed", e);
65             }
66         }
67     }
68
69     private ListenableFuture<?>[] commitAll() {
70         final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()];
71         int index = 0;
72         for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) {
73             ops[index++] = cohort.commit();
74         }
75         return ops;
76     }
77 }