Skip to content

Commit d73aa32

Browse files
authored
feat: POST query variants serializes 'q' parameter into HTTP body (#765)
1 parent 5270ff9 commit d73aa32

File tree

12 files changed

+171
-17
lines changed

12 files changed

+171
-17
lines changed

.github/workflows/master.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
jdk: [3-openjdk-16-slim, 3-jdk-14, 3-jdk-8-slim]
15-
influxdb: [1.1, 1.6, 1.8]
15+
influxdb: [1.1, 1.6, 1.8, 2.0]
1616

1717
steps:
1818
- name: Checkout

.github/workflows/pr.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
jdk: [3-openjdk-16-slim, 3-jdk-14, 3-jdk-8-slim]
15-
influxdb: [1.1, 1.6, 1.8]
15+
influxdb: [1.1, 1.6, 1.8, 2.0]
1616

1717
steps:
1818
- name: Checkout
@@ -27,3 +27,4 @@ jobs:
2727

2828
- name: codecov
2929
run: bash <(curl -s https://codecov.io/bash)
30+
if: matrix.influxdb != '2.0'

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 2.22 [unreleased]
44

5+
### Improvements
6+
7+
- `POST` query variants serializes `'q'` parameter into HTTP body [PR #765](https://github.com/influxdata/influxdb-java/pull/765)
8+
59
## 2.21 [2020-12-04]
610

711
### Fixes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This is the official (and community-maintained) Java client library for [InfluxDB](https://www.influxdata.com/products/influxdb-overview/) (1.x), the open source time series database that is part of the TICK (Telegraf, InfluxDB, Chronograf, Kapacitor) stack.
88

9-
_Note: This library is for use with InfluxDB 1.x. For connecting to InfluxDB 2.x instances, please use the [influxdb-client-java](https://github.com/influxdata/influxdb-client-java) client._
9+
_Note: This library is for use with InfluxDB 1.x and [2.x compatibility API](https://docs.influxdata.com/influxdb/v2.0/reference/api/influxdb-1x/). For full supports of InfluxDB 2.x features, please use the [influxdb-client-java](https://github.com/influxdata/influxdb-client-java) client._
1010

1111
## Adding the library to your project
1212

compile-and-test.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ docker run \
2020
--publish 8086:8086 \
2121
--publish 8089:8089/udp \
2222
--volume ${PWD}/influxdb.conf:/etc/influxdb/influxdb.conf \
23+
--env DOCKER_INFLUXDB_INIT_MODE=setup \
24+
--env DOCKER_INFLUXDB_INIT_USERNAME=my-user \
25+
--env DOCKER_INFLUXDB_INIT_PASSWORD=my-password \
26+
--env DOCKER_INFLUXDB_INIT_ORG=my-org \
27+
--env DOCKER_INFLUXDB_INIT_BUCKET=my-bucket \
28+
--env DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=my-token \
2329
influxdb:${INFLUXDB_VERSION}-alpine
2430

2531
echo "Starting Nginx"
@@ -38,6 +44,20 @@ docker run \
3844
echo "Running tests"
3945
PROXY_API_URL=http://nginx:8080/influx-api/
4046
PROXY_UDP_PORT=8080
47+
if [[ "$INFLUXDB_VERSION" == "2.0" ]]
48+
then
49+
TEST_EXPRESSION="InfluxDB2Test"
50+
# Wait to start InfluxDB
51+
docker run --link influxdb:influxdb ubuntu:20.04 bash -c "apt-get update \
52+
&& apt-get install wget --yes \
53+
&& wget -S --spider --tries=20 --retry-connrefused --waitretry=5 http://influxdb:8086/ping"
54+
# Create DBRP Mapping
55+
BUCKET_ID=$(docker exec influxdb bash -c "influx bucket list -o my-org -n my-bucket | grep my-bucket | xargs | cut -d ' ' -f 0")
56+
docker exec influxdb bash -c "influx v1 dbrp create -o my-org --db mydb --rp autogen --default --bucket-id ${BUCKET_ID}"
57+
docker exec influxdb bash -c "influx v1 auth create -o my-org --username my-user --password my-password --read-bucket ${BUCKET_ID} --write-bucket ${BUCKET_ID}"
58+
else
59+
TEST_EXPRESSION="*"
60+
fi
4161

4262
docker run --rm \
4363
--volume ${PWD}:/usr/src/mymaven \
@@ -49,7 +69,7 @@ docker run --rm \
4969
--env INFLUXDB_IP=influxdb \
5070
--env PROXY_API_URL=${PROXY_API_URL} \
5171
--env PROXY_UDP_PORT=${PROXY_UDP_PORT} \
52-
maven:${MAVEN_JAVA_VERSION} mvn clean install
72+
maven:${MAVEN_JAVA_VERSION} mvn clean install -Dtest="${TEST_EXPRESSION}"
5373

5474
docker kill influxdb || true
5575
docker kill nginx || true

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,11 @@ public void query(final Query query, final int chunkSize, final BiConsumer<Cance
642642
call = this.influxDBService.query(getDatabase(query), query.getCommandWithUrlEncoded(), chunkSize,
643643
boundParameterQuery.getParameterJsonWithUrlEncoded());
644644
} else {
645-
call = this.influxDBService.query(getDatabase(query), query.getCommandWithUrlEncoded(), chunkSize);
645+
if (query.requiresPost()) {
646+
call = this.influxDBService.query(getDatabase(query), query.getCommandWithUrlEncoded(), chunkSize, null);
647+
} else {
648+
call = this.influxDBService.query(getDatabase(query), query.getCommandWithUrlEncoded(), chunkSize);
649+
}
646650
}
647651

648652
call.enqueue(new Callback<ResponseBody>() {
@@ -711,15 +715,20 @@ public void onFailure(final Call<ResponseBody> call, final Throwable t) {
711715
*/
712716
@Override
713717
public QueryResult query(final Query query, final TimeUnit timeUnit) {
714-
Call<QueryResult> call = null;
718+
Call<QueryResult> call;
715719
if (query instanceof BoundParameterQuery) {
716720
BoundParameterQuery boundParameterQuery = (BoundParameterQuery) query;
717721
call = this.influxDBService.query(getDatabase(query),
718722
TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded(),
719723
boundParameterQuery.getParameterJsonWithUrlEncoded());
720724
} else {
721-
call = this.influxDBService.query(getDatabase(query),
722-
TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded());
725+
if (query.requiresPost()) {
726+
call = this.influxDBService.query(getDatabase(query),
727+
TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded(), null);
728+
} else {
729+
call = this.influxDBService.query(getDatabase(query),
730+
TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded());
731+
}
723732
}
724733
return executeQuery(call);
725734
}
@@ -747,7 +756,7 @@ public void deleteDatabase(final String name) {
747756
*/
748757
@Override
749758
public List<String> describeDatabases() {
750-
QueryResult result = executeQuery(this.influxDBService.query(SHOW_DATABASE_COMMAND_ENCODED));
759+
QueryResult result = executeQuery(this.influxDBService.postQuery(SHOW_DATABASE_COMMAND_ENCODED));
751760
// {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]}
752761
// Series [name=databases, columns=[name], values=[[mydb], [unittest_1433605300968]]]
753762
List<List<Object>> databaseNames = result.getResults().get(0).getSeries().get(0).getValues();

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import okhttp3.ResponseBody;
77
import retrofit2.Call;
88
import retrofit2.http.Body;
9+
import retrofit2.http.Field;
10+
import retrofit2.http.FormUrlEncoded;
911
import retrofit2.http.GET;
1012
import retrofit2.http.POST;
1113
import retrofit2.http.Query;
@@ -48,27 +50,28 @@ public Call<QueryResult> query(@Query(DB) String db,
4850
@Query(EPOCH) String epoch, @Query(value = Q, encoded = true) String query);
4951

5052
@POST("query")
53+
@FormUrlEncoded
5154
public Call<QueryResult> query(@Query(DB) String db,
52-
@Query(EPOCH) String epoch, @Query(value = Q, encoded = true) String query,
55+
@Query(EPOCH) String epoch, @Field(value = Q, encoded = true) String query,
5356
@Query(value = PARAMS, encoded = true) String params);
5457

5558
@GET("query")
5659
public Call<QueryResult> query(@Query(DB) String db,
5760
@Query(value = Q, encoded = true) String query);
5861

5962
@POST("query")
63+
@FormUrlEncoded
6064
public Call<QueryResult> postQuery(@Query(DB) String db,
61-
@Query(value = Q, encoded = true) String query);
65+
@Field(value = Q, encoded = true) String query);
6266

6367
@POST("query")
68+
@FormUrlEncoded
6469
public Call<QueryResult> postQuery(@Query(DB) String db,
65-
@Query(value = Q, encoded = true) String query, @Query(value = PARAMS, encoded = true) String params);
66-
67-
@GET("query")
68-
public Call<QueryResult> query(@Query(value = Q, encoded = true) String query);
70+
@Field(value = Q, encoded = true) String query, @Query(value = PARAMS, encoded = true) String params);
6971

7072
@POST("query")
71-
public Call<QueryResult> postQuery(@Query(value = Q, encoded = true) String query);
73+
@FormUrlEncoded
74+
public Call<QueryResult> postQuery(@Field(value = Q, encoded = true) String query);
7275

7376
@Streaming
7477
@GET("query?chunked=true")
@@ -77,6 +80,7 @@ public Call<ResponseBody> query(@Query(DB) String db, @Query(value = Q, encoded
7780

7881
@Streaming
7982
@POST("query?chunked=true")
80-
public Call<ResponseBody> query(@Query(DB) String db, @Query(value = Q, encoded = true) String query,
83+
@FormUrlEncoded
84+
public Call<ResponseBody> query(@Query(DB) String db, @Field(value = Q, encoded = true) String query,
8185
@Query(CHUNK_SIZE) int chunkSize, @Query(value = PARAMS, encoded = true) String params);
8286
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.influxdb;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.CountDownLatch;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import org.influxdb.dto.Query;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
14+
import org.junit.platform.runner.JUnitPlatform;
15+
import org.junit.runner.RunWith;
16+
17+
/**
18+
* @author Jakub Bednar (30/08/2021 11:31)
19+
*/
20+
@RunWith(JUnitPlatform.class)
21+
@EnabledIfEnvironmentVariable(named = "INFLUXDB_VERSION", matches = "2\\.0")
22+
public class InfluxDB2Test {
23+
24+
private InfluxDB influxDB;
25+
26+
@BeforeEach
27+
public void setUp() throws NoSuchFieldException, IllegalAccessException {
28+
String url = String.format("http://%s:%s", TestUtils.getInfluxIP(), TestUtils.getInfluxPORT(true));
29+
influxDB = InfluxDBFactory
30+
.connect(url, "my-user", "my-password")
31+
.setDatabase("mydb")
32+
.setRetentionPolicy("autogen");
33+
}
34+
35+
@AfterEach
36+
public void cleanup() {
37+
influxDB.close();
38+
}
39+
40+
@Test
41+
public void testQuery() throws InterruptedException {
42+
43+
String measurement = TestUtils.getRandomMeasurement();
44+
45+
// prepare data
46+
List<String> records = new ArrayList<>();
47+
records.add(measurement + ",test=a value=1 1");
48+
records.add(measurement + ",test=a value=2 2");
49+
influxDB.write(records);
50+
51+
// query data
52+
final CountDownLatch countDownLatch = new CountDownLatch(1);
53+
influxDB.query(new Query("SELECT * FROM " + measurement), 2, queryResult -> countDownLatch.countDown());
54+
55+
Assertions.assertTrue(countDownLatch.await(2, TimeUnit.SECONDS));
56+
}
57+
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,16 @@ public void testWriteBatchWithoutPrecision() throws Exception {
722722
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0), timeP2);
723723
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), timeP3);
724724

725+
// WHEN I use the post query
726+
queryResult = this.influxDB.query(new Query("SELECT * FROM " + measurement, dbName, true), TimeUnit.NANOSECONDS);
727+
728+
// THEN result will be same
729+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().size(), 3);
730+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().size(), 3);
731+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0), timeP1);
732+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0), timeP2);
733+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), timeP3);
734+
725735
this.influxDB.query(new Query("DROP DATABASE " + dbName));
726736
}
727737

