Skip to content

Commit

Permalink
fix: remove begin call for non retriable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Oct 21, 2024
1 parent 4d7b0aa commit e1c3097
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 148 deletions.
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

0 comments on commit e1c3097

Please sign in to comment.