Fix shard deadlock in 3 nodes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / NoOpTransactionContext.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.Optional;
13 import java.util.SortedSet;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
16 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
17 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
18 import org.opendaylight.mdsal.common.api.DataStoreUnavailableException;
19 import org.opendaylight.mdsal.common.api.ReadFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Future;
23
24 final class NoOpTransactionContext extends AbstractTransactionContext {
25     private static final Logger LOG = LoggerFactory.getLogger(NoOpTransactionContext.class);
26
27     private final Throwable failure;
28
29     NoOpTransactionContext(final Throwable failure, final TransactionIdentifier identifier) {
30         super(identifier);
31         this.failure = failure;
32     }
33
34     @Override
35     public void closeTransaction() {
36         LOG.debug("NoOpTransactionContext {} closeTransaction called", getIdentifier());
37     }
38
39     @Override
40     public Future<Object> directCommit(final Boolean havePermit) {
41         LOG.debug("Tx {} directCommit called, failure: {}", getIdentifier(), failure);
42         return akka.dispatch.Futures.failed(failure);
43     }
44
45     @Override
46     public Future<ActorSelection> readyTransaction(final Boolean havePermit,
47             final Optional<SortedSet<String>> participatingShardNamess) {
48         LOG.debug("Tx {} readyTransaction called, failure: {}", getIdentifier(), failure);
49         return akka.dispatch.Futures.failed(failure);
50     }
51
52     @Override
53     public void executeModification(final AbstractModification modification, final Boolean havePermit) {
54         LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(),
55                 modification.getClass().getSimpleName(), modification.getPath());
56     }
57
58     @Override
59     public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> proxyFuture,
60             final Boolean havePermit) {
61         LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
62                 readCmd.getPath());
63
64         final Throwable t;
65         if (failure instanceof NoShardLeaderException) {
66             t = new DataStoreUnavailableException(failure.getMessage(), failure);
67         } else {
68             t = failure;
69         }
70         proxyFuture.setException(new ReadFailedException("Error executeRead " + readCmd.getClass().getSimpleName()
71                 + " for path " + readCmd.getPath(), t));
72     }
73 }