Skip to content

Commit

Permalink
Unconditionally add Transaction attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Oct 18, 2024
1 parent 927656b commit 508243f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
12 changes: 7 additions & 5 deletions observability-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ describe('EndToEnd', () => {
'Cache hit: has usable session',
'Acquired session',
'Using Session',
'Transaction Attempt Succeeded',
];
assert.deepStrictEqual(
actualEventNames,
Expand Down Expand Up @@ -1610,6 +1611,7 @@ SELECT 1p
'Transaction Attempt Failed',
'Transaction Attempt Aborted',
'exception',
'exception',
];
assert.deepStrictEqual(
actualEventNames,
Expand Down Expand Up @@ -1641,7 +1643,6 @@ SELECT 1p
);
}

console.log('flushing now');
provider.forceFlush();
assertDatabaseRunPlusAwaitTransactionForAlreadyExistentData();
});
Expand Down Expand Up @@ -1793,8 +1794,6 @@ describe('Traces for ExecuteStream broken stream retries', () => {
});
await database.close();

console.log('done here');

const requests = spannerMock
.getRequests()
.filter(val => (val as v1.ExecuteSqlRequest).sql)
Expand Down Expand Up @@ -2101,8 +2100,8 @@ describe('Traces for ExecuteStream broken stream retries', () => {
const [updateCount] = await tx!.runUpdate(insertSql);
assert.strictEqual(updateCount, 1);
await tx!.commit();
await database.close();
});
await database.close();

// The span for a successful invocation of database.runTransaction
// can only be ended after the calling function is completed.
Expand All @@ -2120,13 +2119,13 @@ describe('Traces for ExecuteStream broken stream retries', () => {
const expectedSpanNames = [
'CloudSpanner.Database.batchCreateSessions',
'CloudSpanner.SessionPool.createSessions',
'CloudSpanner.Database.runTransaction',
'CloudSpanner.Snapshot.runStream',
'CloudSpanner.Snapshot.run',
'CloudSpanner.Dml.runUpdate',
'CloudSpanner.Snapshot.begin',
'CloudSpanner.Transaction.commit',
'CloudSpanner.Transaction.commit',
'CloudSpanner.Database.runTransactionAsync',
];
assert.deepStrictEqual(
actualSpanNames,
Expand All @@ -2148,6 +2147,8 @@ describe('Traces for ExecuteStream broken stream retries', () => {
'Acquiring session',
'Waiting for a session to become available',
'Acquired session',
'Using Session',
'Transaction Attempt Succeeded',
];
assert.deepStrictEqual(
actualEventNames,
Expand Down Expand Up @@ -2223,6 +2224,7 @@ describe('Traces for ExecuteStream broken stream retries', () => {
'Waiting for a session to become available',
'Acquired session',
'Using Session',
'Transaction Attempt Succeeded',
];
assert.deepStrictEqual(
actualEventNames,
Expand Down
5 changes: 3 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3399,9 +3399,10 @@ class Database extends common.GrpcServiceObject {
);

try {
return await runner.run();
} finally {
const result = await runner.run();
span.end();
return result;
} finally {
this.pool_.release(session);
}
} catch (e) {
Expand Down
18 changes: 10 additions & 8 deletions src/transaction-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,20 @@ export abstract class Runner<T> {
while (this.attempts === 0 || Date.now() - start < timeout) {
const transaction = await this.getTransaction();

// this.attempts refers to the number of retries, not while loop iterations
// hence without a +1, reading a span entry that says attempt=0 makes no
// sence to a user.
const countableAttempts = this.attempts + 1;

try {
const result = await this._run(transaction);
if (this.attempts > 0) {
// No add to annotate if the transaction wasn't retried.
span.addEvent('Transaction Attempt Succeeded', {
attempt: this.attempts + 1,
});
}
span.addEvent('Transaction Attempt Succeeded', {
attempt: countableAttempts,
});
return result;
} catch (e) {
span.addEvent('Transaction Attempt Failed', {
attempt: this.attempts + 1,
attempt: countableAttempts,
});
this.session.lastError = e as grpc.ServiceError;
lastError = e as grpc.ServiceError;
Expand All @@ -268,7 +270,7 @@ export abstract class Runner<T> {
this.attempts += 1;

const delay = this.getNextDelay(lastError!);
span.addEvent('Backing off', {delay: delay, attempt: this.attempts});
span.addEvent('Backing off', {delay: delay, attempt: countableAttempts});
await new Promise(resolve => setTimeout(resolve, delay));
}

Expand Down

0 comments on commit 508243f

Please sign in to comment.