Skip to content

Commit 908c22a

Browse files
authored
Merge pull request #2884 from murgatroid99/grpc-js_call_committed_consistency
grpc-js: Always use RetryingCall, always call onCommitted
2 parents 5a942ed + a5b6178 commit 908c22a

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

packages/grpc-js/src/internal-channel.ts

-27
Original file line numberDiff line numberDiff line change
@@ -705,33 +705,6 @@ export class InternalChannel {
705705
);
706706
}
707707

708-
createInnerCall(
709-
callConfig: CallConfig,
710-
method: string,
711-
host: string,
712-
credentials: CallCredentials,
713-
deadline: Deadline
714-
): LoadBalancingCall | RetryingCall {
715-
// Create a RetryingCall if retries are enabled
716-
if (this.options['grpc.enable_retries'] === 0) {
717-
return this.createLoadBalancingCall(
718-
callConfig,
719-
method,
720-
host,
721-
credentials,
722-
deadline
723-
);
724-
} else {
725-
return this.createRetryingCall(
726-
callConfig,
727-
method,
728-
host,
729-
credentials,
730-
deadline
731-
);
732-
}
733-
}
734-
735708
createResolvingCall(
736709
method: string,
737710
deadline: Deadline,

packages/grpc-js/src/load-balancing-call.ts

-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ export class LoadBalancingCall implements Call, DeadlineInfoProvider {
254254
);
255255
return;
256256
}
257-
this.callConfig.onCommitted?.();
258257
pickResult.onCallStarted?.();
259258
this.onCallEnded = pickResult.onCallEnded;
260259
this.trace(

packages/grpc-js/src/resolving-call.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class ResolvingCall implements Call {
245245
this.filterStack = this.filterStackFactory.createFilter();
246246
this.filterStack.sendMetadata(Promise.resolve(this.metadata)).then(
247247
filteredMetadata => {
248-
this.child = this.channel.createInnerCall(
248+
this.child = this.channel.createRetryingCall(
249249
config,
250250
this.method,
251251
this.host,

packages/grpc-js/src/retrying-call.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ interface UnderlyingCall {
133133
* transparent retry attempts may still be sent
134134
* COMMITTED: One attempt is committed, and no new attempts will be
135135
* sent
136+
* NO_RETRY: Retries are disabled. Exists to track the transition to COMMITTED
136137
*/
137-
type RetryingCallState = 'RETRY' | 'HEDGING' | 'TRANSPARENT_ONLY' | 'COMMITTED';
138+
type RetryingCallState = 'RETRY' | 'HEDGING' | 'TRANSPARENT_ONLY' | 'COMMITTED' | 'NO_RETRY';
138139

139140
/**
140141
* The different types of objects that can be stored in the write buffer, with
@@ -229,6 +230,9 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
229230
} else if (callConfig.methodConfig.hedgingPolicy) {
230231
this.state = 'HEDGING';
231232
this.maxAttempts = Math.min(callConfig.methodConfig.hedgingPolicy.maxAttempts, maxAttemptsLimit);
233+
} else if (channel.getOptions()['grpc.enable_retries'] === 0) {
234+
this.state = 'NO_RETRY';
235+
this.maxAttempts = 1;
232236
} else {
233237
this.state = 'TRANSPARENT_ONLY';
234238
this.maxAttempts = 1;
@@ -318,8 +322,15 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
318322
if (this.state !== 'COMMITTED') {
319323
return;
320324
}
321-
const earliestNeededMessageIndex =
322-
this.underlyingCalls[this.committedCallIndex!].nextMessageToSend;
325+
let earliestNeededMessageIndex: number;
326+
if (this.underlyingCalls[this.committedCallIndex!].state === 'COMPLETED') {
327+
/* If the committed call is completed, clear all messages, even if some
328+
* have not been sent. */
329+
earliestNeededMessageIndex = this.getNextBufferIndex();
330+
} else {
331+
earliestNeededMessageIndex =
332+
this.underlyingCalls[this.committedCallIndex!].nextMessageToSend;
333+
}
323334
for (
324335
let messageIndex = this.writeBufferOffset;
325336
messageIndex < earliestNeededMessageIndex;
@@ -343,16 +354,14 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
343354
if (this.state === 'COMMITTED') {
344355
return;
345356
}
346-
if (this.underlyingCalls[index].state === 'COMPLETED') {
347-
return;
348-
}
349357
this.trace(
350358
'Committing call [' +
351359
this.underlyingCalls[index].call.getCallNumber() +
352360
'] at index ' +
353361
index
354362
);
355363
this.state = 'COMMITTED';
364+
this.callConfig.onCommitted?.();
356365
this.committedCallIndex = index;
357366
for (let i = 0; i < this.underlyingCalls.length; i++) {
358367
if (i === index) {
@@ -471,6 +480,7 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
471480
) {
472481
switch (this.state) {
473482
case 'COMMITTED':
483+
case 'NO_RETRY':
474484
case 'TRANSPARENT_ONLY':
475485
this.commitCall(callIndex);
476486
this.reportStatus(status);
@@ -566,6 +576,11 @@ export class RetryingCall implements Call, DeadlineInfoProvider {
566576
this.reportStatus(status);
567577
return;
568578
}
579+
if (this.state === 'NO_RETRY') {
580+
this.commitCall(callIndex);
581+
this.reportStatus(status);
582+
return;
583+
}
569584
if (this.state === 'COMMITTED') {
570585
this.reportStatus(status);
571586
return;

0 commit comments

Comments
 (0)