Skip to content

Commit b7e43c3

Browse files
authored
Add shard_duration parameter when creating or altering retention poli… (influxdata#606)
* Add shard_duration parameter when creating or altering retention policies Fixes influxdata#560 * Remove debug print statement
1 parent b7d75af commit b7e43c3

File tree

4 files changed

+108
-11
lines changed

4 files changed

+108
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99
- Finally add a CHANGELOG.md to communicate breaking changes (#598)
1010
- Test multiple versions of InfluxDB in travis
11+
- Add SHARD DURATION parameter to retention policy create/alter
1112
### Changed
1213
- Update POST/GET requests to follow verb guidelines from InfluxDB documentation
1314
- Update test suite to support InfluxDB v1.3.9, v1.4.2, and v1.5.4

influxdb/client.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ def drop_measurement(self, measurement):
615615
method="POST")
616616

617617
def create_retention_policy(self, name, duration, replication,
618-
database=None, default=False):
618+
database=None,
619+
default=False, shard_duration="0s"):
619620
"""Create a retention policy for a database.
620621
621622
:param name: the name of the new retention policy
@@ -634,20 +635,30 @@ def create_retention_policy(self, name, duration, replication,
634635
:type database: str
635636
:param default: whether or not to set the policy as default
636637
:type default: bool
638+
:param shard_duration: the shard duration of the retention policy.
639+
Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and
640+
mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks,
641+
respectively. Infinite retention is not supported. As a workaround,
642+
specify a "1000w" duration to achieve an extremely long shard group
643+
duration. Defaults to "0s", which is interpreted by the database
644+
to mean the default value given the duration.
645+
The minimum shard group duration is 1 hour.
646+
:type shard_duration: str
637647
"""
638648
query_string = \
639649
"CREATE RETENTION POLICY {0} ON {1} " \
640-
"DURATION {2} REPLICATION {3}".format(
650+
"DURATION {2} REPLICATION {3} SHARD DURATION {4}".format(
641651
quote_ident(name), quote_ident(database or self._database),
642-
duration, replication)
652+
duration, replication, shard_duration)
643653

644654
if default is True:
645655
query_string += " DEFAULT"
646656

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

649659
def alter_retention_policy(self, name, database=None,
650-
duration=None, replication=None, default=None):
660+
duration=None, replication=None,
661+
default=None, shard_duration=None):
651662
"""Modify an existing retention policy for a database.
652663
653664
:param name: the name of the retention policy to modify
@@ -667,15 +678,26 @@ def alter_retention_policy(self, name, database=None,
667678
:type replication: int
668679
:param default: whether or not to set the modified policy as default
669680
:type default: bool
681+
:param shard_duration: the shard duration of the retention policy.
682+
Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and
683+
mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks,
684+
respectively. Infinite retention is not supported. As a workaround,
685+
specify a "1000w" duration to achieve an extremely long shard group
686+
duration.
687+
The minimum shard group duration is 1 hour.
688+
:type shard_duration: str
670689
671690
.. note:: at least one of duration, replication, or default flag
672691
should be set. Otherwise the operation will fail.
673692
"""
674693
query_string = (
675694
"ALTER RETENTION POLICY {0} ON {1}"
676-
).format(quote_ident(name), quote_ident(database or self._database))
695+
).format(quote_ident(name),
696+
quote_ident(database or self._database), shard_duration)
677697
if duration:
678698
query_string += " DURATION {0}".format(duration)
699+
if shard_duration:
700+
query_string += " SHARD DURATION {0}".format(shard_duration)
679701
if replication:
680702
query_string += " REPLICATION {0}".format(replication)
681703
if default is True:

influxdb/tests/client_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def test_create_retention_policy_default(self):
649649
self.assertEqual(
650650
m.last_request.qs['q'][0],
651651
'create retention policy "somename" on '
652-
'"db" duration 1d replication 4 default'
652+
'"db" duration 1d replication 4 shard duration 0s default'
653653
)
654654

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

675675
def test_alter_retention_policy(self):
@@ -697,6 +697,14 @@ def test_alter_retention_policy(self):
697697
'alter retention policy "somename" on "db" replication 4'
698698
)
699699

