use of io.questdb.std.str.Path in project questdb by bluestreak01.
the class FilesTest method testOpenCleanRWParallel.
@Test
public void testOpenCleanRWParallel() throws Exception {
File temp = temporaryFolder.getRoot();
int threadCount = 15;
int iteration = 10;
int fileSize = 1024;
try (Path path = new Path().of(temp.getAbsolutePath()).concat("openCleanRWParallel").$()) {
Assert.assertFalse(Files.exists(path));
final CyclicBarrier barrier = new CyclicBarrier(threadCount);
final CountDownLatch halt = new CountDownLatch(threadCount);
final AtomicInteger errors = new AtomicInteger();
for (int k = 0; k < threadCount; k++) {
new Thread(() -> {
try {
barrier.await();
for (int i = 0; i < iteration; i++) {
long fd = Files.openCleanRW(path, fileSize);
if (fd < 0) {
errors.incrementAndGet();
}
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
halt.countDown();
}
}).start();
}
halt.await();
Assert.assertEquals(0, errors.get());
Assert.assertTrue(Files.exists(path));
Assert.assertEquals(fileSize, Files.length(path));
}
}
use of io.questdb.std.str.Path in project questdb by bluestreak01.
the class FilesTest method testListDir.
@Test
public void testListDir() {
String temp = temporaryFolder.getRoot().getAbsolutePath();
ObjList<String> names = new ObjList<>();
try (Path path = new Path().of(temp).$()) {
try (Path cp = new Path()) {
Assert.assertTrue(Files.touch(cp.of(temp).concat("a.txt").$()));
NativeLPSZ name = new NativeLPSZ();
long pFind = Files.findFirst(path);
Assert.assertTrue(pFind != 0);
try {
do {
names.add(name.of(Files.findName(pFind)).toString());
} while (Files.findNext(pFind) > 0);
} finally {
Files.findClose(pFind);
}
}
}
names.sort(Chars::compare);
Assert.assertEquals("[.,..,a.txt]", names.toString());
}
use of io.questdb.std.str.Path in project questdb by bluestreak01.
the class PrefixedClassCatalogueFunctionFactoryTest method testSimple.
@Test
public void testSimple() throws Exception {
assertMemoryLeak(() -> {
sink.clear();
try (RecordCursorFactory factory = compiler.compile("select * from pg_catalog.pg_class() order by relname", sqlExecutionContext).getRecordCursorFactory()) {
RecordCursor cursor = factory.getCursor(sqlExecutionContext);
try {
printer.print(cursor, factory.getMetadata(), true, sink);
TestUtils.assertEquals("relname\trelnamespace\trelkind\trelowner\toid\trelpartbound\n" + "pg_class\t11\tr\t0\t1259\t\n", sink);
compiler.compile("create table xyz (a int)", sqlExecutionContext);
cursor.close();
cursor = factory.getCursor(sqlExecutionContext);
sink.clear();
printer.print(cursor, factory.getMetadata(), true, sink);
TestUtils.assertEquals("relname\trelnamespace\trelkind\trelowner\toid\trelpartbound\n" + "pg_class\t11\tr\t0\t1259\t\n" + "xyz\t2200\tr\t0\t1\t\n", sink);
try (Path path = new Path()) {
path.of(configuration.getRoot());
path.concat("test").$();
Assert.assertEquals(0, FilesFacadeImpl.INSTANCE.mkdirs(path, 0));
}
compiler.compile("create table автомобилей (b double)", sqlExecutionContext);
cursor.close();
cursor = factory.getCursor(sqlExecutionContext);
sink.clear();
printer.print(cursor, factory.getMetadata(), true, sink);
TestUtils.assertEquals("relname\trelnamespace\trelkind\trelowner\toid\trelpartbound\n" + "pg_class\t11\tr\t0\t1259\t\n" + "xyz\t2200\tr\t0\t1\t\n" + "автомобилей\t2200\tr\t0\t2\t\n", sink);
compiler.compile("drop table автомобилей;", sqlExecutionContext);
cursor.close();
cursor = factory.getCursor(sqlExecutionContext);
sink.clear();
printer.print(cursor, factory.getMetadata(), true, sink);
TestUtils.assertEquals("relname\trelnamespace\trelkind\trelowner\toid\trelpartbound\n" + "pg_class\t11\tr\t0\t1259\t\n" + "xyz\t2200\tr\t0\t1\t\n", sink);
} finally {
cursor.close();
}
}
});
}
use of io.questdb.std.str.Path in project questdb by bluestreak01.
the class IODispatcherTest method testSCPFullDownload.
@Test
public void testSCPFullDownload() 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 };
}
@Override
public int getWorkerCount() {
return 2;
}
@Override
public boolean haltOnError() {
return false;
}
});
try (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;
}
});
workerPool.start(LOG);
// create 20Mb file in /tmp directory
try (Path path = new Path().of(baseDir).concat("questdb-temp.txt").$()) {
try {
Rnd rnd = new Rnd();
final int diskBufferLen = 1024 * 1024;
writeRandomFile(path, rnd, 122299092L);
long fd = Net.socketTcp(true);
try {
long sockAddr = Net.sockaddr("127.0.0.1", 9001);
try {
TestUtils.assertConnect(fd, sockAddr);
int netBufferLen = 4 * 1024;
long buffer = Unsafe.calloc(netBufferLen, MemoryTag.NATIVE_DEFAULT);
try {
// send request to server to download file we just created
final String request = "GET /questdb-temp.txt 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";
String expectedResponseHeader = "HTTP/1.1 200 OK\r\n" + "Server: questDB/1.0\r\n" + "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" + "Content-Length: 20971520\r\n" + "Content-Type: text/plain\r\n" + // this is last modified timestamp on the file, we set this value when we created file
"ETag: \"122299092\"\r\n" + "\r\n";
for (int j = 0; j < 10; j++) {
sendRequest(request, fd, buffer);
assertDownloadResponse(fd, rnd, buffer, netBufferLen, diskBufferLen, expectedResponseHeader, 20971667);
}
// send few requests to receive 304
final String request2 = "GET /questdb-temp.txt 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" + // this header should make static processor return 304
"If-None-Match: \"122299092\"\r\n" + "Cookie: textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
String expectedResponseHeader2 = "HTTP/1.1 304 Not Modified\r\n" + "Server: questDB/1.0\r\n" + "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" + "Content-Type: text/html; charset=utf-8\r\n" + "\r\n";
for (int i = 0; i < 3; i++) {
sendRequest(request2, fd, buffer);
assertDownloadResponse(fd, rnd, buffer, netBufferLen, 0, expectedResponseHeader2, 126);
}
// couple more full downloads after 304
for (int j = 0; j < 2; j++) {
sendRequest(request, fd, buffer);
assertDownloadResponse(fd, rnd, buffer, netBufferLen, diskBufferLen, expectedResponseHeader, 20971667);
}
// get a 404 now
final String request3 = "GET /questdb-temp_!.txt 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";
String expectedResponseHeader3 = "HTTP/1.1 404 Not Found\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" + "0b\r\n" + "Not Found\r\n" + "\r\n" + "00\r\n" + "\r\n";
sendAndReceive(NetworkFacadeImpl.INSTANCE, request3, expectedResponseHeader3, 4, 0, false);
// and few more 304s
sendAndReceive(NetworkFacadeImpl.INSTANCE, request2, expectedResponseHeader2, 4, 0, false);
} finally {
Unsafe.free(buffer, netBufferLen, MemoryTag.NATIVE_DEFAULT);
}
} finally {
Net.freeSockAddr(sockAddr);
}
} finally {
Net.close(fd);
LOG.info().$("closed [fd=").$(fd).$(']').$();
}
} finally {
workerPool.halt();
Files.remove(path);
}
}
}
});
}
use of io.questdb.std.str.Path in project questdb by bluestreak01.
the class ImportIODispatcherTest method testImportLocksTable.
@Test
public void testImportLocksTable() throws Exception {
final String tableName = "trips";
new HttpQueryTestBuilder().withTempFolder(temp).withWorkerCount(1).withHttpServerConfigBuilder(new HttpServerConfigurationBuilder()).withTelemetry(false).run((engine) -> {
AtomicBoolean locked = new AtomicBoolean(false);
engine.setPoolListener((factoryType, thread, name, event, segment, position) -> {
if (event == PoolListener.EV_LOCK_SUCCESS && Chars.equalsNc(name, tableName)) {
try (Path path = new Path()) {
if (engine.getStatus(AllowAllCairoSecurityContext.INSTANCE, path, tableName) == TableUtils.TABLE_DOES_NOT_EXIST) {
locked.set(true);
}
}
}
});
new SendAndReceiveRequestBuilder().execute(ValidImportRequest1, ValidImportResponse1);
Assert.assertTrue("Engine must be locked on table creation from upload", locked.get());
});
}
Aggregations