|
| 1 | +/* |
| 2 | + * Hedera Block Node |
| 3 | + * |
| 4 | + * Copyright (C) 2024 Hedera Hashgraph, LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.hedera.block.server; |
| 20 | + |
| 21 | +import com.google.protobuf.Descriptors; |
| 22 | +import com.hedera.block.protos.BlockStreamServiceGrpcProto; |
| 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; |
| 27 | +import io.grpc.stub.StreamObserver; |
| 28 | +import io.helidon.webserver.grpc.GrpcService; |
| 29 | + |
| 30 | +import java.util.logging.Logger; |
| 31 | + |
| 32 | +import static com.hedera.block.server.Constants.*; |
| 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 | + */ |
| 43 | +public class BlockStreamService implements GrpcService { |
| 44 | + |
| 45 | + private final Logger logger = Logger.getLogger(getClass().getName()); |
| 46 | + |
| 47 | + private final long timeoutThresholdMillis; |
| 48 | + private final StreamMediator<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamMediator; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructor for the BlockStreamService class. |
| 52 | + * |
| 53 | + * @param timeoutThresholdMillis the timeout threshold in milliseconds |
| 54 | + * @param streamMediator the stream mediator |
| 55 | + */ |
| 56 | + public BlockStreamService(long timeoutThresholdMillis, |
| 57 | + StreamMediator<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamMediator) { |
| 58 | + |
| 59 | + this.timeoutThresholdMillis = timeoutThresholdMillis; |
| 60 | + this.streamMediator = streamMediator; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the FileDescriptor for the BlockStreamServiceGrpcProto. |
| 65 | + * |
| 66 | + * @return the FileDescriptor for the BlockStreamServiceGrpcProto |
| 67 | + */ |
| 68 | + @Override |
| 69 | + public Descriptors.FileDescriptor proto() { |
| 70 | + return BlockStreamServiceGrpcProto.getDescriptor(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the service name for the BlockStreamService. This service name corresponds to the service name in the proto file. |
| 75 | + * |
| 76 | + * @return the service name |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public String serviceName() { |
| 80 | + return SERVICE_NAME; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Updates the routing for the BlockStreamService. It sets up the bidirectional streaming methods for the service. |
| 85 | + * |
| 86 | + * @param routing the routing for the BlockStreamService |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public void update(Routing routing) { |
| 90 | + routing.bidi(CLIENT_STREAMING_METHOD_NAME, this::streamSink); |
| 91 | + routing.bidi(SERVER_STREAMING_METHOD_NAME, this::streamSource); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * The streamSink method is called by Helidon each time a producer initiates a bidirectional stream. |
| 96 | + * |
| 97 | + * @param responseStreamObserver - Helidon provides a StreamObserver to handle responses back to the producer. |
| 98 | + * |
| 99 | + * @return a custom StreamObserver to handle streaming blocks from the producer to all subscribed consumers |
| 100 | + * via the streamMediator as well as sending responses back to the producer. |
| 101 | + */ |
| 102 | + private StreamObserver<BlockStreamServiceGrpcProto.Block> streamSink(StreamObserver<BlockStreamServiceGrpcProto.BlockResponse> responseStreamObserver) { |
| 103 | + logger.finer("Executing bidirectional streamSink method"); |
| 104 | + |
| 105 | + return new ProducerBlockStreamObserver(streamMediator, responseStreamObserver); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * The streamSource method is called by Helidon each time a consumer initiates a bidirectional stream. |
| 110 | + * |
| 111 | + * @param responseStreamObserver - Helidon provides a StreamObserver to handle responses from the consumer |
| 112 | + * back to the server. |
| 113 | + * |
| 114 | + * @return a custom StreamObserver to handle streaming blocks from the producer to the consumer as well as |
| 115 | + * handling responses from the consumer. |
| 116 | + */ |
| 117 | + private StreamObserver<BlockStreamServiceGrpcProto.BlockResponse> streamSource(StreamObserver<BlockStreamServiceGrpcProto.Block> responseStreamObserver) { |
| 118 | + logger.finer("Executing bidirectional streamSource method"); |
| 119 | + |
| 120 | + // Return a custom StreamObserver to handle streaming blocks from the producer. |
| 121 | + LiveStreamObserver<BlockStreamServiceGrpcProto.Block, BlockStreamServiceGrpcProto.BlockResponse> streamObserver = new LiveStreamObserverImpl( |
| 122 | + timeoutThresholdMillis, |
| 123 | + streamMediator, |
| 124 | + responseStreamObserver); |
| 125 | + streamMediator.subscribe(streamObserver); |
| 126 | + |
| 127 | + return streamObserver; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | + |
0 commit comments