700+
# Test alter shard duration
701+
self.cli.alter_retention_policy('somename', 'db',
702+
shard_duration='1h')
703+
self.assertEqual(
704+
m.last_request.qs['q'][0],
705+
'alter retention policy "somename" on "db" shard duration 1h'
706+
)
707+
700708
# Test alter default
701709
self.cli.alter_retention_policy('somename', 'db',
702710
default=True)

influxdb/tests/server_tests/client_test_with_server.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,57 @@ def test_create_retention_policy(self):
544544
rsp
545545
)
546546

547+
self.cli.drop_retention_policy('somename', 'db')
548+
# recreate the RP
549+
self.cli.create_retention_policy('somename', '1w', 1,
550+
shard_duration='1h')
551+
552+
rsp = self.cli.get_list_retention_policies()
553+
self.assertEqual(
554+
[
555+
{'duration': '0s',
556+
'default': True,
557+
'replicaN': 1,
558+
'shardGroupDuration': u'168h0m0s',
559+
'name': 'autogen'},
560+
{'duration': '168h0m0s',
561+
'default': False,
562+
'replicaN': 1,
563+
'shardGroupDuration': u'1h0m0s',
564+
'name': 'somename'}
565+
],
566+
rsp
567+
)
568+
569+
self.cli.drop_retention_policy('somename', 'db')
570+
# recreate the RP
571+
self.cli.create_retention_policy('somename', '1w', 1)
572+
573+
rsp = self.cli.get_list_retention_policies()
574+
self.assertEqual(
575+
[
576+
{'duration': '0s',
577+
'default': True,
578+
'replicaN': 1,
579+
'shardGroupDuration': u'168h0m0s',
580+
'name': 'autogen'},
581+
{'duration': '168h0m0s',
582+
'default': False,
583+
'replicaN': 1,
584+
'shardGroupDuration': u'24h0m0s',
585+
'name': 'somename'}
586+
],
587+
rsp
588+
)
589+
547590
def test_alter_retention_policy(self):
548591
"""Test alter a retention policy, not default."""
549592
self.cli.create_retention_policy('somename', '1d', 1)
550593

551594
# Test alter duration
552595
self.cli.alter_retention_policy('somename', 'db',
553-
duration='4d')
596+
duration='4d',
597+
shard_duration='2h')
554598
# NB: altering retention policy doesn't change shard group duration
555599
rsp = self.cli.get_list_retention_policies()
556600
self.assertEqual(
@@ -563,7 +607,7 @@ def test_alter_retention_policy(self):
563607
{'duration': '96h0m0s',
564608
'default': False,
565609
'replicaN': 1,
566-
'shardGroupDuration': u'1h0m0s',
610+
'shardGroupDuration': u'2h0m0s',
567611
'name': 'somename'}
568612
],
569613
rsp
@@ -572,6 +616,7 @@ def test_alter_retention_policy(self):
572616
# Test alter replication
573617
self.cli.alter_retention_policy('somename', 'db',
574618
replication=4)
619+
575620
# NB: altering retention policy doesn't change shard group duration
576621
rsp = self.cli.get_list_retention_policies()
577622
self.assertEqual(
@@ -584,7 +629,7 @@ def test_alter_retention_policy(self):
584629
{'duration': '96h0m0s',
585630
'default': False,
586631
'replicaN': 4,
587-
'shardGroupDuration': u'1h0m0s',
632+
'shardGroupDuration': u'2h0m0s',
588633
'name': 'somename'}
589634
],
590635
rsp
@@ -605,7 +650,28 @@ def test_alter_retention_policy(self):
605650
{'duration': '96h0m0s',
606651
'default': True,
607652
'replicaN': 4,
608-
'shardGroupDuration': u'1h0m0s',
653+
'shardGroupDuration': u'2h0m0s',
654+
'name': 'somename'}
655+
],
656+
rsp
657+
)
658+
659+
# Test alter shard_duration
660+
self.cli.alter_retention_policy('somename', 'db',
661+
shard_duration='4h')
662+
663+
rsp = self.cli.get_list_retention_policies()
664+
self.assertEqual(
665+
[
666+
{'duration': '0s',
667+
'default': False,
668+
'replicaN': 1,
669+
'shardGroupDuration': u'168h0m0s',
670+
'name': 'autogen'},
671+
{'duration': '96h0m0s',
672+
'default': True,
673+
'replicaN': 4,
674+
'shardGroupDuration': u'4h0m0s',
609675
'name': 'somename'}
610676
],
611677
rsp

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy