Skip to content

Commit

Permalink
feat(spanner): add a sample for max commit delays (#1993)
Browse files Browse the repository at this point in the history
* Add a sample

* update copywrite year

* Comments.

* Lint

* Change to transaction.commit

* Update test.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Remove added lines from merge

* Change SingerId

* Add an integration test

* Add a comment

* fix regex

---------

Co-authored-by: surbhigarg92 <[email protected]>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 8, 2024
1 parent 4381047 commit 91c7204
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
| Json-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/json-update-data.js,samples/README.md) |
| Lists all databases on the selected instance | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-databases.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-databases.js,samples/README.md) |
| Lists all the available instance configs for the selected project. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-instance-configs.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-instance-configs.js,samples/README.md) |
| Executes request with max commit delay | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/max-commit-delay.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/max-commit-delay.js,samples/README.md) |
| Numeric-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-add-column.js,samples/README.md) |
| Numeric-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-query-parameter.js,samples/README.md) |
| Numeric-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/numeric-update-data.js,samples/README.md) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ and automatic, synchronous replication for high availability.
* [Json-update-data](#json-update-data)
* [Lists all databases on the selected instance](#lists-all-databases-on-the-selected-instance)
* [Lists all the available instance configs for the selected project.](#lists-all-the-available-instance-configs-for-the-selected-project.)
* [Executes request with max commit delay](#executes-request-with-max-commit-delay)
* [Numeric-add-column](#numeric-add-column)
* [Numeric-query-parameter](#numeric-query-parameter)
* [Numeric-update-data](#numeric-update-data)
Expand Down Expand Up @@ -978,6 +979,23 @@ __Usage:__



### Executes request with max commit delay

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/max-commit-delay.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/max-commit-delay.js,samples/README.md)

__Usage:__


`node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`


-----




### Numeric-add-column

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js).
Expand Down
87 changes: 87 additions & 0 deletions samples/max-commit-delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// sample-metadata:
// title: Executes request with max commit delay
// usage: node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project-id'
) {
// [START spanner_set_max_commit_delay]
// Imports the Google Cloud client library.
const {Spanner, protos} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client.
const spanner = new Spanner({
projectId: projectId,
});

async function spannerSetMaxCommitDelay() {
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

database.runTransaction(async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
const [rowCount] = await transaction.runUpdate({
sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (111, @firstName, @lastName)',
params: {
firstName: 'Virginia',
lastName: 'Watson',
},
});

console.log(
`Successfully inserted ${rowCount} record into the Singers table.`
);

await transaction.commit({
// The maximum amount of time to delay the transaction to improve
// throughput.
maxCommitDelay: protos.google.protobuf.Duration({
seconds: 0, // 0 seconds
nanos: 100000000, // 100,000,000 nanoseconds = 100 milliseconds
}),
});
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
database.close();
}
});
}
spannerSetMaxCommitDelay();
// [END spanner_set_max_commit_delay]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
13 changes: 13 additions & 0 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,5 +1365,18 @@ describe('Autogenerated Admin Clients', () => {
);
assert.include(output, 'CREATE TABLE Singers');
});

// max_commit_delay
it('should update rows in Albums example table when max commit delay is set', async () => {
const output = execSync(
`node max-commit-delay.js "${INSTANCE_ID}" "${DATABASE_ID}" "${PROJECT_ID}"`
);
assert.match(
output,
new RegExp(
'Successfully inserted (\\d+) record into the Singers table.'
)
);
});
});
});

0 comments on commit 91c7204

Please sign in to comment.