Refactor ShardManagerIdentifier 36/114336/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Nov 2024 08:53:01 +0000 (09:53 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Nov 2024 08:54:59 +0000 (09:54 +0100)
- make this class a plain record, as it only holds a single field
- separate out toString() and toActorName() -- while the two are
  aliases, we want to have an explicit method
- move to cluster.datastore.shardmanager

Change-Id: Iceea5ea40e46ccf2f0d0a5c830a5d11c28cd57d4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractDataStore.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/identifiers/ShardManagerIdentifier.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerIdentifier.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardPeerAddressResolver.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/identifiers/ShardManagerIdentifierTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java

index 10393358c847156c26fda8d5284daa57e0422dee..f48ea0ba3204f36afecab17ee76b3e42daebe996 100644 (file)
@@ -30,10 +30,10 @@ import org.opendaylight.controller.cluster.common.actor.Dispatchers;
 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
 import org.opendaylight.controller.cluster.databroker.actors.dds.DistributedDataStoreClientActor;
 import org.opendaylight.controller.cluster.datastore.config.Configuration;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
 import org.opendaylight.controller.cluster.datastore.shardmanager.AbstractShardManagerCreator;
 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerCreator;
+import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerIdentifier;
 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker.CommitCohortExtension;
@@ -77,26 +77,25 @@ public abstract class AbstractDataStore implements DistributedDataStoreInterface
         requireNonNull(configuration, "configuration should not be null");
         requireNonNull(datastoreContextFactory, "datastoreContextFactory should not be null");
 
-        String shardManagerId = ShardManagerIdentifier.builder()
-                .type(datastoreContextFactory.getBaseDatastoreContext().getDataStoreName()).build().toString();
-
+        final var baseDatastoreContext = datastoreContextFactory.getBaseDatastoreContext();
+        final var shardManagerId = new ShardManagerIdentifier(baseDatastoreContext.getDataStoreName());
         LOG.info("Creating ShardManager : {}", shardManagerId);
 
-        String shardDispatcher =
-                new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
-
-        PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
-
-        AbstractShardManagerCreator<?> creator = getShardManagerCreator().cluster(cluster).configuration(configuration)
-                .datastoreContextFactory(datastoreContextFactory)
-                .readinessFuture(readinessFuture)
-                .primaryShardInfoCache(primaryShardInfoCache)
-                .restoreFromSnapshot(restoreFromSnapshot)
-                .distributedDataStore(this);
-
-        actorUtils = new ActorUtils(actorSystem, createShardManager(actorSystem, creator, shardDispatcher,
-                shardManagerId), cluster, configuration, datastoreContextFactory.getBaseDatastoreContext(),
-                primaryShardInfoCache);
+        final var shardDispatcher = new Dispatchers(actorSystem.dispatchers())
+            .getDispatcherPath(Dispatchers.DispatcherType.Shard);
+        final var primaryShardInfoCache = new PrimaryShardInfoFutureCache();
+        final var creator = getShardManagerCreator()
+            .cluster(cluster)
+            .configuration(configuration)
+            .datastoreContextFactory(datastoreContextFactory)
+            .readinessFuture(readinessFuture)
+            .primaryShardInfoCache(primaryShardInfoCache)
+            .restoreFromSnapshot(restoreFromSnapshot)
+            .distributedDataStore(this);
+
+        actorUtils = new ActorUtils(actorSystem,
+            createShardManager(actorSystem, creator, shardDispatcher, shardManagerId), cluster, configuration,
+            baseDatastoreContext, primaryShardInfoCache);
 
         final Props clientProps = DistributedDataStoreClientActor.props(cluster.getCurrentMemberName(),
             datastoreContextFactory.getBaseDatastoreContext().getDataStoreName(), actorUtils);
@@ -113,13 +112,11 @@ public abstract class AbstractDataStore implements DistributedDataStoreInterface
         identifier = client.getIdentifier();
         LOG.debug("Distributed data store client {} started", identifier);
 
-        datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(
-                datastoreContextFactory.getBaseDatastoreContext().getDataStoreMXBeanType());
-        datastoreConfigMXBean.setContext(datastoreContextFactory.getBaseDatastoreContext());
+        datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(baseDatastoreContext.getDataStoreMXBeanType());
+        datastoreConfigMXBean.setContext(baseDatastoreContext);
         datastoreConfigMXBean.registerMBean();
 
-        datastoreInfoMXBean = new DatastoreInfoMXBeanImpl(datastoreContextFactory.getBaseDatastoreContext()
-                .getDataStoreMXBeanType(), actorUtils);
+        datastoreInfoMXBean = new DatastoreInfoMXBeanImpl(baseDatastoreContext.getDataStoreMXBeanType(), actorUtils);
         datastoreInfoMXBean.registerMBean();
     }
 
