Skip to content

Commit

Permalink
fix: rollback with no id (#2231)
Browse files Browse the repository at this point in the history
Fixes #2103
  • Loading branch information
surbhigarg92 committed Feb 7, 2025
1 parent 38d688b commit 9627146
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
14 changes: 8 additions & 6 deletions observability-test/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,14 @@ describe('Transaction', () => {
transaction.id = ID;
});

it('error with unset `id`', done => {
it('no error with unset `id`', done => {
const expectedError = new Error(

Check warning on line 505 in observability-test/transaction.ts

View workflow job for this annotation

GitHub Actions / lint

'expectedError' is assigned a value but never used
'Transaction ID is unknown, nothing to rollback.'
);
delete transaction.id;

transaction.rollback(err => {
assert.deepStrictEqual(err, expectedError);
assert.deepStrictEqual(err, null);

const exportResults = extractExportedSpans();
const actualSpanNames = exportResults.spanNames;
Expand All @@ -521,7 +521,9 @@ describe('Transaction', () => {
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
);

const expectedEventNames = [];
const expectedEventNames = [
'Transaction ID is unknown, nothing to rollback.',
];
assert.deepStrictEqual(
actualEventNames,
expectedEventNames,
Expand All @@ -532,12 +534,12 @@ describe('Transaction', () => {
const spans = exportResults.spans;
const firstSpan = spans[0];
assert.strictEqual(
SpanStatusCode.ERROR,
SpanStatusCode.UNSET,
firstSpan.status.code,
'Unexpected an span status code'
'Unexpected span status code'
);
assert.strictEqual(
expectedError.message,
undefined,
firstSpan.status.message,
'Unexpected span status message'
);
Expand Down
25 changes: 19 additions & 6 deletions src/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,7 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
return;
}

try {
await this._createSessions(needed);
} catch (e) {
this.emit('error', e);
}
await this._createSessions(needed);
}

/**
Expand Down Expand Up @@ -993,7 +989,24 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
const pings = sessions.map(session => this._ping(session));

await Promise.all(pings);
return this._fill();
try {
await this._fill();
} catch (error) {
// Ignore `Database not found` error. This allows a user to call instance.database('db-name')
// for a database that does not yet exist with SessionPoolOptions.min > 0.
const err = error as ServiceError;
if (
isDatabaseNotFoundError(err) ||
isInstanceNotFoundError(err) ||
isCreateSessionPermissionError(err) ||
isDefaultCredentialsNotSetError(err) ||
isProjectIdNotSetInEnvironmentError(err)
) {
return;
}
this.emit('error', err);
}
return;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2517,12 +2517,9 @@ export class Transaction extends Dml {
};
return startTrace('Transaction.rollback', traceConfig, span => {
if (!this.id) {
const err = new Error(
'Transaction ID is unknown, nothing to rollback.'
) as ServiceError;
setSpanError(span, err);
span.addEvent('Transaction ID is unknown, nothing to rollback.');
span.end();
callback!(err);
callback(null);
return;
}

Expand Down
21 changes: 18 additions & 3 deletions test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,10 @@ describe('SessionPool', () => {

stub.rejects(error);

sessionPool.once('error', err => {
sessionPool._fill().catch(err => {
assert.strictEqual(err, error);
done();
});

sessionPool._fill();
});
});

Expand Down Expand Up @@ -1286,6 +1284,23 @@ describe('SessionPool', () => {

assert.strictEqual(fillStub.callCount, 1);
});

it('should not throw error when database not found', async () => {
const fakeSessions = [createSession()];
sandbox.stub(sessionPool, '_getIdleSessions').returns(fakeSessions);

const error = {
code: grpc.status.NOT_FOUND,
message: 'Database not found',
} as grpc.ServiceError;
sandbox.stub(sessionPool, '_fill').rejects(error);

try {
await sessionPool._pingIdleSessions();
} catch (err) {
assert.ifError(err);
}
});
});

describe('_release', () => {
Expand Down
9 changes: 2 additions & 7 deletions test/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1975,15 +1975,10 @@ describe('Transaction', () => {
transaction.id = ID;
});

it('should return an error if the `id` is not set', done => {
const expectedError = new Error(
'Transaction ID is unknown, nothing to rollback.'
);

it('should not return an error if the `id` is not set', done => {
delete transaction.id;

transaction.rollback(err => {
assert.deepStrictEqual(err, expectedError);
assert.deepStrictEqual(err, null);
done();
});
});
Expand Down

0 comments on commit 9627146

Please sign in to comment.