|
| 1 | +(cluster-automation)= |
| 2 | +# Automation |
| 3 | + |
| 4 | +Automation in CrateDB Cloud allows users to streamline and manage routine |
| 5 | +database operations efficiently. Two primary automation features available are |
| 6 | +the SQL Scheduler and Table Policies, both of which facilitate the maintenance |
| 7 | +and optimization of database tasks. |
| 8 | + |
| 9 | +:::{important} |
| 10 | +- Automation is available for all newly deployed clusters. |
| 11 | +- For existing clusters, the feature can be enabled on demand. (Contact |
| 12 | + [support](https://support.crate.io/) for activation.) |
| 13 | + |
| 14 | +Automation utilizes a dedicated database user `gc_admin` with full cluster |
| 15 | +privileges to execute scheduled tasks and persists data in the `gc` schema. |
| 16 | +::: |
| 17 | + |
| 18 | +## SQL Scheduler |
| 19 | + |
| 20 | +The SQL Scheduler is designed to automate routine database tasks by scheduling |
| 21 | +SQL queries to run at specific times, in UTC time. This feature supports |
| 22 | +creating job descriptions with valid [cron patterns](https://www.ibm.com/docs/en/db2oc?topic=task-unix-cron-format) |
| 23 | +and SQL statements, enabling a wide range of tasks. Users can manage these jobs |
| 24 | +through the Cloud UI, adding, removing, editing, activating, and deactivating |
| 25 | +them as needed. |
| 26 | + |
| 27 | +### Use Cases |
| 28 | + |
| 29 | +- Regularly updating or aggregating table data. |
| 30 | +- Automating export and import of data. |
| 31 | +- Deleting old/redundant data to maintain database efficiency. |
| 32 | + |
| 33 | +### Accessing and Using the SQL Scheduler |
| 34 | + |
| 35 | +SQL Scheduler can be found in the "Automation" tab in the left-hand |
| 36 | +navigation menu. There are two tabs relevant to the SQL Scheduler: |
| 37 | + |
| 38 | + |
| 39 | +**SQL Scheduler** shows a list of your existing jobs. In the list, you can |
| 40 | +activate/deactivate each job with a toggle in the "Active" column. You can |
| 41 | +also edit and delete jobs with buttons on the right side of the list. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +**Logs** shows a list of *scheduled* job runs, whether they failed or succeeded, |
| 47 | +execution time, run time, and the error in case they were unsuccessful. In case |
| 48 | +of an error, more details can be viewed showing the executed query and a stack |
| 49 | +trace. You can filter the logs by status or by a specific job. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +### Examples |
| 54 | + |
| 55 | +#### Cleanup of Old Files |
| 56 | + |
| 57 | +Cleanup tasks represent a common use case for these types of automated jobs. |
| 58 | +This example deletes records older than 30 days from a specified table once a |
| 59 | +day: |
| 60 | + |
| 61 | +```sql |
| 62 | +DELETE FROM "sample_data" |
| 63 | +WHERE |
| 64 | + "timestamp_column" < NOW() - INTERVAL '30 days'; |
| 65 | +``` |
| 66 | + |
| 67 | +How often you run it, of course, depends on you, but once a day is common for |
| 68 | +cleanup. This expression runs every day at 2:30 PM UTC: |
| 69 | + |
| 70 | +Schedule: `30 14 * * *` |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +#### Copying Logs into a Persistent Table |
| 75 | + |
| 76 | +Another useful example might be copying data to another table for archival |
| 77 | +purposes. This specifically copies from the system logs table into one of |
| 78 | +our own tables. |
| 79 | + |
| 80 | +```sql |
| 81 | +CREATE TABLE IF NOT EXISTS "logs"."persistent_jobs_log" ( |
| 82 | + "classification" OBJECT (DYNAMIC), |
| 83 | + "ended" TIMESTAMP WITH TIME ZONE, |
| 84 | + "error" TEXT, |
| 85 | + "id" TEXT, |
| 86 | + "node" OBJECT (DYNAMIC), |
| 87 | + "started" TIMESTAMP WITH TIME ZONE, |
| 88 | + "stmt" TEXT, |
| 89 | + "username" TEXT, |
| 90 | + PRIMARY KEY (id) |
| 91 | +) CLUSTERED INTO 1 SHARDS; |
| 92 | + |
| 93 | +INSERT INTO |
| 94 | + "logs"."persistent_jobs_log" |
| 95 | +SELECT |
| 96 | + * |
| 97 | +FROM |
| 98 | + sys.jobs_log |
| 99 | +ON CONFLICT ("id") DO NOTHING; |
| 100 | +``` |
| 101 | + |
| 102 | +In this example, we schedule the job to run every hour: |
| 103 | + |
| 104 | +Schedule: `0 * * * *` |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +:::{note} |
| 109 | +Limitations and Known Issues: |
| 110 | +* Only one job can run at a time; subsequent jobs will be queued until the |
| 111 | + current one completes. |
| 112 | +* Long-running jobs may block the execution of queued jobs, leading to |
| 113 | + potential delays. |
| 114 | +::: |
| 115 | + |
| 116 | + |
| 117 | +## Table Policies |
| 118 | + |
| 119 | +Table policies allow automating maintenance operations for **partitioned tables**. |
| 120 | +Automated actions can be set up to be executed daily based on a pre-configured |
| 121 | +ruleset. |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +### Overview |
| 126 | + |
| 127 | +Table policy overview can be found in the left-hand navigation menu under |
| 128 | +"Automation". From the list of policies, you can create, delete, edit, or |
| 129 | +(de)activate them. Logs of executed policies can be found in the "Logs" tab. |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +A new policy can be created with the "Add New Policy" button. |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +After naming the policy and selecting the tables/schemas to be impacted, you |
| 138 | +must specify the time column. This column, which should be a timestamp used for |
| 139 | +partitioning, will determine the data affected by the policy. It is important |
| 140 | +that this time column is consistently present across all targeted tables/schemas. |
| 141 | +While you can apply the policy to tables without the specified time column, |
| 142 | +it will not get executed for those. If your tables have different timestamp |
| 143 | +columns, consider setting up separate policies for each to ensure accuracy. |
| 144 | + |
| 145 | +:::{note} |
| 146 | +The "Time Column" must be of type `TIMESTAMP`. |
| 147 | +::: |
| 148 | + |
| 149 | +Next, a condition is used to determine affected partitions. The system is |
| 150 | +time-based. A partition is eligible for action if the value in the partitioned |
| 151 | +column is smaller (`<`), or smaller or equal (`<=`) than the current date minus |
| 152 | +`n` days, months, or years. |
| 153 | + |
| 154 | +### Actions |
| 155 | + |
| 156 | +Following actions are supported: |
| 157 | +* **Delete:** Deletes eligible partitions along with their data. |
| 158 | +* **Set replicas:** Changes the replication factor of eligible partitions. |
| 159 | +* **Force merge:** Merges segments on eligible partitions to ensure a specified number. |
| 160 | + |
| 161 | +After filling out the info, you can see the affected schemas/tables and the |
| 162 | +number of affected partitions if the policy gets executed at this very moment. |
| 163 | + |
| 164 | +### Examples |
| 165 | + |
| 166 | +Consider a scenario where you have a table and want to optimize space on your |
| 167 | +cluster. For older data (e.g., 30 days), which may have already been snapshotted |
| 168 | +and is only accessed infrequently, meaning it's not used for live analyitcs, it |
| 169 | +might be sufficient for it to exist just once in the cluster without replication. |
| 170 | +Additionally, you may not want to retain data older than 60 days. |
| 171 | + |
| 172 | +Assume the following table schema: |
| 173 | + |
| 174 | +```sql |
| 175 | +CREATE TABLE data_table ( |
| 176 | + ts TIMESTAMP, |
| 177 | + ts_day GENERATED ALWAYS AS date_trunc('day',ts), |
| 178 | + val DOUBLE |
| 179 | +) PARTITIONED BY (ts_day); |
| 180 | +``` |
| 181 | + |
| 182 | +For the outlined scenario, the policies would be as follows: |
| 183 | + |
| 184 | +**Policy 1 - Saving replica space:** |
| 185 | +* **Time Column:** `ts_day` |
| 186 | +* **Condition:** `older than 30 days` |
| 187 | +* **Actions:** `Set replicas to 0.` |
| 188 | + |
| 189 | +**Policy 2 - Data removal:** |
| 190 | +* **Time Column:** `ts_day` |
| 191 | +* **Condition:** `older than 60 days` |
| 192 | +* **Actions:** `Delete eligible partition(s)` |
0 commit comments