From 4499263ab0985d11fc1d12c76e805969a719ddd1 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Wed, 5 Aug 2020 12:55:26 +0530 Subject: [PATCH 1/5] docs: sample for custom timeout and retry option --- samples/dml.js | 82 +++++++++++++++++++++++++++++ samples/system-test/spanner.test.js | 8 +++ 2 files changed, 90 insertions(+) diff --git a/samples/dml.js b/samples/dml.js index 910eb1b76..31f3cc1a4 100644 --- a/samples/dml.js +++ b/samples/dml.js @@ -630,6 +630,74 @@ async function updateUsingBatchDml(instanceId, databaseId, projectId) { // [END spanner_dml_batch_update] } +async function executeSqlWithCustomTimeoutAndRetrySettings( + instanceId, + databaseId, + projectId +) { + // [START spanner_set_custom_timeout_and_retry] + // Imports the Google Cloud client library + const {Spanner} = require('@google-cloud/spanner'); + const {grpc} = require('google-gax'); + + /** + * 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, + }); + + const retryAndTimeoutSettings = { + retry: { + retryCodes: [grpc.status.DEADLINE_EXCEEDED, grpc.status.UNAVAILABLE], + backoffSettings: { + // Configure retry delay settings. + initialRetryDelayMillis: 500, + maxRetryDelayMillis: 64000, + retryDelayMultiplier: 1.5, + // Configure RPC and total timeout settings. + initialRpcTimeoutMillis: 60000, + rpcTimeoutMultiplier: 1.0, + maxRpcTimeoutMillis: 60000, + totalTimeoutMillis: 600000, + }, + }, + }; + + // 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 (16, 'Martha', 'Waller')`, + gaxOptions: retryAndTimeoutSettings, + }); + + console.log(`${rowCount} record inserted.`); + + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + // [END spanner_set_custom_timeout_and_retry] +} + require('yargs') .demand(1) .command( @@ -736,6 +804,17 @@ require('yargs') opts => updateUsingBatchDml(opts.instanceName, opts.databaseName, opts.projectId) ) + .command( + 'executeSqlWithCustomTimeoutAndRetrySettings ', + 'Insert records using custom timeout and retry settings.', + {}, + opts => + executeSqlWithCustomTimeoutAndRetrySettings( + opts.instanceName, + opts.databaseName, + opts.projectId + ) + ) .example('node $0 insertUsingDml "my-instance" "my-database" "my-project-id"') .example('node $0 updateUsingDml "my-instance" "my-database" "my-project-id"') .example('node $0 deleteUsingDml "my-instance" "my-database" "my-project-id"') @@ -764,6 +843,9 @@ require('yargs') .example( 'node $0 updateUsingBatchDml "my-instance" "my-database" "my-project-id"' ) + .example( + 'node $0 executeSqlWithCustomTimeoutAndRetrySettings "my-instance" "my-database" "my-project-id"' + ) .wrap(120) .recommendCommands() .epilogue('For more information, see https://cloud.google.com/spanner/docs') diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 7ea3de341..a6d07c35b 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -876,4 +876,12 @@ describe('Spanner', () => { ); assert.match(output, /Backup deleted./); }); + + // custom_timeout_and_retry + it('should insert using DML with custom timeout and retry settings', async () => { + const output = execSync( + `${dmlCmd} executeSqlWithCustomTimeoutAndRetrySettings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + ); + assert.match(output, /record inserted./); + }); }); From 0a50621ec24082ba0e4ccf932d5cb7402941092c Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Wed, 5 Aug 2020 15:43:27 +0530 Subject: [PATCH 2/5] fix: lint --- samples/dml.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/dml.js b/samples/dml.js index 31f3cc1a4..098997c61 100644 --- a/samples/dml.js +++ b/samples/dml.js @@ -638,7 +638,6 @@ async function executeSqlWithCustomTimeoutAndRetrySettings( // [START spanner_set_custom_timeout_and_retry] // Imports the Google Cloud client library const {Spanner} = require('@google-cloud/spanner'); - const {grpc} = require('google-gax'); /** * TODO(developer): Uncomment the following lines before running the sample. @@ -651,10 +650,11 @@ async function executeSqlWithCustomTimeoutAndRetrySettings( const spanner = new Spanner({ projectId: projectId, }); - + const DEADLINE_EXCEEDED_STATUS_CODE = 4; + const UNAVAILABLE_STATUS_CODE = 14; const retryAndTimeoutSettings = { retry: { - retryCodes: [grpc.status.DEADLINE_EXCEEDED, grpc.status.UNAVAILABLE], + retryCodes: [DEADLINE_EXCEEDED_STATUS_CODE, UNAVAILABLE_STATUS_CODE], backoffSettings: { // Configure retry delay settings. initialRetryDelayMillis: 500, From 79869c22a78007e7317793256af42d04b8349e75 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Thu, 20 Aug 2020 12:54:35 +0530 Subject: [PATCH 3/5] docs: remove stream and use simple insert --- samples/dml.js | 44 +++++++++++------------------ samples/system-test/spanner.test.js | 4 +-- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/samples/dml.js b/samples/dml.js index 098997c61..d36c31a6c 100644 --- a/samples/dml.js +++ b/samples/dml.js @@ -14,6 +14,8 @@ 'use strict'; +const {grpc} = require('google-gax'); + // sample-metadata: // title: DML @@ -630,7 +632,7 @@ async function updateUsingBatchDml(instanceId, databaseId, projectId) { // [END spanner_dml_batch_update] } -async function executeSqlWithCustomTimeoutAndRetrySettings( +async function insertWithCustomTimeoutAndRetrySettings( instanceId, databaseId, projectId @@ -650,11 +652,9 @@ async function executeSqlWithCustomTimeoutAndRetrySettings( const spanner = new Spanner({ projectId: projectId, }); - const DEADLINE_EXCEEDED_STATUS_CODE = 4; - const UNAVAILABLE_STATUS_CODE = 14; const retryAndTimeoutSettings = { retry: { - retryCodes: [DEADLINE_EXCEEDED_STATUS_CODE, UNAVAILABLE_STATUS_CODE], + retryCodes: [grpc.status.DEADLINE_EXCEEDED, grpc.status.UNAVAILABLE], backoffSettings: { // Configure retry delay settings. initialRetryDelayMillis: 500, @@ -672,29 +672,19 @@ async function executeSqlWithCustomTimeoutAndRetrySettings( // Gets a reference to a Cloud Spanner instance and database const instance = spanner.instance(instanceId); const database = instance.database(databaseId); + const table = database.table('Singers'); - database.runTransaction(async (err, transaction) => { - if (err) { - console.error(err); - return; - } - try { - const [rowCount] = await transaction.runUpdate({ - sql: `INSERT Singers (SingerId, FirstName, LastName) - VALUES (16, 'Martha', 'Waller')`, - gaxOptions: retryAndTimeoutSettings, - }); - - console.log(`${rowCount} record inserted.`); + const row = { + SingerId: 16, + FirstName: 'Martha', + LastName: 'Waller', + }; - await transaction.commit(); - } catch (err) { - console.error('ERROR:', err); - } finally { - // Close the database when finished. - database.close(); - } + await table.insert(row, { + gaxOptions: retryAndTimeoutSettings, }); + + console.log('record inserted.'); // [END spanner_set_custom_timeout_and_retry] } @@ -805,11 +795,11 @@ require('yargs') updateUsingBatchDml(opts.instanceName, opts.databaseName, opts.projectId) ) .command( - 'executeSqlWithCustomTimeoutAndRetrySettings ', + 'insertWithCustomTimeoutAndRetrySettings ', 'Insert records using custom timeout and retry settings.', {}, opts => - executeSqlWithCustomTimeoutAndRetrySettings( + insertWithCustomTimeoutAndRetrySettings( opts.instanceName, opts.databaseName, opts.projectId @@ -844,7 +834,7 @@ require('yargs') 'node $0 updateUsingBatchDml "my-instance" "my-database" "my-project-id"' ) .example( - 'node $0 executeSqlWithCustomTimeoutAndRetrySettings "my-instance" "my-database" "my-project-id"' + 'node $0 insertWithCustomTimeoutAndRetrySettings "my-instance" "my-database" "my-project-id"' ) .wrap(120) .recommendCommands() diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index a6d07c35b..cd2d2bd2c 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -878,9 +878,9 @@ describe('Spanner', () => { }); // custom_timeout_and_retry - it('should insert using DML with custom timeout and retry settings', async () => { + it('should insert with custom timeout and retry settings', async () => { const output = execSync( - `${dmlCmd} executeSqlWithCustomTimeoutAndRetrySettings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `${dmlCmd} insertWithCustomTimeoutAndRetrySettings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /record inserted./); }); From 3be744115e577b0c39f00fb13bd87b0c1b62f64b Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Thu, 20 Aug 2020 13:09:33 +0530 Subject: [PATCH 4/5] docs: fix lint --- samples/dml.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/dml.js b/samples/dml.js index d36c31a6c..eecf4cdfc 100644 --- a/samples/dml.js +++ b/samples/dml.js @@ -14,8 +14,6 @@ 'use strict'; -const {grpc} = require('google-gax'); - // sample-metadata: // title: DML @@ -652,9 +650,11 @@ async function insertWithCustomTimeoutAndRetrySettings( const spanner = new Spanner({ projectId: projectId, }); + const DEADLINE_EXCEEDED_STATUS_CODE = 4; + const UNAVAILABLE_STATUS_CODE = 14; const retryAndTimeoutSettings = { retry: { - retryCodes: [grpc.status.DEADLINE_EXCEEDED, grpc.status.UNAVAILABLE], + retryCodes: [DEADLINE_EXCEEDED_STATUS_CODE, UNAVAILABLE_STATUS_CODE], backoffSettings: { // Configure retry delay settings. initialRetryDelayMillis: 500, From 7696a22e9cc900c3d260483f11b35c2ac3b18200 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Thu, 20 Aug 2020 15:26:02 +0530 Subject: [PATCH 5/5] docs: total timeout set to 60 sec --- samples/dml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/dml.js b/samples/dml.js index eecf4cdfc..71a1a1286 100644 --- a/samples/dml.js +++ b/samples/dml.js @@ -664,7 +664,7 @@ async function insertWithCustomTimeoutAndRetrySettings( initialRpcTimeoutMillis: 60000, rpcTimeoutMultiplier: 1.0, maxRpcTimeoutMillis: 60000, - totalTimeoutMillis: 600000, + totalTimeoutMillis: 60000, }, }, };