From 91c7204e2c8f62e229d7a2b2a0ff059d421dd984 Mon Sep 17 00:00:00 2001 From: nginsberg-google <131713109+nginsberg-google@users.noreply.github.com> Date: Fri, 8 Mar 2024 00:08:35 -0800 Subject: [PATCH] feat(spanner): add a sample for max commit delays (#1993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Owl Bot --- README.md | 1 + samples/README.md | 18 ++++++ samples/max-commit-delay.js | 87 +++++++++++++++++++++++++++++ samples/system-test/spanner.test.js | 13 +++++ 4 files changed, 119 insertions(+) create mode 100644 samples/max-commit-delay.js diff --git a/README.md b/README.md index 9ccee1f1a..e40782b4e 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/samples/README.md b/samples/README.md index 3993fbe83..5e99af7cf 100644 --- a/samples/README.md +++ b/samples/README.md @@ -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) @@ -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 ` + + +----- + + + + ### Numeric-add-column View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-add-column.js). diff --git a/samples/max-commit-delay.js b/samples/max-commit-delay.js new file mode 100644 index 000000000..cab871ee7 --- /dev/null +++ b/samples/max-commit-delay.js @@ -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 + +'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)); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index c64e4616c..630aad8db 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -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.' + ) + ); + }); }); });