Skip to content

Commit bbd3c6f

Browse files
Add a field for TTL in WindowTaskRateLimit. (#4385)
1 parent 20997be commit bbd3c6f

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/clusterfuzz/_internal/base/tasks/task_rate_limiting.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class TaskRateLimiter:
3737
"""Rate limiter for tasks. This limits tasks to 100 erroneous runs or 2000
3838
succesful runs in 6 hours. It keeps track of task completion when record_task
3939
is called at the end of every task."""
40-
TASK_RATE_LIMIT_WINDOW = datetime.timedelta(hours=6)
4140
TASK_RATE_LIMIT_MAX_ERRORS = 100
4241
# TODO(metzman): Reevaluate this number, it's probably too high.
4342
TASK_RATE_LIMIT_MAX_COMPLETIONS = 2000
@@ -74,7 +73,9 @@ def is_rate_limited(self) -> bool:
7473
if environment.get_value('COMMAND_OVERRIDE'):
7574
# A user wants to run this task.
7675
return False
77-
window_start = _get_datetime_now() - self.TASK_RATE_LIMIT_WINDOW
76+
window_start = (
77+
_get_datetime_now() -
78+
data_types.WindowRateLimitTask.TASK_RATE_LIMIT_WINDOW)
7879
query = data_types.WindowRateLimitTask.query(
7980
data_types.WindowRateLimitTask.task_name == self.task_name,
8081
data_types.WindowRateLimitTask.task_argument == self.task_argument,

src/clusterfuzz/_internal/datastore/data_types.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Classes for objects stored in the datastore."""
1515

16+
import datetime
1617
import re
1718

1819
from google.cloud import ndb
@@ -1119,13 +1120,21 @@ class WindowRateLimitTask(Model):
11191120
it will have a different lifecycle (it's not needed after the window
11201121
completes). This should have a TTL as TASK_RATE_LIMIT_WINDOW in
11211122
task_rate_limiting.py (6 hours)."""
1122-
# TODO(metzman): Consider using task_id.
1123+
TASK_RATE_LIMIT_WINDOW = datetime.timedelta(hours=6)
1124+
11231125
timestamp = ndb.DateTimeProperty(auto_now_add=True, indexed=True)
1126+
# Only use this for TTL. It should only be saved to by ClusterFuzz, not read.
1127+
ttl_expiry_timestamp = ndb.DateTimeProperty()
1128+
# TODO(metzman): Consider using task_id.
11241129
task_name = ndb.StringProperty(indexed=True)
11251130
task_argument = ndb.StringProperty(indexed=True)
11261131
job_name = ndb.StringProperty(indexed=True)
11271132
status = ndb.StringProperty(choices=[TaskState.ERROR, TaskState.FINISHED])
11281133

1134+
def _pre_put_hook(self):
1135+
self.ttl_expiry_timestamp = (
1136+
datetime.datetime.now() + self.TASK_RATE_LIMIT_WINDOW)
1137+
11291138

11301139
class BuildMetadata(Model):
11311140
"""Metadata associated with a particular archived build."""

src/clusterfuzz/_internal/tests/core/base/tasks/task_rate_limiting_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_is_rate_limited_old_tasks(self):
9191
"""Test is_rate_limited() with old tasks outside the window."""
9292
# Add tasks outside the time window.
9393
window_start = (
94-
self.now - task_rate_limiting.TaskRateLimiter.TASK_RATE_LIMIT_WINDOW)
94+
self.now - data_types.WindowRateLimitTask.TASK_RATE_LIMIT_WINDOW)
9595
self._create_n_tasks(
9696
task_rate_limiting.TaskRateLimiter.TASK_RATE_LIMIT_MAX_COMPLETIONS + 1,
9797
timestamp=window_start - datetime.timedelta(minutes=10))

0 commit comments

Comments
 (0)