Skip to content

Commit

Permalink
WIP introducing PathResolver
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Nov 19, 2024
1 parent 827db48 commit c30b391
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.hedera.block.server.events.ObjectEvent;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.block.server.persistence.storage.StorageType;
import com.hedera.block.server.persistence.storage.path.BlockAsDirPathResolver;
import com.hedera.block.server.persistence.storage.path.BlockAsFilePathResolver;
import com.hedera.block.server.persistence.storage.path.NoOpPathResolver;
import com.hedera.block.server.persistence.storage.path.PathResolver;
import com.hedera.block.server.persistence.storage.read.BlockAsDirReaderBuilder;
import com.hedera.block.server.persistence.storage.read.BlockAsFileReaderBuilder;
import com.hedera.block.server.persistence.storage.read.BlockReader;
Expand Down Expand Up @@ -49,7 +53,6 @@
/** A Dagger module for providing dependencies for Persistence Module. */
@Module
public interface PersistenceInjectionModule {

/**
* Provides a block writer singleton using the block node context.
*
Expand All @@ -59,9 +62,12 @@ public interface PersistenceInjectionModule {
@Provides
@Singleton
static BlockWriter<List<BlockItem>> providesBlockWriter(
@NonNull final BlockNodeContext blockNodeContext, @NonNull final BlockRemover blockRemover) {
@NonNull final BlockNodeContext blockNodeContext,
@NonNull final BlockRemover blockRemover,
@NonNull final PathResolver pathResolver) {
Objects.requireNonNull(blockNodeContext);
Objects.requireNonNull(blockRemover);
Objects.requireNonNull(pathResolver);
final StorageType persistenceType = blockNodeContext
.configuration()
.getConfigData(PersistenceStorageConfig.class)
Expand All @@ -70,11 +76,11 @@ static BlockWriter<List<BlockItem>> providesBlockWriter(
return switch (persistenceType) {
case null -> throw new NullPointerException(
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
case BLOCK_AS_FILE -> BlockAsFileWriterBuilder.newBuilder(blockNodeContext, blockRemover)
case BLOCK_AS_FILE -> BlockAsFileWriterBuilder.newBuilder(blockNodeContext, blockRemover, pathResolver)
.build();
case BLOCK_AS_DIR -> BlockAsDirWriterBuilder.newBuilder(blockNodeContext, blockRemover)
case BLOCK_AS_DIR -> BlockAsDirWriterBuilder.newBuilder(blockNodeContext, blockRemover, pathResolver)
.build();
case NOOP -> new NoOpBlockWriter(blockNodeContext, blockRemover);
case NOOP -> new NoOpBlockWriter(blockNodeContext, blockRemover, pathResolver);
};
} catch (final IOException e) {
throw new RuntimeException("Failed to create BlockWriter", e);
Expand All @@ -84,7 +90,8 @@ static BlockWriter<List<BlockItem>> providesBlockWriter(
/**
* Provides a block reader singleton using the persistence storage config.
*
* @param config the persistence storage configuration needed to build the block reader
* @param config the persistence storage configuration needed to build the
* block reader
* @return a block reader singleton
*/
@Provides
Expand All @@ -93,32 +100,54 @@ static BlockReader<Block> providesBlockReader(@NonNull final PersistenceStorageC
final StorageType persistenceType = Objects.requireNonNull(config).type();
return switch (persistenceType) {
case null -> throw new NullPointerException(
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
"Persistence StorageType cannot be [null], cannot create an instance of BlockReader");
case BLOCK_AS_FILE -> BlockAsFileReaderBuilder.newBuilder().build();
case BLOCK_AS_DIR -> BlockAsDirReaderBuilder.newBuilder(config).build();
case NOOP -> new NoOpBlockReader();
};
}

/**
* Provides a block reader singleton using the persistence storage config.
* Provides a block remover singleton using the persistence storage config.
*
* @param config the persistence storage configuration needed to build the block reader
* @return a block reader singleton
* @param config the persistence storage configuration needed to build the
* block remover
* @return a block remover singleton
*/
@Provides
@Singleton
static BlockRemover providesBlockRemover(@NonNull final PersistenceStorageConfig config) {
final StorageType persistenceType = Objects.requireNonNull(config).type();
return switch (persistenceType) {
case null -> throw new NullPointerException(
"Persistence StorageType cannot be [null], cannot create an instance of BlockWriter");
"Persistence StorageType cannot be [null], cannot create an instance of BlockRemover");
case BLOCK_AS_FILE -> new BlockAsFileRemover();
case BLOCK_AS_DIR -> new BlockAsDirRemover(Path.of(config.rootPath()));
case NOOP -> new NoOpRemover();
};
}

/**
* Provides a path resolver singleton using the persistence storage config.
*
* @param config the persistence storage configuration needed to build the
* path resolver
* @return a path resolver singleton
*/
@Provides
@Singleton
static PathResolver providesPathResolver(@NonNull final PersistenceStorageConfig config) {
final StorageType persistenceType = Objects.requireNonNull(config).type();
final Path root = Path.of(config.rootPath());
return switch (persistenceType) {
case null -> throw new NullPointerException(
"Persistence StorageType cannot be [null], cannot create an instance of PathResolver");
case BLOCK_AS_FILE -> new BlockAsFilePathResolver(root);
case BLOCK_AS_DIR -> new BlockAsDirPathResolver(root);
case NOOP -> new NoOpPathResolver();
};
}

