github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/docs/proposals/block-storage-time-series-deletion.md (about)

     1  ---
     2  title: "Time Series Deletion from Blocks Storage"
     3  linkTitle: "Time Series Deletion from Blocks Storage"
     4  weight: 1
     5  slug: block-storage-time-series-deletion
     6  ---
     7  
     8  - Author: [Ilan Gofman](https://github.com/ilangofman)
     9  - Date: June 2021
    10  - Status: Proposal
    11  
    12  ## Problem
    13  
    14  Currently, Cortex only implements a time series deletion API for chunk storage.  We present a design for implementing time series deletion with block storage. We would like to have the same API for deleting series as currently implemented in Prometheus and in Cortex with chunk storage.
    15  
    16  
    17  This can be very important for users to have as confidential or accidental data might have been incorrectly pushed and needs to be removed. As well as potentially removing high cardinality data that is causing inefficient queries.
    18  
    19  ## Related works
    20  
    21  As previously mentioned, the deletion feature is already implemented with chunk storage. The main functionality is implemented through the purger service. It accepts requests for deletion and processes them.  At first, when a deletion request is made, a tombstone is created. This is used to filter out the data for queries. After some time, a deletion plan is executed where the data is permanently removed from chunk storage.
    22  
    23  Can find more info here:
    24  
    25  - [Cortex documentation for chunk store deletion](https://cortexmetrics.io/docs/guides/deleting-series/)
    26  - [Chunk deletion proposal](https://docs.google.com/document/d/1PeKwP3aGo3xVrR-2qJdoFdzTJxT8FcAbLm2ew_6UQyQ/edit)
    27  
    28  
    29  
    30  ## Background on current storage
    31  
    32  With a block-storage configuration, Cortex stores data that could be potentially deleted by a user in:
    33  
    34  - Object store (GCS, S3, etc..) for long term storage of blocks
    35  - Ingesters for more recent data that should be eventually transferred to the object store
    36  - Cache
    37      - Index cache
    38      - Metadata cache
    39      - Chunks cache (stores the potentially to be deleted data)
    40      - Query results cache  (stores the potentially to be deleted data)
    41  - Compactor during the compaction process
    42  - Store-gateway
    43  
    44  
    45  ## Proposal
    46  
    47  The deletion will not happen right away. Initially, the data will be filtered out from queries using tombstones and will be deleted afterward. This will allow the user some time to cancel the delete request.
    48  
    49  ### API Endpoints
    50  
    51  The existing purger service will be used to process the incoming requests for deletion. The API will follow the same structure as the chunk storage endpoints for deletion, which is also based on the Prometheus deletion API.
    52  
    53  This will enable the following endpoints for Cortex when using block storage:
    54  
    55  `POST /api/v1/admin/tsdb/delete_series` - Accepts [Prometheus style delete request](https://prometheus.io/docs/prometheus/latest/querying/api/#delete-series)  for deleting series.
    56  
    57  Parameters:
    58  
    59  - `start=<rfc3339 | unix_timestamp>`
    60      - Optional. If not provided, will be set to minimum possible time.
    61  - `end=<rfc3339 | unix_timestamp> `
    62      - Optional. If not provided, will be set to maximum possible time (time when request was made). End time cannot be greater than the current UTC time.
    63  - `match[]=<series_selector>`
    64      - Cannot be empty, must contain at least one label matcher argument.
    65  
    66  
    67  `POST /api/v1/admin/tsdb/cancel_delete_request`  - To cancel a request if it has not been processed yet for permanent deletion. This can only be done before the `-purger.delete-request-cancel-period` has passed.
    68  Parameters:
    69  
    70  - `request_id`
    71  
    72  `GET /api/v1/admin/tsdb/delete_series` - Get all delete requests id’s and their current status.
    73  
    74  Prometheus also implements a [clean_tombstones](https://prometheus.io/docs/prometheus/latest/querying/api/#clean-tombstones) API which is not included in this proposal. The tombstones will be deleted automatically once the permanent deletion has taken place which is described in the section below. By default, this should take approximately 24 hours.
    75  
    76  ### Deletion Lifecycle
    77  
    78  The deletion request lifecycle can follow these 3 states:
    79  
    80  1. Pending - Tombstone file is created. During this state, the queriers will be performing query time filtering. The initial time period configured by `-purger.delete-request-cancel-period`, no data will be deleted. Once this period is over, permanent deletion processing will begin and the request is no longer cancellable.
    81  2. Processed - All requested data has been deleted. Initially, will still need to do query time filtering while waiting for the bucket index and store-gateway to pick up the new blocks. Once that period has passed, will no longer require any query time filtering.
    82  3. Deleted - The deletion request was cancelled. A grace period configured by `-purger.delete-request-cancel-period` will allow the user some time to cancel the deletion request if it was made by mistake. The request is no longer cancelable after this period has passed.
    83  
    84  
    85  
    86  ### Filtering data during queries while not yet deleted:
    87  
    88  Once a deletion request is received, a tombstone entry will be created. The object store such as S3, GCS, Azure storage, can be used to store all the deletion requests. See the section below for more detail on how the tombstones will be stored. Using the tombstones, the querier will be able to filter the to-be-deleted data initially. If a cancel delete request is made, then the tombstone file will be deleted. In addition, the existing cache will be invalidated using cache generation numbers, which are described in the later sections.
    89  
    90  The compactor's _BlocksCleaner_ service will scan for new tombstone files and will update the bucket-index with the tombstone information regarding the deletion requests. This will enable the querier to periodically check the bucket index if there are any new tombstone files that are required to be used for filtering. One drawback of this approach is the time it could take to start filtering the data. Since the compactor will update the bucket index with the new tombstones every `-compactor.cleanup-interval` (default 15 min). Then the cached bucket index is refreshed in the querier every `-blocks-storage.bucket-store.sync-interval` (default 15 min). Potentially could take almost 30 min for queriers to start filtering deleted data when using the default values. If the information requested for deletion is confidential/classified, the time delay is something that the user should be aware of, in addition to the time that the data has already been in Cortex.
    91  
    92  An additional thing to consider is that this would mean that the bucket-index would have to be enabled for this API to work. Since the plan is to make to the bucket-index mandatory in the future for block storage, this shouldn't be an issue.
    93  
    94  Similar to the chunk storage deletion implementation, the initial filtering of the deleted data will be done inside the Querier. This will allow filtering the data read from both the store gateway and the ingester. This functionality already exists for the chunk storage implementation. By implementing it in the querier, this would mean that the ruler will be supported too (ruler internally runs the querier).
    95  
    96  #### Storing tombstones in object store
    97  
    98  
    99  The Purger will write the new tombstone entries in a separate folder called `tombstones` in the object store (e.g. S3 bucket) in the respective tenant folder. Each tombstone can have a separate JSON file outlining all the necessary information about the deletion request such as the parameters passed in the request, as well as some meta-data such as the creation date of the file.  The name of the file can be a hash of the API parameters (start, end, markers). This way if a user calls the API twice by accident with the same parameters, it will only create one tombstone. To keep track of the request state, filename extensions can be used. This will allow the tombstone files to be immutable. The 3 different file extensions will be `pending, processed, deleted`. Each time the deletion request moves to a new state, a new file will be added with the same deletion information but a different extension to indicate the new state. The file containing the previous state will be deleted once the new one is created. If a deletion request is cancelled, then a tombstone file with the `.deleted` filename extension will be created.
   100  
   101  When it is determined that the request should move to the next state, then it will first write a new file containing the tombstone information to the object store. The information inside the file will be the same except the `stateCreationTime`, which is replaced with the current timestamp. The extension of the new file will be different to reflect the new state. If the new file is successfully written, the file with the previous state is deleted. If the write of the new file fails, then the previous file is not going to be deleted. Next time the service runs to check the state of each tombstone, it will retry creating the new file with the updated state. If the write is successful but the deletion of the old file is unsuccessful then there will be 2 tombstone files with the same filename but different extension. When `BlocksCleaner` writes the tombstones to the bucket index, the compactor will check for duplicate tombstone files but with different extensions. It will use the tombstone with the most recently updated state and try to delete the file with the older state. There could be a scenario where there are two files with the same request ID but different extensions: {`.pending`, `.processed`} or {`.pending`, `.deleted`}. In this case, the `.processed` or `.deleted ` file will be selected as it is always the later state compared to the `pending` state.
   102  
   103  The tombstone will be stored in a single JSON file per request and state:
   104  
   105  - `/<tenantId>/tombstones/<request_id>.json.<state>`
   106  
   107  
   108  The schema of the JSON file is:
   109  
   110  
   111  ```
   112  {
   113    "requestId": <string>,
   114    "startTime": <int>,
   115    "endTime": <int>,
   116    "requestCreationTime": <int>,
   117    "stateCreationTime": <int>,
   118    "matchers": [
   119      "<string matcher 1>",
   120      ..,
   121      "<string matcher n>"
   122      ]
   123    },
   124    "userID": <string>,
   125  }
   126  ```
   127  
   128  
   129  Pros:
   130  - Allows deletion and un-delete to be done in a single operation.
   131  
   132  Cons:
   133  
   134  - Negative impact on query performance when there are active tombstones. As in the chunk storage implementation, all the series will have to be compared to the matchers contained in the active tombstone files. The impact on performance should be the same as the deletion would have with chunk storage.
   135  
   136  - With the default config, potential 30 minute wait for the data to begin filtering if using the default configuration.
   137  
   138  #### Invalidating cache
   139  
   140  Using block store, the different caches available are:
   141  - Index cache
   142  - Metadata cache
   143  - Chunks cache (stores the potentially to be deleted chunks of data)
   144  - Query results cache (stores the potentially to be deleted data)
   145  
   146  There are two potential caches that could contain deleted data, the chunks cache, and the query results cache. Using the tombstones, the queriers filter out the data received from the ingesters and store-gateway. The cache not being processed through the querier needs to be invalidated to prevent deleted data from coming up in queries.
   147  
   148  Firstly, the query results cache needs to be invalidated for each new delete request or a cancellation of one. This can be accomplished by utilizing cache generation numbers. For each tenant, their cache is prefixed with a cache generation number. When the query front-end discovers a cache generation number that is greater than the previous generation number, then it knows to invalidate the query results cache. However, the cache can only be invalidated once the queriers have loaded the tombstones from the bucket index and have begun filtering the data. Otherwise, to-be deleted data might show up in queries and be cached again. One of the way to guarantee that all the queriers are using the new tombstones is to wait until the bucket index staleness period has passed from the time the tombstones have been written to the bucket index. The staleness period can be configured using the following flag: `-blocks-storage.bucket-store.bucket-index.max-stale-period`. We can use the bucket index staleness period as the delay to wait before the cache generation number is increased. A query will fail inside the querier, if the bucket index last update is older the staleness period. Once this period is over, all the queriers should have the updated tombstones and the query results cache can be invalidated.  Here is the proposed method for accomplishing this:
   149  
   150  
   151  - The cache generation number will be a timestamp. The default value will be 0.
   152  - The bucket index will store the cache generation number.  The query front-end will periodically fetch the bucket index.
   153  - Inside the compactor, the _BlocksCleaner_ will load the tombstones from object store and update the bucket index accordingly. It will calculate the cache generation number by iterating through all the tombstones and their respective times (next bullet point) and selecting the maximum timestamp that is less than (current time minus  `-blocks-storage.bucket-store.bucket-index.max-stale-period`). This would mean that if a deletion request is made or cancelled, the compactor will only update the cache generation number once the staleness period is over, ensuring that all queriers have the updated tombstones.
   154  - For requests in a pending or processed state, the `requestCreationTime` will be used when comparing the maximum timestamps. If a request is in a deleted state, it will use the `stateCreationTime` for comparing the timestamps. This means that the cache gets invalidated only once it has been created or deleted, and the bucket index staleness period has passed. The cache will not be invalidated again when a request advances from pending to processed state.
   155  - The query front-end will fetch the cache generation number from the bucket index. The query front end will compare it to the current cache generation number stored in the front-end. If the cache generation number from the front-end is less than the one from bucket index, then the cache is invalidated.
   156  
   157  In regards to the chunks cache, since it is retrieved from the store gateway and passed to the querier, it will be filtered out like the rest of the time series data in the querier using the tombstones, with the mechanism described in the previous section.
   158  
   159  ### Permanently deleting the data
   160  
   161  The proposed approach is to perform the deletions from the compactor. A new background service inside the compactor called _DeletedSeriesCleaner_ can be created and is responsible for executing the deletion.
   162  
   163  #### Processing
   164  
   165  
   166  This will happen after a grace period has passed once the API request has been made. By default this should be 24 hours. A background task can be created to process the permanent deletion of time series. This background task can be executed each hour.
   167  
   168  To delete the data from the blocks, the same logic as the [Bucket Rewrite Tool](https://thanos.io/tip/components/tools.md/#bucket-rewrite
   169  ) from Thanos can be leveraged. This tool does the following: `tools bucket rewrite rewrites chosen blocks in the bucket, while deleting or modifying series`. The tool itself is a CLI tool that we won’t be using, but instead we can utilize the logic inside it. For more information about the way this tool runs, please see the code [here](https://github.com/thanos-io/thanos/blob/d8b21e708bee6d19f46ca32b158b0509ca9b7fed/cmd/thanos/tools_bucket.go#L809).
   170  
   171  The compactor’s _DeletedSeriesCleaner_ will apply this logic on individual blocks and each time it is run, it creates a new block without the data that matched the deletion request. The original individual blocks containing the data that was requested to be deleted, need to be marked for deletion by the compactor.
   172  
   173  While deleting the data permanently from the block storage, the `meta.json` files will be used to keep track of the deletion progress. Inside each `meta.json` file, we will add a new field called `tombstonesFiltered`. This will store an array of deletion request id's that were used to create this block. Once the rewrite logic is applied to a block, the new block's `meta.json` file will append the deletion request id(s) used for the rewrite operation inside this field. This will let the _DeletedSeriesCleaner_ know that this block has already processed the particular deletions requests listed in this field. Assuming that the deletion requests are quite rare, the size of the meta.json files should remain small.
   174  
   175  The _DeletedSeriesCleaner_ can iterate through all the blocks that the deletion request could apply to. For each of these blocks, if the deletion request ID isn't inside the meta.json `tombstonesFiltered` field, then the compactor can apply the rewrite logic to this block. If there are multiple tombstones that are currently being processing for deletions and apply to a particular block, then the _DeletedSeriesCleaner_ will process both at the same time to prevent additional blocks from being created. If after iterating through all the blocks, it doesn’t find any such blocks requiring deletion, then the `Pending` state is complete and the request progresses to the `Processed` state.
   176  
   177  One important thing to note regarding this rewrite tool is that it should not be used at the same time as when another compactor is touching a block. If the tool is run at the same time as compaction on a particular block, it can cause overlap and the data marked for deletion can already be part of the compacted block. To mitigate such issues, these are some of the proposed solutions:
   178  
   179  Option 1: Only apply the deletion once the blocks are in the final state of compaction.
   180  
   181  Pros:
   182  - Simpler implementation as everything is contained within the DeletedSeriesCleaner.
   183  
   184  Cons:
   185  - Might have to wait for a longer period of time for the compaction to be finished.
   186      - This would mean the earliest time to be able to run the deletion would be once the last time from the block_ranges in the [compactor_config](https://cortexmetrics.io/docs/blocks-storage/compactor/#compactor-configuration) has passed. By default this value is 24 hours, so only once 24 hours have passed and the new compacted blocks have been created, then the rewrite can be safely run.
   187  
   188  
   189  
   190  
   191  Option 2: For blocks that still need to be compacted further after the deletion request cancel period is over, the deletion logic can be applied before the blocks are compacted. This will generate a new block which can then be used instead for compaction with other blocks.
   192  
   193  Pros:
   194  - The deletion can be applied earlier than the previous options.
   195      - Only applies if the deletion request cancel period is less than the last time interval for compaction is.
   196  Cons:
   197  - Added coupling between the compaction and the DeletedSeriesCleaner.
   198  - Might block compaction for a short time while doing the deletion.
   199  
   200  
   201  
   202  Once all the applicable blocks have been rewritten without the deleted data, the deletion request state moves to the `Processed` state. Once in this state, the queriers will still have to perform query time filtering using the tombstones until the old blocks that were marked for deletion are no longer queried by the queriers. This will mean that the query time filtering will last for an additional length of `-compactor.deletion-delay + -compactor.cleanup-interval +  -blocks-storage.bucket-store.sync-interval` in the `Processed` state. Once that time period has passed, the queriers should no longer be querying any of the old blocks that were marked for deletion. The tombstone will no longer be used after this.
   203  
   204  
   205  #### Cancelled Delete Requests
   206  
   207  If a request was successfully cancelled, then a tombstone file a `.deleted` extension is created. This is done to help ensure that the cache generation number is updated and the query results cache is invalidated. The compactor's blocks cleaner can take care of cleaning up `.deleted` tombstones after a period of time of when they are no longer required for cache invalidation. This can be done after 10 times the bucket index max staleness time period has passed. Before removing the file from the object store, the current cache generation number must greater than or equal to when the tombstone was cancelled.
   208  
   209  #### Handling failed/unfinished delete jobs:
   210  
   211  Deletions will be completed and the tombstones will be deleted only when the DeletedSeriesCleaner iterates over all blocks that match the time interval and confirms that they have been re-written without the deleted data.  Otherwise, it will keep iterating over the blocks and process the blocks that haven't been rewritten according to the information in the `meta.json` file. In case of any failure that causes the deletion to stop, any unfinished deletions will be resumed once the service is restarted. If the block rewrite was not completed on a particular block, then the original block will not be marked for deletion. The compactor will continue to iterate over the blocks and process the block again.
   212  
   213  
   214  #### Tenant Deletion API
   215  
   216  If a request is made to delete a tenant, then all the tombstones will be deleted for that user.
   217  
   218  
   219  ## Current Open Questions:
   220  
   221  - If the start and end time is very far apart, it might result in a lot of the data being re-written. Since we create a new block without the deleted data and mark the old one for deletion, there may be a period of time with lots of extra blocks and space used for large deletion queries.
   222  - There will be a delay between the deletion request and the deleted data being filtered during queries.
   223      - In Prometheus, there is no delay.
   224      - One way to filter out immediately is to load the tombstones during query time but this will cause a negative performance impact.
   225  - Adding limits to the API such as:
   226      - Max number of deletion requests allowed in the last 24 hours for a given tenent.
   227      - Max number of pending tombstones for a given tenant.
   228  
   229  
   230  ## Alternatives Considered
   231  
   232  
   233  #### Adding a Pre-processing State
   234  
   235  The process of permanently deleting the data can be separated into 2 stages, preprocessing and processing.
   236  
   237  Pre-processing will begin after the `-purger.delete-request-cancel-period` has passed since the API request has been made. The deletion request will move to a new state called `BuildingPlan`. The compactor will outline all the blocks that may contain data to be deleted. For each separate block that the deletion may be applicable to, the compactor will begin the process by adding a series deletion marker inside the series-deletion-marker.json file. The JSON file will contain an array of deletion request id's that need to be applied to the block, which allows the ability to handle the situation when there are multiple tombstones that could be applicable to a particular block. Then during the processing step, instead of checking the meta.json file, we only need to check if a marker file exists with a specific deletion request id. If the marker file exists, then we apply the rewrite logic.
   238  
   239  #### Alternative Permanent Deletion Processing
   240  
   241  For processing the actual deletions, an alternative approach is not to wait until the final compaction has been completed and filter out the data during compaction. If the data is marked to be deleted, then don’t include it the new bigger block during compaction. For the remaining blocks where the data wasn’t filtered during compaction, the deletion can be done the same as in the previous section.
   242  
   243  Pros:
   244  
   245  - The deletion can happen sooner.
   246  - The rewrite tools creates additional blocks. By filtering the metrics during compaction, the intermediary re-written block will be avoided.
   247  
   248  Cons:
   249  
   250  - A more complicated implementation requiring add more logic to the compactor
   251  - Slower compaction if it needs to filter all the data
   252  - Need to manage which blocks should be deleted with the rewrite vs which blocks already had data filtered during compaction.
   253  - Would need to run the rewrite logic during and outside of compaction because some blocks that might need to be deleted are already in the final compaction state. So that would mean the deletion functionality has to be implemented in multiple places.
   254  - Won’t be leveraging the rewrites tools from Thanos for all the deletion, so potentially more work is duplicated
   255