Skip to content

Commit a54b311

Browse files
committed
adjust qps for costly file path query
1 parent cf835ed commit a54b311

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

prime_backup/mcdr/command/value_suggester.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
from prime_backup.utils.waitable_value import WaitableValue
1919

2020
_T = TypeVar('_T')
21-
_P = TypeVar('_P')
21+
_Arg = TypeVar('_Arg')
2222

2323

24-
class ValueSuggestor(ABC, Generic[_T, _P]):
24+
class ValueSuggestor(ABC, Generic[_T, _Arg]):
2525
def __init__(self, task_manager: TaskManager):
2626
self.task_manager = task_manager
2727
self.logger = logger.get()
@@ -37,18 +37,22 @@ def _create_fallback(self) -> _T:
3737
...
3838

3939
@abstractmethod
40-
def _create_value_task(self, source: CommandSource, arg: _P) -> LightTask[_T]:
40+
def _create_value_task(self, source: CommandSource, arg: _Arg) -> LightTask[_T]:
4141
...
4242

4343
@abstractmethod
44-
def _compute_task_key(self, source: CommandSource, arg: _P) -> str:
44+
def _compute_task_key(self, source: CommandSource, arg: _Arg) -> str:
4545
...
4646

47+
@classmethod
48+
def _min_query_gap(cls) -> float:
49+
return 1
50+
4751
def __reset(self):
4852
self.last_future.set(self._create_fallback())
4953
self.current_future.set(self._create_fallback())
5054

51-
def request(self, source: CommandSource, arg: _P) -> WaitableValue[_T]:
55+
def request(self, source: CommandSource, arg: _Arg) -> WaitableValue[_T]:
5256
with self.lock:
5357
task_key = self._compute_task_key(source, arg)
5458
if task_key != self.current_task_key:
@@ -59,7 +63,7 @@ def request(self, source: CommandSource, arg: _P) -> WaitableValue[_T]:
5963
return self.last_future
6064

6165
now = time.time()
62-
if now - self.last_fetch_time <= 1: # max qps == 1
66+
if now - self.last_fetch_time <= self._min_query_gap():
6367
return self.current_future
6468

6569
def callback(result: Optional[_T], err: Optional[Exception]):
@@ -81,7 +85,7 @@ def callback(result: Optional[_T], err: Optional[Exception]):
8185
self.current_future = wv
8286
return wv
8387

84-
def suggest(self, source: CommandSource, arg: _P, timeout: float = 0.2) -> _T:
88+
def suggest(self, source: CommandSource, arg: _Arg, timeout: float = 0.2) -> _T:
8589
wv = self.request(source, arg)
8690
if wv.wait(timeout) == WaitableValue.EMPTY:
8791
return self._create_fallback()
@@ -116,7 +120,7 @@ def _compute_task_key(self, source: CommandSource, ctx: CommandContext) -> str:
116120
return ''
117121

118122

119-
class BackupFilePathSuggestor(ValueSuggestor[List[str], int]):
123+
class BackupFilePathSuggestor(ValueSuggestor[List[str], str]):
120124
@override
121125
def _create_fallback(self) -> List[str]:
122126
return []
@@ -129,9 +133,14 @@ def _create_value_task(self, source: CommandSource, backup_id_arg: str) -> Light
129133
def _compute_task_key(self, source: CommandSource, backup_id_arg: str) -> str:
130134
return backup_id_arg
131135

136+
@classmethod
137+
@override
138+
def _min_query_gap(cls) -> float:
139+
return 3
140+
132141
@override
133-
def suggest(self, source: CommandSource, backup_id: int, timeout: float = 0.2) -> _T:
134-
return super().suggest(source, backup_id, timeout)
142+
def suggest(self, source: CommandSource, backup_id_arg: str, timeout: float = 0.2) -> _T:
143+
return super().suggest(source, backup_id_arg, timeout)
135144

136145

137146
class FilesetFilePathSuggestor(ValueSuggestor[List[str], int]):
@@ -147,6 +156,11 @@ def _create_value_task(self, source: CommandSource, fileset_id: int) -> LightTas
147156
def _compute_task_key(self, source: CommandSource, fileset_id: int) -> str:
148157
return str(fileset_id)
149158

159+
@classmethod
160+
@override
161+
def _min_query_gap(cls) -> float:
162+
return 3
163+
150164
@override
151165
def suggest(self, source: CommandSource, fileset_id: int, timeout: float = 0.2) -> _T:
152166
return super().suggest(source, fileset_id, timeout)

0 commit comments

Comments
 (0)