Skip to content

Commit 1e81bdb

Browse files
committed
WIP adding more creation logic, cleanup required after seein what will remain
Signed-off-by: Atanas Atanasov <[email protected]>
1 parent fba03f5 commit 1e81bdb

20 files changed

+409
-131
lines changed

server/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ testModuleInfo {
3838
requires("org.junit.jupiter.api")
3939
requires("org.mockito")
4040
requires("org.mockito.junit.jupiter")
41+
requires("org.assertj.core")
4142
requiresStatic("com.github.spotbugs.annotations")
4243
}
4344

server/src/main/java/com/hedera/block/server/metrics/MetricsServiceImpl.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.swirlds.metrics.api.Metrics;
2222
import edu.umd.cs.findbugs.annotations.NonNull;
2323
import java.util.EnumMap;
24+
import java.util.Objects;
2425
import javax.inject.Inject;
2526

2627
/**
@@ -30,9 +31,7 @@
3031
* example, to increment a counter, call {@link Counter#increment()}.
3132
*/
3233
public class MetricsServiceImpl implements MetricsService {
33-
3434
private static final String CATEGORY = "hedera_block_node";
35-
3635
private final EnumMap<BlockNodeMetricTypes.Counter, Counter> counters =
3736
new EnumMap<>(BlockNodeMetricTypes.Counter.class);
3837
private final EnumMap<BlockNodeMetricTypes.Gauge, LongGauge> gauges =
@@ -45,22 +44,21 @@ public class MetricsServiceImpl implements MetricsService {
4544
*/
4645
@Inject
4746
public MetricsServiceImpl(@NonNull final Metrics metrics) {
47+
Objects.requireNonNull(metrics);
4848
// Initialize the counters
49-
for (BlockNodeMetricTypes.Counter counter : BlockNodeMetricTypes.Counter.values()) {
49+
for (final BlockNodeMetricTypes.Counter counter : BlockNodeMetricTypes.Counter.values()) {
5050
counters.put(
5151
counter,
52-
metrics.getOrCreate(
53-
new Counter.Config(CATEGORY, counter.grafanaLabel())
54-
.withDescription(counter.description())));
52+
metrics.getOrCreate(new Counter.Config(CATEGORY, counter.grafanaLabel())
53+
.withDescription(counter.description())));
5554
}
5655

5756
// Initialize the gauges
5857
for (BlockNodeMetricTypes.Gauge gauge : BlockNodeMetricTypes.Gauge.values()) {
5958
gauges.put(
6059
gauge,
6160
metrics.getOrCreate(
62-
new LongGauge.Config(CATEGORY, gauge.grafanaLabel())
63-
.withDescription(gauge.description())));
61+
new LongGauge.Config(CATEGORY, gauge.grafanaLabel()).withDescription(gauge.description())));
6462
}
6563
}
6664

server/src/main/java/com/hedera/block/server/persistence/PersistenceInjectionModule.java

+46-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
2323
import com.hedera.block.server.persistence.storage.StorageType;
2424
import com.hedera.block.server.persistence.storage.read.BlockAsDirReaderBuilder;
25+
import com.hedera.block.server.persistence.storage.read.BlockAsFileReaderBuilder;
2526
import com.hedera.block.server.persistence.storage.read.BlockReader;
27+
import com.hedera.block.server.persistence.storage.read.NoOpBlockReader;
28+
import com.hedera.block.server.persistence.storage.remove.BlockAsDirRemover;
29+
import com.hedera.block.server.persistence.storage.remove.BlockAsFileRemover;
30+
import com.hedera.block.server.persistence.storage.remove.BlockRemover;
31+
import com.hedera.block.server.persistence.storage.remove.NoOpRemover;
2632
import com.hedera.block.server.persistence.storage.write.BlockAsDirWriterBuilder;
2733
import com.hedera.block.server.persistence.storage.write.BlockAsFileWriterBuilder;
2834
import com.hedera.block.server.persistence.storage.write.BlockWriter;
@@ -33,8 +39,11 @@
3339
import dagger.Binds;
3440
import dagger.Module;
3541
import dagger.Provides;
42+
import edu.umd.cs.findbugs.annotations.NonNull;
3643
import java.io.IOException;
44+
import java.nio.file.Path;
3745
import java.util.List;
46+
import java.util.Objects;
3847
import javax.inject.Singleton;
3948

