Skip to content

Commit ceac311

Browse files
committed
fixed ping() when async operation. fixed getting server info
1 parent 386958e commit ceac311

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.clickhouse.client.config.ClickHouseClientOption;
4040
import com.clickhouse.data.ClickHouseColumn;
4141
import com.clickhouse.data.ClickHouseFormat;
42-
import net.jpountz.lz4.LZ4Compressor;
4342
import net.jpountz.lz4.LZ4Factory;
4443
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
4544
import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -192,24 +191,16 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
192191
*
193192
*/
194193
public void loadServerInfo() {
195-
// only if 2 properties are set disable retrieval from server
196-
if (!this.configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey()) && !this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
197-
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
198-
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
199-
if (reader.next() != null) {
200-
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
201-
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
202-
serverVersion = reader.getString("version");
203-
}
194+
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
195+
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
196+
if (reader.next() != null) {
197+
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
198+
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
199+
serverVersion = reader.getString("version");
204200
}
205-
} catch (Exception e) {
206-
throw new ClientException("Failed to get server info", e);
207-
}
208-
} else {
209-
LOG.info("Using server version " + this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()) + " and timezone " + this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()) );
210-
if (this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
211-
serverVersion = this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey());
212201
}
202+
} catch (Exception e) {
203+
throw new ClientException("Failed to get server info", e);
213204
}
214205
}
215206

