2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.shardmanager;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import akka.serialization.Serialization;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.base.Strings;
17 import java.util.HashSet;
18 import java.util.Iterator;
20 import java.util.Objects;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.controller.cluster.access.concepts.MemberName;
25 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
26 import org.opendaylight.controller.cluster.datastore.Shard;
27 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
28 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
29 import org.opendaylight.controller.cluster.datastore.messages.PeerDown;
30 import org.opendaylight.controller.cluster.datastore.messages.PeerUp;
31 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardInitialized;
32 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardReady;
33 import org.opendaylight.controller.cluster.raft.RaftState;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.ReadOnlyDataTree;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public final class ShardInformation {
41 private static final Logger LOG = LoggerFactory.getLogger(ShardInformation.class);
43 private final Set<OnShardInitialized> onShardInitializedSet = new HashSet<>();
44 private final Map<String, String> initialPeerAddresses;
45 private final ShardPeerAddressResolver addressResolver;
46 private final ShardIdentifier shardId;
47 private final String shardName;
49 // This reference indirection is required to have the ability to update the SchemaContext
50 // inside actor props. Otherwise we would be keeping an old SchemaContext there, preventing
51 // it from becoming garbage.
52 private final AtomicShardContextProvider schemaContextProvider = new AtomicShardContextProvider();
53 private ActorRef actor;
55 private Optional<ReadOnlyDataTree> localShardDataTree;
56 private boolean leaderAvailable = false;
58 // flag that determines if the actor is ready for business
59 private boolean actorInitialized = false;
61 private boolean followerSyncStatus = false;
64 private String leaderId;
65 private short leaderVersion;
67 private DatastoreContext datastoreContext;
68 private Shard.AbstractBuilder<?, ?> builder;
69 private boolean activeMember = true;
71 ShardInformation(final String shardName, final ShardIdentifier shardId,
72 final Map<String, String> initialPeerAddresses, final DatastoreContext datastoreContext,
73 final Shard.AbstractBuilder<?, ?> builder, final ShardPeerAddressResolver addressResolver) {
74 this.shardName = shardName;
75 this.shardId = shardId;
76 this.initialPeerAddresses = initialPeerAddresses;
77 this.datastoreContext = datastoreContext;
78 this.builder = builder;
79 this.addressResolver = addressResolver;
83 Props props = requireNonNull(builder).id(shardId).peerAddresses(initialPeerAddresses)
84 .datastoreContext(datastoreContext).schemaContextProvider(schemaContextProvider).props();
89 String getShardName() {
94 @Nullable public ActorRef getActor() {
98 void setActor(final ActorRef actor) {
102 ShardIdentifier getShardId() {
106 void setLocalDataTree(final Optional<ReadOnlyDataTree> dataTree) {
107 this.localShardDataTree = dataTree;
110 Optional<ReadOnlyDataTree> getLocalShardDataTree() {
111 return localShardDataTree;
114 DatastoreContext getDatastoreContext() {
115 return datastoreContext;
118 void setDatastoreContext(final DatastoreContext newDatastoreContext, final ActorRef sender) {
119 this.datastoreContext = newDatastoreContext;
121 LOG.debug("Sending new DatastoreContext to {}", shardId);
122 actor.tell(this.datastoreContext, sender);
126 void updatePeerAddress(final String peerId, final String peerAddress, final ActorRef sender) {
127 LOG.info("updatePeerAddress for peer {} with address {}", peerId, peerAddress);
130 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}", peerId,
131 peerAddress, actor.path());
133 actor.tell(new PeerAddressResolved(peerId, peerAddress), sender);
136 notifyOnShardInitializedCallbacks();
139 void peerDown(final MemberName memberName, final String peerId, final ActorRef sender) {
141 actor.tell(new PeerDown(memberName, peerId), sender);
145 void peerUp(final MemberName memberName, final String peerId, final ActorRef sender) {
147 actor.tell(new PeerUp(memberName, peerId), sender);
151 boolean isShardReady() {
152 return !RaftState.Candidate.name().equals(role) && !Strings.isNullOrEmpty(role);
155 boolean isShardReadyWithLeaderId() {
156 return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role)
157 && !RaftState.PreLeader.name().equals(role)
158 && (isLeader() || addressResolver.resolve(leaderId) != null);
161 boolean isShardInitialized() {
162 return getActor() != null && actorInitialized;
166 return Objects.equals(leaderId, shardId.toString());
169 String getSerializedLeaderActor() {
171 return Serialization.serializedActorPath(getActor());
173 return addressResolver.resolve(leaderId);
177 void setActorInitialized() {
178 LOG.debug("Shard {} is initialized", shardId);
180 this.actorInitialized = true;
182 notifyOnShardInitializedCallbacks();
185 private void notifyOnShardInitializedCallbacks() {
186 if (onShardInitializedSet.isEmpty()) {
190 boolean ready = isShardReadyWithLeaderId();
192 LOG.debug("Shard {} is {} - notifying {} OnShardInitialized callbacks", shardId,
193 ready ? "ready" : "initialized", onShardInitializedSet.size());
195 Iterator<OnShardInitialized> iter = onShardInitializedSet.iterator();
196 while (iter.hasNext()) {
197 OnShardInitialized onShardInitialized = iter.next();
198 if (!(onShardInitialized instanceof OnShardReady) || ready) {
200 onShardInitialized.getTimeoutSchedule().cancel();
201 onShardInitialized.getReplyRunnable().run();
206 void addOnShardInitialized(final OnShardInitialized onShardInitialized) {
207 onShardInitializedSet.add(onShardInitialized);
210 void removeOnShardInitialized(final OnShardInitialized onShardInitialized) {
211 onShardInitializedSet.remove(onShardInitialized);
214 void setRole(final String newRole) {
217 notifyOnShardInitializedCallbacks();
224 void setFollowerSyncStatus(final boolean syncStatus) {
225 this.followerSyncStatus = syncStatus;
229 if (RaftState.Follower.name().equals(this.role)) {
230 return followerSyncStatus;
231 } else if (RaftState.Leader.name().equals(this.role)) {
238 boolean setLeaderId(final String newLeaderId) {
239 final boolean changed = !Objects.equals(this.leaderId, newLeaderId);
240 this.leaderId = newLeaderId;
241 if (newLeaderId != null) {
242 this.leaderAvailable = true;
244 notifyOnShardInitializedCallbacks();
249 String getLeaderId() {
253 void setLeaderAvailable(final boolean leaderAvailable) {
254 this.leaderAvailable = leaderAvailable;
256 if (leaderAvailable) {
257 notifyOnShardInitializedCallbacks();
261 short getLeaderVersion() {
262 return leaderVersion;
265 void setLeaderVersion(final short leaderVersion) {
266 this.leaderVersion = leaderVersion;
269 boolean isActiveMember() {
273 void setActiveMember(final boolean isActiveMember) {
274 this.activeMember = isActiveMember;
277 SchemaContext getSchemaContext() {
278 return schemaContextProvider.getSchemaContext();
281 void setSchemaContext(final SchemaContext schemaContext) {
282 schemaContextProvider.set(requireNonNull(schemaContext));
286 Shard.AbstractBuilder<?, ?> getBuilder() {
291 public String toString() {
292 return "ShardInformation [shardId=" + shardId + ", leaderAvailable=" + leaderAvailable + ", actorInitialized="
293 + actorInitialized + ", followerSyncStatus=" + followerSyncStatus + ", role=" + role + ", leaderId="
294 + leaderId + ", activeMember=" + activeMember + "]";