4049
/** A Dagger module for providing dependencies for Persistence Module. */
@@ -49,7 +58,10 @@ public interface PersistenceInjectionModule {
4958
*/
5059
@Provides
5160
@Singleton
52-
static BlockWriter<List<BlockItem>> providesBlockWriter(final BlockNodeContext blockNodeContext) {
61+
static BlockWriter<List<BlockItem>> providesBlockWriter(
62+
@NonNull final BlockNodeContext blockNodeContext, @NonNull final BlockRemover blockRemover) {
63+
Objects.requireNonNull(blockRemover);
64+
Objects.requireNonNull(blockNodeContext);
5365
final StorageType persistenceType = blockNodeContext
5466
.configuration()
5567
.getConfigData(PersistenceStorageConfig.class)
@@ -58,10 +70,11 @@ static BlockWriter<List<BlockItem>> providesBlockWriter(final BlockNodeContext b
5870
return switch (persistenceType) {
5971
case null -> throw new NullPointerException(
6072
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
61-
case BLOCK_AS_FILE -> BlockAsFileWriterBuilder.newBuilder().build();
62-
case BLOCK_AS_DIR -> BlockAsDirWriterBuilder.newBuilder(blockNodeContext)
73+
case BLOCK_AS_FILE -> BlockAsFileWriterBuilder.newBuilder(blockNodeContext, blockRemover)
6374
.build();
64-
case NOOP -> new NoOpBlockWriter(blockNodeContext);
75+
case BLOCK_AS_DIR -> BlockAsDirWriterBuilder.newBuilder(blockNodeContext, blockRemover)
76+
.build();
77+
case NOOP -> new NoOpBlockWriter(blockNodeContext, blockRemover);
6578
};
6679
} catch (final IOException e) {
6780
throw new RuntimeException("Failed to create BlockWriter", e);
@@ -76,8 +89,34 @@ static BlockWriter<List<BlockItem>> providesBlockWriter(final BlockNodeContext b
7689
*/
7790
@Provides
7891
@Singleton
79-
static BlockReader<Block> providesBlockReader(PersistenceStorageConfig config) {
80-
return BlockAsDirReaderBuilder.newBuilder(config).build();
92+
static BlockReader<Block> providesBlockReader(@NonNull final PersistenceStorageConfig config) {
93+
final StorageType persistenceType = Objects.requireNonNull(config).type();
94+
return switch (persistenceType) {
95+
case null -> throw new NullPointerException(
96+
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
97+
case BLOCK_AS_FILE -> BlockAsFileReaderBuilder.newBuilder().build();
98+
case BLOCK_AS_DIR -> BlockAsDirReaderBuilder.newBuilder(config).build();
99+
case NOOP -> new NoOpBlockReader();
100+
};
101+
}
102+
103+
/**
104+
* Provides a block reader singleton using the persistence storage config.
105+
*
106+
* @param config the persistence storage configuration needed to build the block reader
107+
* @return a block reader singleton
108+
*/
109+
@Provides
110+
@Singleton
111+
static BlockRemover providesBlockRemover(@NonNull final PersistenceStorageConfig config) {
112+
final StorageType persistenceType = Objects.requireNonNull(config).type();
113+
return switch (persistenceType) {
114+
case null -> throw new NullPointerException(
115+
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
116+
case BLOCK_AS_FILE -> new BlockAsFileRemover();
117+
case BLOCK_AS_DIR -> new BlockAsDirRemover(Path.of(config.rootPath()));
118+
case NOOP -> new NoOpRemover();
119+
};
81120
}
82121

83122
/**
@@ -89,5 +128,5 @@ static BlockReader<Block> providesBlockReader(PersistenceStorageConfig config) {
89128
@Binds
90129
@Singleton
91130
BlockNodeEventHandler<ObjectEvent<SubscribeStreamResponse>> bindBlockNodeEventHandler(
92-
StreamPersistenceHandlerImpl streamPersistenceHandler);
131+
@NonNull final StreamPersistenceHandlerImpl streamPersistenceHandler);
93132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.read;
18+
19+
import com.hedera.hapi.block.stream.Block;
20+
import edu.umd.cs.findbugs.annotations.NonNull;
21+
import java.util.Optional;
22+
23+
/**
24+
* TODO: add documentation
25+
*/
26+
public class BlockAsFileReader implements BlockReader<Block> {
27+
@NonNull
28+
@Override
29+
public Optional<Block> read(final long blockNumber) {
30+
throw new UnsupportedOperationException("Not implemented yet");
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.read;
18+
19+
import com.hedera.hapi.block.stream.Block;
20+
21+
/**
22+
* TODO: add documentation
23+
*/
24+
public class BlockAsFileReaderBuilder {
25+
private BlockAsFileReaderBuilder() {}
26+
27+
public static BlockAsFileReaderBuilder newBuilder() {
28+
return new BlockAsFileReaderBuilder();
29+
}
30+
31+
public BlockReader<Block> build() {
32+
return new BlockAsFileReader();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.read;
18+
19+
import com.hedera.hapi.block.stream.Block;
20+
import com.hedera.pbj.runtime.ParseException;
21+
import edu.umd.cs.findbugs.annotations.NonNull;
22+
import java.io.IOException;
23+
import java.util.Optional;
24+
25+
/**
26+
* TODO: add documentation
27+
*/
28+
public class NoOpBlockReader implements BlockReader<Block> {
29+
@NonNull
30+
@Override
31+
public Optional<Block> read(final long blockNumber) throws IOException, ParseException {
32+
return Optional.empty();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.remove;
18+
19+
import java.io.IOException;
20+
21+
/**
22+
* TODO: add documentation
23+
*/
24+
public class BlockAsFileRemover implements BlockRemover {
25+
@Override
26+
public void remove(final long blockNumber) throws IOException {
27+
throw new UnsupportedOperationException("Not implemented yet");
28+
}
29+
}

server/src/main/java/com/hedera/block/server/persistence/storage/remove/BlockRemover.java

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
/** The BlockRemover interface defines the contract for removing a block from storage. */
2222
public interface BlockRemover {
23-
2423
/**
2524
* Remove a block with the given block number.
2625
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.remove;
18+
19+
/**
20+
* TODO: add documentation
21+
*/
22+
public class NoOpRemover implements BlockRemover {
23+
@Override
24+
public void remove(final long blockNumber) {
25+
// do nothing
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hedera.block.server.persistence.storage.write;
18+
19+
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.BlocksPersisted;
20+
21+
import com.hedera.block.server.metrics.MetricsService;
22+
import com.hedera.block.server.persistence.storage.remove.BlockRemover;
23+
import com.swirlds.metrics.api.Counter;
24+
import edu.umd.cs.findbugs.annotations.NonNull;
25+
import java.util.Objects;
26+
27+
/**
28+
* TODO: add documentation
29+
*/
30+
abstract class AbstractBlockWriter<V> implements BlockWriter<V> {
31+
private final Counter blocksPersistedCounter;
32+
protected final BlockRemover blockRemover;
33+
34+
protected AbstractBlockWriter(
35+
@NonNull final MetricsService metricsService, @NonNull final BlockRemover blockRemover) {
36+
this.blocksPersistedCounter =
37+
Objects.requireNonNull(Objects.requireNonNull(metricsService).get(BlocksPersisted));
38+
this.blockRemover = Objects.requireNonNull(blockRemover);
39+
}
40+
41+
protected final void incrementBlocksPersisted() {
42+
blocksPersistedCounter.increment();
43+
}
44+
}

0 commit comments

Comments
 (0)