0633b68f1f992bff6e8095ca69f2790e54e3a0c4
[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.dispatch.ExecutionContexts;
11 import akka.dispatch.OnComplete;
12 import akka.util.Timeout;
13 import com.google.common.base.Preconditions;
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 com.google.common.util.concurrent.MoreExecutors;
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.CompletionStage;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.controller.cluster.access.ABIVersion;
24 import org.opendaylight.controller.cluster.access.client.BackendInfo;
25 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
26 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
27 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.ExecutionContext;
34
35 /**
36  * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Each string-named
37  * shard is assigned a single cookie and this mapping is stored in a bidirectional map. Information about corresponding
38  * shard leader is resolved via {@link ActorContext}. The product of resolution is {@link ShardBackendInfo}.
39  *
40  * @author Robert Varga
41  */
42 final class ModuleShardBackendResolver extends BackendInfoResolver<ShardBackendInfo> {
43     private static final ExecutionContext DIRECT_EXECUTION_CONTEXT =
44             ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
45     private static final CompletableFuture<ShardBackendInfo> NULL_FUTURE = CompletableFuture.completedFuture(null);
46     private static final Logger LOG = LoggerFactory.getLogger(ModuleShardBackendResolver.class);
47
48     /**
49      * Fall-over-dead timeout. If we do not make progress in this long, just fall over and propagate the failure.
50      * All users are expected to fail, possibly attempting to recover by restarting. It is fair to remain
51      * non-operational.
52      */
53     // TODO: maybe make this configurable somehow?
54     private static final Timeout DEAD_TIMEOUT = Timeout.apply(15, TimeUnit.MINUTES);
55
56     private final ActorContext actorContext;
57
58     @GuardedBy("this")
59     private long nextShard = 1;
60
61     private volatile BiMap<String, Long> shards = ImmutableBiMap.of(DefaultShardStrategy.DEFAULT_SHARD, 0L);
62
63     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
64     ModuleShardBackendResolver(final ActorContext actorContext) {
65         this.actorContext = Preconditions.checkNotNull(actorContext);
66     }
67
68     @Override
69     protected void invalidateBackendInfo(final CompletionStage<? extends BackendInfo> info) {
70         LOG.trace("Initiated invalidation of backend information {}", info);
71         info.thenAccept(this::invalidate);
72     }
73
74     private void invalidate(final BackendInfo result) {
75         Preconditions.checkArgument(result instanceof ShardBackendInfo);
76         LOG.debug("Invalidating backend information {}", result);
77         actorContext.getPrimaryShardInfoCache().remove(((ShardBackendInfo)result).getShardName());
78     }
79
80     Long resolveShardForPath(final YangInstanceIdentifier path) {
81         final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path);
82         Long cookie = shards.get(shardName);
83         if (cookie == null) {
84             synchronized (this) {
85                 cookie = shards.get(shardName);
86                 if (cookie == null) {
87                     cookie = nextShard++;
88
89                     Builder<String, Long> b = ImmutableBiMap.builder();
90                     b.putAll(shards);
91                     b.put(shardName, cookie);
92                     shards = b.build();
93                 }
94             }
95         }
96
97         return cookie;
98     }
99
100     @Override
101     protected CompletableFuture<ShardBackendInfo> resolveBackendInfo(final Long cookie) {
102         final String shardName = shards.inverse().get(cookie);
103         if (shardName == null) {
104             LOG.warn("Failing request for non-existent cookie {}", cookie);
105             return NULL_FUTURE;
106         }
107
108         final CompletableFuture<ShardBackendInfo> ret = new CompletableFuture<>();
109
110         actorContext.findPrimaryShardAsync(shardName).onComplete(new OnComplete<PrimaryShardInfo>() {
111             @Override
112             public void onComplete(final Throwable t, final PrimaryShardInfo v) {
113                 if (t != null) {
114                     ret.completeExceptionally(t);
115                 } else {
116                     ret.complete(createBackendInfo(v, shardName, cookie));
117                 }
118             }
119         }, DIRECT_EXECUTION_CONTEXT);
120
121         LOG.debug("Resolving cookie {} to shard {}", cookie, shardName);
122         return ret;
123     }
124
125     private static ABIVersion toABIVersion(final short version) {
126         switch (version) {
127             case DataStoreVersions.BORON_VERSION:
128                 return ABIVersion.BORON;
129         }
130
131         throw new IllegalArgumentException("Unsupported version " + version);
132     }
133
134     private static ShardBackendInfo createBackendInfo(final Object result, final String shardName, final Long cookie) {
135         Preconditions.checkArgument(result instanceof PrimaryShardInfo);
136         final PrimaryShardInfo info = (PrimaryShardInfo) result;
137
138         LOG.debug("Creating backend information for {}", info);
139         return new ShardBackendInfo(info.getPrimaryShardActor().resolveOne(DEAD_TIMEOUT).value().get().get(),
140             toABIVersion(info.getPrimaryShardVersion()), shardName, UnsignedLong.fromLongBits(cookie),
141             info.getLocalShardDataTree());
142      }
143 }