Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement consumer handler in the Simulator #360

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

georgi-l95
Copy link
Contributor

@georgi-l95 georgi-l95 commented Nov 22, 2024

Description:
This pull request aims to make use of the consumer mode in the simulator, by adding needed implementations.
We add:

  • gRPC client and observer for consuming blocks, which utilizes SubscribeStreamRequest and SubscribeStreamResponse, which are part of the subscribeBlockStream rpc.
  • Makes the neccessery changes to initilize managers and clients, depending on mode.
  • Adds neccessery unit and integration tests for sufficient code coverage, by making use of as less mocks as possible.

Related issue(s):

Fixes #121

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

@georgi-l95 georgi-l95 added the Simulator Issue related to Block Stream Simulator label Nov 22, 2024
@georgi-l95 georgi-l95 added this to the 0.3.0 milestone Nov 22, 2024
@georgi-l95 georgi-l95 self-assigned this Nov 22, 2024
@georgi-l95 georgi-l95 linked an issue Nov 22, 2024 that may be closed by this pull request
@georgi-l95 georgi-l95 marked this pull request as ready for review November 27, 2024 21:49
@georgi-l95 georgi-l95 requested a review from a team as a code owner November 27, 2024 21:49
@georgi-l95 georgi-l95 changed the title feat: WIP Implement consumer handler in the Simulator feat: Implement consumer handler in the Simulator Nov 27, 2024
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 97.87234% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97.37%. Comparing base (ad18213) to head (b6e5369).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
...edera/block/simulator/BlockStreamSimulatorApp.java 90.00% 0 Missing and 1 partial ⚠️
.../simulator/generator/BlockAsFileLargeDataSets.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #360      +/-   ##
============================================
+ Coverage     94.62%   97.37%   +2.74%     
- Complexity      316      360      +44     
============================================
  Files            69       72       +3     
  Lines          1228     1333     +105     
  Branches         84       89       +5     
============================================
+ Hits           1162     1298     +136     
+ Misses           55       23      -32     
- Partials         11       12       +1     
Files with missing lines Coverage Δ
...ulator/generator/BlockAsDirBlockStreamManager.java 100.00% <100.00%> (ø)
...lator/generator/BlockAsFileBlockStreamManager.java 90.90% <100.00%> (-9.10%) ⬇️
...dera/block/simulator/grpc/GrpcInjectionModule.java 0.00% <ø> (ø)
...ulator/grpc/impl/ConsumerStreamGrpcClientImpl.java 100.00% <100.00%> (ø)
...ck/simulator/grpc/impl/ConsumerStreamObserver.java 100.00% <100.00%> (ø)
...mulator/grpc/impl/PublishStreamGrpcClientImpl.java 90.19% <ø> (ø)
...ock/simulator/grpc/impl/PublishStreamObserver.java 100.00% <100.00%> (ø)
.../block/simulator/metrics/SimulatorMetricTypes.java 100.00% <100.00%> (ø)
...dera/block/simulator/mode/CombinedModeHandler.java 100.00% <100.00%> (ø)
...dera/block/simulator/mode/ConsumerModeHandler.java 100.00% <100.00%> (ø)
... and 3 more

... and 16 files with indirect coverage changes

Copy link
Contributor

@mattp-swirldslabs mattp-swirldslabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few suggestions

public class BlockStreamSimulatorApp {

/** Logger for this class */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to javadoc private methods or member variables

this.metricsService = requireNonNull(metricsService);
this.publishStreamGrpcClient = requireNonNull(publishStreamGrpcClient);
this.consumerStreamGrpcClient = requireNonNull(consumerStreamGrpcClient);
this.isRunning = new AtomicBoolean(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you moved the assignment from line 46 here. Why move it?

default -> throw new IllegalArgumentException("Unknown SimulatorMode: " + simulatorMode);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
  • IntelliJ is signaling the default case isn't possible given it's an enum. Bad data from the config should break before this class when BlockStreamConfig is injected.


// Configuration
private final BlockStreamConfig blockStreamConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockStreamConfig is never read. Should it be removed?

/**
* Initialize the block stream manager and load blocks into memory.
*/
void init();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change this to a default interface method like: default void init() {} so you don't have to override with a no-op comment in BlockAsFileLargeDataSets

@Override
public void init() {
// Do nothing, because we don't have real initializing and loading blocks into memory for this implementation.
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to remove this init() method. See my comment in BlockStreamManager

public void completeStreaming() throws InterruptedException {
consumerStreamObserver.onCompleted();
// todo(352) Find a suitable solution for removing the sleep
Thread.sleep(100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure but I think Netty will continue to send BlockItems to onNext() as long as the channel is open. Right now, I think onCompleted() will only release the starting thread waiting at line 97. You might need to close the channel in this method and then release the starting thread.

private void processBlockItems(List<BlockItem> blockItems) {
blockItems.stream()
.filter(BlockItem::hasBlockProof)
.forEach(__ -> metricsService.get(LiveBlocksConsumed).increment());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you start the consumer up and start streaming blockitems via the producer to the BN, it looks like nothing is happening on the consumer side. Consider changing this to something like:

blockItems.stream()
                .filter(BlockItem::hasBlockProof)
                .forEach(blockItem -> {
                    metricsService.get(LiveBlocksConsumed).increment();
                    LOGGER.log(INFO, "Received block number: " + blockItem.getBlockProof().getBlock());
                });

That should print the last block received without overwhelming the terminal. This gives the consumer parity with the producer which prints the response hashes right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Simulator Issue related to Block Stream Simulator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement consumer logic in the Simulator
2 participants