Skip to content

Commit 2844703

Browse files
authored
Merge pull request googleapis#3405 from GoogleCloudPlatform/3019-spanner-streaming_chunking_prep
Scripts populating / cleaning DB for streaming / chunking systests.
2 parents 938c68f + 3cdd037 commit 2844703

File tree

7 files changed

+198
-1
lines changed

7 files changed

+198
-1
lines changed

spanner/nox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def system_tests(session, python_version):
6161
session.install('.')
6262

6363
# Run py.test against the system tests.
64-
session.run('py.test', '--quiet', 'tests/system.py')
64+
session.run('py.test', '--quiet', 'tests/system')
6565

6666

6767
@nox.session

spanner/tests/system/__init__.py

Whitespace-only changes.

spanner/tests/system/utils/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"""Depopulate spanner databases with data for streaming system tests."""
16+
17+
from google.cloud.spanner import Client
18+
from google.cloud.spanner.pool import BurstyPool
19+
20+
# Import relative to the script's directory
21+
from streaming_utils import DATABASE_NAME
22+
from streaming_utils import INSTANCE_NAME
23+
from streaming_utils import print_func
24+
25+
26+
def remove_database(client):
27+
instance = client.instance(INSTANCE_NAME)
28+
29+
if not instance.exists():
30+
print_func("Instance does not exist: {}".format(INSTANCE_NAME))
31+
return
32+
33+
print_func("Instance exists: {}".format(INSTANCE_NAME))
34+
instance.reload()
35+
36+
pool = BurstyPool()
37+
database = instance.database(DATABASE_NAME)
38+
39+
if not database.exists():
40+
print_func("Database does not exist: {}".format(DATABASE_NAME))
41+
return
42+
print_func("Dropping database: {}".format(DATABASE_NAME))
43+
database.drop()
44+
45+
46+
if __name__ == '__main__':
47+
client = Client()
48+
remove_database(client)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import os
16+
17+
INSTANCE_NAME = 'gcp-streaming-systests'
18+
DATABASE_NAME = 'testing'
19+
_SHOULD_PRINT = os.getenv('GOOGLE_CLOUD_NO_PRINT') != 'true'
20+
21+
22+
def print_func(message):
23+
if _SHOULD_PRINT:
24+
print(message)

0 commit comments

Comments
 (0)