Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove Begin transaction on non retryable error #2168

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,21 +743,6 @@ export class Snapshot extends EventEmitter {
this._update(response.metadata!.transaction);
}
})
.on('error', err => {
const isServiceError =
err && typeof err === 'object' && 'code' in err;
if (
!this.id &&
this._useInRunner &&
!(
isServiceError &&
(err as grpc.ServiceError).code === grpc.status.ABORTED
)
) {
this.begin();
}
setSpanError(span, err);
})
.on('end', err => {
if (err) {
setSpanError(span, err);
Expand Down Expand Up @@ -1318,21 +1303,6 @@ export class Snapshot extends EventEmitter {
this._update(response.metadata!.transaction);
}
})
.on('error', err => {
setSpanError(span, err as Error);
const isServiceError =
err && typeof err === 'object' && 'code' in err;
if (
!this.id &&
this._useInRunner &&
!(
isServiceError &&
(err as grpc.ServiceError).code === grpc.status.ABORTED
)
) {
this.begin();
}
})
.on('end', err => {
if (err) {
setSpanError(span, err as Error);
Expand Down
119 changes: 1 addition & 118 deletions test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,8 @@ describe('Spanner with mock server', () => {
request.requestOptions!.transactionTag,
'transaction-tag'
);
const beginTxnRequest = spannerMock.getRequests().find(val => {
return (val as v1.BeginTransactionRequest).options?.readWrite;
}) as v1.BeginTransactionRequest;
assert.strictEqual(
beginTxnRequest.options?.readWrite!.readLockMode,
request.transaction?.begin?.readWrite?.readLockMode,
'OPTIMISTIC'
);
});
Expand Down Expand Up @@ -3758,120 +3755,6 @@ describe('Spanner with mock server', () => {
);
});

it('should use beginTransaction on retry for unknown reason', async () => {
const database = newTestDatabase();
await database.runTransactionAsync(async tx => {
try {
await tx.runUpdate(invalidSql);
assert.fail('missing expected error');
} catch (e) {
assert.strictEqual(
(e as ServiceError).message,
`${grpc.status.NOT_FOUND} NOT_FOUND: ${fooNotFoundErr.message}`
);
}
await tx.run(selectSql);
await tx.commit();
});
await database.close();

const beginTxnRequest = spannerMock
.getRequests()
.filter(val => (val as v1.BeginTransactionRequest).options?.readWrite)
.map(req => req as v1.BeginTransactionRequest);
assert.deepStrictEqual(beginTxnRequest.length, 1);
});

it('should use beginTransaction on retry for unknown reason with excludeTxnFromChangeStreams', async () => {
const database = newTestDatabase();
await database.runTransactionAsync(
{
excludeTxnFromChangeStreams: true,
},
async tx => {
try {
await tx.runUpdate(invalidSql);
assert.fail('missing expected error');
} catch (e) {
assert.strictEqual(
(e as ServiceError).message,
`${grpc.status.NOT_FOUND} NOT_FOUND: ${fooNotFoundErr.message}`
);
}
await tx.run(selectSql);
await tx.commit();
}
);
await database.close();

const beginTxnRequest = spannerMock
.getRequests()
.filter(val => (val as v1.BeginTransactionRequest).options?.readWrite)
.map(req => req as v1.BeginTransactionRequest);
assert.deepStrictEqual(beginTxnRequest.length, 1);
assert.strictEqual(
beginTxnRequest[0].options?.excludeTxnFromChangeStreams,
true
);
});

it('should use beginTransaction for streaming on retry for unknown reason', async () => {
const database = newTestDatabase();
await database.runTransactionAsync(async tx => {
try {
await getRowCountFromStreamingSql(tx!, {sql: invalidSql});
assert.fail('missing expected error');
} catch (e) {
assert.strictEqual(
(e as ServiceError).message,
`${grpc.status.NOT_FOUND} NOT_FOUND: ${fooNotFoundErr.message}`
);
}
await tx.run(selectSql);
await tx.commit();
});
await database.close();

const beginTxnRequest = spannerMock
.getRequests()
.filter(val => (val as v1.BeginTransactionRequest).options?.readWrite)
.map(req => req as v1.BeginTransactionRequest);
assert.deepStrictEqual(beginTxnRequest.length, 1);
});

it('should use beginTransaction for streaming on retry for unknown reason with excludeTxnFromChangeStreams', async () => {
const database = newTestDatabase();
await database.runTransactionAsync(
{
excludeTxnFromChangeStreams: true,
},
async tx => {
try {
await getRowCountFromStreamingSql(tx!, {sql: invalidSql});
assert.fail('missing expected error');
} catch (e) {
assert.strictEqual(
(e as ServiceError).message,
`${grpc.status.NOT_FOUND} NOT_FOUND: ${fooNotFoundErr.message}`
);
}
await tx.run(selectSql);
await tx.commit();
}
);
await database.close();

const beginTxnRequest = spannerMock
.getRequests()
.filter(val => (val as v1.BeginTransactionRequest).options?.readWrite)
.map(req => req as v1.BeginTransactionRequest);
assert.deepStrictEqual(beginTxnRequest.length, 1);
assert.strictEqual(
beginTxnRequest[0].options?.excludeTxnFromChangeStreams,
true
);
});

it('should fail if beginTransaction fails', async () => {
const database = newTestDatabase();
const err = {
Expand Down
Loading