51f96e18fde4e2f0ea66d2ca41d3f72d9ea58755
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractShardBackendResolver.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 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import akka.actor.ActorRef;
11 import akka.util.Timeout;
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedLong;
14 import java.util.concurrent.CompletableFuture;
15 import java.util.concurrent.CompletionStage;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicLong;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import javax.annotation.concurrent.GuardedBy;
21 import javax.annotation.concurrent.ThreadSafe;
22 import org.opendaylight.controller.cluster.access.ABIVersion;
23 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
24 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
25 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
26 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
27 import org.opendaylight.controller.cluster.access.concepts.RequestException;
28 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
29 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
30 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
31 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
32 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
33 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
34 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.Function1;
38 import scala.compat.java8.FutureConverters;
39
40 /**
41  * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
42  * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
43  * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
44  *
45  * @author Robert Varga
46  */
47 @ThreadSafe
48 abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
49     static final class ShardState {
50         private final CompletionStage<ShardBackendInfo> stage;
51         @GuardedBy("this")
52         private ShardBackendInfo result;
53
54         ShardState(final CompletionStage<ShardBackendInfo> stage) {
55             this.stage = Preconditions.checkNotNull(stage);
56             stage.whenComplete(this::onStageResolved);
57         }
58
59         @Nonnull CompletionStage<ShardBackendInfo> getStage() {
60             return stage;
61         }
62
63         @Nullable synchronized ShardBackendInfo getResult() {
64             return result;
65         }
66
67         private synchronized void onStageResolved(final ShardBackendInfo result, final Throwable failure) {
68             if (failure == null) {
69                 this.result = Preconditions.checkNotNull(result);
70             } else {
71                 LOG.warn("Failed to resolve shard", failure);
72             }
73         }
74     }
75
76     private static final Logger LOG = LoggerFactory.getLogger(AbstractShardBackendResolver.class);
77
78     /**
79      * Connect request timeout. If the shard does not respond within this interval, we retry the lookup and connection.
80      */
81     // TODO: maybe make this configurable somehow?
82     private static final Timeout CONNECT_TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
83
84     private final AtomicLong nextSessionId = new AtomicLong();
85     private final Function1<ActorRef, ?> connectFunction;
86     private final ActorContext actorContext;
87
88     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
89     AbstractShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext) {
90         this.actorContext = Preconditions.checkNotNull(actorContext);
91         this.connectFunction = ExplicitAsk.toScala(t -> new ConnectClientRequest(clientId, t, ABIVersion.BORON,
92             ABIVersion.current()));
93     }
94
95     protected final void flushCache(final String shardName) {
96         actorContext.getPrimaryShardInfoCache().remove(shardName);
97     }
98
99     protected final ShardState resolveBackendInfo(final String shardName, final long cookie) {
100         LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
101
102         final CompletableFuture<ShardBackendInfo> future = new CompletableFuture<>();
103         FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).whenComplete((info, failure) -> {
104             if (failure == null) {
105                 connectShard(shardName, cookie, info, future);
106                 return;
107             }
108
109             LOG.debug("Shard {} failed to resolve", shardName, failure);
110             if (failure instanceof NoShardLeaderException) {
111                 // FIXME: this actually is an exception we can retry on
112                 future.completeExceptionally(failure);
113             } else if (failure instanceof NotInitializedException) {
114                 // FIXME: this actually is an exception we can retry on
115                 LOG.info("Shard {} has not initialized yet", shardName);
116                 future.completeExceptionally(failure);
117             } else if (failure instanceof PrimaryNotFoundException) {
118                 LOG.info("Failed to find primary for shard {}", shardName);
119                 future.completeExceptionally(failure);
120             } else {
121                 future.completeExceptionally(failure);
122             }
123         });
124
125         return new ShardState(future);
126     }
127
128     private void connectShard(final String shardName, final long cookie, final PrimaryShardInfo info,
129             final CompletableFuture<ShardBackendInfo> future) {
130         LOG.debug("Shard {} resolved to {}, attempting to connect", shardName, info);
131
132         FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, CONNECT_TIMEOUT))
133             .whenComplete((response, failure) -> {
134                 if (failure != null) {
135                     LOG.debug("Connect attempt to {} failed", shardName, failure);
136                     future.completeExceptionally(failure);
137                     return;
138                 }
139                 if (response instanceof RequestFailure) {
140                     final RequestException cause = ((RequestFailure<?, ?>) response).getCause();
141                     LOG.debug("Connect attempt to {} failed to process", shardName, cause);
142                     future.completeExceptionally(cause);
143                     return;
144                 }
145
146                 LOG.debug("Resolved backend information to {}", response);
147                 Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response {}",
148                     response);
149                 final ConnectClientSuccess success = (ConnectClientSuccess) response;
150                 future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
151                     success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
152                     success.getMaxMessages()));
153             });
154     }
155 }