Skip to content

Commit 508243f

Browse files
committed
Unconditionally add Transaction attempts
1 parent 927656b commit 508243f

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

observability-test/spanner.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ describe('EndToEnd', () => {
467467
'Cache hit: has usable session',
468468
'Acquired session',
469469
'Using Session',
470+
'Transaction Attempt Succeeded',
470471
];
471472
assert.deepStrictEqual(
472473
actualEventNames,
@@ -1610,6 +1611,7 @@ SELECT 1p
16101611
'Transaction Attempt Failed',
16111612
'Transaction Attempt Aborted',
16121613
'exception',
1614+
'exception',
16131615
];
16141616
assert.deepStrictEqual(
16151617
actualEventNames,
@@ -1641,7 +1643,6 @@ SELECT 1p
16411643
);
16421644
}
16431645

1644-
console.log('flushing now');
16451646
provider.forceFlush();
16461647
assertDatabaseRunPlusAwaitTransactionForAlreadyExistentData();
16471648
});
@@ -1793,8 +1794,6 @@ describe('Traces for ExecuteStream broken stream retries', () => {
17931794
});
17941795
await database.close();
17951796

1796-
console.log('done here');
1797-
17981797
const requests = spannerMock
17991798
.getRequests()
18001799
.filter(val => (val as v1.ExecuteSqlRequest).sql)
@@ -2101,8 +2100,8 @@ describe('Traces for ExecuteStream broken stream retries', () => {
21012100
const [updateCount] = await tx!.runUpdate(insertSql);
21022101
assert.strictEqual(updateCount, 1);
21032102
await tx!.commit();
2104-
await database.close();
21052103
});
2104+
await database.close();
21062105

21072106
// The span for a successful invocation of database.runTransaction
21082107
// can only be ended after the calling function is completed.
@@ -2120,13 +2119,13 @@ describe('Traces for ExecuteStream broken stream retries', () => {
21202119
const expectedSpanNames = [
21212120
'CloudSpanner.Database.batchCreateSessions',
21222121
'CloudSpanner.SessionPool.createSessions',
2123-
'CloudSpanner.Database.runTransaction',
21242122
'CloudSpanner.Snapshot.runStream',
21252123
'CloudSpanner.Snapshot.run',
21262124
'CloudSpanner.Dml.runUpdate',
21272125
'CloudSpanner.Snapshot.begin',
21282126
'CloudSpanner.Transaction.commit',
21292127
'CloudSpanner.Transaction.commit',
2128+
'CloudSpanner.Database.runTransactionAsync',
21302129
];
21312130
assert.deepStrictEqual(
21322131
actualSpanNames,
@@ -2148,6 +2147,8 @@ describe('Traces for ExecuteStream broken stream retries', () => {
21482147
'Acquiring session',
21492148
'Waiting for a session to become available',
21502149
'Acquired session',
2150+
'Using Session',
2151+
'Transaction Attempt Succeeded',
21512152
];
21522153
assert.deepStrictEqual(
21532154
actualEventNames,
@@ -2223,6 +2224,7 @@ describe('Traces for ExecuteStream broken stream retries', () => {
22232224
'Waiting for a session to become available',
22242225
'Acquired session',
22252226
'Using Session',
2227+
'Transaction Attempt Succeeded',
22262228
];
22272229
assert.deepStrictEqual(
22282230
actualEventNames,

src/database.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,9 +3399,10 @@ class Database extends common.GrpcServiceObject {
33993399
);
34003400

34013401
try {
3402-
return await runner.run();
3403-
} finally {
3402+
const result = await runner.run();
34043403
span.end();
3404+
return result;
3405+
} finally {
34053406
this.pool_.release(session);
34063407
}
34073408
} catch (e) {

src/transaction-runner.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,20 @@ export abstract class Runner<T> {
234234
while (this.attempts === 0 || Date.now() - start < timeout) {
235235
const transaction = await this.getTransaction();
236236

237+
// this.attempts refers to the number of retries, not while loop iterations
238+
// hence without a +1, reading a span entry that says attempt=0 makes no
239+
// sence to a user.
240+
const countableAttempts = this.attempts + 1;
241+
237242
try {
238243
const result = await this._run(transaction);
239-
if (this.attempts > 0) {
240-
// No add to annotate if the transaction wasn't retried.
241-
span.addEvent('Transaction Attempt Succeeded', {
242-
attempt: this.attempts + 1,
243-
});
244-
}
244+
span.addEvent('Transaction Attempt Succeeded', {
245+
attempt: countableAttempts,
246+
});
245247
return result;
246248
} catch (e) {
247249
span.addEvent('Transaction Attempt Failed', {
248-
attempt: this.attempts + 1,
250+
attempt: countableAttempts,
249251
});
250252
this.session.lastError = e as grpc.ServiceError;
251253
lastError = e as grpc.ServiceError;
@@ -268,7 +270,7 @@ export abstract class Runner<T> {
268270
this.attempts += 1;
269271

270272
const delay = this.getNextDelay(lastError!);
271-
span.addEvent('Backing off', {delay: delay, attempt: this.attempts});
273+
span.addEvent('Backing off', {delay: delay, attempt: countableAttempts});
272274
await new Promise(resolve => setTimeout(resolve, delay));
273275
}
274276

0 commit comments

Comments
 (0)