a1018967e76668b8e1640f86c83d7c2769a5b3d9
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ModuleShardBackendResolver.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.base.Throwables;
14 import com.google.common.collect.BiMap;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableBiMap.Builder;
17 import com.google.common.primitives.UnsignedLong;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.CompletionStage;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicLong;
23 import javax.annotation.concurrent.GuardedBy;
24 import org.opendaylight.controller.cluster.access.ABIVersion;
25 import org.opendaylight.controller.cluster.access.client.BackendInfo;
26 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
27 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
28 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
29 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
30 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
31 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
32 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
33 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION",
48                     justification = "Pertains to the NULL_FUTURE field below. Null is allowed and is intended")
49 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
50     private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
51     private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
52
53     /**
54      * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
55      * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
56      * non-operational.
57      */
58     // TODO: maybe make this configurable somehow?
59     private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
60
61     private final ActorContext actorContext;
62     // FIXME: this counter should be in superclass somewhere
63     private final AtomicLong nextSessionId = new AtomicLong();
64     private final Function1<ActorRef, ?> connectFunction;
65
66     @GuardedBy("this")
67     private long nextShard = 1;
68
69     private volatile BiMap<String, Long> shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L);
70
71     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
72     ModuleShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext) {
73         this.actorContext = Preconditions.checkNotNull(actorContext);
74         this.connectFunction = ExplicitAsk.toScala(t -> new ConnectClientRequest(clientId, t, ABIVersion.BORON,
75             ABIVersion.current()));
76     }
77
78     @Override
79     protected void invalidateBackendInfo(final CompletionStage<? extends BackendInfo> info) {
80         LOG.trace("Initiated invalidation of backend information {}", info);
81         info.thenAccept(this::invalidate);
82     }
83
84     private void invalidate(final BackendInfo result) {
85         Preconditions.checkArgument(result instanceof ShardBackendInfo);
86         LOG.debug("Invalidating backend information {}", result);
87         actorContext.getPrimaryShardInfoCache().remove(((ShardBackendInfo)result).getShardName());
88     }
89
90     Long resolveShardForPath(final YangInstanceIdentifier path) {
91         final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
92         Long cookie = shards.get(shardName);
93         if (cookie == null) {
94             synchronized (this) {
95                 cookie = shards.get(shardName);
96                 if (cookie == null) {
97                     cookie = nextShard++;
98
99                     Builder<String, Long> builder = ImmutableBiMap.builder();
100                     builder.putAll(shards);
101                     builder.put(shardName, cookie);
102                     shards = builder.build();
103                 }
104             }
105         }
106
107         return cookie;
108     }
109
110     @Override
111     protected CompletableFuture<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
112         final String shardName = shards.inverse().get(cookie);
113         if (shardName == null) {
114             LOG.warn("Failing request for non-existent cookie {}", cookie);
115             return NULL_FUTURE;
116         }
117
118         final CompletableFuture<ShardBackendInfo> ret = new CompletableFuture<>();
119
120         FutureConverters.toJava(actorContext.findPrimaryShardAsync(shardName)).thenCompose(info -> {
121             LOG.debug("Looking up primary info for {} from {}", shardName, info);
122             return FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, DEAD_TIMEOUT));
123         }).thenApply(response -> {
124             if (response instanceof RequestFailure) {
125                 final RequestFailure<?, ?> failure = (RequestFailure<?, ?>) response;
126                 LOG.debug("Connect request failed {}", failure, failure.getCause());
127                 throw Throwables.propagate(failure.getCause());
128             }
129
130             LOG.debug("Resolved backend information to {}", response);
131
132             Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response {}", response);
133             final ConnectClientSuccess success = (ConnectClientSuccess) response;
134
135             return new ShardBackendInfo(success.getBackend(),
136                 nextSessionId.getAndIncrement(), success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie),
137                 success.getDataTree(), success.getMaxMessages());
138         }).whenComplete((info, throwablw) -> {
139             if (throwablw != null) {
140                 ret.completeExceptionally(throwablw);
141             } else {
142                 ret.complete(info);
143             }
144         });
145
146         LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
147         return ret;
148     }
149 }