Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Add shard_duration parameter when creating or altering retention poli… #606

Merged
merged 3 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Finally add a CHANGELOG.md to communicate breaking changes (#598)
- Test multiple versions of InfluxDB in travis
- Add SHARD DURATION parameter to retention policy create/alter
### Changed
- Update POST/GET requests to follow verb guidelines from InfluxDB documentation
- Update test suite to support InfluxDB v1.3.9, v1.4.2, and v1.5.4
Expand Down
32 changes: 27 additions & 5 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ def drop_measurement(self, measurement):
method="POST")

def create_retention_policy(self, name, duration, replication,
database=None, default=False):
database=None,
default=False, shard_duration="0s"):
"""Create a retention policy for a database.

:param name: the name of the new retention policy
Expand All @@ -634,20 +635,30 @@ def create_retention_policy(self, name, duration, replication,
:type database: str
:param default: whether or not to set the policy as default
:type default: bool
:param shard_duration: the shard duration of the retention policy.
Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and
mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks,
respectively. Infinite retention is not supported. As a workaround,
specify a "1000w" duration to achieve an extremely long shard group
duration. Defaults to "0s", which is interpreted by the database
to mean the default value given the duration.
The minimum shard group duration is 1 hour.
:type shard_duration: str
"""
query_string = \
"CREATE RETENTION POLICY {0} ON {1} " \
"DURATION {2} REPLICATION {3}".format(
"DURATION {2} REPLICATION {3} SHARD DURATION {4}".format(
quote_ident(name), quote_ident(database or self._database),
duration, replication)
duration, replication, shard_duration)

if default is True:
query_string += " DEFAULT"

self.query(query_string, method="POST")

def alter_retention_policy(self, name, database=None,
duration=None, replication=None, default=None):
duration=None, replication=None,
default=None, shard_duration=None):
"""Modify an existing retention policy for a database.

:param name: the name of the retention policy to modify
Expand All @@ -667,15 +678,26 @@ def alter_retention_policy(self, name, database=None,
:type replication: int
:param default: whether or not to set the modified policy as default
:type default: bool
:param shard_duration: the shard duration of the retention policy.
Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and
mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks,
respectively. Infinite retention is not supported. As a workaround,
specify a "1000w" duration to achieve an extremely long shard group
duration.
The minimum shard group duration is 1 hour.
:type shard_duration: str

.. note:: at least one of duration, replication, or default flag
should be set. Otherwise the operation will fail.
"""
query_string = (
"ALTER RETENTION POLICY {0} ON {1}"
).format(quote_ident(name), quote_ident(database or self._database))
).format(quote_ident(name),
quote_ident(database or self._database), shard_duration)
if duration:
query_string += " DURATION {0}".format(duration)
if shard_duration:
query_string += " SHARD DURATION {0}".format(shard_duration)
if replication:
query_string += " REPLICATION {0}".format(replication)
if default is True:
Expand Down
12 changes: 10 additions & 2 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def test_create_retention_policy_default(self):
self.assertEqual(
m.last_request.qs['q'][0],
'create retention policy "somename" on '
'"db" duration 1d replication 4 default'
'"db" duration 1d replication 4 shard duration 0s default'
)

def test_create_retention_policy(self):
Expand All @@ -669,7 +669,7 @@ def test_create_retention_policy(self):
self.assertEqual(
m.last_request.qs['q'][0],
'create retention policy "somename" on '
'"db" duration 1d replication 4'
'"db" duration 1d replication 4 shard duration 0s'
)

def test_alter_retention_policy(self):
Expand Down Expand Up @@ -697,6 +697,14 @@ def test_alter_retention_policy(self):
'alter retention policy "somename" on "db" replication 4'
)

# Test alter shard duration
self.cli.alter_retention_policy('somename', 'db',
shard_duration='1h')
self.assertEqual(
m.last_request.qs['q'][0],
'alter retention policy "somename" on "db" shard duration 1h'
)

# Test alter default
self.cli.alter_retention_policy('somename', 'db',
default=True)
Expand Down
74 changes: 70 additions & 4 deletions influxdb/tests/server_tests/client_test_with_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,57 @@ def test_create_retention_policy(self):
rsp
)

self.cli.drop_retention_policy('somename', 'db')
# recreate the RP
self.cli.create_retention_policy('somename', '1w', 1,
shard_duration='1h')

rsp = self.cli.get_list_retention_policies()
self.assertEqual(
[
{'duration': '0s',
'default': True,
'replicaN': 1,
'shardGroupDuration': u'168h0m0s',
'name': 'autogen'},
{'duration': '168h0m0s',
'default': False,
'replicaN': 1,
'shardGroupDuration': u'1h0m0s',
'name': 'somename'}
],
rsp
)

self.cli.drop_retention_policy('somename', 'db')
# recreate the RP
self.cli.create_retention_policy('somename', '1w', 1)

rsp = self.cli.get_list_retention_policies()
self.assertEqual(
[
{'duration': '0s',
'default': True,
'replicaN': 1,
'shardGroupDuration': u'168h0m0s',
'name': 'autogen'},
{'duration': '168h0m0s',
'default': False,
'replicaN': 1,
'shardGroupDuration': u'24h0m0s',
'name': 'somename'}
],
rsp
)

def test_alter_retention_policy(self):
"""Test alter a retention policy, not default."""
self.cli.create_retention_policy('somename', '1d', 1)

# Test alter duration
self.cli.alter_retention_policy('somename', 'db',
duration='4d')
duration='4d',
shard_duration='2h')
# NB: altering retention policy doesn't change shard group duration
rsp = self.cli.get_list_retention_policies()
self.assertEqual(
Expand All @@ -563,7 +607,7 @@ def test_alter_retention_policy(self):
{'duration': '96h0m0s',
'default': False,
'replicaN': 1,
'shardGroupDuration': u'1h0m0s',
'shardGroupDuration': u'2h0m0s',
'name': 'somename'}
],
rsp
Expand All @@ -572,6 +616,7 @@ def test_alter_retention_policy(self):
# Test alter replication
self.cli.alter_retention_policy('somename', 'db',
replication=4)

# NB: altering retention policy doesn't change shard group duration
rsp = self.cli.get_list_retention_policies()
self.assertEqual(
Expand All @@ -584,7 +629,7 @@ def test_alter_retention_policy(self):
{'duration': '96h0m0s',
'default': False,
'replicaN': 4,
'shardGroupDuration': u'1h0m0s',
'shardGroupDuration': u'2h0m0s',
'name': 'somename'}
],
rsp
Expand All @@ -605,7 +650,28 @@ def test_alter_retention_policy(self):
{'duration': '96h0m0s',
'default': True,
'replicaN': 4,
'shardGroupDuration': u'1h0m0s',
'shardGroupDuration': u'2h0m0s',
'name': 'somename'}
],
rsp
)

# Test alter shard_duration
self.cli.alter_retention_policy('somename', 'db',
shard_duration='4h')

rsp = self.cli.get_list_retention_policies()
self.assertEqual(
[
{'duration': '0s',
'default': False,
'replicaN': 1,
'shardGroupDuration': u'168h0m0s',
'name': 'autogen'},
{'duration': '96h0m0s',
'default': True,
'replicaN': 4,
'shardGroupDuration': u'4h0m0s',
'name': 'somename'}
],
rsp
Expand Down