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

Allow sending streams and not just receiving them #121

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions core/src/main/scala/com/devsisters/shardcake/interfaces/Pods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,28 @@ trait Pods {
*/
def sendMessage(pod: PodAddress, message: BinaryMessage): Task[Option[Array[Byte]]]

/**
* Send a stream of messages to a pod
*/
def sendStream(
pod: PodAddress,
entityId: String,
messages: ZStream[Any, Throwable, BinaryMessage]
): Task[Option[Array[Byte]]]

/**
* Send a message to a pod and receive a stream of replies
*/
def sendMessageStreaming(pod: PodAddress, message: BinaryMessage): ZStream[Any, Throwable, Array[Byte]]
def sendMessageAndReceiveStream(pod: PodAddress, message: BinaryMessage): ZStream[Any, Throwable, Array[Byte]]

Choose a reason for hiding this comment

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

Any particular reason to diverge the wording between sendMessageAndReceiveStream and sendAndReceiveStream?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mostly to avoid renaming existing sendMessage which would increase breaking changes.


/**
* Send a stream of messages to a pod and receive a stream of replies
*/
def sendStreamAndReceiveStream(
pod: PodAddress,
entityId: String,
messages: ZStream[Any, Throwable, BinaryMessage]
): ZStream[Any, Throwable, Array[Byte]]
}

object Pods {
Expand All @@ -46,12 +64,22 @@ object Pods {
*/
val noop: ULayer[Pods] =
ZLayer.succeed(new Pods {
def assignShards(pod: PodAddress, shards: Set[ShardId]): Task[Unit] = ZIO.unit
def unassignShards(pod: PodAddress, shards: Set[ShardId]): Task[Unit] = ZIO.unit
def ping(pod: PodAddress): Task[Unit] = ZIO.unit
def sendMessage(pod: PodAddress, message: BinaryMessage): Task[Option[Array[Byte]]] = ZIO.none
def sendMessageStreaming(pod: PodAddress, message: BinaryMessage): ZStream[Any, Throwable, Array[Byte]] =
def assignShards(pod: PodAddress, shards: Set[ShardId]): Task[Unit] = ZIO.unit
def unassignShards(pod: PodAddress, shards: Set[ShardId]): Task[Unit] = ZIO.unit
def ping(pod: PodAddress): Task[Unit] = ZIO.unit
def sendMessage(pod: PodAddress, message: BinaryMessage): Task[Option[Array[Byte]]] = ZIO.none
def sendStream(
pod: PodAddress,
entityId: String,
messages: ZStream[Any, Throwable, BinaryMessage]
): Task[Option[Array[Byte]]] = ZIO.none
def sendMessageAndReceiveStream(pod: PodAddress, message: BinaryMessage): ZStream[Any, Throwable, Array[Byte]] =
ZStream.empty
def sendStreamAndReceiveStream(
pod: PodAddress,
entityId: String,
messages: ZStream[Any, Throwable, BinaryMessage]
): ZStream[Any, Throwable, Array[Byte]] = ZStream.empty
})

case class BinaryMessage(entityId: String, entityType: String, body: Array[Byte], replyId: Option[String])
Expand Down
51 changes: 47 additions & 4 deletions entities/src/main/scala/com/devsisters/shardcake/Messenger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ trait Messenger[-Msg] {
* streaming responses. See `sendStreamAutoRestart` for an alternative that will automatically restart the stream
* in case of rebalance.
*/
def sendStream[Res](entityId: String)(msg: StreamReplier[Res] => Msg): Task[ZStream[Any, Throwable, Res]]
def sendAndReceiveStream[Res](entityId: String)(msg: StreamReplier[Res] => Msg): Task[ZStream[Any, Throwable, Res]]

/**
* Send a stream of messages.
*/
def sendStream(entityId: String)(messages: ZStream[Any, Throwable, Msg]): Task[Unit]

/**
* Send a stream of messages and receive a stream of responses of type `Res`.
*/
def sendStreamAndReceiveStream[Res](entityId: String)(
messages: StreamReplier[Res] => ZStream[Any, Throwable, Msg]
): Task[ZStream[Any, Throwable, Res]]

/**
* Send a message and receive a stream of responses of type `Res` while restarting the stream when the remote entity
Expand All @@ -38,11 +50,42 @@ trait Messenger[-Msg] {
* cursor from the responses so that when the remote entity is rebalanced, a new message can be sent with the right
* cursor according to what we've seen in the previous stream of responses.
*/
def sendStreamAutoRestart[Cursor, Res](entityId: String, cursor: Cursor)(msg: (Cursor, StreamReplier[Res]) => Msg)(
def sendAndReceiveStreamAutoRestart[Cursor, Res](entityId: String, cursor: Cursor)(
msg: (Cursor, StreamReplier[Res]) => Msg
)(
updateCursor: (Cursor, Res) => Cursor
): ZStream[Any, Throwable, Res] =
ZStream
.unwrap(sendAndReceiveStream[Res](entityId)(msg(cursor, _)))
.either
.mapAccum(cursor) {
case (c, Right(res)) => updateCursor(c, res) -> Right(res)
case (c, Left(err)) => (c, Left(c -> err))
}
.flatMap {
case Right(res) => ZStream.succeed(res)
case Left((lastSeenCursor, StreamCancelled)) =>
ZStream.execute(ZIO.sleep(200.millis)) ++
sendAndReceiveStreamAutoRestart(entityId, lastSeenCursor)(msg)(updateCursor)
case Left((_, err)) => ZStream.fail(err)
}

/**
* Send a stream of messages and receive a stream of responses of type `Res` while restarting the stream when the
* remote entity is rebalanced.
*
* To do so, we need a "cursor" so the stream of responses can be restarted where it ended before the rebalance. That
* is, the first message sent to the remote entity contains the given initial cursor value and we extract an updated
* cursor from the responses so that when the remote entity is rebalanced, a new message can be sent with the right
* cursor according to what we've seen in the previous stream of responses.
*/
def sendStreamAndReceiveStreamAutoRestart[Cursor, Res](entityId: String, cursor: Cursor)(
msg: (Cursor, StreamReplier[Res]) => ZStream[Any, Throwable, Msg]
)(
updateCursor: (Cursor, Res) => Cursor
): ZStream[Any, Throwable, Res] =
ZStream
.unwrap(sendStream[Res](entityId)(msg(cursor, _)))
.unwrap(sendStreamAndReceiveStream[Res](entityId)(msg(cursor, _)))
.either
.mapAccum(cursor) {
case (c, Right(res)) => updateCursor(c, res) -> Right(res)
Expand All @@ -52,7 +95,7 @@ trait Messenger[-Msg] {
case Right(res) => ZStream.succeed(res)
case Left((lastSeenCursor, StreamCancelled)) =>
ZStream.execute(ZIO.sleep(200.millis)) ++
sendStreamAutoRestart(entityId, lastSeenCursor)(msg)(updateCursor)
sendStreamAndReceiveStreamAutoRestart(entityId, lastSeenCursor)(msg)(updateCursor)
case Left((_, err)) => ZStream.fail(err)
}
}
Expand Down
Loading
Loading