Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #485 : MessagePack queries: Exception during parsing InfluxDB version [macOS] #487

Merged
merged 1 commit into from
Aug 18, 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
26 changes: 21 additions & 5 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Implementation of a InluxDB API.
Expand Down Expand Up @@ -95,6 +97,7 @@ public class InfluxDBImpl implements InfluxDB {
private String retentionPolicy = "autogen";
private ConsistencyLevel consistency = ConsistencyLevel.ONE;
private final boolean messagePack;
private Boolean messagePackSupport;
private final ChunkProccesor chunkProccesor;

/**
Expand Down Expand Up @@ -666,13 +669,26 @@ static class ErrorMessage {
public String error;
}

private boolean checkMessagePackSupport() {
Matcher matcher = Pattern.compile("(\\d+\\.*)+").matcher(version());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, Pattern.compile creates an immutable object so you can reuse it. There is another regular expression that would help you so you don't have to split: (?:(\d+)\.)(\d+). (you can see it working here). You still have to convert it to int, though.

if (!matcher.find()) {
return false;
}
String s = matcher.group();
String[] versionNumbers = s.split("\\.");
final int major = Integer.parseInt(versionNumbers[0]);
final int minor = Integer.parseInt(versionNumbers[1]);
final int fromMinor = 4;
return (major >= 2) || ((major == 1) && (minor >= fromMinor));
}

private QueryResult executeQuery(final Call<QueryResult> call) {
if (messagePack) {
String[] versionNumbers = version().split("\\.");
final int major = Integer.parseInt(versionNumbers[0]);
final int minor = Integer.parseInt(versionNumbers[1]);
final int fromMinor = 4;
if ((major < 2) && ((major != 1) || (minor < fromMinor))) {
if (messagePackSupport == null) {
messagePackSupport = checkMessagePackSupport();
}

if (!messagePackSupport) {
throw new UnsupportedOperationException(
"MessagePack format is only supported from InfluxDB version 1.4 and later");
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/org/influxdb/MessagePackInfluxDBTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.influxdb;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -183,4 +187,41 @@ public void testWriteRecordsWithPrecision() throws Exception {
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), timeP3);
this.influxDB.deleteDatabase(dbName);
}

@Test
public void testInfluxDBVersionChecking() throws InterruptedException, IOException {

InfluxDB spy = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));

doReturn("1.5.2").when(spy).version();
spy.databaseExists("abc");
spy.close();

spy = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
doReturn("v1.6.0").when(spy).version();
spy.databaseExists("abc");
spy.close();

assertThrows(UnsupportedOperationException.class, () -> {
InfluxDB spy1 = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
try {
doReturn("1.3.0").when(spy1).version();
spy1.databaseExists("abc");
} finally {
spy1.close();
}

});

assertThrows(UnsupportedOperationException.class, () -> {
InfluxDB spy1 = spy(TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK));
try {
doReturn("a.b.c").when(spy1).version();
spy1.databaseExists("abc");
} finally {
spy1.close();
}
});

}
}