@@ -1213,8 +1204,11 @@ public boolean ping() {
12131204
*/
12141205
public boolean ping(long timeout) {
12151206
long startTime = System.nanoTime();
1216-
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
1217-
return true;
1207+
try {
1208+
CompletableFuture<QueryResponse> future = query("SELECT 1 FORMAT TabSeparated");
1209+
try (QueryResponse response = timeout > 0 ? future.get(timeout, TimeUnit.MILLISECONDS) : future.get()) {
1210+
return true;
1211+
}
12181212
} catch (Exception e) {
12191213
LOG.debug("Failed to connect to the server (Duration: {})", System.nanoTime() - startTime, e);
12201214
return false;
@@ -2194,7 +2188,7 @@ public String getUser() {
21942188
}
21952189

21962190
public String getServerVersion() {
2197-
return this.serverVersion;
2191+
return this.serverVersion == null ? "unknown" : this.serverVersion;
21982192
}
21992193

22002194
public String getServerTimeZone() {

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class ClientTests extends BaseIntegrationTest {
2828
private static final Logger LOGGER = LoggerFactory.getLogger(ClientTests.class);
2929

30-
@Test(dataProvider = "clientProvider")
30+
@Test(groups = {"integration"}, dataProvider = "secureClientProvider")
3131
public void testAddSecureEndpoint(Client client) {
3232
if (isCloud()) {
3333
return; // will fail in other tests
@@ -52,31 +52,35 @@ public void testAddSecureEndpoint(Client client) {
5252
}
5353
}
5454

55-
@DataProvider(name = "clientProvider")
56-
private static Client[] secureClientProvider() throws Exception {
55+
@DataProvider
56+
public static Object[][] secureClientProvider() throws Exception {
5757
ClickHouseNode node = ClickHouseServerForTest.getClickHouseNode(ClickHouseProtocol.HTTP,
5858
true, ClickHouseNode.builder()
5959
.addOption(ClickHouseClientOption.SSL_MODE.getKey(), "none")
6060
.addOption(ClickHouseClientOption.SSL.getKey(), "true").build());
61-
return new Client[]{
62-
new Client.Builder()
63-
.addEndpoint("https://" + node.getHost() + ":" + node.getPort())
64-
.setUsername("default")
65-
.setPassword("")
66-
.setRootCertificate("containers/clickhouse-server/certs/localhost.crt")
67-
.build(),
68-
new Client.Builder()
69-
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), true)
70-
.setUsername("default")
71-
.setPassword("")
72-
.setRootCertificate("containers/clickhouse-server/certs/localhost.crt")
73-
.setClientKey("user.key")
74-
.setClientCertificate("user.crt")
75-
.build()
61+
return new Client[][]{
62+
{
63+
new Client.Builder()
64+
.addEndpoint("https://" + node.getHost() + ":" + node.getPort())
65+
.setUsername("default")
66+
.setPassword("")
67+
.setRootCertificate("containers/clickhouse-server/certs/localhost.crt")
68+
.build()
69+
},
70+
{
71+
new Client.Builder()
72+
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), true)
73+
.setUsername("default")
74+
.setPassword("")
75+
.setRootCertificate("containers/clickhouse-server/certs/localhost.crt")
76+
.setClientKey("user.key")
77+
.setClientCertificate("user.crt")
78+
.build()
79+
}
7680
};
7781
}
7882

79-
@Test
83+
@Test(groups = {"integration"})
8084
public void testRawSettings() {
8185
Client client = newClient()
8286
.setOption("custom_setting_1", "value_1")
@@ -102,33 +106,39 @@ public void testRawSettings() {
102106
}
103107
}
104108

105-
@Test
109+
@Test(groups = {"integration"})
106110
public void testPing() {
107111
try (Client client = newClient().build()) {
108112
Assert.assertTrue(client.ping());
109113
}
110114
}
111115

112-
@Test
116+
@Test(groups = {"integration"})
113117
public void testPingUnpooled() {
114118
try (Client client = newClient().enableConnectionPool(false).build()) {
115119
Assert.assertTrue(client.ping());
116120
}
117121
}
118122

119-
@Test
123+
@Test(groups = {"integration"})
120124
public void testPingFailure() {
121125
try (Client client = new Client.Builder()
122126
.addEndpoint("http://localhost:12345")
123127
.setUsername("default")
124128
.setPassword("")
125-
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
126129
.build()) {
127130
Assert.assertFalse(client.ping(TimeUnit.SECONDS.toMillis(20)));
128131
}
129132
}
130133

131-
@Test
134+
@Test(groups = {"integration"})
135+
public void testPingAsync() {
136+
try (Client client = newClient().useAsyncRequests(true).build()) {
137+
Assert.assertTrue(client.ping());
138+
}
139+
}
140+
141+
@Test(groups = {"integration"})
132142
public void testSetOptions() {
133143
Map<String, String> options = new HashMap<>();
134144
String productName = "my product_name (version 1.0)";
@@ -140,7 +150,7 @@ public void testSetOptions() {
140150
}
141151
}
142152

143-
@Test
153+
@Test(groups = {"integration"})
144154
public void testProvidedExecutor() throws Exception {
145155

146156
ExecutorService executorService = Executors.newSingleThreadExecutor();
@@ -159,19 +169,19 @@ public void testProvidedExecutor() throws Exception {
159169
Assert.assertFalse(flag.get());
160170
}
161171

162-
@Test
172+
@Test(groups = {"integration"})
163173
public void testLoadingServerContext() throws Exception {
164174
long start = System.nanoTime();
165175
try (Client client = newClient().build()) {
166176
long initTime = (System.nanoTime() - start) / 1_000_000;
167177
Assert.assertTrue(initTime < 100);
168-
Assert.assertNull(client.getServerVersion());
178+
Assert.assertEquals(client.getServerVersion(), "unknown");
169179
client.loadServerInfo();
170180
Assert.assertNotNull(client.getServerVersion());
171181
}
172182
}
173183

174-
@Test
184+
@Test(groups = {"integration"})
175185
public void testDisableNative() {
176186
try (Client client = newClient().disableNativeCompression(true).build()) {
177187
Assert.assertTrue(client.toString().indexOf("JavaUnsafe") != -1);
@@ -185,7 +195,6 @@ protected Client.Builder newClient() {
185195
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure)
186196
.setUsername("default")
187197
.setPassword(ClickHouseServerForTest.getPassword())
188-
.setDefaultDatabase(ClickHouseServerForTest.getDatabase())
189-
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true"));
198+
.setDefaultDatabase(ClickHouseServerForTest.getDatabase());
190199
}
191200
}

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void testConnectionRequestTimeout() {
203203
}
204204
}
205205

206-
@Test
206+
@Test(groups = {"integration"})
207207
public void testConnectionReuseStrategy() {
208208
if (isCloud()) {
209209
return; // mocked server

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
9595
this.client = this.config.applyClientProperties(new Client.Builder())
9696
.setClientName(clientName)
9797
.build();
98-
this.client.loadServerInfo();
98+
String serverTimezone = this.client.getServerTimeZone();
99+
if (serverTimezone == null) {
100+
// we cannot operate without timezone
101+
this.client.loadServerInfo();
102+
}
99103
this.schema = client.getDefaultDatabase();
100104
this.defaultQuerySettings = new QuerySettings()
101105
.serverSetting(ServerSettings.ASYNC_INSERT, "0")

0 commit comments

Comments
 (0)