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