Skip to content

Commit

Permalink
Merge pull request #1686 from ClickHouse/fixing-proxy-support
Browse files Browse the repository at this point in the history
Fixing proxy support and added a test to cover
  • Loading branch information
Paultagoras authored Jun 10, 2024
2 parents d1356d8 + 3241b1b commit 5be856e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
6 changes: 3 additions & 3 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ public Builder addProxy(ProxyType type, String host, int port) {
ValidationUtils.checkNonBlank(host, "host");
ValidationUtils.checkRange(port, 1, ValidationUtils.TCP_PORT_NUMBER_MAX, "port");

this.configuration.put(String.valueOf(ClickHouseClientOption.PROXY_TYPE), type.toString());
this.configuration.put(String.valueOf(ClickHouseClientOption.PROXY_HOST), host);
this.configuration.put(String.valueOf(ClickHouseClientOption.PROXY_PORT), String.valueOf(port));
this.configuration.put(ClickHouseClientOption.PROXY_TYPE.getKey(), type.toString());
this.configuration.put(ClickHouseClientOption.PROXY_HOST.getKey(), host);
this.configuration.put(ClickHouseClientOption.PROXY_PORT.getKey(), String.valueOf(port));
return this;
}

Expand Down
117 changes: 117 additions & 0 deletions client-v2/src/test/java/com/clickhouse/client/ProxyTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.clickhouse.client;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.ClientException;
import com.clickhouse.client.api.enums.Protocol;
import com.clickhouse.client.api.enums.ProxyType;
import com.clickhouse.client.api.insert.InsertResponse;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.client.api.metrics.ClientMetrics;
import com.clickhouse.client.api.metrics.OperationMetrics;
import com.clickhouse.client.api.metrics.ServerMetrics;
import com.clickhouse.client.insert.SamplePOJO;
import eu.rekawek.toxiproxy.Proxy;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import org.testcontainers.containers.ToxiproxyContainer;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

public class ProxyTests extends BaseIntegrationTest{
private Client client;
ToxiproxyContainer toxiproxy = null;
ToxiproxyClient toxiproxyClient = null;
Proxy proxy = null;

@BeforeMethod(groups = { "integration" }, enabled = true)
public void setUp() throws IOException {
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
toxiproxy = new ToxiproxyContainer(ClickHouseServerForTest.getProxyImage())
.withNetwork(ClickHouseServerForTest.getNetwork());
toxiproxy.start();

toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort());
proxy = toxiproxyClient.createProxy("clickhouse", "0.0.0.0:8666",
ClickHouseServerForTest.hasClickHouseContainer()
? "clickhouse:" + ClickHouseProtocol.HTTP.getDefaultPort()
: ClickHouseServerForTest.getClickHouseAddress(ClickHouseProtocol.HTTP, true));

client = new Client.Builder()
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false)
.setUsername("default")
.setPassword("")
.addProxy(ProxyType.HTTP, toxiproxy.getHost(), toxiproxy.getMappedPort(8666))
.build();
}

@AfterMethod(groups = { "integration" }, enabled = true)
public void teardown() {
if (toxiproxy != null) {
toxiproxy.stop();
}
}

private void createTable(String tableQuery) throws ClickHouseException {
try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig())
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP))
.build()) {
client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close();
}
}


@Test(groups = { "integration" }, enabled = false)
public void simpleProxyTest() throws Exception {
String tableName = "simple_pojo_proxy_table";
String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
System.out.println(createSQL);
createTable(createSQL);

client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName));
List<Object> simplePOJOs = new ArrayList<>();

for (int i = 0; i < 1000; i++) {
simplePOJOs.add(new SamplePOJO());
}
proxy.enable();
InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS);

OperationMetrics metrics = response.getMetrics();
assertEquals(simplePOJOs.size(), metrics.getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong());
assertEquals(simplePOJOs.size(), response.getWrittenRows());
assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0);
}

@Test(groups = { "integration" }, enabled = true)
public void simpleDisabledProxyTest() throws Exception {
String tableName = "simple_pojo_proxy_table";
String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
System.out.println(createSQL);
createTable(createSQL);

client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName));
List<Object> simplePOJOs = new ArrayList<>();

for (int i = 0; i < 1000; i++) {
simplePOJOs.add(new SamplePOJO());
}
//proxy.disable();
try {
InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS);
} catch (Exception e) {
//FOR NOW THIS WILL FAIL
//IF IT SUCCEEDS (meaning the test fails), YAY PROXY WORKS WE SHOULD REIMPLEMENT THE TESTS!
assertTrue(e.getMessage().contains("Operation has likely timed out."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand All @@ -31,7 +32,7 @@ public class InsertTests extends BaseIntegrationTest {
private InsertSettings settings;

@BeforeMethod(groups = { "integration" }, enabled = true)
public void setUp() {
public void setUp() throws IOException {
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
client = new Client.Builder()
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false)
Expand Down

0 comments on commit 5be856e

Please sign in to comment.