|
20 | 20 |
|
21 | 21 | import com.google.protobuf.Descriptors;
|
22 | 22 | import com.hedera.block.protos.BlockStreamServiceGrpcProto;
|
23 |
| -import com.hedera.block.server.observe.InboundBlockStreamObserver; |
24 |
| -import com.hedera.block.server.observe.mediate.StreamMediator; |
25 |
| -import com.hedera.block.server.observe.mediate.StreamMediatorImpl; |
26 |
| -import com.hedera.block.server.observe.subscribe.HistoricStreamObserver; |
27 |
| -import com.hedera.block.server.observe.subscribe.LiveStreamObserver; |
28 |
| -import com.hedera.block.server.persistence.WriteThroughCacheHandler; |
29 |
| -import com.hedera.block.server.persistence.cache.BlockCache; |
30 |
| -import com.hedera.block.server.persistence.cache.LRUCache; |
31 |
| -import com.hedera.block.server.persistence.storage.BlockStorage; |
32 |
| -import com.hedera.block.server.persistence.storage.FileSystemBlockStorage; |
| 23 | +import com.hedera.block.server.producer.ProducerBlockStreamObserver; |
| 24 | +import com.hedera.block.server.mediator.StreamMediator; |
| 25 | +import com.hedera.block.server.consumer.LiveStreamObserverImpl; |
| 26 | +import com.hedera.block.server.consumer.LiveStreamObserver; |
33 | 27 | import io.grpc.stub.StreamObserver;
|
34 |
| -import io.helidon.config.Config; |
35 | 28 | import io.helidon.webserver.grpc.GrpcService;
|
36 | 29 |
|
37 |
| -import java.io.IOException; |
38 | 30 | import java.util.logging.Logger;
|
39 | 31 |
|
40 | 32 | import static com.hedera.block.server.Constants.*;
|
41 | 33 |
|
| 34 | +/** |
| 35 | + * This class implements the GrpcService interface and provides the functionality for the BlockStreamService. |
| 36 | + * It sets up the bidirectional streaming methods for the service and handles the routing for these methods. |
| 37 | + * It also initializes the StreamMediator, BlockStorage, and BlockCache upon creation. |
| 38 | + * |
| 39 | + * <p>The class provides two main methods, streamSink and streamSource, which handle the client and server streaming |
| 40 | + * respectively. These methods return custom StreamObservers which are used to observe and respond to the streams. |
| 41 | + * |
| 42 | + */ |
42 | 43 | public class BlockStreamService implements GrpcService {
|
43 | 44 |
|
44 | 45 | private final Logger logger = Logger.getLogger(getClass().getName());
|
45 |
| - private final StreamMediator<BlockStreamServiceGrpcProto.BlockResponse, BlockStreamServiceGrpcProto.Block> streamMediator; |
46 |
| - |
47 |
| - public BlockStreamService() { |
48 |
| - try { |
49 |
| - |
50 |
| - Config config = Config.create(); |
51 |
| - Config.global(config); |
52 |
| - |
53 |
| - BlockStorage<BlockStreamServiceGrpcProto.Block> blockStorage = new FileSystemBlockStorage(BLOCKNODE_STORAGE_ROOT_PATH_KEY, config); |
54 |
| - BlockCache<BlockStreamServiceGrpcProto.Block> blockCache = new LRUCache(10L); |
55 |
| - this.streamMediator = new StreamMediatorImpl(new WriteThroughCacheHandler(blockStorage, blockCache)); |
56 |
| - |
57 |
| - } catch (IOException e) { |
58 |
| - throw new RuntimeException(e); |
59 |
| - } |
| 46 | + private final StreamMediator<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamMediator; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor for the BlockStreamService class. It initializes the StreamMediator. |
| 50 | + * |
| 51 | + * @param streamMediator the stream mediator |
| 52 | + */ |
| 53 | + public BlockStreamService(StreamMediator<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamMediator) { |
| 54 | + this.streamMediator = streamMediator; |
60 | 55 | }
|
61 | 56 |
|
| 57 | + /** |
| 58 | + * Returns the FileDescriptor for the BlockStreamServiceGrpcProto. |
| 59 | + * |
| 60 | + * @return the FileDescriptor for the BlockStreamServiceGrpcProto |
| 61 | + */ |
62 | 62 | @Override
|
63 | 63 | public Descriptors.FileDescriptor proto() {
|
64 | 64 | return BlockStreamServiceGrpcProto.getDescriptor();
|
65 | 65 | }
|
66 | 66 |
|
| 67 | + /** |
| 68 | + * Returns the service name for the BlockStreamService. This service name corresponds to the service name in the proto file. |
| 69 | + * |
| 70 | + * @return the service name |
| 71 | + */ |
67 | 72 | @Override
|
68 | 73 | public String serviceName() {
|
69 | 74 | return SERVICE_NAME;
|
70 | 75 | }
|
71 | 76 |
|
| 77 | + /** |
| 78 | + * Updates the routing for the BlockStreamService. It sets up the bidirectional streaming methods for the service. |
| 79 | + * |
| 80 | + * @param routing the routing for the BlockStreamService |
| 81 | + */ |
72 | 82 | @Override
|
73 | 83 | public void update(Routing routing) {
|
74 | 84 | routing.bidi(CLIENT_STREAMING_METHOD_NAME, this::streamSink);
|
75 | 85 | routing.bidi(SERVER_STREAMING_METHOD_NAME, this::streamSource);
|
76 | 86 | }
|
77 | 87 |
|
| 88 | + /** |
| 89 | + * The streamSink method is called by Helidon each time a producer initiates a bidirectional stream. |
| 90 | + * |
| 91 | + * @param responseStreamObserver - Helidon provides a StreamObserver to handle responses back to the producer. |
| 92 | + * |
| 93 | + * @return a custom StreamObserver to handle streaming blocks from the producer to all subscribed consumers |
| 94 | + * via the streamMediator as well as sending responses back to the producer. |
| 95 | + */ |
78 | 96 | private StreamObserver<BlockStreamServiceGrpcProto.Block> streamSink(StreamObserver<BlockStreamServiceGrpcProto.BlockResponse> responseStreamObserver) {
|
79 |
| - logger.info("Executing bidirectional streamSink method"); |
| 97 | + logger.finer("Executing bidirectional streamSink method"); |
80 | 98 |
|
81 |
| - return new InboundBlockStreamObserver(streamMediator, responseStreamObserver); |
| 99 | + return new ProducerBlockStreamObserver(streamMediator, responseStreamObserver); |
82 | 100 | }
|
83 | 101 |
|
| 102 | + /** |
| 103 | + * The streamSource method is called by Helidon each time a consumer initiates a bidirectional stream. |
| 104 | + * |
| 105 | + * @param responseStreamObserver - Helidon provides a StreamObserver to handle responses from the consumer |
| 106 | + * back to the server. |
| 107 | + * |
| 108 | + * @return a custom StreamObserver to handle streaming blocks from the producer to the consumer as well as |
| 109 | + * handling responses from the consumer. |
| 110 | + */ |
84 | 111 | private StreamObserver<BlockStreamServiceGrpcProto.BlockResponse> streamSource(StreamObserver<BlockStreamServiceGrpcProto.Block> responseStreamObserver) {
|
85 |
| - logger.info("Executing bidirectional streamSource method"); |
| 112 | + logger.finer("Executing bidirectional streamSource method"); |
86 | 113 |
|
87 |
| - LiveStreamObserver<BlockStreamServiceGrpcProto.BlockResponse, BlockStreamServiceGrpcProto.Block> streamObserver = new HistoricStreamObserver( |
| 114 | + // Return a custom StreamObserver to handle streaming blocks from the producer. |
| 115 | + LiveStreamObserver<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamObserver = new LiveStreamObserverImpl( |
88 | 116 | streamMediator,
|
89 | 117 | responseStreamObserver);
|
90 | 118 | streamMediator.subscribe(streamObserver);
|
|
0 commit comments