Skip to content

Commit 13bc9f5

Browse files
committed
Revert Database.runTransactionAsync changes to allow a spin-off PR
1 parent 3db6a9c commit 13bc9f5

File tree

2 files changed

+77
-85
lines changed

2 files changed

+77
-85
lines changed

observability-test/spanner.ts

+40-41
Original file line numberDiff line numberDiff line change
@@ -427,53 +427,55 @@ describe('EndToEnd', () => {
427427
);
428428
});
429429

430-
it('runTransaction', async () => {
430+
it('runTransaction', done => {
431431
const withAllSpansHaveDBName = generateWithAllSpansHaveDBName(
432432
database.formattedName_
433433
);
434434

435-
await database.runTransactionAsync(async transaction => {
436-
return await transaction!.run('SELECT 1');
437-
});
435+
database.runTransaction(async (err, transaction) => {
436+
assert.ifError(err);
437+
const [rows] = await transaction!.run('SELECT 1');
438+
await transaction!.commit();
438439

439-
traceExporter.forceFlush();
440-
const spans = traceExporter.getFinishedSpans();
441-
withAllSpansHaveDBName(spans);
440+
traceExporter.forceFlush();
441+
const spans = traceExporter.getFinishedSpans();
442+
withAllSpansHaveDBName(spans);
442443

443-
const actualEventNames: string[] = [];
444-
const actualSpanNames: string[] = [];
445-
spans.forEach(span => {
446-
actualSpanNames.push(span.name);
447-
span.events.forEach(event => {
448-
actualEventNames.push(event.name);
444+
const actualEventNames: string[] = [];
445+
const actualSpanNames: string[] = [];
446+
spans.forEach(span => {
447+
actualSpanNames.push(span.name);
448+
span.events.forEach(event => {
449+
actualEventNames.push(event.name);
450+
});
449451
});
450-
});
451452

452-
const expectedSpanNames = [
453-
'CloudSpanner.Snapshot.runStream',
454-
'CloudSpanner.Snapshot.run',
455-
'CloudSpanner.Database.runTransactionAsync',
456-
];
457-
assert.deepStrictEqual(
458-
actualSpanNames,
459-
expectedSpanNames,
460-
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
461-
);
453+
const expectedSpanNames = [
454+
'CloudSpanner.Snapshot.runStream',
455+
'CloudSpanner.Snapshot.run',
456+
'CloudSpanner.Database.runTransaction',
457+
];
458+
assert.deepStrictEqual(
459+
actualSpanNames,
460+
expectedSpanNames,
461+
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
462+
);
462463

463-
const expectedEventNames = [
464-
'Starting stream',
465-
'Transaction Creation Done',
466-
'Acquiring session',
467-
'Cache hit: has usable session',
468-
'Acquired session',
469-
'Using Session',
470-
'Transaction Attempt Succeeded',
471-
];
472-
assert.deepStrictEqual(
473-
actualEventNames,
474-
expectedEventNames,
475-
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
476-
);
464+
const expectedEventNames = [
465+
'Starting stream',
466+
'Transaction Creation Done',
467+
'Acquiring session',
468+
'Cache hit: has usable session',
469+
'Acquired session',
470+
'Using Session',
471+
'Transaction Attempt Succeeded',
472+
];
473+
assert.deepStrictEqual(
474+
actualEventNames,
475+
expectedEventNames,
476+
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
477+
);
478+
});
477479
});
478480

479481
it('writeAtLeastOnce', done => {
@@ -1508,7 +1510,6 @@ SELECT 1p
15081510
'CloudSpanner.Snapshot.begin',
15091511
'CloudSpanner.Transaction.commit',
15101512
'CloudSpanner.Transaction.commit',
1511-
'CloudSpanner.Database.runTransactionAsync',
15121513
];
15131514
assert.deepStrictEqual(
15141515
actualSpanNames,
@@ -2125,7 +2126,6 @@ describe('Traces for ExecuteStream broken stream retries', () => {
21252126
'CloudSpanner.Snapshot.begin',
21262127
'CloudSpanner.Transaction.commit',
21272128
'CloudSpanner.Transaction.commit',
2128-
'CloudSpanner.Database.runTransactionAsync',
21292129
];
21302130
assert.deepStrictEqual(
21312131
actualSpanNames,
@@ -2207,7 +2207,6 @@ describe('Traces for ExecuteStream broken stream retries', () => {
22072207
const expectedSpanNames = [
22082208
'CloudSpanner.Database.batchCreateSessions',
22092209
'CloudSpanner.SessionPool.createSessions',
2210-
'CloudSpanner.Database.runTransactionAsync',
22112210
];
22122211
assert.deepStrictEqual(
22132212
actualSpanNames,

src/database.ts

+37-44
Original file line numberDiff line numberDiff line change
@@ -3385,53 +3385,46 @@ class Database extends common.GrpcServiceObject {
33853385

33863386
let sessionId = '';
33873387
const getSession = this.pool_.getSession.bind(this.pool_);
3388+
const span = getActiveOrNoopSpan();
3389+
// Loop to retry 'Session not found' errors.
3390+
// (and yes, we like while (true) more than for (;;) here)
3391+
// eslint-disable-next-line no-constant-condition
3392+
while (true) {
3393+
try {
3394+
const [session, transaction] = await promisify(getSession)();
3395+
transaction.requestOptions = Object.assign(
3396+
transaction.requestOptions || {},
3397+
options.requestOptions
3398+
);
3399+
if (options.optimisticLock) {
3400+
transaction.useOptimisticLock();
3401+
}
3402+
if (options.excludeTxnFromChangeStreams) {
3403+
transaction.excludeTxnFromChangeStreams();
3404+
}
3405+
sessionId = session?.id;
3406+
span.addEvent('Using Session', {'session.id': sessionId});
3407+
const runner = new AsyncTransactionRunner<T>(
3408+
session,
3409+
transaction,
3410+
runFn,
3411+
options
3412+
);
33883413

3389-
return startTrace(
3390-
'Database.runTransactionAsync',
3391-
this._traceConfig,
3392-
async span => {
3393-
// Loop to retry 'Session not found' errors.
3394-
// (and yes, we like while (true) more than for (;;) here)
3395-
// eslint-disable-next-line no-constant-condition
3396-
while (true) {
3397-
try {
3398-
const [session, transaction] = await promisify(getSession)();
3399-
transaction.requestOptions = Object.assign(
3400-
transaction.requestOptions || {},
3401-
options.requestOptions
3402-
);
3403-
if (options.optimisticLock) {
3404-
transaction.useOptimisticLock();
3405-
}
3406-
if (options.excludeTxnFromChangeStreams) {
3407-
transaction.excludeTxnFromChangeStreams();
3408-
}
3409-
sessionId = session?.id;
3410-
span.addEvent('Using Session', {'session.id': sessionId});
3411-
const runner = new AsyncTransactionRunner<T>(
3412-
session,
3413-
transaction,
3414-
runFn,
3415-
options
3416-
);
3417-
3418-
try {
3419-
const result = await runner.run();
3420-
span.end();
3421-
return result;
3422-
} finally {
3423-
this.pool_.release(session);
3424-
}
3425-
} catch (e) {
3426-
if (!isSessionNotFoundError(e as ServiceError)) {
3427-
setSpanErrorAndException(span, e as Error);
3428-
span.end();
3429-
throw e;
3430-
}
3431-
}
3414+
try {
3415+
return await runner.run();
3416+
} finally {
3417+
this.pool_.release(session);
3418+
}
3419+
} catch (e) {
3420+
if (!isSessionNotFoundError(e as ServiceError)) {
3421+
span.addEvent('No session available', {
3422+
'session.id': sessionId,
3423+
});
3424+
throw e;
34323425
}
34333426
}
3434-
);
3427+
}
34353428
}
34363429

34373430
/**

0 commit comments

Comments
 (0)