-
Notifications
You must be signed in to change notification settings - Fork 1
add query to client/server #36
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
Merged
somdoron
merged 5 commits into
unit-finance:main
from
somdoron:005-client-server-libraries
Oct 28, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
client-server-client/src/main/scala/zio/raft/client/PendingQueries.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package zio.raft.client | ||
|
|
||
| import zio.* | ||
| import zio.raft.protocol.* | ||
| import scodec.bits.ByteVector | ||
| import java.time.Instant | ||
|
|
||
| /** Manages pending queries with lastSentAt timestamps for retry. */ | ||
| case class PendingQueries( | ||
| queries: Map[CorrelationId, PendingQueries.PendingQueryData] | ||
| ) { | ||
| def contains(correlationId: CorrelationId): Boolean = queries.contains(correlationId) | ||
|
|
||
| def add( | ||
| correlationId: CorrelationId, | ||
| payload: ByteVector, | ||
| promise: Promise[Nothing, ByteVector], | ||
| sentAt: Instant | ||
| ): PendingQueries = | ||
| copy(queries = queries.updated(correlationId, PendingQueries.PendingQueryData(payload, promise, sentAt, sentAt))) | ||
|
|
||
| def complete(correlationId: CorrelationId, result: ByteVector): UIO[PendingQueries] = | ||
| queries.get(correlationId) match { | ||
| case Some(data) => data.promise.succeed(result).as(copy(queries = queries.removed(correlationId))) | ||
| case None => ZIO.succeed(this) | ||
| } | ||
|
|
||
| /** Resend all pending queries (used after successful connection). */ | ||
| def resendAll(transport: ClientTransport): UIO[PendingQueries] = | ||
| ZIO.foldLeft(queries.toList)(this) { case (pending, (correlationId, data)) => | ||
| for { | ||
| now <- Clock.instant | ||
| _ <- transport.sendMessage(Query(correlationId, data.payload, now)).orDie | ||
| _ <- ZIO.logDebug(s"Resending pending query: ${CorrelationId.unwrap(correlationId)}") | ||
| updatedData = data.copy(lastSentAt = now) | ||
| } yield PendingQueries(pending.queries.updated(correlationId, updatedData)) | ||
| } | ||
|
|
||
| /** Resend expired queries and update lastSentAt. */ | ||
| def resendExpired(transport: ClientTransport, currentTime: Instant, timeout: Duration): UIO[PendingQueries] = | ||
| ZIO.foldLeft(queries.toList)(this) { case (pending, (correlationId, data)) => | ||
| val elapsed = Duration.fromInterval(data.lastSentAt, currentTime) | ||
| if (elapsed > timeout) { | ||
| for { | ||
| _ <- transport.sendMessage(Query(correlationId, data.payload, currentTime)).orDie | ||
| _ <- ZIO.logDebug(s"Resending timed out query: ${CorrelationId.unwrap(correlationId)}") | ||
| updatedData = data.copy(lastSentAt = currentTime) | ||
| } yield PendingQueries(pending.queries.updated(correlationId, updatedData)) | ||
| } else { | ||
| ZIO.succeed(pending) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object PendingQueries { | ||
| def empty: PendingQueries = PendingQueries(Map.empty) | ||
|
|
||
| case class PendingQueryData( | ||
| payload: ByteVector, | ||
| promise: Promise[Nothing, ByteVector], | ||
| createdAt: Instant, | ||
| lastSentAt: Instant | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
client-server-client/src/test/scala/zio/raft/client/PendingQueriesSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package zio.raft.client | ||
|
|
||
| import zio.* | ||
| import zio.test.* | ||
| import zio.test.Assertion.* | ||
| import scodec.bits.ByteVector | ||
| import zio.raft.protocol.* | ||
| import zio.stream.ZStream | ||
|
|
||
| object PendingQueriesSpec extends ZIOSpecDefault { | ||
|
|
||
| override def spec: Spec[Environment & TestEnvironment & Scope, Any] = | ||
| suiteAll("PendingQueries") { | ||
|
|
||
| test("resendAll resends all pending queries and updates lastSentAt") { | ||
| for { | ||
| p <- Promise.make[Nothing, ByteVector] | ||
| now <- Clock.instant | ||
| pq = PendingQueries.empty.add(CorrelationId.fromString("c1"), ByteVector(1,2,3), p, now) | ||
| sentRef <- Ref.make(0) | ||
| transport = new ClientTransport { | ||
| def connect(address: String) = ZIO.unit | ||
| def disconnect() = ZIO.unit | ||
| def sendMessage(message: ClientMessage) = sentRef.update(_ + 1).unit | ||
| def incomingMessages = ZStream.empty | ||
| } | ||
| _ <- pq.resendAll(transport) | ||
| sent <- sentRef.get | ||
| } yield assertTrue(sent == 1) | ||
| } | ||
|
|
||
| test("complete delivers single completion and removes pending entry") { | ||
| for { | ||
| p <- Promise.make[Nothing, ByteVector] | ||
| now <- Clock.instant | ||
| cid = CorrelationId.fromString("c2") | ||
| pq = PendingQueries.empty.add(cid, ByteVector(9), p, now) | ||
| pq2 <- pq.complete(cid, ByteVector(7)) | ||
| r <- p.await | ||
| } yield assertTrue(r == ByteVector(7)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment references task T024 which is already marked as complete in tasks.md. This comment should be removed or updated since the implementation is now present.