com.clickzetta.platform.test.http.MockWebServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clickzetta-java Show documentation
Show all versions of clickzetta-java Show documentation
The java SDK for clickzetta's Lakehouse
package com.clickzetta.platform.test.http;
import com.clickzetta.platform.client.proxy.ResponseConstructor;
import cz.proto.ingestion.Ingestion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static java.lang.Thread.sleep;
public class MockWebServer implements AutoCloseable {
private Logger LOG = LoggerFactory.getLogger(this.getClass());
private final int bindPort;
private final Ingestion.HostPortTuple controllerTuple;
private final List workerTuples;
private MockHttpServer httpServer;
private HttpRequestHandler httpRequestHandler;
private final AtomicReference serverException = new AtomicReference<>();
private Thread webThread;
public MockWebServer(int bindPort,
Ingestion.HostPortTuple controllerTuple,
List workerTuples) {
this.bindPort = bindPort;
this.controllerTuple = controllerTuple;
this.workerTuples = workerTuples;
this.httpRequestHandler = new MockIgsHttpRequestHandler(controllerTuple, workerTuples);
this.httpServer = new MockHttpServer(bindPort, this.httpRequestHandler);
}
public MockWebServer(int bindPort,
Ingestion.HostPortTuple controllerTuple,
List workerTuples,
HttpRequestHandler httpRequestHandler) {
this.bindPort = bindPort;
this.controllerTuple = controllerTuple;
this.workerTuples = workerTuples;
this.httpRequestHandler = httpRequestHandler;
this.httpServer = new MockHttpServer(bindPort, httpRequestHandler);
}
public void start() throws Exception {
if (webThread == null) {
webThread = new Thread(() -> {
try {
this.httpServer.start();
} catch (Exception e) {
serverException.set(e);
LOG.error("start web thread {} failed.", MockWebServer.this.getClass());
}
});
webThread.setName("web-thread");
webThread.start();
sleep(1 * 1000);
validException();
LOG.info("start mock web server success.");
}
}
public void join() throws IOException {
try {
this.webThread.join();
} catch (InterruptedException ite) {
throw new IOException(ite);
}
}
public HttpRequestHandler getHttpRequestHandler() {
return this.httpRequestHandler;
}
private void validException() {
if (serverException.get() != null) {
throw new RuntimeException(serverException.get());
}
}
@Override
public void close() throws Exception {
if (this.httpServer != null) {
this.httpServer.close();
}
if (webThread != null) {
webThread.stop();
}
if (serverException.get() != null) {
throw serverException.get();
}
LOG.info("stop mock web server success.");
}
public String getWebHttpHostPort() {
return this.httpServer.getHostPort();
}
private class MockIgsHttpRequestHandler extends HttpRequestHandler {
MockIgsHttpRequestHandler(Ingestion.HostPortTuple controllerTuple,
List workerTuples) {
super(controllerTuple, workerTuples);
}
@Override
public String getTableMeta(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.GET_TABLE_META, requestStr));
}
@Override
public String createTablet(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.CREATE_TABLET, requestStr));
}
@Override
public String getMutateWorkers(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.GET_MUTATE_WORKER, requestStr));
}
@Override
public String commitTablets(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.COMMIT_TABLET, requestStr));
}
@Override
public String dropTablets(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.DROP_TABLET, requestStr));
}
@Override
public String checkTableExists(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.CHECK_TABLE_EXISTS, requestStr));
}
@Override
public String createBulkloadStream(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.CREATE_BULK_LOAD_STREAM_V2, requestStr));
}
@Override
public String commitBulkloadStream(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.COMMIT_BULK_LOAD_STREAM_V2, requestStr));
}
@Override
public String getBulkloadStream(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.GET_BULK_LOAD_STREAM_V2, requestStr));
}
@Override
public String openBulkloadStreamWriter(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.OPEN_BULK_LOAD_STREAM_WRITER_V2, requestStr));
}
@Override
public String finishBulkloadStreamWriter(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.FINISH_BULK_LOAD_STREAM_WRITER_V2, requestStr));
}
@Override
public String getBulkloadStreamStsToken(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.GET_BULK_LOAD_STREAM_STS_TOKEN_V2, requestStr));
}
@Override
public String gateway(String requestStr) throws IOException {
return ResponseConstructor.toRespString(buildPBResponse(Ingestion.MethodEnum.GATEWAY_RPC_CALL, requestStr));
}
}
public static void main(String[] args) {
Ingestion.HostPortTuple controllerTuple = Ingestion.HostPortTuple.newBuilder().setHost("localhost").setPort(1000).build();
List workerTuples = new ArrayList() {{
add(Ingestion.HostPortTuple.newBuilder().setHost("localhost").setPort(1001).build());
}};
try (MockWebServer webServer = new MockWebServer(12138, controllerTuple, workerTuples)) {
webServer.start();
webServer.join();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy