Skip to content

Commit f31b2fd

Browse files
authored
Merge pull request #487 from bonitoo-io/issue_485
fix issue #485 : MessagePack queries: Exception during parsing InfluxDB version [macOS]
2 parents becbfb5 + f134e5b commit f31b2fd

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

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

+21-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import java.util.concurrent.atomic.LongAdder;
5555
import java.util.function.BiConsumer;
5656
import java.util.function.Consumer;
57+
import java.util.regex.Matcher;
58+
import java.util.regex.Pattern;
5759

5860
/**
5961
* Implementation of a InluxDB API.
@@ -95,6 +97,7 @@ public class InfluxDBImpl implements InfluxDB {
9597
private String retentionPolicy = "autogen";
9698
private ConsistencyLevel consistency = ConsistencyLevel.ONE;
9799
private final boolean messagePack;
100+
private Boolean messagePackSupport;
98101
private final ChunkProccesor chunkProccesor;
99102

100103
/**
@@ -666,13 +669,26 @@ static class ErrorMessage {
666669
public String error;
667670
}
668671

672+
private boolean checkMessagePackSupport() {
673+
Matcher matcher = Pattern.compile("(\\d+\\.*)+").matcher(version());
674+
if (!matcher.find()) {
675+
return false;
676+
}
677+
String s = matcher.group();
678+
String[] versionNumbers = s.split("\\.");
679+
final int major = Integer.parseInt(versionNumbers[0]);
680+
final int minor = Integer.parseInt(versionNumbers[1]);
681+
final int fromMinor = 4;
682+
return (major >= 2) || ((major == 1) && (minor >= fromMinor));
683+
}
684+
669685
private QueryResult executeQuery(final Call<QueryResult> call) {
670686
if (messagePack) {
671-
String[] versionNumbers = version().split("\\.");
672-
final int major = Integer.parseInt(versionNumbers[0]);
673-
final int minor = Integer.parseInt(versionNumbers[1]);
674-
final int fromMinor = 4;
675-
if ((major < 2) && ((major != 1) || (minor < fromMinor))) {
687+
if (messagePackSupport == null) {
688+
messagePackSupport = checkMessagePackSupport();
689+
}
690+
691+
if (!messagePackSupport) {
676692
throw new UnsupportedOperationException(
677693
"MessagePack format is only supported from InfluxDB version 1.4 and later");
678694
}

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

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.influxdb;
22

3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import static org.mockito.Mockito.doReturn;
5+
import static org.mockito.Mockito.spy;
6+
37
import java.io.IOException;
48
import java.util.ArrayList;
59
import java.util.List;
@@ -183,4 +187,41 @@ public void testWriteRecordsWithPrecision() throws Exception {
183187
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), timeP3);
184188
this.influxDB.deleteDatabase(dbName);
185189
}
190+
191+
@Test
192+
public void testInfluxDBVersionChecking() throws InterruptedException, IOException {
193+
194+
InfluxDB spy = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
195+
196+
doReturn("1.5.2").when(spy).version();
197+
spy.databaseExists("abc");
198+
spy.close();
199+
200+
spy = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
201+
doReturn("v1.6.0").when(spy).version();
202+
spy.databaseExists("abc");
203+
spy.close();
204+
205+
assertThrows(UnsupportedOperationException.class, () -> {
206+
InfluxDB spy1 = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
207+
try {
208+
doReturn("1.3.0").when(spy1).version();
209+
spy1.databaseExists("abc");
210+
} finally {
211+
spy1.close();
212+
}
213+
214+
});
215+
216+
assertThrows(UnsupportedOperationException.class, () -> {
217+
InfluxDB spy1 = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
218+
try {
219+
doReturn("a.b.c").when(spy1).version();
220+
spy1.databaseExists("abc");
221+
} finally {
222+
spy1.close();
223+
}
224+
});
225+
226+
}
186227
}

0 commit comments

Comments
 (0)