Skip to content

Commit e46360e

Browse files
authored
Fix race on table create delete (#13394)
1 parent 2555770 commit e46360e

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

ydb/tests/stress/olap_workload/__main__.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import random
77
import threading
8+
from enum import Enum
89

910
ydb.interceptor.monkey_patch_event_handler()
1011

@@ -137,12 +138,17 @@ def join(self):
137138

138139

139140
class WorkloadTablesCreateDrop(WorkloadBase):
141+
class TableStatus(Enum):
142+
CREATING = "Creating",
143+
AVAILABLE = "Available",
144+
DELITING = "Deleting"
145+
140146
def __init__(self, client, prefix, stop, allow_nullables_in_pk):
141147
super().__init__(client, prefix, "create_drop", stop)
142148
self.allow_nullables_in_pk = allow_nullables_in_pk
143149
self.created = 0
144150
self.deleted = 0
145-
self.tables = set()
151+
self.tables = {}
146152
self.lock = threading.Lock()
147153

148154
def get_stat(self):
@@ -154,13 +160,16 @@ def _generate_new_table_n(self):
154160
r = random.randint(1, 40000)
155161
with self.lock:
156162
if r not in self.tables:
163+
self.tables[r] = WorkloadTablesCreateDrop.TableStatus.CREATING
157164
return r
158165

159-
def _get_existing_table_n(self):
166+
def _get_table_to_delete(self):
160167
with self.lock:
161-
if len(self.tables) == 0:
162-
return None
163-
return next(iter(self.tables))
168+
for n, s in self.tables.items():
169+
if s == WorkloadTablesCreateDrop.TableStatus.AVAILABLE:
170+
self.tables[n] = WorkloadTablesCreateDrop.TableStatus.DELITING
171+
return n
172+
return None
164173

165174
def create_table(self, table):
166175
path = self.get_table_path(table)
@@ -196,19 +205,19 @@ def _create_tables_loop(self):
196205
n = self._generate_new_table_n()
197206
self.create_table(str(n))
198207
with self.lock:
199-
self.tables.add(n)
208+
self.tables[n] = WorkloadTablesCreateDrop.TableStatus.AVAILABLE
200209
self.created += 1
201210

202211
def _delete_tables_loop(self):
203212
while not self.is_stop_requested():
204-
n = self._get_existing_table_n()
213+
n = self._get_table_to_delete()
205214
if n is None:
206215
print("create_drop: No tables to delete")
207216
time.sleep(10)
208217
continue
209218
self.client.drop_table(self.get_table_path(str(n)))
210219
with self.lock:
211-
self.tables.remove(n)
220+
del self.tables[n]
212221
self.deleted += 1
213222

214223
def get_workload_thread_funcs(self):

0 commit comments

Comments
 (0)