From 52fc788eeeb4a7d65cca99b264f6b9d09aa67e3b Mon Sep 17 00:00:00 2001 From: Lalit Arvind Date: Sat, 23 Nov 2024 15:50:38 -0700 Subject: [PATCH] Added resources, created a prototype trigger and its corresponding dockerfile for update configurations, Testing required --- clickhouse/config_update_scripts/Dockerfile | 9 +++++ .../config_update_scripts/requirements.txt | 8 +++++ .../config_update_scripts/update_compose.py | 9 ++--- .../config_update_scripts/update_trigger.py | 34 +++++++++++++++++++ clickhouse/docker-compose.yaml | 23 ++++++++----- 5 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 clickhouse/config_update_scripts/Dockerfile create mode 100644 clickhouse/config_update_scripts/requirements.txt create mode 100644 clickhouse/config_update_scripts/update_trigger.py diff --git a/clickhouse/config_update_scripts/Dockerfile b/clickhouse/config_update_scripts/Dockerfile new file mode 100644 index 0000000..79e3995 --- /dev/null +++ b/clickhouse/config_update_scripts/Dockerfile @@ -0,0 +1,9 @@ +FROM python + +WORKDIR /update_scripts + +COPY . /update_scripts/ + +RUN pip install --no-cache-dir -r requirements.txt + +CMD ["python3","update_trigger.py"] \ No newline at end of file diff --git a/clickhouse/config_update_scripts/requirements.txt b/clickhouse/config_update_scripts/requirements.txt new file mode 100644 index 0000000..711648d --- /dev/null +++ b/clickhouse/config_update_scripts/requirements.txt @@ -0,0 +1,8 @@ +subprocess +json +jinja2 +yaml +re +xml +schedule +time \ No newline at end of file diff --git a/clickhouse/config_update_scripts/update_compose.py b/clickhouse/config_update_scripts/update_compose.py index 74d85d1..4786cbb 100644 --- a/clickhouse/config_update_scripts/update_compose.py +++ b/clickhouse/config_update_scripts/update_compose.py @@ -4,6 +4,7 @@ import subprocess import json import xml.etree.ElementTree as ET import os +import re if __name__ == "__main__": @@ -15,14 +16,10 @@ if __name__ == "__main__": all_services = [json.loads(s) for s in all_services] # extracting the name, removing the custom id from it and storing it in a list - all_service_names = [service['Names'].split('.')[0] for service in all_services] - # extracting only 'keeper1', 'server1'... + all_service_names = [service['Names'].split('.')[0] for service in all_services if re.findall(r'clickhouse-server',service['Names'])] + # extracting only 'server1','server2'... all_service_names = [ name.split('-')[-1] for name in all_service_names] - # removing all keeepers - all_service_names.remove('keeper1') - all_service_names.remove('keeper2') - all_service_names.remove('keeper3') curr_num_servers = sorted(all_service_names)[-1][-1] replication_factor = 2 diff --git a/clickhouse/config_update_scripts/update_trigger.py b/clickhouse/config_update_scripts/update_trigger.py new file mode 100644 index 0000000..fe154f5 --- /dev/null +++ b/clickhouse/config_update_scripts/update_trigger.py @@ -0,0 +1,34 @@ +import subprocess +import json +import re +import schedule +import time + +def check_util_exec(): + # extracting details of each running container in json format + try: + all_services = subprocess.check_output(["docker","stats","--no-stream","--format","json"],text=True).split('\n')[:-1] + except subprocess.CalledProcessError as e: + print(f"Command failed with return code {e.returncode}") + + all_services = [json.loads(s) for s in all_services] + + resource_util_exceed_flag = True # Flag to check if all of the containers have exceeded 80% memory utilization + for service in all_services: + if re.findall(r'clickhouse-server',service['Name']): + if float(service['MemPerc'][:-1]) < 80: + resource_util_exceed_flag = False + + if resource_util_exceed_flag: + process = subprocess.Popen(['python3','update_compose.py'],text=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) + stdout, stderr = process.communicate() # Wait for the process to finish and capture output + print("Standard Output:", stdout) + print("Standard Error:", stderr) + +if __name__ == "__main__": + schedule.every(30).seconds.do(check_util_exec) + while True: + schedule.run_pending() + time.sleep(1) + + \ No newline at end of file diff --git a/clickhouse/docker-compose.yaml b/clickhouse/docker-compose.yaml index 7ee9aea..ad37e56 100644 --- a/clickhouse/docker-compose.yaml +++ b/clickhouse/docker-compose.yaml @@ -1,4 +1,9 @@ services: + registry: + image: registry:2 + ports: + - "5000:5000" + clickhouse-keeper1: image: clickhouse/clickhouse-server:latest container_name: clickhouse-keeper1 @@ -69,10 +74,10 @@ services: # constraints: [node.labels.role == server] update_config: delay: 10s - # resources: - # limits: - # cpus: "0.50" - # memory: 100M + resources: + limits: + cpus: "0.50" + memory: 1200M depends_on: - clickhouse-keeper1 - clickhouse-keeper2 @@ -100,10 +105,10 @@ services: # constraints: [node.labels.role == server] update_config: delay: 10s - # resources: - # limits: - # cpus: "0.50" - # memory: 100M + resources: + limits: + cpus: "0.50" + memory: 1200M depends_on: - clickhouse-keeper1 - clickhouse-keeper2 @@ -115,8 +120,10 @@ services: networks: clickhouse-server-network: driver: overlay + attachable: true clickhouse-keeper-network: driver: overlay + attachable: true volumes: clickhouse_data1: