Skip to content

Commit d3e532a

Browse files
authored
Merge pull request #351 from gurlWhoCodes/feature/create-drop-rp
feature: #295 Create and Drop retention policies
2 parents 7d94535 + b8ad9de commit d3e532a

File tree

6 files changed

+172
-5
lines changed

6 files changed

+172
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- InfluxDBResultMapper now is able to process QueryResult created when a GROUP BY clause was used [PR #345](https://github.com/influxdata/influxdb-java/pull/345)
66
- InfluxDB will now handle the timestamp on its own if none is provided [PR#350](https://github.com/influxdata/influxdb-java/pull/350)
77

8+
#### Features
9+
10+
- API: add InfluxDB#createRetentionPolicy and InfluxDB#dropRetentionPolicy to be able to create and drop Retention Policies [PR #351](https://github.com/influxdata/influxdb-java/pull/351)
811

912
## v2.7 [2017-06-26]
1013

README.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ All low level REST Api calls are available.
2020
InfluxDB influxDB = InfluxDBFactory.connect("http://172.17.0.2:8086", "root", "root");
2121
String dbName = "aTimeSeries";
2222
influxDB.createDatabase(dbName);
23+
String rpName = "aRetentionPolicy";
24+
influxDB.createRetentionPolicy(rpName, dbName, "30d", "30m", 2, true);
2325

2426
BatchPoints batchPoints = BatchPoints
2527
.database(dbName)
2628
.tag("async", "true")
27-
.retentionPolicy("autogen")
29+
.retentionPolicy(rpName)
2830
.consistency(ConsistencyLevel.ALL)
2931
.build();
3032
Point point1 = Point.measurement("cpu")
@@ -43,16 +45,22 @@ batchPoints.point(point2);
4345
influxDB.write(batchPoints);
4446
Query query = new Query("SELECT idle FROM cpu", dbName);
4547
influxDB.query(query);
48+
influxDB.dropRetentionPolicy(rpName, dbName);
4649
influxDB.deleteDatabase(dbName);
4750
```
48-
Note : If you are using influxdb < 1.0.0, you should use 'default' instead of 'autogen'
51+
Note:
52+
* APIs to create and drop retention policies are supported only in versions > 2.7
53+
* If you are using influxdb < 2.8, you should use retention policy: 'autogen'
54+
* If you are using influxdb < 1.0.0, you should use 'default' instead of 'autogen'
4955

5056
If your application produces only single Points, you can enable the batching functionality of influxdb-java:
5157

5258
```java
5359
InfluxDB influxDB = InfluxDBFactory.connect("http://172.17.0.2:8086", "root", "root");
5460
String dbName = "aTimeSeries";
5561
influxDB.createDatabase(dbName);
62+
String rpName = "aRetentionPolicy";
63+
influxDB.createRetentionPolicy(rpName, dbName, "30d", "30m", 2, true);
5664

5765
// Flush every 2000 Points, at least every 100ms
5866
influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS);
@@ -69,10 +77,11 @@ Point point2 = Point.measurement("disk")
6977
.addField("free", 1L)
7078
.build();
7179

72-
influxDB.write(dbName, "autogen", point1);
73-
influxDB.write(dbName, "autogen", point2);
80+
influxDB.write(dbName, rpName, point1);
81+
influxDB.write(dbName, rpName, point2);
7482
Query query = new Query("SELECT idle FROM cpu", dbName);
7583
influxDB.query(query);
84+
influxDB.dropRetentionPolicy(rpName, dbName);
7685
influxDB.deleteDatabase(dbName);
7786
```
7887
Note that the batching functionality creates an internal thread pool that needs to be shutdown explicitly as part of a graceful application shut-down, or the application will not shut down properly. To do so simply call: ```influxDB.close()```
@@ -85,7 +94,9 @@ InfluxDB influxDB = InfluxDBFactory.connect("http://172.17.0.2:8086", "root", "r
8594
String dbName = "aTimeSeries";
8695
influxDB.createDatabase(dbName);
8796
influxDB.setDatabase(dbName);
88-
influxDB.setRetentionPolicy("autogen");
97+
String rpName = "aRetentionPolicy";
98+
influxDB.createRetentionPolicy(rpName, dbName, "30d", "30m", 2, true);
99+
influxDB.setRetentionPolicy(rpName);
89100

90101
// Flush every 2000 Points, at least every 100ms
91102
influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS);
@@ -105,6 +116,7 @@ influxDB.write(Point.measurement("disk")
105116

106117
Query query = new Query("SELECT idle FROM cpu", dbName);
107118
influxDB.query(query);
119+
influxDB.dropRetentionPolicy(rpName, dbName);
108120
influxDB.deleteDatabase(dbName);
109121
```
110122

src/main/java/org/influxdb/InfluxDB.java

+40
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,44 @@ public void write(final String database, final String retentionPolicy,
395395
* @return the InfluxDB instance to be able to use it in a fluent manner.
396396
*/
397397
public InfluxDB setRetentionPolicy(final String retentionPolicy);
398+
399+
/**
400+
* Creates a retentionPolicy.
401+
* @param rpName the name of the retentionPolicy(rp)
402+
* @param database the name of the database
403+
* @param duration the duration of the rp
404+
* @param shardDuration the shardDuration
405+
* @param replicationFactor the replicationFactor of the rp
406+
* @param isDefault if the rp is the default rp for the database or not
407+
*/
408+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
409+
final String shardDuration, final int replicationFactor, final boolean isDefault);
410+
411+
/**
412+
* Creates a retentionPolicy. (optional shardDuration)
413+
* @param rpName the name of the retentionPolicy(rp)
414+
* @param database the name of the database
415+
* @param duration the duration of the rp
416+
* @param replicationFactor the replicationFactor of the rp
417+
* @param isDefault if the rp is the default rp for the database or not
418+
*/
419+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
420+
final int replicationFactor, final boolean isDefault);
421+
422+
/**
423+
* Creates a retentionPolicy. (optional shardDuration and isDefault)
424+
* @param rpName the name of the retentionPolicy(rp)
425+
* @param database the name of the database
426+
* @param duration the duration of the rp
427+
* @param replicationFactor the replicationFactor of the rp
428+
*/
429+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
430+
final String shardDuration, final int replicationFactor);
431+
432+
/**
433+
* Drops a retentionPolicy in a database.
434+
* @param rpName the name of the retentionPolicy
435+
* @param database the name of the database
436+
*/
437+
public void dropRetentionPolicy(final String rpName, final String database);
398438
}

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+69
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,73 @@ public InfluxDB setRetentionPolicy(final String retentionPolicy) {
557557
this.retentionPolicy = retentionPolicy;
558558
return this;
559559
}
560+
561+
/**
562+
* {@inheritDoc}
563+
*/
564+
@Override
565+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
566+
final String shardDuration, final int replicationFactor, final boolean isDefault) {
567+
Preconditions.checkNonEmptyString(rpName, "retentionPolicyName");
568+
Preconditions.checkNonEmptyString(database, "database");
569+
Preconditions.checkNonEmptyString(duration, "retentionDuration");
570+
Preconditions.checkDuration(duration, "retentionDuration");
571+
if (shardDuration != null && !shardDuration.isEmpty()) {
572+
Preconditions.checkDuration(shardDuration, "shardDuration");
573+
}
574+
Preconditions.checkPositiveNumber(replicationFactor, "replicationFactor");
575+
576+
StringBuilder queryBuilder = new StringBuilder("CREATE RETENTION POLICY \"");
577+
queryBuilder.append(rpName)
578+
.append("\" ON \"")
579+
.append(database)
580+
.append("\" DURATION ")
581+
.append(duration)
582+
.append(" REPLICATION ")
583+
.append(replicationFactor);
584+
if (shardDuration != null && !shardDuration.isEmpty()) {
585+
queryBuilder.append(" SHARD DURATION ");
586+
queryBuilder.append(shardDuration);
587+
}
588+
if (isDefault) {
589+
queryBuilder.append(" DEFAULT");
590+
}
591+
execute(this.influxDBService.postQuery(this.username, this.password, Query.encode(queryBuilder.toString())));
592+
}
593+
594+
/**
595+
* {@inheritDoc}
596+
*/
597+
@Override
598+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
599+
final int replicationFactor, final boolean isDefault) {
600+
createRetentionPolicy(rpName, database, duration, null, replicationFactor, isDefault);
601+
}
602+
603+
/**
604+
* {@inheritDoc}
605+
*/
606+
@Override
607+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
608+
final String shardDuration, final int replicationFactor) {
609+
createRetentionPolicy(rpName, database, duration, null, replicationFactor, false);
610+
}
611+
612+
/**
613+
* {@inheritDoc}
614+
* @param rpName the name of the retentionPolicy
615+
* @param database the name of the database
616+
*/
617+
@Override
618+
public void dropRetentionPolicy(final String rpName, final String database) {
619+
Preconditions.checkNonEmptyString(rpName, "retentionPolicyName");
620+
Preconditions.checkNonEmptyString(database, "database");
621+
StringBuilder queryBuilder = new StringBuilder("DROP RETENTION POLICY \"");
622+
queryBuilder.append(rpName)
623+
.append("\" ON \"")
624+
.append(database)
625+
.append("\"");
626+
execute(this.influxDBService.postQuery(this.username, this.password,
627+
Query.encode(queryBuilder.toString())));
628+
}
560629
}

