github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/operations/storage/table-manager.md (about)

     1  ---
     2  title: Table manager
     3  ---
     4  # Table Manager
     5  
     6  Grafana Loki supports storing indexes and chunks in table-based data storages. When
     7  such a storage type is used, multiple tables are created over the time: each
     8  table - also called periodic table - contains the data for a specific time
     9  range.
    10  
    11  This design brings two main benefits:
    12  1. **Schema config changes**: each table is bounded to a schema config and
    13     version, so that changes can be introduced over the time and multiple schema
    14     configs can coexist
    15  1. **Retention**: the retention is implemented deleting an entire table, which
    16     allows to have fast delete operations
    17  
    18  The **Table Manager** is a Loki component which takes care of creating a
    19  periodic table before its time period begins, and deleting it once its data
    20  time range exceeds the retention period.
    21  
    22  The Table Manager supports the following backends:
    23  
    24  - **Index store**
    25    - [Single Store (boltdb-shipper)](../boltdb-shipper/)
    26    - [Amazon DynamoDB](https://aws.amazon.com/dynamodb)
    27    - [Google Bigtable](https://cloud.google.com/bigtable)
    28    - [Apache Cassandra](https://cassandra.apache.org)
    29    - [BoltDB](https://github.com/boltdb/bolt) (primarily used for local environments)
    30  - **Chunk store**
    31    - [Amazon DynamoDB](https://aws.amazon.com/dynamodb)
    32    - [Google Bigtable](https://cloud.google.com/bigtable)
    33    - [Apache Cassandra](https://cassandra.apache.org)
    34    - Filesystem (primarily used for local environments)
    35  
    36  The object storages - like Amazon S3 and Google Cloud Storage - supported by Loki
    37  to store chunks, are not managed by the Table Manager, and a custom bucket policy
    38  should be set to delete old data.
    39  
    40  For detailed information on configuring the Table Manager, refer to the
    41  [`table_manager`](../../../configuration#table_manager)
    42  section in the Loki configuration document.
    43  
    44  
    45  ## Tables and schema config
    46  
    47  A periodic table stores the index or chunk data relative to a specific period
    48  of time. The duration of the time range of the data stored in a single table and
    49  its storage type is configured in the
    50  [`schema_config`](../../../configuration#schema_config) configuration
    51  block.
    52  
    53  The [`schema_config`](../../../configuration#schema_config) can contain
    54  one or more `configs`. Each config, defines the storage used between the day
    55  set in `from` (in the format `yyyy-mm-dd`) and the next config, or "now"
    56  in the case of the last schema config entry.
    57  
    58  This allows to have multiple non-overlapping schema configs over the time, in
    59  order to perform schema version upgrades or change storage settings (including
    60  changing the storage type).
    61  
    62  ![periodic_tables](../table-manager-periodic-tables.png)
    63  
    64  The write path hits the table where the log entry timestamp falls into (usually
    65  the last table, except short periods close to the end of a table and the
    66  beginning of the next one), while the read path hits the tables containing data
    67  for the query time range.
    68  
    69  
    70  ### Schema config example
    71  
    72  For example, the following `schema_config` defines two configurations: the first
    73  one using the schema `v10` and the current one using the `v11`.
    74  
    75  The first config stores data between `2019-01-01` and `2019-04-14` (included),
    76  then a new config has been added - to upgrade the schema version to `v11` -
    77  storing data using the `v11` schema from `2019-04-15` on.
    78  
    79  For each config, multiple tables are created, each one storing data for
    80  `period` time (168 hours = 7 days).
    81  
    82  ```yaml
    83  schema_config:
    84    configs:
    85      - from:   2019-01-01
    86        store:  dynamo
    87        schema: v10
    88        index:
    89          prefix: loki_
    90          period: 168h
    91      - from:   2019-04-15
    92        store:  dynamo
    93        schema: v11
    94        index:
    95          prefix: loki_
    96          period: 168h
    97  ```
    98  
    99  
   100  ### Table creation
   101  
   102  The Table Manager creates new tables slightly ahead of their start period, in
   103  order to make sure that the new table is ready once the current table end
   104  period is reached.
   105  
   106  The `creation_grace_period` property - in the
   107  [`table_manager`](../../../configuration#table_manager)
   108  configuration block - defines how long before a table should be created.
   109  
   110  
   111  ## Retention
   112  
   113  The retention - managed by the Table Manager - is disabled by default, due to
   114  its destructive nature. You can enable the data retention explicitly enabling
   115  it in the configuration and setting a `retention_period` greater than zero:
   116  
   117  ```yaml
   118  table_manager:
   119    retention_deletes_enabled: true
   120    retention_period: 336h
   121  ```
   122  
   123  The Table Manager implements the retention deleting the entire tables whose
   124  data exceeded the `retention_period`. This design allows to have fast delete
   125  operations, at the cost of having a retention granularity controlled by the
   126  table's `period`.
   127  
   128  Given each table contains data for `period` of time and that the entire table
   129  is deleted, the Table Manager keeps the last tables alive using this formula:
   130  
   131  ```
   132  number_of_tables_to_keep = floor(retention_period / table_period) + 1
   133  ```
   134  
   135  ![retention](../table-manager-retention.png)
   136  
   137  It's important to note that - due to the internal implementation - the table
   138  `period` and `retention_period` **must** be multiples of `24h` in order to get
   139  the expected behavior.
   140  
   141  For detailed information on configuring the retention, refer to the
   142  [Loki Storage Retention](../retention/)
   143  documentation.
   144  
   145  
   146  ## Active / inactive tables
   147  
   148  A table can be active or inactive.
   149  
   150  A table is considered **active** if the current time is within the range:
   151  - Table start period - [`creation_grace_period`](../../../configuration#table_manager)
   152  - Table end period + max chunk age (hardcoded to `12h`)
   153  
   154  ![active_vs_inactive_tables](../table-manager-active-vs-inactive-tables.png)
   155  
   156  Currently, the difference between an active and inactive table **only applies
   157  to the DynamoDB storage** settings: capacity mode (on-demand or provisioned),
   158  read/write capacity units and autoscaling.
   159  
   160  | DynamoDB            | Active table                            | Inactive table                       |
   161  | ------------------- | --------------------------------------- | ------------------------------------ |
   162  | Capacity mode       | `enable_ondemand_throughput_mode` | `enable_inactive_throughput_on_demand_mode` |
   163  | Read capacity unit  | `provisioned_read_throughput`           | `inactive_read_throughput`           |
   164  | Write capacity unit | `provisioned_write_throughput`          | `inactive_write_throughput`          |
   165  | Autoscaling         | Enabled (if configured)                 | Always disabled                      |
   166  
   167  
   168  ## DynamoDB Provisioning
   169  
   170  When configuring DynamoDB with the Table Manager, the default [on-demand
   171  provisioning](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html)
   172  capacity units for reads are set to 300 and writes are set to 3000. The
   173  defaults can be overwritten:
   174  
   175  ```yaml
   176  table_manager:
   177    index_tables_provisioning:
   178      provisioned_write_throughput: 10
   179      provisioned_read_throughput: 10
   180    chunk_tables_provisioning:
   181      provisioned_write_throughput: 10
   182      provisioned_read_throughput: 10
   183  ```
   184  
   185  If Table Manager is not automatically managing DynamoDB, old data cannot easily
   186  be erased and the index will grow indefinitely. Manual configurations should
   187  ensure that the primary index key is set to `h` (string) and the sort key is set
   188  to `r` (binary). The "period" attribute in the configuration YAML should be set
   189  to `0`.
   190  
   191  
   192  ## Table Manager deployment mode
   193  
   194  The Table Manager can be executed in two ways:
   195  
   196  1. Implicitly executed when Loki runs in monolithic mode (single process)
   197  1. Explicitly executed when Loki runs in microservices mode
   198  
   199  
   200  ### Monolithic mode
   201  
   202  When Loki runs in [monolithic mode](../../../fundamentals/architecture#modes-of-operation),
   203  the Table Manager is also started as component of the entire stack.
   204  
   205  
   206  ### Microservices mode
   207  
   208  When Loki runs in [microservices mode](../../../fundamentals/architecture#modes-of-operation),
   209  the Table Manager should be started as separate service named `table-manager`.
   210  
   211  You can check out a production grade deployment example at
   212  [`table-manager.libsonnet`](https://github.com/grafana/loki/tree/master/production/ksonnet/loki/table-manager.libsonnet).