/**
* Binds the block node event handler to the stream persistence handler.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage.path;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Path;
import java.util.Objects;

/**
* TODO: add documentation
*/
abstract class AbstractPathResolver implements PathResolver {
private final Path root;

AbstractPathResolver(@NonNull final Path root) {
this.root = Objects.requireNonNull(root);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage.path;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Path;

/**
* TODO: add documentation
*/
public final class BlockAsDirPathResolver extends AbstractPathResolver {
public BlockAsDirPathResolver(@NonNull final Path root) {
super(root);
}

@Override
public Path resolvePathToBlock(final long blockNumber) {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage.path;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Path;

/**
* TODO: add documentation
*/
public final class BlockAsFilePathResolver extends AbstractPathResolver {
public BlockAsFilePathResolver(@NonNull final Path root) {
super(root);
}

@Override
public Path resolvePathToBlock(final long blockNumber) {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage.path;

import static java.lang.System.Logger.Level.INFO;

import java.nio.file.Path;

/**
* TODO: add documentation
*/
public final class NoOpPathResolver implements PathResolver {
public NoOpPathResolver() {
System.getLogger(getClass().getName()).log(INFO, "Using " + getClass().getSimpleName());
}

@Override
public Path resolvePathToBlock(final long blockNumber) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage.path;

import java.nio.file.Path;

/**
* TODO: add documentation
*/
public interface PathResolver {
Path resolvePathToBlock(final long blockNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.hedera.block.server.persistence.storage.read;

import static java.lang.System.Logger.Level.INFO;

import com.hedera.hapi.block.stream.Block;
import com.hedera.pbj.runtime.ParseException;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -26,6 +28,10 @@
* TODO: add documentation
*/
public class NoOpBlockReader implements BlockReader<Block> {
public NoOpBlockReader() {
System.getLogger(getClass().getName()).log(INFO, "Using " + getClass().getSimpleName());
}

@NonNull
@Override
public Optional<Block> read(final long blockNumber) throws IOException, ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package com.hedera.block.server.persistence.storage.remove;

import static java.lang.System.Logger.Level.INFO;

/**
* TODO: add documentation
*/
public class NoOpRemover implements BlockRemover {
public NoOpRemover() {
System.getLogger(getClass().getName()).log(INFO, "Using " + getClass().getSimpleName());
}

@Override
public void remove(final long blockNumber) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.hedera.block.server.metrics.BlockNodeMetricTypes.Counter.BlocksPersisted;

import com.hedera.block.server.metrics.MetricsService;
import com.hedera.block.server.persistence.storage.path.PathResolver;
import com.hedera.block.server.persistence.storage.remove.BlockRemover;
import com.swirlds.metrics.api.Counter;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -30,12 +31,16 @@
abstract class AbstractBlockWriter<V> implements BlockWriter<V> {
private final Counter blocksPersistedCounter;
protected final BlockRemover blockRemover;
protected final PathResolver pathResolver;

protected AbstractBlockWriter(
@NonNull final MetricsService metricsService, @NonNull final BlockRemover blockRemover) {
@NonNull final MetricsService metricsService,
@NonNull final BlockRemover blockRemover,
@NonNull final PathResolver pathResolver) {
Objects.requireNonNull(metricsService);
this.blocksPersistedCounter = Objects.requireNonNull(metricsService.get(BlocksPersisted));
this.blockRemover = Objects.requireNonNull(blockRemover);
this.pathResolver = Objects.requireNonNull(pathResolver);
}

protected final void incrementBlocksPersisted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hedera.block.common.utils.FileUtilities;
import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.block.server.persistence.storage.path.PathResolver;
import com.hedera.block.server.persistence.storage.remove.BlockRemover;
import com.hedera.hapi.block.stream.BlockItem;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -58,21 +59,24 @@ class BlockAsDirWriter extends AbstractBlockWriter<List<BlockItem>> {
private Path currentBlockDir;

/**
* Use the corresponding builder to construct a new BlockAsDirWriter with the given parameters.
* Use the corresponding builder to construct a new BlockAsDirWriter with
* the given parameters.
*
* @param blockNodeContext the block node context to use for writing blocks
* @param blockRemover the block remover to use for removing blocks
* @param folderPermissions the folder permissions to use for writing blocks, if null provided then defaults
* will be used
* @param pathResolver used internally to resolve paths
* @param folderPermissions the folder permissions to use for writing
* blocks, if null provided then defaults will be used
*
* @throws IOException if an error occurs while initializing the BlockAsDirWriter
*/
BlockAsDirWriter(
@NonNull final BlockNodeContext blockNodeContext,
@NonNull final BlockRemover blockRemover,
@NonNull final PathResolver pathResolver,
final FileAttribute<Set<PosixFilePermission>> folderPermissions)
throws IOException {
super(blockNodeContext.metricsService(), blockRemover);
super(blockNodeContext.metricsService(), blockRemover, pathResolver);

LOGGER.log(INFO, "Initializing FileSystemBlockStorage");

Expand Down
Loading

0 comments on commit c30b391

Please sign in to comment.