You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ratis.apache.org by teligen_ligc <te...@163.com> on 2026/08/23 17:38:11 UTC
[Question] Ordering of setFirstRequest() and newRequest() in OrderedAsync
Hi Ratis developers,
I am studying the SlidingWindow and OrderedAsync implementation in Apache Ratis, and I have a question about the intended invariant behind the first-request flag handling in OrderedAsync#sendRequestWithRetry.
The relevant code is approximately:
final RaftClientRequest request = pending.newRequest();
if (request == null) {
return;
}
if (getSlidingWindow(request).isFirst(pending.getSeqNum())) {
pending.setFirstRequest();
}
client.getClientRpc().sendRequestAsync(request);
However, PendingOrderedRequest#newRequestImpl constructs the SlidingWindowEntry using the current value of PendingOrderedRequest.isFirst:
public RaftClientRequest newRequestImpl() {
return Optional.ofNullable(requestConstructor.get())
.map(f -> f.apply(
ProtoUtils.toSlidingWindowEntry(seqNum, isFirst)))
.orElse(null);
}
My understanding is:
pending.newRequest() copies the current value of pending.isFirst into the SlidingWindowEntry of the newly constructed RaftClientRequest.
After the RaftClientRequest has been constructed, the code checks whether the pending request is currently the first request in the corresponding SlidingWindow.
If the pending request is now the first request in the client window, pending.setFirstRequest() changes the state of PendingOrderedRequest, but it does not modify the SlidingWindowEntry already contained in the current RaftClientRequest.
Therefore, the current attempt may still be sent with first=false. The updated first=true value would only be used if another retry constructs a new RaftClientRequest later.
I may be missing an invariant or another execution path, so I would like to ask:
Is this ordering intentional?
Otherwise, should the SlidingWindow first-request check and pending.setFirstRequest() happen before pending.newRequest() constructs the RaftClientRequest?
Thank you for your time and clarification.
Best regards,
Teligen_ligc
Re: [Question] Ordering of setFirstRequest() and newRequest() in OrderedAsync
Posted by Tsz Wo Sze <sz...@apache.org>.
Hi Teligen_ligc,
Thanks for working on RATIS-2677! I see that your JIRA account has been
created and RATIS-2677 is now assigned to you. (Thanks Attila!)
Tsz-Wo
On Sun, Aug 30, 2026 at 2:16 AM teligen_ligc <te...@163.com> wrote:
> Hi Tsz-Wo,
>
>
> Thank you for the detailed investigation and for the invitation. Yes, I'd
> be happy to work on RATIS-2677.
>
>
> I don't have an ASF Jira account yet, so I will request one and share my
> Jira username once it is approved. Meanwhile, I will start preparing and
> testing the patch.
>
>
> Best regards,
> Teligen_ligc
>
>
>
>
> ----- Original Message -----
> From: "Tsz Wo Sze" <sz...@apache.org>
> To: dev@ratis.apache.org
> Sent: Thu, 27 Aug 2026 11:07:09 -0700
> Subject: Re: [Question] Ordering of setFirstRequest() and newRequest() in
> OrderedAsync
>
> Hi Teligen_ligc,
>
> Thanks for studying the code in Ratis in such great detail!
>
> Tried to add some trace info and run a test [2]. Found out that
> the setFirstRequest() in OrderedAsync is not needed since it is already set
> by SlidingWindow$Client.sendOrDelayRequest(..); see RATIS-2677 [1].
>
> Since you have raised the question, I wonder if you are interested in
> working on RATIS-2677 [1] for improving the code?
>
> Tsz-Wo
> [1] (a new JIRA) https://issues.apache.org/jira/browse/RATIS-2677
> [2] (attached to RATIS-2677)
>
> https://issues.apache.org/jira/secure/attachment/13084155/RATIS-2677_test.patch
>
> On Sun, Aug 23, 2026 at 10:38 AM teligen_ligc <te...@163.com>
> wrote:
>
> > Hi Ratis developers,
> >
> > I am studying the SlidingWindow and OrderedAsync implementation in Apache
> > Ratis, and I have a question about the intended invariant behind the
> > first-request flag handling in OrderedAsync#sendRequestWithRetry.
> >
> > The relevant code is approximately:
> >
> > final RaftClientRequest request = pending.newRequest();
> >
> > if (request == null) {
> > return;
> > }
> >
> > if (getSlidingWindow(request).isFirst(pending.getSeqNum())) {
> > pending.setFirstRequest();
> > }
> >
> > client.getClientRpc().sendRequestAsync(request);
> >
> > However, PendingOrderedRequest#newRequestImpl constructs the
> > SlidingWindowEntry using the current value of
> PendingOrderedRequest.isFirst:
> >
> > public RaftClientRequest newRequestImpl() {
> > return Optional.ofNullable(requestConstructor.get())
> > .map(f -> f.apply(
> > ProtoUtils.toSlidingWindowEntry(seqNum, isFirst)))
> > .orElse(null);
> > }
> >
> > My understanding is:
> >
> > pending.newRequest() copies the current value of pending.isFirst into the
> > SlidingWindowEntry of the newly constructed RaftClientRequest.
> >
> > After the RaftClientRequest has been constructed, the code checks
> whether
> > the pending request is currently the first request in the corresponding
> > SlidingWindow.
> >
> > If the pending request is now the first request in the client window,
> > pending.setFirstRequest() changes the state of PendingOrderedRequest, but
> > it does not modify the SlidingWindowEntry already contained in the
> current
> > RaftClientRequest.
> >
> > Therefore, the current attempt may still be sent with first=false. The
> > updated first=true value would only be used if another retry constructs a
> > new RaftClientRequest later.
> >
> > I may be missing an invariant or another execution path, so I would like
> > to ask:
> >
> > Is this ordering intentional?
> >
> > Otherwise, should the SlidingWindow first-request check and
> > pending.setFirstRequest() happen before pending.newRequest() constructs
> the
> > RaftClientRequest?
> >
> > Thank you for your time and clarification.
> >
> > Best regards,
> > Teligen_ligc
> >
> >
> >
>
Re: [Question] Ordering of setFirstRequest() and newRequest() in OrderedAsync
Posted by teligen_ligc <te...@163.com>.
Hi Tsz-Wo,
Thank you for the detailed investigation and for the invitation. Yes, I'd be happy to work on RATIS-2677.
I don't have an ASF Jira account yet, so I will request one and share my Jira username once it is approved. Meanwhile, I will start preparing and testing the patch.
Best regards,
Teligen_ligc
----- Original Message -----
From: "Tsz Wo Sze" <sz...@apache.org>
To: dev@ratis.apache.org
Sent: Thu, 27 Aug 2026 11:07:09 -0700
Subject: Re: [Question] Ordering of setFirstRequest() and newRequest() in OrderedAsync
Hi Teligen_ligc,
Thanks for studying the code in Ratis in such great detail!
Tried to add some trace info and run a test [2]. Found out that
the setFirstRequest() in OrderedAsync is not needed since it is already set
by SlidingWindow$Client.sendOrDelayRequest(..); see RATIS-2677 [1].
Since you have raised the question, I wonder if you are interested in
working on RATIS-2677 [1] for improving the code?
Tsz-Wo
[1] (a new JIRA) https://issues.apache.org/jira/browse/RATIS-2677
[2] (attached to RATIS-2677)
https://issues.apache.org/jira/secure/attachment/13084155/RATIS-2677_test.patch
On Sun, Aug 23, 2026 at 10:38 AM teligen_ligc <te...@163.com> wrote:
> Hi Ratis developers,
>
> I am studying the SlidingWindow and OrderedAsync implementation in Apache
> Ratis, and I have a question about the intended invariant behind the
> first-request flag handling in OrderedAsync#sendRequestWithRetry.
>
> The relevant code is approximately:
>
> final RaftClientRequest request = pending.newRequest();
>
> if (request == null) {
> return;
> }
>
> if (getSlidingWindow(request).isFirst(pending.getSeqNum())) {
> pending.setFirstRequest();
> }
>
> client.getClientRpc().sendRequestAsync(request);
>
> However, PendingOrderedRequest#newRequestImpl constructs the
> SlidingWindowEntry using the current value of PendingOrderedRequest.isFirst:
>
> public RaftClientRequest newRequestImpl() {
> return Optional.ofNullable(requestConstructor.get())
> .map(f -> f.apply(
> ProtoUtils.toSlidingWindowEntry(seqNum, isFirst)))
> .orElse(null);
> }
>
> My understanding is:
>
> pending.newRequest() copies the current value of pending.isFirst into the
> SlidingWindowEntry of the newly constructed RaftClientRequest.
>
> After the RaftClientRequest has been constructed, the code checks whether
> the pending request is currently the first request in the corresponding
> SlidingWindow.
>
> If the pending request is now the first request in the client window,
> pending.setFirstRequest() changes the state of PendingOrderedRequest, but
> it does not modify the SlidingWindowEntry already contained in the current
> RaftClientRequest.
>
> Therefore, the current attempt may still be sent with first=false. The
> updated first=true value would only be used if another retry constructs a
> new RaftClientRequest later.
>
> I may be missing an invariant or another execution path, so I would like
> to ask:
>
> Is this ordering intentional?
>
> Otherwise, should the SlidingWindow first-request check and
> pending.setFirstRequest() happen before pending.newRequest() constructs the
> RaftClientRequest?
>
> Thank you for your time and clarification.
>
> Best regards,
> Teligen_ligc
>
>
>
Re: [Question] Ordering of setFirstRequest() and newRequest() in OrderedAsync
Posted by Tsz Wo Sze <sz...@apache.org>.
Hi Teligen_ligc,
Thanks for studying the code in Ratis in such great detail!
Tried to add some trace info and run a test [2]. Found out that
the setFirstRequest() in OrderedAsync is not needed since it is already set
by SlidingWindow$Client.sendOrDelayRequest(..); see RATIS-2677 [1].
Since you have raised the question, I wonder if you are interested in
working on RATIS-2677 [1] for improving the code?
Tsz-Wo
[1] (a new JIRA) https://issues.apache.org/jira/browse/RATIS-2677
[2] (attached to RATIS-2677)
https://issues.apache.org/jira/secure/attachment/13084155/RATIS-2677_test.patch
On Sun, Aug 23, 2026 at 10:38 AM teligen_ligc <te...@163.com> wrote:
> Hi Ratis developers,
>
> I am studying the SlidingWindow and OrderedAsync implementation in Apache
> Ratis, and I have a question about the intended invariant behind the
> first-request flag handling in OrderedAsync#sendRequestWithRetry.
>
> The relevant code is approximately:
>
> final RaftClientRequest request = pending.newRequest();
>
> if (request == null) {
> return;
> }
>
> if (getSlidingWindow(request).isFirst(pending.getSeqNum())) {
> pending.setFirstRequest();
> }
>
> client.getClientRpc().sendRequestAsync(request);
>
> However, PendingOrderedRequest#newRequestImpl constructs the
> SlidingWindowEntry using the current value of PendingOrderedRequest.isFirst:
>
> public RaftClientRequest newRequestImpl() {
> return Optional.ofNullable(requestConstructor.get())
> .map(f -> f.apply(
> ProtoUtils.toSlidingWindowEntry(seqNum, isFirst)))
> .orElse(null);
> }
>
> My understanding is:
>
> pending.newRequest() copies the current value of pending.isFirst into the
> SlidingWindowEntry of the newly constructed RaftClientRequest.
>
> After the RaftClientRequest has been constructed, the code checks whether
> the pending request is currently the first request in the corresponding
> SlidingWindow.
>
> If the pending request is now the first request in the client window,
> pending.setFirstRequest() changes the state of PendingOrderedRequest, but
> it does not modify the SlidingWindowEntry already contained in the current
> RaftClientRequest.
>
> Therefore, the current attempt may still be sent with first=false. The
> updated first=true value would only be used if another retry constructs a
> new RaftClientRequest later.
>
> I may be missing an invariant or another execution path, so I would like
> to ask:
>
> Is this ordering intentional?
>
> Otherwise, should the SlidingWindow first-request check and
> pending.setFirstRequest() happen before pending.newRequest() constructs the
> RaftClientRequest?
>
> Thank you for your time and clarification.
>
> Best regards,
> Teligen_ligc
>
>
>