@@ -1301,6 +1311,37 @@ public void testChunkingOnFailureConnectionError() throws InterruptedException {
13011311

13021312
}
13031313

1314+
@Test
1315+
public void testChunkingQueryPost() throws InterruptedException {
1316+
if (this.influxDB.version().startsWith("0.") || this.influxDB.version().startsWith("1.0")) {
1317+
// do not test version 0.13 and 1.0
1318+
return;
1319+
}
1320+
1321+
String dbName = "write_unittest_" + System.currentTimeMillis();
1322+
this.influxDB.query(new Query("CREATE DATABASE " + dbName));
1323+
String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version());
1324+
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy(rp).build();
1325+
Point point1 = Point.measurement("disk").tag("atag", "a").addField("used", 60L).addField("free", 1L).build();
1326+
Point point2 = Point.measurement("disk").tag("atag", "b").addField("used", 70L).addField("free", 2L).build();
1327+
Point point3 = Point.measurement("disk").tag("atag", "c").addField("used", 80L).addField("free", 3L).build();
1328+
batchPoints.point(point1);
1329+
batchPoints.point(point2);
1330+
batchPoints.point(point3);
1331+
this.influxDB.write(batchPoints);
1332+
1333+
CountDownLatch countDownLatch = new CountDownLatch(3);
1334+
1335+
Thread.sleep(2000);
1336+
Query query = new Query("SELECT * FROM disk", dbName, true);
1337+
this.influxDB.query(query, 2, result -> countDownLatch.countDown());
1338+
1339+
boolean await = countDownLatch.await(10, TimeUnit.SECONDS);
1340+
Assertions.assertTrue(await, "The QueryResults did not arrive!");
1341+
1342+
this.influxDB.query(new Query("DROP DATABASE " + dbName));
1343+
}
1344+
13041345
@Test
13051346
public void testFlushPendingWritesWhenBatchingEnabled() {
13061347
String dbName = "flush_tests_" + System.currentTimeMillis();

src/test/java/org/influxdb/MessagePackInfluxDBTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ public void testWriteBatchWithoutPrecision() throws Exception {
164164
value = Double.valueOf(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0).toString());
165165
Assertions.assertEquals(value, timeP3);
166166

167+
// WHEN I use the post query
168+
queryResult = this.influxDB.query(new Query("SELECT * FROM " + measurement, dbName, true), TimeUnit.NANOSECONDS);
169+
170+
// THEN result will be same
171+
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().size(), 3);
172+
value = Double.valueOf(queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0).toString());
173+
Assertions.assertEquals(value, timeP1);
174+
value = Double.valueOf(queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0).toString());
175+
Assertions.assertEquals(value, timeP2);
176+
value = Double.valueOf(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0).toString());
177+
Assertions.assertEquals(value, timeP3);
178+
167179
this.influxDB.query(new Query("DROP DATABASE " + dbName));
168180
}
169181

0 commit comments

Comments
 (0)