use of io.questdb.network.PeerDisconnectedException in project questdb by bluestreak01.
the class IODispatcherTest method testSendHttpGetAndSimpleResponse.
@Test
public void testSendHttpGetAndSimpleResponse() throws Exception {
LOG.info().$("started testSendHttpGetAndSimpleResponse").$();
final String request = "GET /status?x=1&a=%26b&c&d=x HTTP/1.1\r\n" + "Host: localhost:9000\r\n" + "Connection: keep-alive\r\n" + "Cache-Control: max-age=0\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "Accept-Encoding: gzip,deflate,sdch\r\n" + "Accept-Language: en-US,en;q=0.8\r\n" + "Cookie: textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
// the difference between request and expected is url encoding (and ':' padding, which can easily be fixed)
final String expected = "GET /status?x=1&a=&b&c&d=x HTTP/1.1\r\n" + "host:localhost:9000\r\n" + "connection:keep-alive\r\n" + "cache-control:max-age=0\r\n" + "accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "accept-encoding:gzip,deflate,sdch\r\n" + "accept-language:en-US,en;q=0.8\r\n" + "cookie:textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
final String expectedResponse = "HTTP/1.1 200 OK\r\n" + "Server: questDB/1.0\r\n" + "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "04\r\n" + "OK\r\n" + "\r\n" + "00\r\n" + "\r\n";
assertMemoryLeak(() -> {
HttpServerConfiguration httpServerConfiguration = new DefaultHttpServerConfiguration(new DefaultHttpContextConfiguration() {
@Override
public MillisecondClock getClock() {
return () -> 0;
}
});
SOCountDownLatch connectLatch = new SOCountDownLatch(1);
SOCountDownLatch contextClosedLatch = new SOCountDownLatch(1);
AtomicInteger closeCount = new AtomicInteger(0);
try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration(), new IOContextFactory<HttpConnectionContext>() {
@Override
public HttpConnectionContext newInstance(long fd, IODispatcher<HttpConnectionContext> dispatcher1) {
connectLatch.countDown();
return new HttpConnectionContext(httpServerConfiguration.getHttpContextConfiguration()) {
@Override
public void close() {
// context is closed
if (closeCount.incrementAndGet() == 1) {
super.close();
contextClosedLatch.countDown();
}
}
}.of(fd, dispatcher1);
}
})) {
StringSink sink = new StringSink();
final HttpRequestProcessorSelector selector = new HttpRequestProcessorSelector() {
@Override
public HttpRequestProcessor select(CharSequence url) {
return null;
}
@Override
public HttpRequestProcessor getDefaultProcessor() {
return new HttpRequestProcessor() {
@Override
public void onHeadersReady(HttpConnectionContext context) {
HttpRequestHeader headers = context.getRequestHeader();
sink.put(headers.getMethodLine());
sink.put("\r\n");
ObjList<CharSequence> headerNames = headers.getHeaderNames();
for (int i = 0, n = headerNames.size(); i < n; i++) {
sink.put(headerNames.getQuick(i)).put(':');
sink.put(headers.getHeader(headerNames.getQuick(i)));
sink.put("\r\n");
}
sink.put("\r\n");
}
@Override
public void onRequestComplete(HttpConnectionContext context) throws PeerDisconnectedException, PeerIsSlowToReadException {
context.simpleResponse().sendStatusWithDefaultMessage(200);
}
};
}
@Override
public void close() {
}
};
AtomicBoolean serverRunning = new AtomicBoolean(true);
SOCountDownLatch serverHaltLatch = new SOCountDownLatch(1);
new Thread(() -> {
while (serverRunning.get()) {
dispatcher.run(0);
dispatcher.processIOQueue((operation, context) -> context.handleClientOperation(operation, selector, EmptyRescheduleContext));
}
serverHaltLatch.countDown();
}).start();
long fd = Net.socketTcp(true);
try {
long sockAddr = Net.sockaddr("127.0.0.1", 9001);
try {
TestUtils.assertConnect(fd, sockAddr);
connectLatch.await();
int len = request.length();
long buffer = TestUtils.toMemory(request);
try {
Assert.assertEquals(len, Net.send(fd, buffer, len));
// read response we expect
StringSink sink2 = new StringSink();
final int expectedLen = expectedResponse.length();
int read = 0;
while (read < expectedLen) {
int n = Net.recv(fd, buffer, len);
Assert.assertTrue(n > 0);
for (int i = 0; i < n; i++) {
sink2.put((char) Unsafe.getUnsafe().getByte(buffer + i));
}
// copy response bytes to sink
read += n;
}
TestUtils.assertEquals(expectedResponse, sink2);
} finally {
Unsafe.free(buffer, len, MemoryTag.NATIVE_DEFAULT);
}
Assert.assertEquals(0, Net.close(fd));
LOG.info().$("closed [fd=").$(fd).$(']').$();
fd = -1;
contextClosedLatch.await();
serverRunning.set(false);
serverHaltLatch.await();
Assert.assertEquals(0, dispatcher.getConnectionCount());
TestUtils.assertEquals(expected, sink);
} finally {
Net.freeSockAddr(sockAddr);
}
} finally {
if (fd != -1) {
Net.close(fd);
}
}
Assert.assertEquals(1, closeCount.get());
}
});
}
use of io.questdb.network.PeerDisconnectedException in project questdb by bluestreak01.
the class JsonQueryProcessor method execute0.
public void execute0(JsonQueryProcessorState state) throws PeerDisconnectedException, PeerIsSlowToReadException, ServerDisconnectException {
metrics.jsonQuery().markStart();
state.startExecutionTimer();
final HttpConnectionContext context = state.getHttpConnectionContext();
// do not set random for new request to avoid copying random from previous request into next one
// the only time we need to copy random from state is when we resume request execution
sqlExecutionContext.with(context.getCairoSecurityContext(), null, null, context.getFd(), interruptor.of(context.getFd()));
state.info().$("exec [q='").utf8(state.getQuery()).$("']").$();
final RecordCursorFactory factory = QueryCache.getInstance().poll(state.getQuery());
try {
if (factory != null) {
try {
sqlExecutionContext.storeTelemetry(CompiledQuery.SELECT, Telemetry.ORIGIN_HTTP_JSON);
executeCachedSelect(state, factory, configuration.getKeepAliveHeader());
} catch (ReaderOutOfDateException e) {
LOG.info().$(e.getFlyweightMessage()).$();
Misc.free(factory);
compileQuery(state);
}
} else {
// new query
compileQuery(state);
}
} catch (SqlException e) {
syntaxError(context.getChunkedResponseSocket(), e, state, configuration.getKeepAliveHeader());
readyForNextRequest(context);
} catch (EntryUnavailableException e) {
LOG.info().$("[fd=").$(context.getFd()).$("] Resource busy, will retry").$();
throw RetryOperationException.INSTANCE;
} catch (CairoError | CairoException e) {
internalError(context.getChunkedResponseSocket(), e.getFlyweightMessage(), e, state);
readyForNextRequest(context);
} catch (PeerIsSlowToReadException | PeerDisconnectedException e) {
// re-throw the exception
throw e;
} catch (Throwable e) {
state.error().$("Uh-oh. Error!").$(e).$();
throw ServerDisconnectException.INSTANCE;
}
}
Aggregations