X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fshardmanager%2FShardInformation.java;h=270c99d86cb58396d36815748a1ff4fa96624aec;hp=262eb6d246493d697870e1e61676e714d5b1cc6b;hb=3859df9beca8f13f1ff2b2744ed3470a1715bec3;hpb=4e3f49788c05730b29468deebc2aaa4ed0d94eef diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardInformation.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardInformation.java index 262eb6d246..270c99d86c 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardInformation.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardInformation.java @@ -7,10 +7,12 @@ */ package org.opendaylight.controller.cluster.datastore.shardmanager; +import static java.util.Objects.requireNonNull; + import akka.actor.ActorRef; import akka.actor.Props; import akka.serialization.Serialization; -import com.google.common.base.Preconditions; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import java.util.HashSet; import java.util.Iterator; @@ -18,7 +20,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; -import javax.annotation.Nullable; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.controller.cluster.access.concepts.MemberName; import org.opendaylight.controller.cluster.datastore.DatastoreContext; import org.opendaylight.controller.cluster.datastore.Shard; @@ -29,7 +31,7 @@ import org.opendaylight.controller.cluster.datastore.messages.PeerUp; import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardInitialized; import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager.OnShardReady; import org.opendaylight.controller.cluster.raft.RaftState; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.ReadOnlyDataTree; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,8 +44,14 @@ final class ShardInformation { private final ShardPeerAddressResolver addressResolver; private final ShardIdentifier shardId; private final String shardName; + + // This reference indirection is required to have the ability to update the SchemaContext + // inside actor props. Otherwise we would be keeping an old SchemaContext there, preventing + // it from becoming garbage. + private final AtomicShardContextProvider schemaContextProvider = new AtomicShardContextProvider(); private ActorRef actor; - private Optional localShardDataTree; + + private Optional localShardDataTree; private boolean leaderAvailable = false; // flag that determines if the actor is ready for business @@ -57,11 +65,11 @@ final class ShardInformation { private DatastoreContext datastoreContext; private Shard.AbstractBuilder builder; - private boolean isActiveMember = true; + private boolean activeMember = true; - ShardInformation(String shardName, ShardIdentifier shardId, - Map initialPeerAddresses, DatastoreContext datastoreContext, - Shard.AbstractBuilder builder, ShardPeerAddressResolver addressResolver) { + ShardInformation(final String shardName, final ShardIdentifier shardId, + final Map initialPeerAddresses, final DatastoreContext datastoreContext, + final Shard.AbstractBuilder builder, final ShardPeerAddressResolver addressResolver) { this.shardName = shardName; this.shardId = shardId; this.initialPeerAddresses = initialPeerAddresses; @@ -70,10 +78,9 @@ final class ShardInformation { this.addressResolver = addressResolver; } - Props newProps(SchemaContext schemaContext) { - Preconditions.checkNotNull(builder); - Props props = builder.id(shardId).peerAddresses(initialPeerAddresses).datastoreContext(datastoreContext). - schemaContext(schemaContext).props(); + Props newProps() { + Props props = requireNonNull(builder).id(shardId).peerAddresses(initialPeerAddresses) + .datastoreContext(datastoreContext).schemaContextProvider(schemaContextProvider).props(); builder = null; return props; } @@ -82,12 +89,11 @@ final class ShardInformation { return shardName; } - @Nullable - ActorRef getActor(){ + @Nullable ActorRef getActor() { return actor; } - void setActor(ActorRef actor) { + void setActor(final ActorRef actor) { this.actor = actor; } @@ -95,11 +101,11 @@ final class ShardInformation { return shardId; } - void setLocalDataTree(Optional localShardDataTree) { - this.localShardDataTree = localShardDataTree; + void setLocalDataTree(final Optional dataTree) { + this.localShardDataTree = dataTree; } - Optional getLocalShardDataTree() { + Optional getLocalShardDataTree() { return localShardDataTree; } @@ -107,22 +113,20 @@ final class ShardInformation { return datastoreContext; } - void setDatastoreContext(DatastoreContext datastoreContext, ActorRef sender) { - this.datastoreContext = datastoreContext; + void setDatastoreContext(final DatastoreContext newDatastoreContext, final ActorRef sender) { + this.datastoreContext = newDatastoreContext; if (actor != null) { LOG.debug("Sending new DatastoreContext to {}", shardId); actor.tell(this.datastoreContext, sender); } } - void updatePeerAddress(String peerId, String peerAddress, ActorRef sender){ + void updatePeerAddress(final String peerId, final String peerAddress, final ActorRef sender) { LOG.info("updatePeerAddress for peer {} with address {}", peerId, peerAddress); - if(actor != null) { - if(LOG.isDebugEnabled()) { - LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}", - peerId, peerAddress, actor.path()); - } + if (actor != null) { + LOG.debug("Sending PeerAddressResolved for peer {} with address {} to {}", peerId, + peerAddress, actor.path()); actor.tell(new PeerAddressResolved(peerId, peerAddress), sender); } @@ -130,14 +134,14 @@ final class ShardInformation { notifyOnShardInitializedCallbacks(); } - void peerDown(MemberName memberName, String peerId, ActorRef sender) { - if(actor != null) { + void peerDown(final MemberName memberName, final String peerId, final ActorRef sender) { + if (actor != null) { actor.tell(new PeerDown(memberName, peerId), sender); } } - void peerUp(MemberName memberName, String peerId, ActorRef sender) { - if(actor != null) { + void peerUp(final MemberName memberName, final String peerId, final ActorRef sender) { + if (actor != null) { actor.tell(new PeerUp(memberName, peerId), sender); } } @@ -147,8 +151,9 @@ final class ShardInformation { } boolean isShardReadyWithLeaderId() { - return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role) && - (isLeader() || addressResolver.resolve(leaderId) != null); + return leaderAvailable && isShardReady() && !RaftState.IsolatedLeader.name().equals(role) + && !RaftState.PreLeader.name().equals(role) + && (isLeader() || addressResolver.resolve(leaderId) != null); } boolean isShardInitialized() { @@ -160,7 +165,7 @@ final class ShardInformation { } String getSerializedLeaderActor() { - if(isLeader()) { + if (isLeader()) { return Serialization.serializedActorPath(getActor()); } else { return addressResolver.resolve(leaderId); @@ -176,7 +181,7 @@ final class ShardInformation { } private void notifyOnShardInitializedCallbacks() { - if(onShardInitializedSet.isEmpty()) { + if (onShardInitializedSet.isEmpty()) { return; } @@ -186,7 +191,7 @@ final class ShardInformation { ready ? "ready" : "initialized", onShardInitializedSet.size()); Iterator iter = onShardInitializedSet.iterator(); - while(iter.hasNext()) { + while (iter.hasNext()) { OnShardInitialized onShardInitialized = iter.next(); if (!(onShardInitialized instanceof OnShardReady) || ready) { iter.remove(); @@ -196,38 +201,42 @@ final class ShardInformation { } } - void addOnShardInitialized(OnShardInitialized onShardInitialized) { + void addOnShardInitialized(final OnShardInitialized onShardInitialized) { onShardInitializedSet.add(onShardInitialized); } - void removeOnShardInitialized(OnShardInitialized onShardInitialized) { + void removeOnShardInitialized(final OnShardInitialized onShardInitialized) { onShardInitializedSet.remove(onShardInitialized); } - void setRole(String newRole) { + void setRole(final String newRole) { this.role = newRole; notifyOnShardInitializedCallbacks(); } - void setFollowerSyncStatus(boolean syncStatus){ + String getRole() { + return role; + } + + void setFollowerSyncStatus(final boolean syncStatus) { this.followerSyncStatus = syncStatus; } - boolean isInSync(){ - if(RaftState.Follower.name().equals(this.role)){ + boolean isInSync() { + if (RaftState.Follower.name().equals(this.role)) { return followerSyncStatus; - } else if(RaftState.Leader.name().equals(this.role)){ + } else if (RaftState.Leader.name().equals(this.role)) { return true; } return false; } - boolean setLeaderId(String leaderId) { - boolean changed = !Objects.equals(this.leaderId, leaderId); - this.leaderId = leaderId; - if(leaderId != null) { + boolean setLeaderId(final String newLeaderId) { + final boolean changed = !Objects.equals(this.leaderId, newLeaderId); + this.leaderId = newLeaderId; + if (newLeaderId != null) { this.leaderAvailable = true; } notifyOnShardInitializedCallbacks(); @@ -239,10 +248,10 @@ final class ShardInformation { return leaderId; } - void setLeaderAvailable(boolean leaderAvailable) { + void setLeaderAvailable(final boolean leaderAvailable) { this.leaderAvailable = leaderAvailable; - if(leaderAvailable) { + if (leaderAvailable) { notifyOnShardInitializedCallbacks(); } } @@ -251,15 +260,37 @@ final class ShardInformation { return leaderVersion; } - void setLeaderVersion(short leaderVersion) { + void setLeaderVersion(final short leaderVersion) { this.leaderVersion = leaderVersion; } boolean isActiveMember() { - return isActiveMember; + return activeMember; } - void setActiveMember(boolean isActiveMember) { - this.isActiveMember = isActiveMember; + void setActiveMember(final boolean isActiveMember) { + this.activeMember = isActiveMember; } -} \ No newline at end of file + + SchemaContext getSchemaContext() { + return schemaContextProvider.getSchemaContext(); + } + + void setSchemaContext(final SchemaContext schemaContext) { + schemaContextProvider.set(requireNonNull(schemaContext)); + } + + @VisibleForTesting + Shard.AbstractBuilder getBuilder() { + return builder; + } + + @Override + public String toString() { + return "ShardInformation [shardId=" + shardId + ", leaderAvailable=" + leaderAvailable + ", actorInitialized=" + + actorInitialized + ", followerSyncStatus=" + followerSyncStatus + ", role=" + role + ", leaderId=" + + leaderId + ", activeMember=" + activeMember + "]"; + } + + +}