Skip to content

Commit 1df7784

Browse files
authored
feat: Create a dedicated command for compaction in the State Operator (#21327)
Signed-off-by: Nikita Lebedev <[email protected]>
1 parent 1c4e452 commit 1df7784

File tree

6 files changed

+84
-49
lines changed

6 files changed

+84
-49
lines changed

hedera-state-validator/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ of a corrupted state.
3232
- [`account`](/src/main/java/com/hedera/statevalidation/validators/servicesstate/AccountValidator.java) - Ensures all accounts have a positive balance, calculates the total HBAR supply,
3333
and verifies it totals exactly 50 billion HBAR.
3434
- [`tokenRelations`](/src/main/java/com/hedera/statevalidation/validators/servicesstate/TokenRelationsIntegrity.java) - Verifies that the accounts and tokens for every token relationship exist.
35-
- [`compaction`](/src/main/java/com/hedera/statevalidation/validators/merkledb/Compaction.java) - Not a validation per se, but it allows for the compaction of state files.
3635

3736
## Introspect
3837

@@ -149,6 +148,19 @@ Notes:
149148
- Order of entries is consistent across runs and ordered by path, unless `-Dsorted=true` is specified.
150149
- In case of `-Dsorted=true`, the data is sorted by the **byte representation of the key**, which doesn't always map to natural ordering. For example, varint encoding does not preserve numerical ordering under lexicographical byte comparison, particularly when values cross boundaries that affect the number of bytes or the leading byte values. However, it will produce a stable ordering across different versions of the state, which is critically important for differential testing.
151150

151+
## Compact
152+
153+
[CompactionCommand](src/main/java/com/hedera/statevalidation/CompactionCommand.java) allows you to perform compaction of state files.
154+
155+
### Usage
156+
157+
1. Download the state files.
158+
2. Run the following command to execute the compaction:
159+
160+
```shell
161+
java -jar ./validator-<version>.jar {path-to-state-round} compact
162+
```
163+
152164
## Updating State with a Block Stream
153165

154166
The `apply-blocks` command uses a set of block files to advance a given state from the current state to the target state.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.statevalidation;
3+
4+
import static com.hedera.statevalidation.compaction.Compaction.runCompaction;
5+
6+
import com.hedera.statevalidation.parameterresolver.StateResolver;
7+
import com.swirlds.platform.state.MerkleNodeState;
8+
import com.swirlds.platform.state.snapshot.DeserializedSignedState;
9+
import picocli.CommandLine;
10+
11+
@CommandLine.Command(name = "compact", description = "Performs compaction of state files.")
12+
public class CompactionCommand implements Runnable {
13+
14+
@CommandLine.ParentCommand
15+
private StateOperatorCommand parent;
16+
17+
private CompactionCommand() {}
18+
19+
@Override
20+
public void run() {
21+
System.setProperty("state.dir", parent.getStateDir().getAbsolutePath());
22+
23+
try {
24+
final DeserializedSignedState deserializedSignedState = StateResolver.initState();
25+
final MerkleNodeState merkleNodeState =
26+
deserializedSignedState.reservedSignedState().get().getState();
27+
runCompaction(merkleNodeState);
28+
} catch (Exception e) {
29+
throw new RuntimeException(e);
30+
}
31+
}
32+
}

hedera-state-validator/src/main/java/com/hedera/statevalidation/StateOperatorCommand.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
@CommandLine.Command(
1111
name = "operator",
1212
mixinStandardHelpOptions = true,
13-
subcommands = {ValidateCommand.class, IntrospectCommand.class, ExportCommand.class, ApplyBlocksCommand.class},
13+
subcommands = {
14+
ValidateCommand.class,
15+
IntrospectCommand.class,
16+
ExportCommand.class,
17+
CompactionCommand.class,
18+
ApplyBlocksCommand.class
19+
},
1420
description = "CLI tool with validation and introspection modes")
1521
public class StateOperatorCommand implements Runnable {
1622

@@ -26,7 +32,7 @@ File getStateDir() {
2632
@Override
2733
public void run() {
2834
// This runs if no subcommand is provided
29-
System.out.println("Specify a subcommand (validate/introspect/export/apply-blocks).");
35+
System.out.println("Specify a subcommand (validate/introspect/export/compact/apply-blocks).");
3036
CommandLine.usage(this, System.out);
3137
}
3238

hedera-state-validator/src/main/java/com/hedera/statevalidation/ValidateCommand.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,9 @@ public class ValidateCommand implements Callable<Integer> {
3737
@CommandLine.Parameters(
3838
arity = "1..*",
3939
description =
40-
"Tag to run: [stateAnalyzer, internal, leaf, hdhm, account, tokenRelations, rehash, files, compaction, entityIds]")
40+
"Tag to run: [stateAnalyzer, internal, leaf, hdhm, account, tokenRelations, rehash, files, entityIds]")
4141
private String[] tags = {
42-
"stateAnalyzer",
43-
"internal",
44-
"leaf",
45-
"hdhm",
46-
"account",
47-
"tokenRelations",
48-
"rehash",
49-
"files",
50-
"compaction",
51-
"entityIds"
42+
"stateAnalyzer", "internal", "leaf", "hdhm", "account", "tokenRelations", "rehash", "files", "entityIds"
5243
};
5344

5445
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.statevalidation.compaction;
3+
4+
import static java.util.Objects.requireNonNull;
5+
6+
import com.swirlds.merkledb.MerkleDbDataSource;
7+
import com.swirlds.platform.state.MerkleNodeState;
8+
import com.swirlds.virtualmap.VirtualMap;
9+
import edu.umd.cs.findbugs.annotations.NonNull;
10+
11+
public final class Compaction {
12+
13+
private Compaction() {}
14+
15+
public static void runCompaction(@NonNull final MerkleNodeState merkleNodeState) {
16+
final VirtualMap virtualMap = (VirtualMap) merkleNodeState.getRoot();
17+
requireNonNull(virtualMap);
18+
MerkleDbDataSource vds = (MerkleDbDataSource) virtualMap.getDataSource();
19+
requireNonNull(vds);
20+
21+
vds.enableBackgroundCompaction();
22+
23+
vds.runKeyToPathStoreCompaction();
24+
vds.runPathToKeyStoreCompaction();
25+
vds.runHashStoreCompaction();
26+
27+
vds.awaitForCurrentCompactionsToComplete(0);
28+
}
29+
}

hedera-state-validator/src/main/java/com/hedera/statevalidation/validators/merkledb/Compaction.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)