Skip to content

Commit

Permalink
Fix race on table create delete (#13394)
Browse files Browse the repository at this point in the history
  • Loading branch information
zverevgeny authored Jan 15, 2025
1 parent 2555770 commit e46360e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions ydb/tests/stress/olap_workload/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import random
import threading
from enum import Enum

ydb.interceptor.monkey_patch_event_handler()

Expand Down Expand Up @@ -137,12 +138,17 @@ def join(self):


class WorkloadTablesCreateDrop(WorkloadBase):
class TableStatus(Enum):
CREATING = "Creating",
AVAILABLE = "Available",
DELITING = "Deleting"

def __init__(self, client, prefix, stop, allow_nullables_in_pk):
super().__init__(client, prefix, "create_drop", stop)
self.allow_nullables_in_pk = allow_nullables_in_pk
self.created = 0
self.deleted = 0
self.tables = set()
self.tables = {}
self.lock = threading.Lock()

def get_stat(self):
Expand All @@ -154,13 +160,16 @@ def _generate_new_table_n(self):
r = random.randint(1, 40000)
with self.lock:
if r not in self.tables:
self.tables[r] = WorkloadTablesCreateDrop.TableStatus.CREATING
return r

def _get_existing_table_n(self):
def _get_table_to_delete(self):
with self.lock:
if len(self.tables) == 0:
return None
return next(iter(self.tables))
for n, s in self.tables.items():
if s == WorkloadTablesCreateDrop.TableStatus.AVAILABLE:
self.tables[n] = WorkloadTablesCreateDrop.TableStatus.DELITING
return n
return None

def create_table(self, table):
path = self.get_table_path(table)
Expand Down Expand Up @@ -196,19 +205,19 @@ def _create_tables_loop(self):
n = self._generate_new_table_n()
self.create_table(str(n))
with self.lock:
self.tables.add(n)
self.tables[n] = WorkloadTablesCreateDrop.TableStatus.AVAILABLE
self.created += 1

def _delete_tables_loop(self):
while not self.is_stop_requested():
n = self._get_existing_table_n()
n = self._get_table_to_delete()
if n is None:
print("create_drop: No tables to delete")
time.sleep(10)
continue
self.client.drop_table(self.get_table_path(str(n)))
with self.lock:
self.tables.remove(n)
del self.tables[n]
self.deleted += 1

def get_workload_thread_funcs(self):
Expand Down

0 comments on commit e46360e

Please sign in to comment.