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.datastore.DatastoreContext;
25 import org.opendaylight.controller.cluster.datastore.Shard;
26 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
27 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
28 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardInitialized;
29 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardReady;
30 import org.opendaylight.controller.cluster.raft.RaftState;
31 import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public final class ShardInformation {
38 private static final Logger LOG = LoggerFactory.getLogger(ShardInformation.class);
40 private final Set<OnShardInitialized> onShardInitializedSet = new HashSet<>();
41 private final Map<String, String> initialPeerAddresses;
42 private final ShardPeerAddressResolver addressResolver;
43 private final ShardIdentifier shardId;
44 private final String shardName;
46 // This reference indirection is required to have the ability to update the SchemaContext
47 // inside actor props. Otherwise we would be keeping an old SchemaContext there, preventing
48 // it from becoming garbage.
49 private final AtomicShardContextProvider schemaContextProvider = new AtomicShardContextProvider();
50 private ActorRef actor;
52 private Optional<ReadOnlyDataTree> localShardDataTree;
53 private boolean leaderAvailable = false;
55 // flag that determines if the actor is ready for business
56 private boolean actorInitialized = false;
58 private boolean followerSyncStatus = false;
61 private String leaderId;
62 private short leaderVersion;
64 private DatastoreContext datastoreContext;
65 private Shard.AbstractBuilder<?, ?> builder;
66 private boolean activeMember = true;
68 ShardInformation(final String shardName, final ShardIdentifier shardId,
69 final Map<String, String> initialPeerAddresses, final DatastoreContext datastoreContext,
70 final Shard.AbstractBuilder<?, ?> builder, final ShardPeerAddressResolver addressResolver) {
71 this.shardName = shardName;
72 this.shardId = shardId;
73 this.initialPeerAddresses = initialPeerAddresses;
74 this.datastoreContext = datastoreContext;
75 this.builder = builder;
76 this.addressResolver = addressResolver;
80 Props props = requireNonNull(builder).id(shardId).peerAddresses(initialPeerAddresses)
81 .datastoreContext(datastoreContext).schemaContextProvider(schemaContextProvider::modelContext).props();
86 String getShardName() {
91 @Nullable public ActorRef getActor() {
95 void setActor(final ActorRef actor) {
99 ShardIdentifier getShardId() {
103 void setLocalDataTree(final ReadOnlyDataTree dataTree) {
104 localShardDataTree = Optional.ofNullable(dataTree);
107 Optional<ReadOnlyDataTree> getLocalShardDataTree() {
108 return localShardDataTree;
111 DatastoreContext getDatastoreContext() {
112 return datastoreContext;
115 void setDatastoreContext(final DatastoreContext newDatastoreContext, final ActorRef sender) {
116 datastoreContext = newDatastoreContext;
118 LOG.debug("Sending new DatastoreContext to {}", shardId);
119 actor.tell(datastoreContext, sender);
123 void updatePeerAddress(final String peerId, final String peerAddress, final ActorRef sender) {
124 LOG.info("updatePeerAddress for peer {} with address {}", peerId, peerAddress);
127 LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}", peerId,
128 peerAddress, actor.path());
130 actor.tell(new PeerAddressResolved(peerId, peerAddress), sender);
133 notifyOnShardInitializedCallbacks();
136 boolean isShardReady() {
137 return !RaftState.Candidate.name().equals(role) && !Strings.isNullOrEmpty(role);
140 boolean isShardReadyWithLeaderId() {
141 return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role)
142 && !RaftState.PreLeader.name().equals(role)
143 && (isLeader() || addressResolver.resolve(leaderId) != null);
146 boolean isShardInitialized() {
147 return getActor() != null && actorInitialized;
151 return Objects.equals(leaderId, shardId.toString());
154 String getSerializedLeaderActor() {
155 return isLeader() ? Serialization.serializedActorPath(getActor()) : addressResolver.resolve(leaderId);
158 void setActorInitialized() {
159 LOG.debug("Shard {} is initialized", shardId);
161 actorInitialized = true;
163 notifyOnShardInitializedCallbacks();
166 private void notifyOnShardInitializedCallbacks() {
167 if (onShardInitializedSet.isEmpty()) {
171 final boolean ready = isShardReadyWithLeaderId();
172 final String readyStr = ready ? "ready" : "initialized";
173 LOG.debug("Shard {} is {} - notifying {} OnShardInitialized callbacks", shardId, readyStr,
174 onShardInitializedSet.size());
176 Iterator<OnShardInitialized> iter = onShardInitializedSet.iterator();
177 while (iter.hasNext()) {
178 OnShardInitialized onShardInitialized = iter.next();
179 if (!(onShardInitialized instanceof OnShardReady) || ready) {
181 onShardInitialized.getTimeoutSchedule().cancel();
182 onShardInitialized.getReplyRunnable().run();
187 void addOnShardInitialized(final OnShardInitialized onShardInitialized) {
188 onShardInitializedSet.add(onShardInitialized);
191 void removeOnShardInitialized(final OnShardInitialized onShardInitialized) {
192 onShardInitializedSet.remove(onShardInitialized);
195 void setRole(final String newRole) {
198 notifyOnShardInitializedCallbacks();
205 void setFollowerSyncStatus(final boolean syncStatus) {
206 followerSyncStatus = syncStatus;
210 if (RaftState.Follower.name().equals(role)) {
211 return followerSyncStatus;
212 } else if (RaftState.Leader.name().equals(role)) {
219 boolean setLeaderId(final String newLeaderId) {
220 final boolean changed = !Objects.equals(leaderId, newLeaderId);
221 leaderId = newLeaderId;
222 if (newLeaderId != null) {
223 leaderAvailable = true;
225 notifyOnShardInitializedCallbacks();
230 String getLeaderId() {
234 void setLeaderAvailable(final boolean leaderAvailable) {
235 this.leaderAvailable = leaderAvailable;
237 if (leaderAvailable) {
238 notifyOnShardInitializedCallbacks();
242 short getLeaderVersion() {
243 return leaderVersion;
246 void setLeaderVersion(final short leaderVersion) {
247 this.leaderVersion = leaderVersion;
250 boolean isActiveMember() {
254 void setActiveMember(final boolean isActiveMember) {
255 activeMember = isActiveMember;
258 EffectiveModelContext getSchemaContext() {
259 return schemaContextProvider.modelContext();
262 void setSchemaContext(final EffectiveModelContext schemaContext) {
263 schemaContextProvider.set(requireNonNull(schemaContext));
267 Shard.AbstractBuilder<?, ?> getBuilder() {
272 public String toString() {
273 return "ShardInformation [shardId=" + shardId + ", leaderAvailable=" + leaderAvailable + ", actorInitialized="
274 + actorInitialized + ", followerSyncStatus=" + followerSyncStatus + ", role=" + role + ", leaderId="
275 + leaderId + ", activeMember=" + activeMember + "]";