Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow disabling of auto-resize of crawler pods #1964

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/btrixcloud/operator/baseoperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import yaml
from btrixcloud.k8sapi import K8sAPI
from btrixcloud.utils import is_bool


if TYPE_CHECKING:
Expand All @@ -30,6 +31,7 @@ class K8sOpAPI(K8sAPI):
"""Additional k8s api for operators"""

has_pod_metrics: bool
enable_auto_resize: bool
max_crawler_memory_size: int

def __init__(self):
Expand All @@ -39,6 +41,7 @@ def __init__(self):
self.shared_params = yaml.safe_load(fh_config)

self.has_pod_metrics = False
self.enable_auto_resize = False
self.max_crawler_memory_size = 0

self.compute_crawler_resources()
Expand Down Expand Up @@ -127,6 +130,11 @@ async def async_init(self) -> None:
self.has_pod_metrics = await self.is_pod_metrics_available()
print("Pod Metrics Available:", self.has_pod_metrics)

self.enable_auto_resize = self.has_pod_metrics and is_bool(
os.environ.get("ENABLE_AUTO_RESIZE_CRAWLERS")
)
print("Auto-Resize Enabled", self.enable_auto_resize)


# pylint: disable=too-many-instance-attributes, too-many-arguments
# ============================================================================
Expand Down
25 changes: 15 additions & 10 deletions backend/btrixcloud/operator/crawls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@
StorageRef,
)

from btrixcloud.utils import (
from_k8s_date,
to_k8s_date,
dt_now,
)
from btrixcloud.utils import from_k8s_date, to_k8s_date, dt_now

from .baseoperator import BaseOperator, Redis
from .models import (
Expand Down Expand Up @@ -84,6 +80,13 @@
class CrawlOperator(BaseOperator):
"""CrawlOperator Handler"""

done_key: str
pages_key: str
errors_key: str

fast_retry_secs: int
log_failed_crawl_lines: int

def __init__(self, *args):
super().__init__(*args)

Expand Down Expand Up @@ -221,8 +224,7 @@ async def sync_crawls(self, data: MCSyncData):
data.related.get(METRICS, {}),
)

# auto-scaling not possible without pod metrics
if self.k8s.has_pod_metrics:
if self.k8s.enable_auto_resize:
# auto sizing handled here
await self.handle_auto_size(status.podStatus)

Expand Down Expand Up @@ -377,7 +379,10 @@ def _load_crawler(self, params, i, status, children):
params["priorityClassName"] = pri_class
params["cpu"] = pod_info.newCpu or params.get(cpu_field)
params["memory"] = pod_info.newMemory or params.get(mem_field)
params["memory_limit"] = float(params["memory"]) * MEM_LIMIT_PADDING
if self.k8s.enable_auto_resize:
params["memory_limit"] = float(params["memory"]) * MEM_LIMIT_PADDING
else:
params["memory_limit"] = self.k8s.max_crawler_memory_size
params["workers"] = params.get(worker_field) or 1
params["do_restart"] = (
pod_info.should_restart_pod() or params.get("force_restart")
Expand Down Expand Up @@ -555,7 +560,7 @@ def get_related(self, data: MCBaseRequest):
},
]

if self.k8s.has_pod_metrics:
if self.k8s.enable_auto_resize:
related_resources.append(
{
"apiVersion": METRICS_API,
Expand Down Expand Up @@ -1072,7 +1077,7 @@ async def add_used_stats(
pod_info.used.storage = storage

# if no pod metrics, get memory estimate from redis itself
if not self.k8s.has_pod_metrics:
if not self.k8s.enable_auto_resize:
stats = await redis.info("memory")
pod_info.used.memory = int(stats.get("used_memory_rss", 0))

Expand Down
2 changes: 2 additions & 0 deletions chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ data:

MAX_CRAWLER_MEMORY: "{{ .Values.max_crawler_memory }}"

ENABLE_AUTO_RESIZE_CRAWLERS: "{{ .Values.enable_auto_resize_crawlers }}"

BILLING_ENABLED: "{{ .Values.billing_enabled }}"

SALES_EMAIL: "{{ .Values.sales_email }}"
Expand Down
6 changes: 6 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ crawler_extra_memory_per_browser: 768Mi
# crawler_memory = crawler_memory_base + crawler_memory_per_extra_browser * (crawler_browser_instances - 1)
# crawler_memory:

# Crawler Autoscaling
# ---------------------

# if set to true, automatically adjust crawler memory usage up to max_crawler_memory
enable_auto_resize_crawlers: false


# max crawler memory, if set, will enable auto-resizing of crawler pods up to this size
# if not set, no auto-resizing is done, and crawls always use 'crawler_memory' memory
Expand Down
Loading