Rename ActorContext to ActorUtils
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / SimpleShardBackendResolver.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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.concurrent.CompletionStage;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Unlike the full
23  * {@link ModuleShardBackendResolver}, this resolver is used in situations where the client corresponds exactly to one
24  * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all.
25  *
26  * @author Robert Varga
27  */
28 @ThreadSafe
29 final class SimpleShardBackendResolver extends AbstractShardBackendResolver {
30     private static final Logger LOG = LoggerFactory.getLogger(SimpleShardBackendResolver.class);
31
32     private final String shardName;
33
34     private volatile ShardState state;
35
36     // FIXME: we really need just ActorContext.findPrimaryShardAsync()
37     SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils,
38             final String shardName) {
39         super(clientId, actorUtils);
40         this.shardName = requireNonNull(shardName);
41     }
42
43     private CompletionStage<ShardBackendInfo> getBackendInfo(final long cookie) {
44         checkArgument(cookie == 0);
45
46         final ShardState existing = state;
47         if (existing != null) {
48             return existing.getStage();
49         }
50
51         synchronized (this) {
52             final ShardState recheck = state;
53             if (recheck != null) {
54                 return recheck.getStage();
55             }
56
57             final ShardState newState = resolveBackendInfo(shardName, 0);
58             state = newState;
59
60             final CompletionStage<ShardBackendInfo> stage = newState.getStage();
61             stage.whenComplete((info, failure) -> {
62                 if (failure != null) {
63                     synchronized (SimpleShardBackendResolver.this) {
64                         if (state == newState) {
65                             state = null;
66                         }
67                     }
68                 }
69             });
70
71             return stage;
72         }
73     }
74
75     @Override
76     public CompletionStage<ShardBackendInfo> getBackendInfo(final Long cookie) {
77         return getBackendInfo(cookie.longValue());
78     }
79
80     @Override
81     public CompletionStage<? extends ShardBackendInfo> refreshBackendInfo(final Long cookie,
82             final ShardBackendInfo staleInfo) {
83
84         final ShardState existing = state;
85         if (existing != null) {
86             if (!staleInfo.equals(existing.getResult())) {
87                 return existing.getStage();
88             }
89
90             synchronized (this) {
91                 LOG.debug("Invalidating backend information {}", staleInfo);
92                 flushCache(shardName);
93                 LOG.trace("Invalidated cache {}", staleInfo);
94                 state = null;
95             }
96         }
97
98         return getBackendInfo(cookie);
99     }
100
101     @Override
102     public String resolveCookieName(Long cookie) {
103         return shardName;
104     }
105 }