/
tuq_throttling.py
163 lines (142 loc) · 10.4 KB
/
tuq_throttling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from .tuq import QueryTests
import re
import requests
from membase.api.exception import CBQError
class QueryThrottlingTests(QueryTests):
def setUp(self):
super(QueryThrottlingTests, self).setUp()
self.bucket = "default"
self.kv_wu, self.kv_ru = 1024, 4096
self.index_wu, self.index_ru = 1024, 256
self.scope_limit, self.collection_limit = 100, 100
self.gsi_limit = 200
self.kvThrottleLimit = self.get_throttle_limits(service='kvThrottleLimit')
self.log.info(f'kvThrottleLimit is {self.kvThrottleLimit}')
def suite_setUp(self):
super(QueryThrottlingTests, self).suite_setUp()
def tearDown(self):
super(QueryThrottlingTests, self).tearDown()
def suite_tearDown(self):
super(QueryThrottlingTests, self).suite_tearDown()
def get_throttle_limits(self, service='kvThrottleLimit'):
api = f"https://{self.server.ip}:18091/settings/throttle"
response = requests.get(api, auth = requests.auth.HTTPBasicAuth(self.rest.username, self.rest.password), verify=False)
throttle_limits = response.json()
return throttle_limits[service]
def set_throttle_limits(self, value=5000, service='kvThrottleLimit'):
api = f"https://{self.server.ip}:18091/settings/throttle"
data = {}
data[service] = value
response = requests.post(api, data=data, auth = requests.auth.HTTPBasicAuth(self.rest.username, self.rest.password), verify=False)
if response.status_code not in (200,201):
self.fail(f'Fail to set throttle limits: {response.text}')
def get_throttling_metrics(self, bucket='default', service='kv'):
throttle_count_total, throttle_seconds_total = 0, 0
api = f"https://{self.server.ip}:18091/metrics"
throttle_seconds_pattern = re.compile(f'throttle_seconds_total{{bucket="{bucket}",for="{service}".*}} (\d+)')
throttle_count_pattern = re.compile(f'throttle_count_total{{bucket="{bucket}",for="{service}".*}} (\d+)')
response = requests.get(api, auth = requests.auth.HTTPBasicAuth(self.rest.username, self.rest.password), verify=False)
if throttle_seconds_pattern.search(response.text):
throttle_seconds_total = int(throttle_seconds_pattern.findall(response.text)[0])
if throttle_count_pattern.search(response.text):
throttle_count_total = int(throttle_count_pattern.findall(response.text)[0])
return throttle_count_total, throttle_seconds_total
def test_kv_insert(self):
before_throttle_count_total, before_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle before, count: {before_throttle_count_total}, seconds: {before_throttle_seconds_total}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.kv_wu})}} as v FROM array_range(0,{self.kvThrottleLimit}) d')
after_throttle_count_total, after_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle after, count: {after_throttle_count_total}, seconds: {after_throttle_seconds_total}')
self.assertTrue(after_throttle_count_total > before_throttle_count_total)
def test_kv_select(self):
self.run_cbq_query(f'DELETE from {self.bucket}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.kv_ru})}} as v FROM array_range(0,{self.kvThrottleLimit}) d')
before_throttle_count_total, before_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle before, count: {before_throttle_count_total}, seconds: {before_throttle_seconds_total}')
self.sleep(3)
self.run_cbq_query(f'SELECT count(city) FROM {self.bucket}')
after_throttle_count_total, after_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle after, count: {after_throttle_count_total}, seconds: {after_throttle_seconds_total}')
self.assertTrue(after_throttle_count_total > before_throttle_count_total)
def test_kv_below_throttle(self):
before_throttle_count_total, before_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle before, count: {before_throttle_count_total}, seconds: {before_throttle_seconds_total}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.kv_wu})}} as v FROM array_range(0,{self.kvThrottleLimit/10}) d')
after_throttle_count_total, after_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle after, count: {after_throttle_count_total}, seconds: {after_throttle_seconds_total}')
self.assertEqual(after_throttle_count_total, before_throttle_count_total)
def test_kv_set_throttle(self):
self.run_cbq_query(f'DELETE from {self.bucket}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.kv_ru})}} as v FROM array_range(0,{self.kvThrottleLimit}) d')
self.set_throttle_limits(value=int(self.kvThrottleLimit*30/100), service='kvThrottleLimit')
result = self.run_cbq_query(f'SELECT count(city) FROM {self.bucket}')
execution_time_at_30pct = float(result['metrics']['executionTime'][:-1])
self.set_throttle_limits(value=self.kvThrottleLimit, service='kvThrottleLimit')
result = self.run_cbq_query(f'SELECT count(city) FROM {self.bucket}')
execution_time_at_100pct = float(result['metrics']['executionTime'][:-1])
# We expect the execution with higher throttle level (100pct) to be faster than the run at lower
# throttle level (30pct) by some factor, in this case 1.75, to be within margin as we cannot
# garantee expected execution time to be consistant.
self.assertTrue(execution_time_at_30pct > execution_time_at_100pct * 1.5, f"Expected Ratio of 1.5 or higher but got: {execution_time_at_30pct/execution_time_at_100pct}")
def test_gsi_create(self):
self.run_cbq_query(f'DELETE from {self.bucket}')
self.run_cbq_query(f'DROP INDEX idx_name IF EXISTS on {self.bucket}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.index_wu})}} as v FROM array_range(0,{self.kvThrottleLimit}) d')
before_throttle_count_total, before_throttle_seconds_total = self.get_throttling_metrics(service='index')
self.log.info(f'Throttle before, count: {before_throttle_count_total}, seconds: {before_throttle_seconds_total}')
self.run_cbq_query(f'CREATE INDEX idx_name on {self.bucket}(city)')
self.wait_for_all_indexes_online
after_throttle_count_total, after_throttle_seconds_total = self.get_throttling_metrics(service='index')
self.log.info(f'Throttle after, count: {after_throttle_count_total}, seconds: {after_throttle_seconds_total}')
self.assertTrue(after_throttle_count_total > before_throttle_count_total)
def test_gsi_below_throttle(self):
self.run_cbq_query(f'DELETE from {self.bucket}')
self.run_cbq_query(f'DROP INDEX idx_name IF EXISTS on {self.bucket}')
self.run_cbq_query(f'INSERT INTO {self.bucket} (key k, value v) SELECT uuid() as k , {{"city": repeat("a", {self.kv_wu})}} as v FROM array_range(0,{self.kvThrottleLimit/10}) d')
before_throttle_count_total, before_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle before, count: {before_throttle_count_total}, seconds: {before_throttle_seconds_total}')
self.run_cbq_query(f'CREATE INDEX idx_name on {self.bucket}(city)')
after_throttle_count_total, after_throttle_seconds_total = self.get_throttling_metrics()
self.log.info(f'Throttle after, count: {after_throttle_count_total}, seconds: {after_throttle_seconds_total}')
self.assertEqual(after_throttle_count_total, before_throttle_count_total)
def test_scope_limit(self):
for i in range(self.scope_limit):
self.run_cbq_query(f'CREATE SCOPE {self.bucket}.scope{i} IF NOT EXISTS')
try:
self.run_cbq_query(f'CREATE SCOPE {self.bucket}.scope{self.scope_limit+1}')
self.fail(f'We should not be able to create scope beyond limit of {self.scope_limit}')
except CBQError as ex:
error = self.process_CBQE(ex)
self.assertEqual(error['code'], 12025)
self.assertTrue(f'_:Maximum number of scopes ({self.scope_limit}) for this bucket has been reached' in error['msg'])
finally:
for i in range(self.scope_limit):
self.run_cbq_query(f'DROP SCOPE {self.bucket}.scope{i} IF EXISTS')
def test_collection_limit(self):
for i in range(self.collection_limit):
self.run_cbq_query(f'CREATE COLLECTION {self.bucket}.`_default`.collection{i} IF NOT EXISTS')
try:
self.run_cbq_query(f'CREATE COLLECTION {self.bucket}.`_default`.collection{self.collection_limit+1}')
self.fail(f'We should not be able to create scope beyond limit of {self.collection_limit}')
except CBQError as ex:
error = self.process_CBQE(ex)
self.assertEqual(error['code'], 12027)
self.assertTrue(f'_:Maximum number of collections ({self.collection_limit}) for this bucket has been reached' in error['msg'])
finally:
for i in range(self.collection_limit):
self.run_cbq_query(f'DROP COLLECTION {self.bucket}.`_default`.collection{i} IF EXISTS')
def test_gsi_limit(self):
indexes = self.run_cbq_query('SELECT raw count(*) FROM system:indexes WHERE `using` = "gsi"')
count = indexes['results'][0]
for i in range(self.gsi_limit - count):
self.run_cbq_query(f'CREATE INDEX idx{i} IF NOT EXISTS ON {self.bucket}(a)')
try:
self.run_cbq_query(f'CREATE INDEX idx{self.gsi_limit+1} IF NOT EXISTS ON {self.bucket}(a)')
self.fail(f'We should not be able to create index beyond limit of {self.gsi_limit}')
except CBQError as ex:
error = self.process_CBQE(ex)
self.assertEqual(error['code'], 4350)
self.assertEqual(error['msg'], f'GSI CreateIndex() - cause: Limit for number of indexes that can be created per bucket has been reached. Limit : {self.gsi_limit}')
finally:
for i in range(self.gsi_limit):
self.run_cbq_query(f'DROP INDEX idx{i} IF EXISTS ON {self.bucket}')