Skip to content

Commit 3dd03b6

Browse files
wip: docs
Signed-off-by: Matt Peterson <matt.peterson@swirldslabs.com>
1 parent 1f62b7e commit 3dd03b6

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

server/src/main/java/com/hedera/block/server/consumer/ConsumerBlockItemObserver.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616

1717
package com.hedera.block.server.consumer;
1818

19-
import static com.hedera.block.protos.BlockStreamService.BlockItem;
20-
import static com.hedera.block.protos.BlockStreamService.SubscribeStreamResponse;
21-
2219
import com.hedera.block.server.data.ObjectEvent;
2320
import com.hedera.block.server.mediator.StreamMediator;
2421
import io.grpc.stub.ServerCallStreamObserver;
2522
import io.grpc.stub.StreamObserver;
23+
2624
import java.time.InstantSource;
2725
import java.util.concurrent.atomic.AtomicBoolean;
2826

27+
import static com.hedera.block.protos.BlockStreamService.BlockItem;
28+
import static com.hedera.block.protos.BlockStreamService.SubscribeStreamResponse;
29+
2930
/**
30-
* The LiveStreamObserverImpl class implements the LiveStreamObserver interface to pass blocks to
31-
* the downstream consumer via the notify method and manage the bidirectional stream to the consumer
32-
* via the onNext, onError, and onCompleted methods.
31+
* The ConsumerBlockItemObserver class is the primary integration point between the LMAX Disruptor and
32+
* an instance of a downstream consumer (represented by subscribeStreamResponseObserver provided by Helidon).
33+
* The ConsumerBlockItemObserver implements the EventHandler interface so the Disruptor can invoke
34+
* the onEvent() method when a new SubscribeStreamResponse is available.
3335
*/
3436
public class ConsumerBlockItemObserver
3537
implements BlockItemEventHandler<ObjectEvent<SubscribeStreamResponse>> {
@@ -50,9 +52,12 @@ public class ConsumerBlockItemObserver
5052
protected Runnable onClose;
5153

5254
/**
53-
* Constructor for the LiveStreamObserverImpl class.
55+
* Constructor for the ConsumerBlockItemObserver class.
5456
*
55-
* @param subscribeStreamResponseObserver the response stream observer
57+
* @param timeoutThresholdMillis The timeout threshold in milliseconds.
58+
* @param producerLivenessClock The producer liveness clock.
59+
* @param streamMediator The StreamMediator instance.
60+
* @param subscribeStreamResponseObserver The StreamObserver instance.
5661
*/
5762
public ConsumerBlockItemObserver(
5863
final long timeoutThresholdMillis,
@@ -109,23 +114,28 @@ public void onEvent(
109114
final long currentMillis = producerLivenessClock.millis();
110115
if (currentMillis - producerLivenessMillis > timeoutThresholdMillis) {
111116
streamMediator.unsubscribe(this);
112-
LOGGER.log(System.Logger.Level.DEBUG, "Unsubscribed handler");
117+
LOGGER.log(System.Logger.Level.DEBUG, "Unsubscribed ConsumerBlockItemObserver due to producer timeout");
113118
} else {
114119

115-
// Refresh the producer liveness and pass the BlockItem to the downstream observer.
116-
producerLivenessMillis = currentMillis;
117-
118-
// Only start sending BlockItems after we've reached
119-
// the beginning of a block.
120-
final SubscribeStreamResponse subscribeStreamResponse = event.get();
121-
final BlockItem blockItem = subscribeStreamResponse.getBlockItem();
122-
if (!streamStarted && blockItem.hasHeader()) {
123-
streamStarted = true;
124-
}
125-
126-
if (streamStarted && isResponsePermitted.get()) {
127-
LOGGER.log(System.Logger.Level.INFO, "Send BlockItem downstream: {0} ", blockItem);
128-
subscribeStreamResponseObserver.onNext(subscribeStreamResponse);
120+
// Only send the response if the consumer has not cancelled
121+
// or closed the stream.
122+
if (isResponsePermitted.get()) {
123+
124+
// Refresh the producer liveness and pass the BlockItem to the downstream observer.
125+
producerLivenessMillis = currentMillis;
126+
127+
// Only start sending BlockItems after we've reached
128+
// the beginning of a block.
129+
final SubscribeStreamResponse subscribeStreamResponse = event.get();
130+
final BlockItem blockItem = subscribeStreamResponse.getBlockItem();
131+
if (!streamStarted && blockItem.hasHeader()) {
132+
streamStarted = true;
133+
}
134+
135+
if (streamStarted) {
136+
LOGGER.log(System.Logger.Level.DEBUG, "Send BlockItem downstream: {0} ", blockItem);
137+
subscribeStreamResponseObserver.onNext(subscribeStreamResponse);
138+
}
129139
}
130140
}
131141
}

server/src/main/java/com/hedera/block/server/mediator/StreamMediator.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@
2020
import java.io.IOException;
2121

2222
/**
23-
* The StreamMediator interface represents a one-to-many bridge between a bidirectional stream of
24-
* blocks from a producer (e.g. a Consensus Node) and N consumers each requesting a bidirectional
25-
* connection to get a "live stream" of blocks from the producer. StreamMediator satisfies Helidon's
26-
* type requirements for a bidirectional StreamObserver representing a stream of blocks returned
27-
* FROM the downstream consuming client. However, the StreamObserver type may be distinct from Block
28-
* type streamed TO the client. The type definition for the onNext() method provides the flexibility
29-
* for the StreamObserver and the Block types to vary independently.
23+
* The StreamMediator interface represents a bridge between a bidirectional stream of
24+
* items from a producer (e.g. a Consensus Node) and N consumers each requesting a gRPC
25+
* server stream of items. The StreamMediator manages adding and removing consumers
26+
* dynamically to receive items as they arrive from upstream.
3027
*
31-
* @param <U> The type required by the RingBuffer implementation
28+
* @param <U> The type of items sent by the upstream producer.
29+
* @param <V> The type of the response published to the downstream consumers.
3230
*/
3331
public interface StreamMediator<U, V> {
3432

33+
/**
34+
* Publishes an item received from a producer to the downstream consumers.
35+
*
36+
* @param blockItem The block item to publish.
37+
* @throws IOException .
38+
*/
3539
void publishEvent(final U blockItem) throws IOException;
3640

3741
void subscribe(final BlockItemEventHandler<V> handler);

0 commit comments

Comments
 (0)