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