Skip to content

Commit a9a49fe

Browse files
Create autoscaling_script.ipynb
Added an example Junyper Notebook.
1 parent 956cacb commit a9a49fe

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 7,
6+
"id": "4c9711ff-fe70-45d1-a79f-d71df86e64cd",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import requests\n",
11+
"from requests.auth import HTTPBasicAuth\n",
12+
"from datetime import datetime\n",
13+
"\n",
14+
"auth_data = HTTPBasicAuth('API-KEY', 'API-SECRET')\n",
15+
"\n",
16+
"date_format = '%Y-%m-%dT%H:%M:%S.%f'\n",
17+
"\n",
18+
"def get_metrics(organization_id):\n",
19+
" api_url = f'https://console.cratedb.cloud/api/v2/organizations/{organization_id}/metrics/prometheus/'\n",
20+
" headers = {'accept': 'plain/text'}\n",
21+
" response = requests.get(api_url, auth=auth_data)\n",
22+
" return response.text\n",
23+
"\n",
24+
"def num_shards(metrics):\n",
25+
" for row in metrics.split('\\n'):\n",
26+
" line = row.split(' ')\n",
27+
" if line[0].startswith('crate_node{name=\"shard_stats\"') and 'property=\"total\"' in line[0] and 'crate-data-hot-97ebb337-b44e-446f-adca-a092fd1df0aa' in line[0]:\n",
28+
" return float(line[1])\n",
29+
"\n",
30+
"def get_cluster_status(cluster_id):\n",
31+
" api_url = f'https://console.cratedb.cloud/api/v2/clusters/{cluster_id}/'\n",
32+
" return requests.get(api_url, auth=auth_data).json()\n",
33+
"\n",
34+
"def get_cluster_running_op(cluster_status, cluster_id):\n",
35+
" if (datetime.now() - datetime.strptime(cluster_status['health']['last_seen'], date_format)).seconds > 30:\n",
36+
" cluster_status = get_cluster_status(cluster_id)\n",
37+
" return cluster_status['health']['running_operation']\n",
38+
"\n",
39+
"def get_cluster_num_nodes(cluster_status, cluster_id):\n",
40+
" if (datetime.now() - datetime.strptime(cluster_status['health']['last_seen'], date_format)).seconds > 30:\n",
41+
" cluster_status = get_cluster_status(cluster_id)\n",
42+
" return cluster_status['num_nodes'] - 1\n"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 9,
48+
"id": "c14c779c-1809-4e19-b995-e850cfa667ba",
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"Current number of shards: 25.0\n",
56+
"Start scaling out from 3 to 4\n"
57+
]
58+
}
59+
],
60+
"source": [
61+
"import time\n",
62+
"\n",
63+
"delay_seconds = 5\n",
64+
"max_num_shard = 30\n",
65+
"\n",
66+
"organization_id = 'Use your organisation ID here'\n",
67+
"cluster_id = 'Use your Cluster ID here'\n",
68+
"\n",
69+
"api_url = f'https://console.cratedb.cloud/api/v2/clusters/{cluster_id}/scale/'\n",
70+
"\n",
71+
"while True:\n",
72+
" metrics = get_metrics(organization_id) # Pass organization_id to get_metrics function\n",
73+
" cluster_status = get_cluster_status(cluster_id)\n",
74+
" num = num_shards(metrics)\n",
75+
" if num is not None: # Check if num is not None\n",
76+
" print(f'Current number of shards: {num}')\n",
77+
" if num > (0.8 * max_num_shard):\n",
78+
" num_nodes = get_cluster_num_nodes(cluster_status, cluster_id) # Pass cluster_id to get_cluster_num_nodes function\n",
79+
" print(f'Start scaling out from {num_nodes + 1} to {num_nodes + 2}')\n",
80+
" requests.put(api_url, json={'product_unit':num_nodes + 1}, auth=auth_data)\n",
81+
" time.sleep(delay_seconds)\n",
82+
" while get_cluster_running_op(cluster_status, cluster_id) != '':\n",
83+
" cluster_status = get_cluster_status(cluster_id)\n",
84+
" continue\n",
85+
" print('Scaled up successfully!')\n",
86+
" \n",
87+
" elif num < (0.5 * max_num_shard):\n",
88+
" num_nodes = get_cluster_num_nodes(cluster_status, cluster_id) # Pass cluster_id to get_cluster_num_nodes function\n",
89+
" print(f'Start scaling down from {num_nodes + 1} to {num_nodes}')\n",
90+
" requests.put(api_url, json={'product_unit':num_nodes - 1}, auth=auth_data)\n",
91+
" time.sleep(delay_seconds)\n",
92+
" while get_cluster_running_op(cluster_status, cluster_id) != '':\n",
93+
" cluster_status = get_cluster_status(cluster_id)\n",
94+
" continue\n",
95+
" print('Scaled down successfully!')\n",
96+
" else:\n",
97+
" print('Nothing to do!')\n",
98+
" else:\n",
99+
" print('Failed to retrieve shard metrics.')\n",
100+
" time.sleep(delay_seconds)\n",
101+
"\n"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "Python 3 (ipykernel)",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.11.6"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 5
126+
}

0 commit comments

Comments
 (0)