Skip to content

Commit da018ad

Browse files
authored
[fga] retry write relationships (#18683)
1 parent d83c242 commit da018ad

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

components/server/src/authorization/spicedb-authorizer.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,40 @@ export class SpiceDBAuthorizer {
5555
}
5656

5757
async writeRelationships(...updates: v1.RelationshipUpdate[]): Promise<v1.WriteRelationshipsResponse | undefined> {
58-
const timer = spicedbClientLatency.startTimer();
59-
let error: Error | undefined;
60-
try {
61-
const response = await this.client.writeRelationships(
62-
v1.WriteRelationshipsRequest.create({
63-
updates,
64-
}),
65-
this.callOptions,
66-
);
67-
log.info("[spicedb] Successfully wrote relationships.", { response, updates });
58+
let tries = 0;
59+
// we do sometimes see INTERNAL errors from SpiceDB, so we retry a few times
60+
// last time we checked it was 15 times per day (check logs)
61+
while (tries++ < 3) {
62+
const timer = spicedbClientLatency.startTimer();
63+
let error: Error | undefined;
64+
try {
65+
const response = await this.client.writeRelationships(
66+
v1.WriteRelationshipsRequest.create({
67+
updates,
68+
}),
69+
this.callOptions,
70+
);
71+
log.info("[spicedb] Successfully wrote relationships.", { response, updates, tries });
6872

69-
return response;
70-
} catch (err) {
71-
error = err;
72-
log.error("[spicedb] Failed to write relationships.", err, { updates: new TrustedValue(updates) });
73-
} finally {
74-
observeSpicedbClientLatency("write", error, timer());
73+
return response;
74+
} catch (err) {
75+
error = err;
76+
if (err.code === grpc.status.INTERNAL && tries < 3) {
77+
log.warn("[spicedb] Failed to write relationships.", err, {
78+
updates: new TrustedValue(updates),
79+
tries,
80+
});
81+
} else {
82+
log.error("[spicedb] Failed to write relationships.", err, {
83+
updates: new TrustedValue(updates),
84+
tries,
85+
});
86+
// we don't try again on other errors
87+
return;
88+
}
89+
} finally {
90+
observeSpicedbClientLatency("write", error, timer());
91+
}
7592
}
7693
}
7794

components/server/src/orgs/usage-service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { inject, injectable } from "inversify";
1717
import { Authorizer } from "../authorization/authorizer";
1818
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1919
import { CostCenterJSON, ListUsageRequest, ListUsageResponse } from "@gitpod/gitpod-protocol/lib/usage";
20+
import { TrustedValue } from "@gitpod/gitpod-protocol/lib/util/scrubbing";
2021

2122
@injectable()
2223
export class UsageService {
@@ -149,19 +150,19 @@ export class UsageService {
149150
const currentInvoiceCredits = creditBalance.usedCredits;
150151
const usageLimit = creditBalance.usageLimit;
151152
if (currentInvoiceCredits >= usageLimit) {
152-
log.info({ userId }, "Usage limit reached", {
153+
log.info({ userId, organizationId }, "Usage limit reached", {
153154
attributionId,
154-
currentInvoiceCredits,
155+
currentInvoiceCredits: new TrustedValue(currentInvoiceCredits),
155156
usageLimit,
156157
});
157158
return {
158159
reached: true,
159160
attributionId,
160161
};
161162
} else if (currentInvoiceCredits > usageLimit * 0.8) {
162-
log.info({ userId }, "Usage limit almost reached", {
163+
log.info({ userId, organizationId }, "Usage limit almost reached", {
163164
attributionId,
164-
currentInvoiceCredits,
165+
currentInvoiceCredits: new TrustedValue(currentInvoiceCredits),
165166
usageLimit,
166167
});
167168
return {

0 commit comments

Comments
 (0)