From: Robert Varga Date: Fri, 1 Nov 2024 10:12:02 +0000 (+0100) Subject: FrontendClientMetadata is a plain record X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=b9c6217d8c99507703c6898fb872e60d5d7e8214;p=controller.git FrontendClientMetadata is a plain record This is an immutable DTO: annotate it and make it a record. Change-Id: I19ad27fad4821a86c6742b48cc1b41638ad60503 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/FrontendClientMetadataBuilder.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/FrontendClientMetadataBuilder.java index 73649bf3ab..5377351ee5 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/FrontendClientMetadataBuilder.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/FrontendClientMetadataBuilder.java @@ -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; diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java index 6fe06780ec..b91e006a6b 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java @@ -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 currentHistories; - private final @NonNull ImmutableUnsignedLongSet purgedHistories; - private final @NonNull ClientIdentifier clientId; - - public FrontendClientMetadata(final ClientIdentifier clientId, final ImmutableUnsignedLongSet purgedHistories, - final Collection currentHistories) { - this.clientId = requireNonNull(clientId); - this.purgedHistories = requireNonNull(purgedHistories); - this.currentHistories = ImmutableList.copyOf(currentHistories); - } - - public @NonNull ClientIdentifier clientId() { - return clientId; - } - - public @NonNull ImmutableList getCurrentHistories() { - return currentHistories; - } - - public @NonNull ImmutableUnsignedLongSet getPurgedHistories() { - return purgedHistories; +public record FrontendClientMetadata( + @NonNull ClientIdentifier clientId, + @NonNull ImmutableUnsignedLongSet purgedHistories, + @NonNull ImmutableList 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(); - } } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractDistributedDataStoreIntegrationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractDistributedDataStoreIntegrationTest.java index 3176498aca..e5d4790d4e 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractDistributedDataStoreIntegrationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractDistributedDataStoreIntegrationTest.java @@ -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(); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java index 2aa3e22a66..2ea1b42f6d 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java @@ -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(); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java index 556772456c..cfff365865 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java @@ -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))); }