use of io.questdb.network.NetworkFacade in project questdb by bluestreak01.
the class IODispatcherTest method testImportMultipleOnSameConnectionSlow.
@Test
public void testImportMultipleOnSameConnectionSlow() throws Exception {
assertMemoryLeak(() -> {
final String baseDir = temp.getRoot().getAbsolutePath();
final DefaultHttpServerConfiguration httpConfiguration = createHttpServerConfiguration(baseDir, false);
final WorkerPool workerPool = new WorkerPool(new WorkerPoolConfiguration() {
@Override
public int[] getWorkerAffinity() {
return new int[] { -1, -1, -1 };
}
@Override
public int getWorkerCount() {
return 3;
}
@Override
public boolean haltOnError() {
return false;
}
});
try (CairoEngine engine = new CairoEngine(new DefaultCairoConfiguration(baseDir));
HttpServer httpServer = new HttpServer(httpConfiguration, workerPool, false)) {
httpServer.bind(new HttpRequestProcessorFactory() {
@Override
public HttpRequestProcessor newInstance() {
return new StaticContentProcessor(httpConfiguration);
}
@Override
public String getUrl() {
return HttpServerConfiguration.DEFAULT_PROCESSOR_URL;
}
});
httpServer.bind(new HttpRequestProcessorFactory() {
@Override
public HttpRequestProcessor newInstance() {
return new TextImportProcessor(engine);
}
@Override
public String getUrl() {
return "/upload";
}
});
workerPool.start(LOG);
// send multipart request to server
final String request = "POST /upload HTTP/1.1\r\n" + "Host: localhost:9001\r\n" + "User-Agent: curl/7.64.0\r\n" + "Accept: */*\r\n" + "Content-Length: 437760673\r\n" + "Content-Type: multipart/form-data; boundary=------------------------27d997ca93d2689d\r\n" + "Expect: 100-continue\r\n" + "\r\n" + "--------------------------27d997ca93d2689d\r\n" + "Content-Disposition: form-data; name=\"schema\"; filename=\"schema.json\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "[\r\n" + " {\r\n" + " \"name\": \"date\",\r\n" + " \"type\": \"DATE\",\r\n" + " \"pattern\": \"d MMMM y.\",\r\n" + " \"locale\": \"ru-RU\"\r\n" + " }\r\n" + "]\r\n" + "\r\n" + "--------------------------27d997ca93d2689d\r\n" + "Content-Disposition: form-data; name=\"data\"; filename=\"fhv_tripdata_2017-02.csv\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "Dispatching_base_num,Pickup_DateTime,DropOff_datetime,PUlocationID,DOlocationID\r\n" + "B00008,2017-02-01 00:30:00,,,\r\n" + "B00008,2017-02-01 00:40:00,,,\r\n" + "B00009,2017-02-01 00:30:00,,,\r\n" + "B00013,2017-02-01 00:11:00,,,\r\n" + "B00013,2017-02-01 00:41:00,,,\r\n" + "B00013,2017-02-01 00:00:00,,,\r\n" + "B00013,2017-02-01 00:53:00,,,\r\n" + "B00013,2017-02-01 00:44:00,,,\r\n" + "B00013,2017-02-01 00:05:00,,,\r\n" + "B00013,2017-02-01 00:54:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "B00014,2017-02-01 00:46:00,,,\r\n" + "B00014,2017-02-01 00:54:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "B00014,2017-02-01 00:26:00,,,\r\n" + "B00014,2017-02-01 00:55:00,,,\r\n" + "B00014,2017-02-01 00:47:00,,,\r\n" + "B00014,2017-02-01 00:05:00,,,\r\n" + "B00014,2017-02-01 00:58:00,,,\r\n" + "B00014,2017-02-01 00:33:00,,,\r\n" + "B00014,2017-02-01 00:45:00,,,\r\n" + "\r\n" + "--------------------------27d997ca93d2689d--";
NetworkFacade nf = new NetworkFacadeImpl() {
int totalSent = 0;
@Override
public int send(long fd, long buffer, int bufferLen) {
if (bufferLen > 0) {
int result = super.send(fd, buffer, 1);
totalSent += result;
if (totalSent > 20) {
LockSupport.parkNanos(10000);
totalSent = 0;
}
return result;
}
return 0;
}
};
try {
sendAndReceive(nf, request, ValidImportResponse, 3, 0, false);
} finally {
workerPool.halt();
}
}
});
}
use of io.questdb.network.NetworkFacade in project questdb by bluestreak01.
the class IODispatcherTest method testJsonQueryWithCompressedResults1.
@Test
public void testJsonQueryWithCompressedResults1() throws Exception {
Zip.init();
assertMemoryLeak(() -> {
final NetworkFacade nf = NetworkFacadeImpl.INSTANCE;
final String baseDir = temp.getRoot().getAbsolutePath();
final DefaultHttpServerConfiguration httpConfiguration = createHttpServerConfiguration(nf, baseDir, 256, false, true);
final WorkerPool workerPool = new WorkerPool(new WorkerPoolConfiguration() {
@Override
public int[] getWorkerAffinity() {
return new int[] { -1, -1 };
}
@Override
public int getWorkerCount() {
return 2;
}
@Override
public boolean haltOnError() {
return false;
}
});
try (CairoEngine engine = new CairoEngine(new DefaultCairoConfiguration(baseDir));
HttpServer httpServer = new HttpServer(httpConfiguration, workerPool, false)) {
httpServer.bind(new HttpRequestProcessorFactory() {
@Override
public HttpRequestProcessor newInstance() {
return new StaticContentProcessor(httpConfiguration);
}
@Override
public String getUrl() {
return HttpServerConfiguration.DEFAULT_PROCESSOR_URL;
}
});
httpServer.bind(new HttpRequestProcessorFactory() {
@Override
public HttpRequestProcessor newInstance() {
return new JsonQueryProcessor(httpConfiguration.getJsonQueryProcessorConfiguration(), engine, workerPool.getWorkerCount(), metrics);
}
@Override
public String getUrl() {
return "/query";
}
});
workerPool.start(LOG);
try {
// create table with all column types
CairoTestUtils.createTestTable(engine.getConfiguration(), 30, new Rnd(), new TestRecord.ArrayBinarySequence());
// send multipart request to server
final String request = "GET /query?query=x HTTP/1.1\r\n" + "Host: localhost:9001\r\n" + "Connection: keep-alive\r\n" + "Cache-Control: max-age=0\r\n" + "Upgrade-Insecure-Requests: 1\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n" + "Accept-Encoding: gzip, deflate, br\r\n" + "Accept-Language: en-GB,en-US;q=0.9,en;q=0.8\r\n" + "\r\n";
ByteArrayResponse expectedResponse;
try (InputStream is = getClass().getResourceAsStream(getClass().getSimpleName() + ".testJsonQueryWithCompressedResults1.bin")) {
Assert.assertNotNull(is);
byte[] bytes = new byte[20 * 1024];
int len = is.read(bytes);
expectedResponse = new ByteArrayResponse(bytes, len);
}
sendAndReceive(nf, request, expectedResponse, 10, 100L, false);
} finally {
workerPool.halt();
}
}
});
}
use of io.questdb.network.NetworkFacade in project questdb by bluestreak01.
the class IODispatcherTest method testCannotSetNonBlocking.
@Test
public void testCannotSetNonBlocking() throws Exception {
assertMemoryLeak(() -> {
final HttpContextConfiguration httpContextConfiguration = new DefaultHttpContextConfiguration();
final NetworkFacade nf = new NetworkFacadeImpl() {
long theFd;
@Override
public long accept(long serverFd) {
long fd = super.accept(serverFd);
theFd = fd;
return fd;
}
@Override
public int configureNonBlocking(long fd) {
if (fd == theFd) {
return -1;
}
return super.configureNonBlocking(fd);
}
};
try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration() {
@Override
public NetworkFacade getNetworkFacade() {
return nf;
}
}, (fd, dispatcher1) -> new HttpConnectionContext(httpContextConfiguration).of(fd, dispatcher1))) {
// spin up dispatcher thread
AtomicBoolean dispatcherRunning = new AtomicBoolean(true);
SOCountDownLatch dispatcherHaltLatch = new SOCountDownLatch(1);
new Thread(() -> {
while (dispatcherRunning.get()) {
dispatcher.run(0);
}
dispatcherHaltLatch.countDown();
}).start();
try {
long socketAddr = Net.sockaddr(Net.parseIPv4("127.0.0.1"), 9001);
long fd = Net.socketTcp(true);
try {
TestUtils.assertConnect(fd, socketAddr);
int bufLen = 512;
long mem = Unsafe.malloc(bufLen, MemoryTag.NATIVE_DEFAULT);
try {
Assert.assertEquals(-2, Net.recv(fd, mem, bufLen));
} finally {
Unsafe.free(mem, bufLen, MemoryTag.NATIVE_DEFAULT);
}
} finally {
Net.close(fd);
Net.freeSockAddr(socketAddr);
}
} finally {
dispatcherRunning.set(false);
dispatcherHaltLatch.await();
}
}
});
}
use of io.questdb.network.NetworkFacade in project questdb by bluestreak01.
the class IODispatcherTest method testTwoThreadsSendTwoThreadsRead.
@Test
public // dispatcher or Http parser.
void testTwoThreadsSendTwoThreadsRead() throws Exception {
LOG.info().$("started testSendHttpGet").$();
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 int N = 100;
final int serverThreadCount = 2;
final int senderCount = 2;
assertMemoryLeak(() -> {
HttpServerConfiguration httpServerConfiguration = new DefaultHttpServerConfiguration();
final NetworkFacade nf = NetworkFacadeImpl.INSTANCE;
final AtomicInteger requestsReceived = new AtomicInteger();
final AtomicBoolean finished = new AtomicBoolean(false);
final SOCountDownLatch senderHalt = new SOCountDownLatch(senderCount);
try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration(), (fd, dispatcher1) -> new HttpConnectionContext(httpServerConfiguration.getHttpContextConfiguration()).of(fd, dispatcher1))) {
// server will publish status of each request to this queue
final RingQueue<Status> queue = new RingQueue<>(Status::new, 1024);
final MPSequence pubSeq = new MPSequence(queue.getCycle());
SCSequence subSeq = new SCSequence();
pubSeq.then(subSeq).then(pubSeq);
final AtomicBoolean serverRunning = new AtomicBoolean(true);
final SOCountDownLatch serverHaltLatch = new SOCountDownLatch(serverThreadCount);
try {
for (int j = 0; j < serverThreadCount; j++) {
new Thread(() -> {
final StringSink sink = new StringSink();
final long responseBuf = Unsafe.malloc(32, MemoryTag.NATIVE_DEFAULT);
Unsafe.getUnsafe().putByte(responseBuf, (byte) 'A');
final HttpRequestProcessor processor = new HttpRequestProcessor() {
@Override
public void onHeadersReady(HttpConnectionContext context) {
HttpRequestHeader headers = context.getRequestHeader();
sink.clear();
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");
boolean result;
try {
TestUtils.assertEquals(expected, sink);
result = true;
} catch (Exception e) {
result = false;
}
while (true) {
long cursor = pubSeq.next();
if (cursor < 0) {
continue;
}
queue.get(cursor).valid = result;
pubSeq.done(cursor);
break;
}
requestsReceived.incrementAndGet();
nf.send(context.getFd(), responseBuf, 1);
}
};
HttpRequestProcessorSelector selector = new HttpRequestProcessorSelector() {
@Override
public HttpRequestProcessor select(CharSequence url) {
return null;
}
@Override
public HttpRequestProcessor getDefaultProcessor() {
return processor;
}
@Override
public void close() {
}
};
while (serverRunning.get()) {
dispatcher.run(0);
dispatcher.processIOQueue((operation, context) -> context.handleClientOperation(operation, selector, EmptyRescheduleContext));
}
Unsafe.free(responseBuf, 32, MemoryTag.NATIVE_DEFAULT);
serverHaltLatch.countDown();
}).start();
}
AtomicInteger completedCount = new AtomicInteger();
for (int j = 0; j < senderCount; j++) {
int k = j;
new Thread(() -> {
long sockAddr = Net.sockaddr("127.0.0.1", 9001);
try {
for (int i = 0; i < N && !finished.get(); i++) {
long fd = Net.socketTcp(true);
try {
TestUtils.assertConnect(fd, sockAddr);
int len = request.length();
long buffer = TestUtils.toMemory(request);
try {
Assert.assertEquals(len, Net.send(fd, buffer, len));
Assert.assertEquals("fd=" + fd + ", i=" + i, 1, Net.recv(fd, buffer, 1));
LOG.info().$("i=").$(i).$(", j=").$(k).$();
Assert.assertEquals('A', Unsafe.getUnsafe().getByte(buffer));
} finally {
Unsafe.free(buffer, len, MemoryTag.NATIVE_DEFAULT);
}
} finally {
Net.close(fd);
}
}
} finally {
completedCount.incrementAndGet();
Net.freeSockAddr(sockAddr);
senderHalt.countDown();
}
}).start();
}
int receiveCount = 0;
while (receiveCount < N * senderCount) {
long cursor = subSeq.next();
if (cursor < 0) {
if (cursor == -1 && completedCount.get() == senderCount) {
Assert.fail("Not all requests successful, test failed, see previous failures");
break;
}
Thread.yield();
continue;
}
boolean valid = queue.get(cursor).valid;
subSeq.done(cursor);
Assert.assertTrue(valid);
receiveCount++;
}
} catch (Throwable e) {
e.printStackTrace();
throw e;
} finally {
serverRunning.set(false);
serverHaltLatch.await();
}
} catch (Throwable e) {
e.printStackTrace();
throw e;
} finally {
finished.set(true);
senderHalt.await();
}
Assert.assertEquals(N * senderCount, requestsReceived.get());
});
}
use of io.questdb.network.NetworkFacade in project questdb by bluestreak01.
the class LinuxLineUdpProtoReceiverTest method assertCannotOpenSocket.
private void assertCannotOpenSocket(ReceiverFactory factory) throws Exception {
TestUtils.assertMemoryLeak(() -> {
NetworkFacade nf = new NetworkFacadeImpl() {
@Override
public long socketUdp() {
return -1;
}
};
LineUdpReceiverConfiguration receiverCfg = new DefaultLineUdpReceiverConfiguration() {
@Override
public NetworkFacade getNetworkFacade() {
return nf;
}
};
assertConstructorFail(receiverCfg, factory);
});
}
Aggregations