|
| 1 | +# Copyright 2017 Google Inc. |
| 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 | +"""Populate spanner databases with data for streaming system tests.""" |
| 16 | + |
| 17 | +from google.cloud.spanner import Client |
| 18 | +from google.cloud.spanner.keyset import KeySet |
| 19 | +from google.cloud.spanner.pool import BurstyPool |
| 20 | + |
| 21 | +# Import relative to the script's directory |
| 22 | +from streaming_utils import DATABASE_NAME |
| 23 | +from streaming_utils import INSTANCE_NAME |
| 24 | +from streaming_utils import print_func |
| 25 | + |
| 26 | +DDL = """\ |
| 27 | +CREATE TABLE four_kay ( |
| 28 | + pkey INT64, |
| 29 | + chunk_me STRING(4096) ) |
| 30 | + PRIMARY KEY (pkey); |
| 31 | +CREATE TABLE forty_kay ( |
| 32 | + pkey INT64, |
| 33 | + chunk_me STRING(40960) ) |
| 34 | + PRIMARY KEY (pkey); |
| 35 | +CREATE TABLE four_hundred_kay ( |
| 36 | + pkey INT64, |
| 37 | + chunk_me STRING(409600) ) |
| 38 | + PRIMARY KEY (pkey); |
| 39 | +CREATE TABLE four_meg ( |
| 40 | + pkey INT64, |
| 41 | + chunk_me STRING(2097152), |
| 42 | + chunk_me_2 STRING(2097152) ) |
| 43 | + PRIMARY KEY (pkey); |
| 44 | +""" |
| 45 | + |
| 46 | +DDL_STATEMENTS = [stmt.strip() for stmt in DDL.split(';') if stmt.strip()] |
| 47 | + |
| 48 | + |
| 49 | +def ensure_database(client): |
| 50 | + instance = client.instance(INSTANCE_NAME) |
| 51 | + |
| 52 | + if not instance.exists(): |
| 53 | + configs = list(client.list_instance_configs()) |
| 54 | + config_name = configs[0].name |
| 55 | + print_func("Creating instance: {}".format(INSTANCE_NAME)) |
| 56 | + instance = client.instance(INSTANCE_NAME, config_name) |
| 57 | + operation = instance.create() |
| 58 | + operation.result(30) |
| 59 | + else: |
| 60 | + print_func("Instance exists: {}".format(INSTANCE_NAME)) |
| 61 | + instance.reload() |
| 62 | + |
| 63 | + pool = BurstyPool() |
| 64 | + database = instance.database( |
| 65 | + DATABASE_NAME, ddl_statements=DDL_STATEMENTS, pool=pool) |
| 66 | + |
| 67 | + if not database.exists(): |
| 68 | + print_func("Creating database: {}".format(DATABASE_NAME)) |
| 69 | + operation = database.create() |
| 70 | + operation.result(30) |
| 71 | + else: |
| 72 | + print_func("Database exists: {}".format(DATABASE_NAME)) |
| 73 | + database.reload() |
| 74 | + |
| 75 | + return database |
| 76 | + |
| 77 | + |
| 78 | +def populate_table(database, table_name, row_count, val_size): |
| 79 | + all_ = KeySet(all_=True) |
| 80 | + columns = ('pkey', 'chunk_me') |
| 81 | + rows = list(database.execute_sql( |
| 82 | + 'SELECT COUNT(*) FROM {}'.format(table_name))) |
| 83 | + assert len(rows) == 1 |
| 84 | + count = rows[0][0] |
| 85 | + if count != row_count: |
| 86 | + print_func("Repopulating table: {}".format(table_name)) |
| 87 | + chunk_me = 'X' * val_size |
| 88 | + row_data = [(index, chunk_me) for index in range(row_count)] |
| 89 | + with database.batch() as batch: |
| 90 | + batch.delete(table_name, all_) |
| 91 | + batch.insert(table_name, columns, row_data) |
| 92 | + else: |
| 93 | + print_func("Leaving table: {}".format(table_name)) |
| 94 | + |
| 95 | + |
| 96 | +def populate_table_2_columns(database, table_name, row_count, val_size): |
| 97 | + all_ = KeySet(all_=True) |
| 98 | + columns = ('pkey', 'chunk_me', 'chunk_me_2') |
| 99 | + rows = list(database.execute_sql( |
| 100 | + 'SELECT COUNT(*) FROM {}'.format(table_name))) |
| 101 | + assert len(rows) == 1 |
| 102 | + count = rows[0][0] |
| 103 | + if count != row_count: |
| 104 | + print_func("Repopulating table: {}".format(table_name)) |
| 105 | + chunk_me = 'X' * val_size |
| 106 | + row_data = [(index, chunk_me, chunk_me) for index in range(row_count)] |
| 107 | + with database.batch() as batch: |
| 108 | + batch.delete(table_name, all_) |
| 109 | + batch.insert(table_name, columns, row_data) |
| 110 | + else: |
| 111 | + print_func("Leaving table: {}".format(table_name)) |
| 112 | + |
| 113 | + |
| 114 | +def populate_streaming(client): |
| 115 | + database = ensure_database(client) |
| 116 | + populate_table(database, 'four_kay', 1000, 4096) |
| 117 | + populate_table(database, 'forty_kay', 100, 4096 * 10) |
| 118 | + populate_table(database, 'four_hundred_kay', 25, 4096 * 100) |
| 119 | + # Max STRING column size is just larger than 2 Mb, so use two columns |
| 120 | + populate_table_2_columns(database, 'four_meg', 10, 2048 * 1024) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + client = Client() |
| 125 | + populate_streaming(client) |
0 commit comments