@@ -303,17 +300,19 @@ public abstract class AbstractDataStore implements DistributedDataStoreInterface
     @SuppressWarnings("checkstyle:IllegalCatch")
     private static ActorRef createShardManager(final ActorSystem actorSystem,
             final AbstractShardManagerCreator<?> creator, final String shardDispatcher,
-            final String shardManagerId) {
+            final ShardManagerIdentifier shardManagerId) {
         Exception lastException = null;
 
         for (int i = 0; i < 100; i++) {
             try {
-                return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher), shardManagerId);
+                return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher),
+                    shardManagerId.toActorName());
             } catch (Exception e) {
                 lastException = e;
                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
-                LOG.debug("Could not create actor {} because of {} - waiting for sometime before retrying "
-                        + "(retry count = {})", shardManagerId, e.getMessage(), i);
+                LOG.debug(
+                    "Could not create actor {} because of {} - waiting for sometime before retrying (retry count = {})",
+                    shardManagerId, e.getMessage(), i);
             }
         }
 
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/identifiers/ShardManagerIdentifier.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/identifiers/ShardManagerIdentifier.java
deleted file mode 100644 (file)
index bb47e7c..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.controller.cluster.datastore.identifiers;
-
-public class ShardManagerIdentifier {
-    private final String type;
-
-    public ShardManagerIdentifier(final String type) {
-        this.type = type;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null || getClass() != obj.getClass()) {
-            return false;
-        }
-        return type.equals(((ShardManagerIdentifier) obj).type);
-    }
-
-    @Override
-    public int hashCode() {
-        return type.hashCode();
-    }
-
-    @Override public String toString() {
-        return "shardmanager-" + type;
-    }
-
-    public static Builder builder() {
-        return new Builder();
-    }
-
-    public static class Builder {
-        private String type;
-
-        public Builder type(final String newType) {
-            type = newType;
-            return this;
-        }
-
-        public ShardManagerIdentifier build() {
-            return new ShardManagerIdentifier(type);
-        }
-    }
-}
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerIdentifier.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerIdentifier.java
new file mode 100644 (file)
index 0000000..1fd94ce
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.shardmanager;
+
+import static java.util.Objects.requireNonNull;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+@NonNullByDefault
+public record ShardManagerIdentifier(String type) {
+    public ShardManagerIdentifier {
+        requireNonNull(type);
+    }
+
+    public String toActorName() {
+        return "shardmanager-" + type;
+    }
+
+    @Override
+    public String toString() {
+        return toActorName();
+    }
+}
index 0c52d3bea18f6ff984bbcadb3851f34e0196c73e..f0c6e8c656eea53f3dc4d6f5854cefeb9da49c52 100644 (file)
@@ -10,16 +10,13 @@ package org.opendaylight.controller.cluster.datastore.shardmanager;
 import static java.util.Objects.requireNonNull;
 
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Map;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 import org.apache.pekko.actor.Address;
 import org.apache.pekko.actor.AddressFromURIString;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
 import org.opendaylight.controller.cluster.raft.PeerAddressResolver;
 
 /**
@@ -31,13 +28,13 @@ import org.opendaylight.controller.cluster.raft.PeerAddressResolver;
 class ShardPeerAddressResolver implements PeerAddressResolver {
     // Stores a mapping between a member name and the address of the member. The map is concurrent as it
     // will be accessed by multiple threads via the public resolve method.
-    private final ConcurrentMap<MemberName, Address> memberNameToAddress = new ConcurrentHashMap<>();
-    private final String shardManagerIdentifier;
+    private final ConcurrentHashMap<MemberName, Address> memberNameToAddress = new ConcurrentHashMap<>();
+    private final ShardManagerIdentifier shardManagerIdentifier;
     private final String shardManagerType;
     private final MemberName localMemberName;
 
     ShardPeerAddressResolver(final String shardManagerType, final MemberName localMemberName) {
-        this.shardManagerIdentifier = ShardManagerIdentifier.builder().type(shardManagerType).build().toString();
+        shardManagerIdentifier = new ShardManagerIdentifier(shardManagerType);
         this.shardManagerType = shardManagerType;
         this.localMemberName = requireNonNull(localMemberName);
     }
@@ -51,16 +48,16 @@ class ShardPeerAddressResolver implements PeerAddressResolver {
     }
 
     Set<MemberName> getPeerMembers() {
-        return this.memberNameToAddress.keySet();
+        return memberNameToAddress.keySet();
     }
 
     Address getPeerAddress(final MemberName memberName) {
         return memberNameToAddress.get(memberName);
     }
 
-    Collection<String> getShardManagerPeerActorAddresses() {
-        Collection<String> peerAddresses = new ArrayList<>();
-        for (Map.Entry<MemberName, Address> entry: memberNameToAddress.entrySet()) {
+    List<String> getShardManagerPeerActorAddresses() {
+        final var peerAddresses = new ArrayList<String>();
+        for (var entry: memberNameToAddress.entrySet()) {
             if (!localMemberName.equals(entry.getKey())) {
                 peerAddresses.add(getShardManagerActorPathBuilder(entry.getValue()).toString());
             }
@@ -74,17 +71,15 @@ class ShardPeerAddressResolver implements PeerAddressResolver {
     }
 
     String getShardActorAddress(final String shardName, final MemberName memberName) {
-        Address memberAddress = memberNameToAddress.get(memberName);
-        if (memberAddress != null) {
-            return getShardManagerActorPathBuilder(memberAddress).append("/").append(
-                    getShardIdentifier(memberName, shardName)).toString();
-        }
-
-        return null;
+        final var memberAddress = memberNameToAddress.get(memberName);
+        return memberAddress == null ? null : getShardManagerActorPathBuilder(memberAddress)
+            .append("/").append(getShardIdentifier(memberName, shardName))
+            .toString();
     }
 
     StringBuilder getShardManagerActorPathBuilder(final Address address) {
-        return new StringBuilder().append(address.toString()).append("/user/").append(shardManagerIdentifier);
+        return new StringBuilder().append(address.toString())
+            .append("/user/").append(shardManagerIdentifier.toActorName());
     }
 
     @Override
@@ -93,7 +88,7 @@ class ShardPeerAddressResolver implements PeerAddressResolver {
             return null;
         }
 
-        ShardIdentifier shardId = ShardIdentifier.fromShardIdString(peerId);
+        final var shardId = ShardIdentifier.fromShardIdString(peerId);
         return getShardActorAddress(shardId.getShardName(), shardId.getMemberName());
     }
 
index 628584aa44c3e32fd5d4b6d61919cf2a41967a2f..19592215483a0a8b84536c89928edb018f7fe918 100644 (file)
@@ -5,18 +5,18 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.datastore.identifiers;
 
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
-public class ShardManagerIdentifierTest {
+import org.junit.jupiter.api.Test;
+import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerIdentifier;
 
+class ShardManagerIdentifierTest {
     @Test
-    public void testIdentifier() {
-        assertEquals("shardmanager-operational", ShardManagerIdentifier.builder().type("operational")
-                .build().toString());
+    void testIdentifier() {
+        final var id = new ShardManagerIdentifier("operational");
+        assertEquals("shardmanager-operational", id.toActorName());
+        assertEquals("shardmanager-operational", id.toString());
     }
 }
index 0bf707803fae3e8630340e82768ce5ffb374498d..eca5dd4e148aab81a1ef1699707cfc510c6b456b 100644 (file)
@@ -89,7 +89,6 @@ import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderExc
 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
-import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
 import org.opendaylight.controller.cluster.datastore.messages.AddShardReplica;
 import org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus;
@@ -163,7 +162,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
             .dataStoreName(shardMrgIDSuffix).shardInitializationTimeout(600, TimeUnit.MILLISECONDS)
             .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(6);
 
-    private final String shardMgrID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+    private final String shardMgrID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
     @BeforeClass
     public static void beforeClass() {
@@ -671,7 +670,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
     @Test
     public void testOnReceiveFindPrimaryForRemoteShard() {
         LOG.info("testOnReceiveFindPrimaryForRemoteShard starting");
-        String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        final var shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
         // Create an ActorSystem ShardManager actor for member-1.
 
@@ -679,9 +678,10 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
         Cluster.get(system1).join(AddressFromURIString.parse("pekko://cluster-test@127.0.0.1:2558"));
 
         final TestActorRef<TestShardManager> shardManager1 = TestActorRef.create(system1,
-                newTestShardMgrBuilderWithMockShardActor().cluster(
-                        new ClusterWrapperImpl(system1)).props().withDispatcher(
-                                Dispatchers.DefaultDispatcherId()), shardManagerID);
+                newTestShardMgrBuilderWithMockShardActor()
+                    .cluster(new ClusterWrapperImpl(system1))
+                    .props().withDispatcher(Dispatchers.DefaultDispatcherId()),
+                shardManagerID);
 
         // Create an ActorSystem ShardManager actor for member-2.
 
@@ -696,9 +696,10 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
                         .put("astronauts", Arrays.asList("member-2")).build());
 
         final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2,
-                newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2).cluster(
-                        new ClusterWrapperImpl(system2)).props().withDispatcher(
-                                Dispatchers.DefaultDispatcherId()), shardManagerID);
+                newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2)
+                    .cluster(new ClusterWrapperImpl(system2))
+                    .props().withDispatcher(Dispatchers.DefaultDispatcherId()),
+                shardManagerID);
 
         final TestKit kit = new TestKit(system1);
         shardManager1.tell(new UpdateSchemaContext(TEST_SCHEMA_CONTEXT), kit.getRef());
@@ -739,7 +740,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
     @Test
     public void testShardAvailabilityOnChangeOfMemberReachability() {
         LOG.info("testShardAvailabilityOnChangeOfMemberReachability starting");
-        String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        String shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
         // Create an ActorSystem ShardManager actor for member-1.
 
@@ -843,7 +844,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
     @Test
     public void testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange() {
         LOG.info("testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange starting");
-        String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        String shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
         // Create an ActorSystem ShardManager actor for member-1.
 
@@ -934,7 +935,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
     @Test
     public void testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable() {
         LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable starting");
-        String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        String shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
         MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder()
                 .put("default", Arrays.asList("member-256", "member-2")).build());
@@ -1497,7 +1498,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
                 ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2"))
                         .put("astronauts", Arrays.asList("member-2")).build());
 
-        final String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        final String shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
         datastoreContextBuilder.shardManagerPersistenceId(shardManagerID);
 
         // Create an ActorSystem ShardManager actor for member-1.
@@ -1776,7 +1777,7 @@ public class ShardManagerTest extends AbstractClusterRefActorTest {
                 ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2"))
                         .put("astronauts", Arrays.asList("member-1")).build());
 
-        String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
+        String shardManagerID = new ShardManagerIdentifier(shardMrgIDSuffix).toActorName();
 
         // Create an ActorSystem ShardManager actor for member-1.
         final ActorSystem system1 = newActorSystem("Member1");