FrontendClientMetadata is a plain record 92/114292/4
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Nov 2024 10:12:02 +0000 (11:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Nov 2024 16:04:58 +0000 (17:04 +0100)
This is an immutable DTO: annotate it and make it a record.

Change-Id: I19ad27fad4821a86c6742b48cc1b41638ad60503
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/FrontendClientMetadataBuilder.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractDistributedDataStoreIntegrationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java

index 73649bf3ab7d489cb73b2e8d8d015ace94ba4953..5377351ee5d6204e98447d659434f3056821f52c 100644 (file)
@@ -11,7 +11,7 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.VerifyException;
-import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
 import java.util.HashMap;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
@@ -50,32 +50,30 @@ final class FrontendClientMetadataBuilder {
         this.shardName = requireNonNull(shardName);
         clientId = meta.clientId();
 
-        purgedHistories = meta.getPurgedHistories().mutableCopy();
-        for (var historyMeta : meta.getCurrentHistories()) {
-            final var builder = new FrontendHistoryMetadataBuilder(clientId(), historyMeta);
+        purgedHistories = meta.purgedHistories().mutableCopy();
+        for (var historyMeta : meta.currentHistories()) {
+            final var builder = new FrontendHistoryMetadataBuilder(clientId, historyMeta);
             currentHistories.put(builder.getIdentifier(), builder);
         }
 
         // Sanity check and recovery
         standaloneId = new LocalHistoryIdentifier(clientId, 0);
-        if (!currentHistories.containsKey(standaloneId)) {
+        currentHistories.computeIfAbsent(standaloneId, missingId -> {
             LOG.warn("{}: Client {} recovered histories {} do not contain stand-alone history, attempting recovery",
-                shardName, clientId(), currentHistories);
-            currentHistories.put(standaloneId, new FrontendHistoryMetadataBuilder(standaloneId));
-        }
+                shardName, clientId, currentHistories);
+            return new FrontendHistoryMetadataBuilder(missingId);
+        });
     }
 
-    ClientIdentifier clientId() {
+    @NonNull ClientIdentifier clientId() {
         return clientId;
     }
 
-    String shardName() {
-        return shardName;
-    }
-
     FrontendClientMetadata build() {
-        return new FrontendClientMetadata(clientId(), purgedHistories.immutableCopy(),
-            Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
+        return new FrontendClientMetadata(clientId, purgedHistories.immutableCopy(),
+            currentHistories.values().stream()
+                .map(FrontendHistoryMetadataBuilder::build)
+                .collect(ImmutableList.toImmutableList()));
     }
 
     /**
@@ -101,15 +99,15 @@ final class FrontendClientMetadataBuilder {
         }
 
         final AbstractFrontendHistory singleHistory;
-        final var singleHistoryMeta = currentHistories.get(new LocalHistoryIdentifier(clientId(), 0));
+        final var singleHistoryMeta = currentHistories.get(new LocalHistoryIdentifier(clientId, 0));
         if (singleHistoryMeta == null) {
             final var tree = shard.getDataStore();
-            singleHistory = StandaloneFrontendHistory.create(shard.persistenceId(), clientId(), tree);
+            singleHistory = StandaloneFrontendHistory.create(shard.persistenceId(), clientId, tree);
         } else {
             singleHistory = singleHistoryMeta.toLeaderState(shard);
         }
 
-        return new LeaderFrontendState.Enabled(shard.persistenceId(), clientId(), shard.getDataStore(),
+        return new LeaderFrontendState.Enabled(shard.persistenceId(), clientId, shard.getDataStore(),
             purgedHistories.mutableCopy(), singleHistory, histories);
     }
 
@@ -118,9 +116,9 @@ final class FrontendClientMetadataBuilder {
         final var oldMeta = currentHistories.putIfAbsent(historyId, newMeta);
         if (oldMeta != null) {
             // This should not be happening, warn about it
-            LOG.warn("{}: Reused local history {}", shardName(), historyId);
+            LOG.warn("{}: Reused local history {}", shardName, historyId);
         } else {
-            LOG.debug("{}: Created local history {}", shardName(), historyId);
+            LOG.debug("{}: Created local history {}", shardName, historyId);
         }
     }
 
@@ -128,9 +126,9 @@ final class FrontendClientMetadataBuilder {
         final var builder = currentHistories.get(historyId);
         if (builder != null) {
             builder.onHistoryClosed();
-            LOG.debug("{}: Closed history {}", shardName(), historyId);
+            LOG.debug("{}: Closed history {}", shardName, historyId);
         } else {
-            LOG.warn("{}: Closed unknown history {}, ignoring", shardName(), historyId);
+            LOG.warn("{}: Closed unknown history {}, ignoring", shardName, historyId);
         }
     }
 
@@ -140,13 +138,13 @@ final class FrontendClientMetadataBuilder {
         if (history == null) {
             if (!purgedHistories.contains(historyBits)) {
                 purgedHistories.add(historyBits);
-                LOG.warn("{}: Purging unknown history {}", shardName(), historyId);
+                LOG.warn("{}: Purging unknown history {}", shardName, historyId);
             } else {
-                LOG.warn("{}: Duplicate purge of history {}", shardName(), historyId);
+                LOG.warn("{}: Duplicate purge of history {}", shardName, historyId);
             }
         } else {
             purgedHistories.add(historyBits);
-            LOG.debug("{}: Purged history {}", shardName(), historyId);
+            LOG.debug("{}: Purged history {}", shardName, historyId);
         }
     }
 
@@ -154,9 +152,9 @@ final class FrontendClientMetadataBuilder {
         final var history = getHistory(txId);
         if (history != null) {
             history.onTransactionAborted(txId);
-            LOG.debug("{}: Aborted transaction {}", shardName(), txId);
+            LOG.debug("{}: Aborted transaction {}", shardName, txId);
         } else {
-            LOG.warn("{}: Unknown history for aborted transaction {}, ignoring", shardName(), txId);
+            LOG.warn("{}: Unknown history for aborted transaction {}, ignoring", shardName, txId);
         }
     }
 
@@ -164,9 +162,9 @@ final class FrontendClientMetadataBuilder {
         final var history = getHistory(txId);
         if (history != null) {
             history.onTransactionCommitted(txId);
-            LOG.debug("{}: Committed transaction {}", shardName(), txId);
+            LOG.debug("{}: Committed transaction {}", shardName, txId);
         } else {
-            LOG.warn("{}: Unknown history for commited transaction {}, ignoring", shardName(), txId);
+            LOG.warn("{}: Unknown history for commited transaction {}, ignoring", shardName, txId);
         }
     }
 
@@ -174,19 +172,19 @@ final class FrontendClientMetadataBuilder {
         final var history = getHistory(txId);
         if (history != null) {
             history.onTransactionPurged(txId);
-            LOG.debug("{}: Purged transaction {}", shardName(), txId);
+            LOG.debug("{}: Purged transaction {}", shardName, txId);
         } else {
-            LOG.warn("{}: Unknown history for purged transaction {}, ignoring", shardName(), txId);
+            LOG.warn("{}: Unknown history for purged transaction {}, ignoring", shardName, txId);
         }
     }
 
     void onTransactionsSkipped(final LocalHistoryIdentifier historyId, final ImmutableUnsignedLongSet txIds) {
-        final FrontendHistoryMetadataBuilder history = getHistory(historyId);
+        final var history = getHistory(historyId);
         if (history != null) {
             history.onTransactionsSkipped(txIds);
-            LOG.debug("{}: History {} skipped transactions {}", shardName(), historyId, txIds);
+            LOG.debug("{}: History {} skipped transactions {}", shardName, historyId, txIds);
         } else {
-            LOG.warn("{}: Unknown history {} for skipped transactions, ignoring", shardName(), historyId);
+            LOG.warn("{}: Unknown history {} for skipped transactions, ignoring", shardName, historyId);
         }
     }
 
@@ -208,7 +206,7 @@ final class FrontendClientMetadataBuilder {
         if (historyId.getHistoryId() == 0 && historyId.getCookie() != 0) {
             // We are pre-creating the history for free-standing transactions with a zero cookie, hence our lookup
             // needs to account for that.
-            LOG.debug("{}: looking up {} instead of {}", shardName(), standaloneId, historyId);
+            LOG.debug("{}: looking up {} instead of {}", shardName, standaloneId, historyId);
             local = standaloneId;
         } else {
             local = historyId;
index 6fe06780ec8b2e701aeab47a40073e4b23fab22d..b91e006a6bc5a3cf64f74117d7eea997a761014b 100644 (file)
@@ -9,39 +9,24 @@ package org.opendaylight.controller.cluster.datastore.persisted;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
-import java.util.Collection;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
+import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.WritableObject;
 
-public final class FrontendClientMetadata implements WritableObject {
-    private final @NonNull ImmutableList<FrontendHistoryMetadata> currentHistories;
-    private final @NonNull ImmutableUnsignedLongSet purgedHistories;
-    private final @NonNull ClientIdentifier clientId;
-
-    public FrontendClientMetadata(final ClientIdentifier clientId, final ImmutableUnsignedLongSet purgedHistories,
-            final Collection<FrontendHistoryMetadata> currentHistories) {
-        this.clientId = requireNonNull(clientId);
-        this.purgedHistories = requireNonNull(purgedHistories);
-        this.currentHistories = ImmutableList.copyOf(currentHistories);
-    }
-
-    public @NonNull ClientIdentifier clientId() {
-        return clientId;
-    }
-
-    public @NonNull ImmutableList<FrontendHistoryMetadata> getCurrentHistories() {
-        return currentHistories;
-    }
-
-    public @NonNull ImmutableUnsignedLongSet getPurgedHistories() {
-        return purgedHistories;
+public record FrontendClientMetadata(
+        @NonNull ClientIdentifier clientId,
+        @NonNull ImmutableUnsignedLongSet purgedHistories,
+        @NonNull ImmutableList<FrontendHistoryMetadata> currentHistories) implements Immutable, WritableObject {
+    public FrontendClientMetadata {
+        requireNonNull(clientId);
+        requireNonNull(purgedHistories);
+        requireNonNull(currentHistories);
     }
 
     @Override
@@ -67,10 +52,4 @@ public final class FrontendClientMetadata implements WritableObject {
 
         return new FrontendClientMetadata(clientId, purgedHistories, currentBuilder.build());
     }
-
-    @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(FrontendClientMetadata.class)
-            .add("clientId", clientId).add("current", currentHistories).add("purged", purgedHistories).toString();
-    }
 }
index 3176498aca94ea41a372b79495559341e3c363d7..e5d4790d4e0007f925171971f3de8d6241dace9f 100644 (file)
@@ -262,7 +262,7 @@ public abstract class AbstractDistributedDataStoreIntegrationTest {
                             .executeOperation(localShard, new RequestFrontendMetadata());
 
                     final var clientMeta = frontendMetadata.getClients().get(0);
-                    final var iterator = clientMeta.getCurrentHistories().iterator();
+                    final var iterator = clientMeta.currentHistories().iterator();
                     var metadata = iterator.next();
                     while (iterator.hasNext() && metadata.getHistoryId() != 1) {
                         metadata = iterator.next();
index 2aa3e22a66304be176074ff56561d5105f1420d1..2ea1b42f6db917390b36d3aa231a681b42f8bdda 100644 (file)
@@ -379,7 +379,7 @@ public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
     }
 
     private static void assertClientMetadata(final FrontendClientMetadata clientMeta, final long lastPurged) {
-        final var iterator = clientMeta.getCurrentHistories().iterator();
+        final var iterator = clientMeta.currentHistories().iterator();
         var metadata = iterator.next();
         while (iterator.hasNext() && metadata.getHistoryId() != 1) {
             metadata = iterator.next();
index 556772456c284c582c96ae7d44c5766466ada6ca..cfff365865e985e5101a7f3c75e374203ae3fbf9 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.primitives.UnsignedLong;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -79,8 +80,8 @@ public class FrontendShardDataTreeSnapshotMetadataTest {
         origIdent.values().forEach(client -> {
             final var copyClient = copyIdent.get(client.clientId());
             testObject(client.clientId(), copyClient.clientId());
-            assertEquals(client.getPurgedHistories(), copyClient.getPurgedHistories());
-            assertEquals(client.getCurrentHistories(), copyClient.getCurrentHistories());
+            assertEquals(client.purgedHistories(), copyClient.purgedHistories());
+            assertEquals(client.currentHistories(), copyClient.currentHistories());
         });
     }
 
@@ -104,7 +105,7 @@ public class FrontendShardDataTreeSnapshotMetadataTest {
         final ClientIdentifier clientIdentifier = ClientIdentifier.create(frontendIdentifier, num);
         final ImmutableUnsignedLongSet purgedHistories = MutableUnsignedLongSet.of(0).immutableCopy();
 
-        return new FrontendClientMetadata(clientIdentifier, purgedHistories, List.of(
+        return new FrontendClientMetadata(clientIdentifier, purgedHistories, ImmutableList.of(
             new FrontendHistoryMetadata(num, num, true,
                 UnsignedLongBitmap.copyOf(Map.of(UnsignedLong.ZERO, Boolean.TRUE)), purgedHistories)));
     }