{question}
How to Schedule Resizing of Workspaces (Up and Down) Using the Management API?
{question}
{answer}
You can resize the cluster programmatically using the Management API. This also allows you to schedule the resize at specific times using SingleStore Notebooks, which can be set up to run as jobs at regular intervals eliminating the need for external cron jobs.
In this article, we’ll provide examples of resizing workspaces up and down.
In order to use management API, you need to create an API key. In SingleStore Portal, navigate to, Configuration -> API Keys -> Create API Key
Give a Name, expiry date and Access level to create the API Key:
Once the API Key is created, you will use this in your python program to connect to the workspace and perform actions using management API.
import requests
# Replace with your actual values
API_TOKEN = "Your API Key"
WORKSPACE_ID = "Your workspace ID"
NEW_SIZE = "S-1" # Example: change to desired size like "S-2", "S-4", etc.
SCALE_FACTOR= 1
# Base URL for SingleStore Management API (Cloud)
BASE_URL = "https://api.singlestore.com/v1"
# Endpoint to update the workspace
endpoint = f"{BASE_URL}/workspaces/{WORKSPACE_ID}"
# Headers for authentication and content type
headers = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json"
}
# Payload to update the size
payload = {
"size": NEW_SIZE
}
# Make the PATCH request
response = requests.patch(endpoint, headers=headers, json=payload)
# Print response
if response.status_code == 200:
print("Workspace size updated successfully.")
print(response.json())
else:
print(f"Failed to update workspace size. Status Code: {response.status_code}")
print(response.text)
Resize Up The Workspace
We will be using the above code to Resize up the workspace from size, S-00 to S-1.
In your case, make the changes to the above code to add your API Key, Workspace ID and the size to update. In this case, we will name this code scaling.py and will execute as python3 scaling.py. After this the workspace will resize to S-1.
Size has been changed to S-1.
Resize Down The Workspace
We will be resizing back the workspace from S-1 to S-00. For this just replace the NEW_SIZE value in the above code from S-1 to S-00 and run the script again. This will resize the workspace back to S-00.
Changing the ScaleFactor of the workspace
Scaling operations are performed by changing the scaleFactor of the deployment. For example, changing the scaleFactor from "1" to "2" or "4". This will automatically increase the amount of vCPU and memory available to workloads from 1x to up to 4x. Let us see an example of how to change the scale factor from 1X to 2X.
In the above code change,
SCALE_FACTOR= 2 and in the PAYLOAD section, replace, "size": NEW_SIZE with scaleFactor = SCALE_FACTOR
Scheduling the Resize via notebooks
Create a notebook to include the above code. Go to Singlestore Portal -> Develop -> Data Studio -> New -> New NoteBook. Create a New shared Notebook.
Select Python and copy over the above code to the notebook. It will be automatically saved. Test the code to see it works. Once confirmed to be working, create a new job.
Create a New Scheduled Job
To create a new job, Go to Singlestore Portal -> Develop -> Data Studio -> Jobs -> Schedule.
Give Name, Description and select your saved notebook. Click on Next.
In the next page, set a schedule to have your notebook run at the interval when you want the resizing to be run, Example below,