|
| 1 | +(airflow-data-retention-hot-cold)= |
| 2 | +# Building a hot and cold storage data retention policy in CrateDB with Apache Airflow |
| 3 | + |
| 4 | +In this fourth article on automating recurrent CrateDB queries with [Apache Airflow](https://airflow.apache.org/), we will present a second strategy for implementing a data retention policy. Previously, we shared the {ref}`Data Retention Delete DAG <airflow-data-retention-policy>`, which dropped old partitions after a certain period of time. In that article, we introduce the complementary strategy of a hot/cold storage approach. |
| 5 | + |
| 6 | +## What is a hot/cold storage strategy? |
| 7 | + |
| 8 | +A hot/cold storage strategy is often motivated by a tradeoff between performance and cost-effectiveness. In a database such as CrateDB, more recent data tends to have a higher significance for analytical queries. Well-performing disks (hot storage) play a key role on the infrastructure side to support performance requirements but can come at a high cost. As data ages and gets less business-critical for near-real-time analysis, transitioning it to slower/cheaper disks (cold storage) helps to improve the cost-performance ratio. |
| 9 | + |
| 10 | +In a CrateDB cluster, nodes can have different hardware specifications. Hence, a cluster can consist of a combination of hot and cold storage nodes, each with respective disks. By assigning corresponding attributes to nodes, CrateDB can be made aware of such node types and consider if when allocating partitions. |
| 11 | + |
| 12 | +## CrateDB setup |
| 13 | + |
| 14 | +To create a multi-node setup, we make use of [Docker Compose](https://crate.io/docs/crate/howtos/en/latest/deployment/containers/docker.html#docker-compose) to spin up three nodes – two hot nodes, and one cold node. For the scope of this article, we will not actually use different hardware specifications for disks, but each disk is represented as a separate Docker volume. |
| 15 | + |
| 16 | +The designation of a node type is done by passing the `-Cnode.attr.storage` parameter to each node with the value hot or cold. The resulting `docker-compose.yml` file with two hot nodes and one cold node is as follows: |
| 17 | + |
| 18 | +```yaml |
| 19 | +services: |
| 20 | + cratedb01: |
| 21 | + image: crate:latest |
| 22 | + ports: |
| 23 | + - "4201:4200" |
| 24 | + - "5532:5432" |
| 25 | + volumes: |
| 26 | + - /tmp/crate/hot-01:/data |
| 27 | + command: ["crate", |
| 28 | + "-Ccluster.name=crate-docker-cluster", |
| 29 | + "-Cnode.name=cratedb01", |
| 30 | + "-Cnode.attr.storage=hot", |
| 31 | + "-Cnetwork.host=_site_", |
| 32 | + "-Cdiscovery.seed_hosts=cratedb02,cratedb03", |
| 33 | + "-Ccluster.initial_master_nodes=cratedb01,cratedb02,cratedb03", |
| 34 | + "-Cgateway.expected_data_nodes=3", |
| 35 | + "-Cgateway.recover_after_data_nodes=2"] |
| 36 | + environment: |
| 37 | + - CRATE_HEAP_SIZE=2g |
| 38 | + |
| 39 | + cratedb02: |
| 40 | + image: crate:latest |
| 41 | + ports: |
| 42 | + - "4202:4200" |
| 43 | + - "5632:5432" |
| 44 | + volumes: |
| 45 | + - /tmp/crate/hot-02:/data |
| 46 | + command: ["crate", |
| 47 | + "-Ccluster.name=crate-docker-cluster", |
| 48 | + "-Cnode.name=cratedb02", |
| 49 | + "-Cnode.attr.storage=hot", |
| 50 | + "-Cnetwork.host=_site_", |
| 51 | + "-Cdiscovery.seed_hosts=cratedb01,cratedb03", |
| 52 | + "-Ccluster.initial_master_nodes=cratedb01,cratedb02,cratedb03", |
| 53 | + "-Cgateway.expected_data_nodes=3", |
| 54 | + "-Cgateway.recover_after_data_nodes=2"] |
| 55 | + environment: |
| 56 | + - CRATE_HEAP_SIZE=2g |
| 57 | + |
| 58 | + cratedb03: |
| 59 | + image: crate:latest |
| 60 | + ports: |
| 61 | + - "4203:4200" |
| 62 | + - "5732:5432" |
| 63 | + volumes: |
| 64 | + - /tmp/crate/cold-03:/data |
| 65 | + command: ["crate", |
| 66 | + "-Ccluster.name=crate-docker-cluster", |
| 67 | + "-Cnode.name=cratedb03", |
| 68 | + "-Cnode.attr.storage=cold", |
| 69 | + "-Cnetwork.host=_site_", |
| 70 | + "-Cdiscovery.seed_hosts=cratedb01,cratedb02", |
| 71 | + "-Ccluster.initial_master_nodes=cratedb01,cratedb02,cratedb03", |
| 72 | + "-Cgateway.expected_data_nodes=3", |
| 73 | + "-Cgateway.recover_after_data_nodes=2"] |
| 74 | + environment: |
| 75 | + - CRATE_HEAP_SIZE=2g |
| 76 | +``` |
| 77 | +
|
| 78 | +The cluster is started via `docker-compose up`. For more details, please see the above-linked documentation. |
| 79 | + |
| 80 | +Once the cluster is up and running, we create our partitioned time-series table. By specifying `"routing.allocation.require.storage" = 'hot'` in the `WITH` clause, we configure new partitions to be placed on a hot node. |
| 81 | + |
| 82 | +```sql |
| 83 | +CREATE TABLE IF NOT EXISTS doc.raw_metrics ( |
| 84 | + "variable" TEXT, |
| 85 | + "timestamp" TIMESTAMP WITH TIME ZONE, |
| 86 | + "ts_day" TIMESTAMP GENERATED ALWAYS AS DATE_TRUNC('day', "timestamp"), |
| 87 | + "value" REAL, |
| 88 | + "quality" INTEGER, |
| 89 | + PRIMARY KEY ("variable", "timestamp", "ts_day") |
| 90 | +) |
| 91 | +PARTITIONED BY (ts_day) |
| 92 | +WITH ("routing.allocation.require.storage" = 'hot'); |
| 93 | +``` |
| 94 | + |
| 95 | +To validate the allocation of shards we insert a sample row: |
| 96 | + |
| 97 | +```sql |
| 98 | +INSERT INTO doc.raw_metrics (variable, timestamp, value, quality) VALUES ('water-flow', NOW() - '5 months'::INTERVAL, 12, 1); |
| 99 | +``` |
| 100 | + |
| 101 | +The `INSERT` statement will implicitly trigger the creation of a new partition consisting of six shards. Since we configured `cratedb01` and `cratedb02` as hot nodes, we expect shards to be allocated only on those two nodes, and not on `cratedb03` (which is a cold node). The allocation can be validated by navigating to the “Shards” section in the Admin UI: |
| 102 | + |
| 103 | +{height=320px} |
| 104 | + |
| 105 | +As expected, primary shards, as well as replicas, are evenly distributed between the first two nodes, while no shards are stored on the third node. |
| 106 | + |
| 107 | +Next, we will create a table storing the retention policy used to transition partitions from hot to cold nodes: |
| 108 | + |
| 109 | +```sql |
| 110 | +CREATE TABLE IF NOT EXISTS doc.retention_policies ( |
| 111 | + "table_schema" TEXT, |
| 112 | + "table_name" TEXT, |
| 113 | + "partition_column" TEXT NOT NULL, |
| 114 | + "retention_period" INTEGER NOT NULL, |
| 115 | + "reallocation_attribute_name" TEXT, |
| 116 | + "reallocation_attribute_value" TEXT, |
| 117 | + "target_repository_name" TEXT, |
| 118 | + "strategy" TEXT NOT NULL, |
| 119 | + PRIMARY KEY ("table_schema", "table_name", "strategy") |
| 120 | +) |
| 121 | +CLUSTERED INTO 1 SHARDS; |
| 122 | +``` |
| 123 | + |
| 124 | +The schema is an extension of what was introduced in the first article on the [Data Retention Delete DAG](https://community.cratedb.com/t/cratedb-and-apache-airflow-implementation-of-data-retention-policy/913). The `strategy` column allows switching between the previously introduced dropping of partitions (`delete`) and the now added reallocation (`reallocate`). For our `raw_metrics` table, we add a policy of transitioning from hot to cold nodes after 60 days: |
| 125 | + |
| 126 | +```sql |
| 127 | +INSERT INTO doc.retention_policies VALUES ('doc', 'raw_metrics', 'ts_day', 60, 'storage', 'cold', NULL, 'reallocate'); |
| 128 | +``` |
| 129 | + |
| 130 | +To remember which partitions have already been reallocated, we can make use of the `attributes` column in `sys.nodes` which reflects the hot/cold storage attribute we configured in the Docker Compose setup. |
| 131 | + |
| 132 | +## Airflow setup |
| 133 | + |
| 134 | +We assume that a basic Astronomer/Airflow setup is already in place, as described in our [first post](https://community.cratedb.com/t/cratedb-and-apache-airflow-part-one/901) of this series. The general idea behind the hot/cold DAG implementation is similar to the one introduced in the [initial data retention post](https://community.cratedb.com/t/cratedb-and-apache-airflow-implementation-of-data-retention-policy/913). Let’s quickly go through the three steps of the algorithm: |
| 135 | + |
| 136 | +1. `get_policies`: A query on `doc.retention_policies` and `information_schema.table_partitions` identifies partitions affected by a retention policy. |
| 137 | +2. `map_policy`: A helper method transforming the output of `get_policies` into a Python `dict` data structure for easier handling. |
| 138 | +4. `reallocate_partitions`: Executes an SQL statement for each mapped policy: `ALTER TABLE <table> PARTITION (<partition key> = <partition value>) SET ("routing.allocation.require.storage" = 'cold');` |
| 139 | +The CrateDB cluster will then automatically initiate the relocation of the affected partition to a node that fulfills the requirement (`cratedb03` in our case). |
| 140 | + |
| 141 | +The full implementation is available as [data_retention_reallocate_dag.py](https://github.com/crate/crate-airflow-tutorial/blob/main/dags/data_retention_reallocate_dag.py) on GitHub. |
| 142 | + |
| 143 | +To validate our implementation, we trigger the DAG once manually via the Airflow UI at [http://localhost:8081](http://localhost:8081/). Once executed, log messages of the `reallocate_partitions` task confirm the reallocation was triggered for the partition with the sample data set up earlier: |
| 144 | + |
| 145 | +```text |
| 146 | +[2021-12-08, 12:39:44 UTC] {data_cleanup_dag.py:47} INFO - Reallocating partition ts_day = 1625702400000 for table doc.raw_metrics to storage = cold |
| 147 | +[2021-12-08, 12:39:44 UTC] {dbapi.py:225} INFO - Running statement: ALTER TABLE doc.raw_metrics PARTITION (ts_day = 1625702400000) SET ("routing.allocation.require.storage" = 'cold'); |
| 148 | +, parameters: None |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +Revisiting the “Shards” section in the CrateDB Admin UI confirms that all shards have been moved to `cratedb03`. Since the default replication setting is `0-1` and there is only one cold node in this setup, replicas have been discarded. |
| 153 | + |
| 154 | +{height=320px} |
| 155 | + |
| 156 | +## Combined hot/cold and deletion strategy |
| 157 | + |
| 158 | +The presented hot/cold storage strategy also integrates seamlessly with the previously introduced [Data Retention Delete DAG](https://community.cratedb.com/t/cratedb-and-apache-airflow-implementation-of-data-retention-policy/913). Both strategies can be combined: |
| 159 | + |
| 160 | +1. Transition to cold nodes: Reallocates partitions from (expensive) hot nodes to (cheaper) cold nodes |
| 161 | +2. Deletion from cold nodes: After another retention period on cold nodes, permanently delete partitions |
| 162 | + |
| 163 | +Both DAGs use the same control table for retention policies. In our example, we already added an entry for the reallocate strategy after 60 days. If we want to keep partitions on cold nodes for another 60 days and then discard them, we add an additional `delete` policy. Note that the retention periods are not additive, i.e. we need to specify the `delete` retention period as 120 days: |
| 164 | + |
| 165 | +```sql |
| 166 | +INSERT INTO doc.retention_policies (table_schema, table_name, partition_column, retention_period, strategy) VALUES ('doc', 'raw_metrics', 'ts_day', 120, 'delete'); |
| 167 | +``` |
| 168 | + |
| 169 | +## Summary |
| 170 | + |
| 171 | +Building upon the previously discussed data retention policy implementation, we showed that reallocating partitions integrates seemingly and consists only of a single SQL statement. |
| 172 | +CrateDB’s self-organization capabilities take care of all low-level operations and the actual moving of partitions. Furthermore, we showed that a multi-staged approach to data retention policies can be achieved by first reallocating and eventually deleting partitions permanently. |
0 commit comments