Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default retention policy and duration for InfluxDB via configuration

I am using the official InfluxDB docker image. I want to set the retention policy to be 14 days by default.

There are various ENV variables that I can set to change the config for InfluxDB, such as INFLUXDB_RETENTION_POLICY. This expects the name of a retention policy such as "default" to be used as the default retention policy.

The problem is that this default policy has a duration of 7 days. I need to set it to 14 days.

The documentation is rather poor. I cannot find any ENV variable to adjust the default duration. I could also set the INFLUXDB_RETENTION_POLICY variable to a different name of a different retention policy, but I don't see how I can create that retention policy through configuration.

Is anyone aware of: 1) a way to change the default duration for retention via configuration or 2) a way to create a retention policy through configuration

like image 955
Tutan Ramen Avatar asked Jan 12 '17 18:01

Tutan Ramen


People also ask

How do I check my retention policy in InfluxDB?

Retention policies can be managed using the InfluxDB query language commands SHOW RETENTION POLICIES , CREATE RETENTION POLICY , ALTER RETENTION POLICY and DROP RETENTION POLICY . You can find more about this commands in the InfluxDB documentation.

How do I check my InfluxDB configuration?

The InfluxDB system has internal defaults for all of the settings in the configuration file. To view the default configuration settings, use the influxd config command. The local InfluxDB configuration file is located here: Linux: /etc/influxdb/influxdb.

How does InfluxDB store time?

InfluxDB stores all timestamps as nanosecond values, regardless of the write precision supplied. It is important to note that when returning query results, the database silently drops trailing zeros from timestamps which obscures the initial write precision.

What is shard duration in InfluxDB?

SHARD DURATIONThe minimum allowable SHARD GROUP DURATION is 1h . If the CREATE RETENTION POLICY query attempts to set the SHARD GROUP DURATION to less than 1h and greater than 0s , InfluxDB automatically sets the SHARD GROUP DURATION to 1h .


2 Answers

Unfortunately there is no way to set the default retention policy via the configuration. The reason for this is that typically retention policy duration is defined during database creation.

CREATE DATABASE <database_name>
[WITH [DURATION <duration>] [REPLICATION <n>]
      [SHARD DURATION <duration>] [NAME <retention-policy-name>]]

If users were allowed to set a default retention duration via the configuration, the results of the command

CREATE DATABASE mydb 

would vary from instance to instance. While this isn't necessarily problematic, it isn't ideal either.

The problem is that this default policy has a duration of 7 days. I need to set it to 14 days.

The default retention policy in InfluxDB should be infinite.

> CREATE DATABASE mydb
> SHOW RETENTION POLICIES ON mydb
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        true

Here we see that the duration of the retention policy is 0s which is an alias for infinite and the shard group duration is 168h0m0s which is 7 days.

I think the main point of confusion here is relatively common--and mostly due to retention policies being poorly named. In InfluxDB an Database is a container for Retention Policies and a Retention Policy is a container for the actual time series data. That is to say a Retention Policy isn't so much of a policy as it as a container that has a policy for all data it contains.

My recommendation would be to always be fully explicit when creating a database in InfluxDB. Doing so will always guarantee that your database will have the correct retention policy duration. So for creating a database with a 14 day retention policy you'd issue the command

CREATE DATABASE mydb WITH DURATION 14d
like image 168
Michael Desa Avatar answered Sep 29 '22 20:09

Michael Desa


To answer the question for those coming from google with an existing database (this was my situation), there are three ways to set the retention policy:

  • On a new database, with database create (as above)
  • On create of retention policy, by adding DEFAULT at the end of the create statement
  • By altering an existing retention policy to be the default for the db (see below)

Creating a database

CREATE DATABASE "NOAA_water_database" WITH DURATION 3d REPLICATION 1 SHARD DURATION 1h NAME "liquid"

Creating a policy

CREATE RETENTION POLICY "one_day_only" ON "NOAA_water_database" DURATION 1d REPLICATION 1

Updating an existing policy

ALTER RETENTION POLICY "what_is_time" ON "NOAA_water_database" DURATION 3w SHARD DURATION 2h DEFAULT

So for an existing database and retention policy which you wish to make the default, the simplest solution is to use alter retention policy.

One point to note on adding/updating a policy on an existing db - data prior to the policy expiration will be immediately dropped, so you will lose all older data.

like image 33
Kenny Grant Avatar answered Sep 29 '22 21:09

Kenny Grant