|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// sample-metadata: |
| 16 | +// title: Runs an execute sql request with directed read options |
| 17 | +// usage: node directed-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +function main( |
| 22 | + instanceId = 'my-instance', |
| 23 | + databaseId = 'my-database', |
| 24 | + projectId = 'my-project-id' |
| 25 | +) { |
| 26 | + // [START spanner_directed_read] |
| 27 | + // Imports the Google Cloud Spanner client library |
| 28 | + const {Spanner, protos} = require('@google-cloud/spanner'); |
| 29 | + |
| 30 | + // Only one of excludeReplicas or includeReplicas can be set |
| 31 | + // Each accepts a list of replicaSelections which contains location and type |
| 32 | + // * `location` - The location must be one of the regions within the |
| 33 | + // multi-region configuration of your database. |
| 34 | + // * `type` - The type of the replica |
| 35 | + // Some examples of using replicaSelectors are: |
| 36 | + // * `location:us-east1` --> The "us-east1" replica(s) of any available type |
| 37 | + // will be used to process the request. |
| 38 | + // * `type:READ_ONLY` --> The "READ_ONLY" type replica(s) in nearest |
| 39 | + //. available location will be used to process the |
| 40 | + // request. |
| 41 | + // * `location:us-east1 type:READ_ONLY` --> The "READ_ONLY" type replica(s) |
| 42 | + // in location "us-east1" will be used to process |
| 43 | + // the request. |
| 44 | + // includeReplicas also contains an option for autoFailover which when set |
| 45 | + // Spanner will not route requests to a replica outside the |
| 46 | + // includeReplicas list when all the specified replicas are unavailable |
| 47 | + // or unhealthy. The default value is `false` |
| 48 | + const directedReadOptionsForClient = { |
| 49 | + excludeReplicas: { |
| 50 | + replicaSelections: [ |
| 51 | + { |
| 52 | + location: 'us-east4', |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + // Instantiates a client with directedReadOptions |
| 59 | + const spanner = new Spanner({ |
| 60 | + projectId: projectId, |
| 61 | + directedReadOptions: directedReadOptionsForClient, |
| 62 | + }); |
| 63 | + |
| 64 | + async function spannerDirectedReads() { |
| 65 | + // Gets a reference to a Cloud Spanner instance and backup |
| 66 | + const instance = spanner.instance(instanceId); |
| 67 | + const database = instance.database(databaseId); |
| 68 | + const directedReadOptionsForRequest = { |
| 69 | + includeReplicas: { |
| 70 | + replicaSelections: [ |
| 71 | + { |
| 72 | + type: protos.google.spanner.v1.DirectedReadOptions.ReplicaSelection |
| 73 | + .Type.READ_ONLY, |
| 74 | + }, |
| 75 | + ], |
| 76 | + autoFailoverDisabled: true, |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + await database.getSnapshot(async (err, transaction) => { |
| 81 | + if (err) { |
| 82 | + console.error(err); |
| 83 | + return; |
| 84 | + } |
| 85 | + try { |
| 86 | + // Read rows while passing directedReadOptions directly to the query. |
| 87 | + // These will override the options passed at Client level. |
| 88 | + const [rows] = await transaction.run({ |
| 89 | + sql: 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums', |
| 90 | + directedReadOptions: directedReadOptionsForRequest, |
| 91 | + }); |
| 92 | + rows.forEach(row => { |
| 93 | + const json = row.toJSON(); |
| 94 | + console.log( |
| 95 | + `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}` |
| 96 | + ); |
| 97 | + }); |
| 98 | + console.log( |
| 99 | + 'Successfully executed read-only transaction with directedReadOptions' |
| 100 | + ); |
| 101 | + } catch (err) { |
| 102 | + console.error('ERROR:', err); |
| 103 | + } finally { |
| 104 | + transaction.end(); |
| 105 | + // Close the database when finished. |
| 106 | + await database.close(); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + spannerDirectedReads(); |
| 111 | + // [END spanner_directed_read] |
| 112 | +} |
| 113 | +process.on('unhandledRejection', err => { |
| 114 | + console.error(err.message); |
| 115 | + process.exitCode = 1; |
| 116 | +}); |
| 117 | +main(...process.argv.slice(2)); |
0 commit comments