Skip to content

Commit 3132783

Browse files
authored
feat: 20823 Added progress output to export command (#20825) (#20835)
Signed-off-by: Ivan Malygin <[email protected]>
1 parent f51c3ce commit 3132783

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ public void run() {
4646
}
4747

4848
MerkleNodeState state;
49+
System.out.println("Initializing the state");
50+
long start = System.currentTimeMillis();
4951
try {
5052
DeserializedSignedState deserializedSignedState = StateResolver.initState();
5153
state = deserializedSignedState.reservedSignedState().get().getState();
5254
} catch (IOException e) {
5355
throw new RuntimeException(e);
5456
}
5557

58+
System.out.printf("State has been initialized in %d seconds. \n", (System.currentTimeMillis() - start) / 1000);
59+
5660
((VirtualMap) state.getRoot()).getDataSource().stopAndDisableBackgroundCompaction();
5761

5862
final boolean sorted = Boolean.parseBoolean(System.getProperty("sorted", "false"));
@@ -70,6 +74,8 @@ public void run() {
7074
final JsonExporter exporter = new JsonExporter(resultDir, state, serviceName, stateName);
7175
exporter.export();
7276
}
77+
78+
System.out.printf("Total time is %d seconds. \n", (System.currentTimeMillis() - start) / 1000);
7379
}
7480

7581
private List<Pair<String, String>> prepareServiceNameAndStateKeys() {

hedera-state-validator/src/main/java/com/hedera/statevalidation/exporters/JsonExporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.ConcurrentHashMap;
3232
import java.util.concurrent.ExecutorService;
3333
import java.util.concurrent.Executors;
34+
import java.util.concurrent.atomic.AtomicLong;
3435

3536
/**
3637
* This class exports the state into JSON file(s)
@@ -54,6 +55,9 @@ public class JsonExporter {
5455

5556
private final boolean allStates;
5657

58+
private final AtomicLong objectsProcessed = new AtomicLong(0);
59+
private long totalNumber;
60+
5761
public JsonExporter(File resultDir, MerkleNodeState state, String serviceName, String stateKeyName) {
5862
this.resultDir = resultDir;
5963
this.state = state;
@@ -75,14 +79,16 @@ public JsonExporter(File resultDir, MerkleNodeState state, String serviceName, S
7579
public void export() {
7680
final long startTimestamp = System.currentTimeMillis();
7781
final VirtualMap vm = (VirtualMap) state.getRoot();
82+
System.out.println("Start exporting state");
7883
List<CompletableFuture<Void>> futures = traverseVmInParallel(vm);
7984
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
80-
System.out.println("Export time: " + (System.currentTimeMillis() - startTimestamp) + "ms");
85+
System.out.println("Export time: " + ((System.currentTimeMillis() - startTimestamp) / 1000) + " seconds");
8186
executorService.close();
8287
}
8388

8489
private List<CompletableFuture<Void>> traverseVmInParallel(final VirtualMap virtualMap) {
8590
VirtualMapMetadata metadata = virtualMap.getMetadata();
91+
totalNumber = metadata.getSize();
8692
List<CompletableFuture<Void>> futures = new ArrayList<>();
8793
for (int i = 0; i < writingParallelism; i++) {
8894
String fileName;
@@ -137,6 +143,10 @@ private void processRange(String fileName, long start, long end) {
137143
.formatted(path, keyToJson(stateKey.key()), valueToJson(stateValue.value())));
138144
}
139145
emptyFile = false;
146+
long currentObjCount = objectsProcessed.incrementAndGet();
147+
if (currentObjCount % MAX_OBJ_PER_FILE == 0) {
148+
System.out.printf("%s objects of %s are processed\n", currentObjCount, totalNumber);
149+
}
140150
} catch (ParseException e) {
141151
throw new RuntimeException(e);
142152
}

hedera-state-validator/src/main/java/com/hedera/statevalidation/exporters/SortedJsonExporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.ConcurrentSkipListSet;
3636
import java.util.concurrent.ExecutorService;
3737
import java.util.concurrent.Executors;
38+
import java.util.concurrent.atomic.AtomicLong;
3839
import java.util.stream.LongStream;
3940

4041
/**
@@ -54,6 +55,9 @@ public class SortedJsonExporter {
5455
private final Map<Integer, Set<Pair<Long, Bytes>>> keysByExpectedStateIds;
5556
private final Map<Integer, Pair<String, String>> nameByStateId;
5657

58+
private final AtomicLong objectsProcessed = new AtomicLong(0);
59+
private long totalNumber;
60+
5761
public SortedJsonExporter(File resultDir, MerkleNodeState state, String serviceName, String stateKeyName) {
5862
this(resultDir, state, List.of(Pair.of(serviceName, stateKeyName)));
5963
}
@@ -109,6 +113,8 @@ public SortedJsonExporter(
109113
public void export() {
110114
final long startTimestamp = System.currentTimeMillis();
111115
final VirtualMap vm = (VirtualMap) state.getRoot();
116+
totalNumber = vm.size();
117+
System.out.println("Collecting keys from the state...");
112118
collectKeys(vm);
113119
keysByExpectedStateIds.forEach((key, values) -> {
114120
if (values.isEmpty()) {
@@ -119,7 +125,7 @@ public void export() {
119125

120126
List<CompletableFuture<Void>> futures = traverseVmInParallel();
121127
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
122-
System.out.println("Export time: " + (System.currentTimeMillis() - startTimestamp) + "ms");
128+
System.out.println("Export time: " + ((System.currentTimeMillis() - startTimestamp) / 1000) + " seconds");
123129
executorService.close();
124130
}
125131

@@ -201,6 +207,10 @@ private void processRange(final List<Pair<Long, Bytes>> keys, String fileName, i
201207
} catch (ParseException e) {
202208
throw new RuntimeException(e);
203209
}
210+
long currentObjCount = objectsProcessed.incrementAndGet();
211+
if (currentObjCount % MAX_OBJ_PER_FILE == 0) {
212+
System.out.printf("%s objects of %s are processed\n", currentObjCount, totalNumber);
213+
}
204214
}
205215
} catch (IOException e) {
206216
throw new RuntimeException(e);

0 commit comments

Comments
 (0)