Skip to content

Commit

Permalink
fix: 17467: Back out changes for 15448 from release 0.58
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Ananev <[email protected]>
  • Loading branch information
artemananiev committed Jan 21, 2025
1 parent ad53b0c commit a48110e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 877 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2022-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -261,66 +261,4 @@ public void read() throws Exception {

afterTest(true);
}

@Benchmark
public void queueMode() throws Exception {
beforeTest("queueMode");

final long[] map = new long[verify ? maxKey : 0];
VirtualMap<BenchmarkKey, BenchmarkValue> virtualMap = createMap(map);

final int roundsPerCopy = maxKey / numFiles;
for (int i = 0; i < maxKey; i++) {
// Add
int index = i;
final BenchmarkKey keyToAdd = new BenchmarkKey(index);
long val = nextValue();
virtualMap.put(keyToAdd, new BenchmarkValue(val));
if (verify) {
map[index] = val;
}
// Update
if (i >= numRecords / 2) {
index = i - numRecords / 2;
final BenchmarkKey keyToUpdate = new BenchmarkKey(index);
val = nextValue();
virtualMap.put(keyToUpdate, new BenchmarkValue(val));
if (verify) {
map[index] = val;
}
}
// Remove
if (i >= numRecords) {
index = i - numRecords;
final BenchmarkKey keyToRemove = new BenchmarkKey(index);
virtualMap.remove(keyToRemove);
if (verify) {
map[index] = 0;
}
}

if (i % roundsPerCopy == 0) {
virtualMap = copyMap(virtualMap);
}
}

// Ensure the map is done with hashing/merging/flushing
final var finalMap = flushMap(virtualMap);

verifyMap(map, finalMap);

afterTest(true, () -> {
finalMap.release();
finalMap.getDataSource().close();
});
}

public static void main(String[] args) throws Exception {
final VirtualMapBench bench = new VirtualMapBench();
bench.setup();
bench.beforeTest();
bench.queueMode();
bench.afterTest();
bench.destroy();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2024 Hedera Hashgraph, LLC
* Copyright (C) 2021-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.swirlds.common.config.singleton.ConfigurationHolder;
import com.swirlds.common.constructable.ClassConstructorPair;
import com.swirlds.common.constructable.ConstructableRegistry;
import com.swirlds.common.constructable.ConstructableRegistryException;
Expand All @@ -39,13 +38,11 @@
import com.swirlds.common.merkle.crypto.MerkleCryptoFactory;
import com.swirlds.common.merkle.route.MerkleRoute;
import com.swirlds.config.api.Configuration;
import com.swirlds.config.extensions.test.fixtures.TestConfigBuilder;
import com.swirlds.merkledb.test.fixtures.ExampleFixedSizeVirtualValue;
import com.swirlds.merkledb.test.fixtures.ExampleFixedSizeVirtualValueSerializer;
import com.swirlds.merkledb.test.fixtures.ExampleLongKeyFixedSize;
import com.swirlds.virtualmap.VirtualMap;
import com.swirlds.virtualmap.config.VirtualMapConfig;
import com.swirlds.virtualmap.config.VirtualMapConfig_;
import com.swirlds.virtualmap.datasource.VirtualDataSourceBuilder;
import com.swirlds.virtualmap.internal.cache.VirtualNodeCache;
import com.swirlds.virtualmap.internal.merkle.VirtualInternalNode;
Expand Down Expand Up @@ -379,87 +376,4 @@ void serializeFlushedAndUnflushedData(final int count) throws InterruptedExcepti

MILLISECONDS.sleep(100); // Hack. Release methods may not have finished their work yet.
}

@Test
void inMemoryModeSerde() throws IOException {
final Configuration configuration = new TestConfigBuilder()
.withValue(VirtualMapConfig_.COPY_FLUSH_THRESHOLD, 1_000_000)
.getOrCreateConfig();
ConfigurationHolder.getInstance().setConfiguration(configuration);

VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> map = new VirtualMap<>(
"inMemoryModeSerde", KEY_SERIALIZER, VALUE_SERIALIZER, constructBuilder(configuration), configuration);

// Copy 0
for (int i = 0; i < 100; i++) {
final ExampleLongKeyFixedSize key = new ExampleLongKeyFixedSize(i);
final ExampleFixedSizeVirtualValue value = new ExampleFixedSizeVirtualValue(1000000 + i);
map.put(key, value);
}

// Copy 1
final VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> copy1 = map.copy();
map.release();
map = copy1;
for (int i = 100; i < 200; i++) {
final ExampleLongKeyFixedSize key = new ExampleLongKeyFixedSize(i);
final ExampleFixedSizeVirtualValue value = new ExampleFixedSizeVirtualValue(1000000 + i);
map.put(key, value);
}
// Add more entries to copy 1 to force it to flush
for (int i = 100000; i < 120000; i++) {
final ExampleLongKeyFixedSize key = new ExampleLongKeyFixedSize(i);
final ExampleFixedSizeVirtualValue value = new ExampleFixedSizeVirtualValue(1000000 + i);
map.put(key, value);
}

final int nCopies = 100;
for (int copyNo = 2; copyNo < nCopies; copyNo++) {
final VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> copy = map.copy();
map.release();
map = copy;
for (int i = 0; i < 100; i++) {
final int toAdd = copyNo * 100 + i;
final ExampleLongKeyFixedSize keyToAdd = new ExampleLongKeyFixedSize(toAdd);
final ExampleFixedSizeVirtualValue value = new ExampleFixedSizeVirtualValue(1000000 + toAdd);
map.put(keyToAdd, value);
final int toRemove = (copyNo - 2) * 100 + i + 75;
final ExampleLongKeyFixedSize keytoRemove = new ExampleLongKeyFixedSize(toRemove);
final ExampleFixedSizeVirtualValue removed = map.remove(keytoRemove);
assertNotNull(removed);
}
}

// Final copy
final VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> copyF = map.copy();
map.release();
map = copyF;

// And one more to make sure copyF is immutable and can be serialized
final VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> copyOneMore = map.copy();

final Hash originalHash = MerkleCryptoFactory.getInstance().digestTreeSync(copyF);

final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final Path tmp = LegacyTemporaryFileBuilder.buildTemporaryDirectory("inMemoryModeSerde", configuration);
try (final SerializableDataOutputStream out = new SerializableDataOutputStream(bout)) {
copyF.serialize(out, tmp);
}

MerkleDb.resetDefaultInstancePath();

final ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
map = new VirtualMap<>(configuration);
try (final SerializableDataInputStream in = new SerializableDataInputStream(bin)) {
map.deserialize(in, tmp, 3);
}

final VirtualMap<ExampleLongKeyFixedSize, ExampleFixedSizeVirtualValue> copyAfter = map.copy();

final Hash restoredHash = MerkleCryptoFactory.getInstance().digestTreeSync(map);
assertEquals(originalHash, restoredHash);

copyOneMore.release();
copyAfter.release();
}
}
Loading

0 comments on commit a48110e

Please sign in to comment.