src/main/java/org/influxdb/impl/Preconditions.java

+13
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ public static void checkPositiveNumber(final Number number, final String name) t
3535
throw new IllegalArgumentException("Expecting a positive number for " + name);
3636
}
3737
}
38+
39+
/**
40+
* Enforces that the duration is a valid influxDB duration.
41+
* @param duration the duration to test
42+
* @param name variable name for reporting
43+
* @throws IllegalArgumentException
44+
*/
45+
public static void checkDuration(final String duration, final String name) throws IllegalArgumentException {
46+
if (!duration.matches("(\\d+[wdmhs])+")) {
47+
throw new IllegalArgumentException("Invalid InfluxDB duration: " + duration
48+
+ "for " + name);
49+
}
50+
}
3851
}

src/test/java/org/influxdb/InfluxDBTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,34 @@ public void testFlushThrowsIfBatchingIsNotEnabled() {
715715
this.influxDB.flush();
716716
}
717717

718+
/**
719+
* Test creation and deletion of retention policies
720+
*/
721+
@Test
722+
public void testCreateDropRetentionPolicies() {
723+
String dbName = "rpTest_" + System.currentTimeMillis();
724+
this.influxDB.createDatabase(dbName);
725+
726+
this.influxDB.createRetentionPolicy("testRP1", dbName, "30h", 2, false);
727+
this.influxDB.createRetentionPolicy("testRP2", dbName, "10d", "20m", 2, false);
728+
this.influxDB.createRetentionPolicy("testRP3", dbName, "2d4w", "20m", 2);
729+
730+
Query query = new Query("SHOW RETENTION POLICIES", dbName);
731+
QueryResult result = this.influxDB.query(query);
732+
Assert.assertNull(result.getError());
733+
List<List<Object>> retentionPolicies = result.getResults().get(0).getSeries().get(0).getValues();
734+
Assert.assertTrue(retentionPolicies.get(1).contains("testRP1"));
735+
Assert.assertTrue(retentionPolicies.get(2).contains("testRP2"));
736+
Assert.assertTrue(retentionPolicies.get(3).contains("testRP3"));
737+
738+
this.influxDB.dropRetentionPolicy("testRP1", dbName);
739+
this.influxDB.dropRetentionPolicy("testRP2", dbName);
740+
this.influxDB.dropRetentionPolicy("testRP3", dbName);
741+
742+
result = this.influxDB.query(query);
743+
Assert.assertNull(result.getError());
744+
retentionPolicies = result.getResults().get(0).getSeries().get(0).getValues();
745+
Assert.assertTrue(retentionPolicies.size() == 1);
746+
}
747+
718748
}

0 commit comments

Comments
 (0)