Search in sources :

Example 1 with FatalError

use of com.questdb.std.ex.FatalError in project questdb by bluestreak01.

the class Os method loadLib.

private static void loadLib(String lib) {
    InputStream is = Os.class.getResourceAsStream(lib);
    if (is == null) {
        throw new FatalError("Internal error: cannot find " + lib + ", broken package?");
    }
    try {
        File tempLib = null;
        try {
            int dot = lib.indexOf('.');
            tempLib = File.createTempFile(lib.substring(0, dot), lib.substring(dot));
            // copy to tempLib
            try (FileOutputStream out = new FileOutputStream(tempLib)) {
                byte[] buf = new byte[4096];
                while (true) {
                    int read = is.read(buf);
                    if (read == -1) {
                        break;
                    }
                    out.write(buf, 0, read);
                }
            } finally {
                tempLib.deleteOnExit();
            }
            System.load(tempLib.getAbsolutePath());
        } catch (IOException e) {
            throw new FatalError("Internal error: cannot unpack " + tempLib, e);
        }
    } finally {
        Misc.free(is);
    }
}
Also used : FatalError(com.questdb.std.ex.FatalError) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 2 with FatalError

use of com.questdb.std.ex.FatalError in project questdb by bluestreak01.

the class ZipTest method testGzip.

@Test
public void testGzip() throws Exception {
    try (Path path = new Path()) {
        File outFile = temp.newFile("x");
        File expected = new File(ZipTest.class.getResource("/large.csv").getFile());
        final int available = 64 * 1024;
        long in = Unsafe.malloc(available);
        long out = Unsafe.malloc(available / 2);
        try {
            long strm = Zip.deflateInit();
            try {
                long pIn = 0;
                long pOut = 0;
                long fdIn = Files.openRO(path.of(expected.getAbsolutePath()).$());
                try {
                    long fdOut = Files.openRW(path.of(outFile.getAbsolutePath()).$());
                    try {
                        // header
                        Files.write(fdOut, Zip.gzipHeader, Zip.gzipHeaderLen, pOut);
                        pOut += Zip.gzipHeaderLen;
                        int len;
                        int crc = 0;
                        while ((len = (int) Files.read(fdIn, in, available, pIn)) > 0) {
                            pIn += len;
                            Zip.setInput(strm, in, len);
                            crc = Zip.crc32(crc, in, len);
                            do {
                                int ret;
                                if ((ret = Zip.deflate(strm, out, available, false)) < 0) {
                                    throw new FatalError("Error in deflator: " + ret);
                                }
                                int have = available - Zip.availOut(strm);
                                if (have > 0) {
                                    Files.write(fdOut, out, have, pOut);
                                    pOut += have;
                                }
                            } while (Zip.availIn(strm) > 0);
                        }
                        int ret;
                        do {
                            if ((ret = Zip.deflate(strm, out, available, true)) < 0) {
                                throw new FatalError("Error in deflator: " + ret);
                            }
                            int have = available - Zip.availOut(strm);
                            if (have > 0) {
                                Files.write(fdOut, out, have, pOut);
                                pOut += have;
                            }
                        } while (ret != 1);
                        // write trailer
                        Unsafe.getUnsafe().putInt(out, crc);
                        Unsafe.getUnsafe().putInt(out + 4, (int) pIn);
                        Files.write(fdOut, out, 8, pOut);
                    } finally {
                        Files.close(fdOut);
                    }
                } finally {
                    Files.close(fdIn);
                }
            } finally {
                Zip.deflateEnd(strm);
            }
        } finally {
            Unsafe.free(in, available);
            Unsafe.free(out, available / 2);
        }
        // ok. read what we produced
        File actual = temp.newFile();
        try (GZIPInputStream is = new GZIPInputStream(new FileInputStream(outFile));
            FileOutputStream fos = new FileOutputStream(actual)) {
            byte[] buf = new byte[16 * 1024];
            int l;
            while ((l = is.read(buf)) > 0) {
                fos.write(buf, 0, l);
            }
        }
        TestUtils.assertEquals(expected, actual);
    }
}
Also used : Path(com.questdb.std.str.Path) GZIPInputStream(java.util.zip.GZIPInputStream) FatalError(com.questdb.std.ex.FatalError) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with FatalError

use of com.questdb.std.ex.FatalError in project questdb by bluestreak01.

the class AuthorizationTest method testServerAuthException.

@Test
public void testServerAuthException() throws Exception {
    JournalServer server = new JournalServer(new ServerConfig() {

        {
            setHeartbeatFrequency(TimeUnit.MILLISECONDS.toMillis(500));
            setEnableMultiCast(false);
        }
    }, getFactory(), (token, requestedKeys) -> {
        throw new FatalError("BANG!");
    });
    final AtomicInteger authErrorCount = new AtomicInteger();
    final CountDownLatch serverError = new CountDownLatch(1);
    JournalClient client = new JournalClient(local, getFactory(), "SECRET"::getBytes, evt -> {
        switch(evt) {
            case JournalClientEvents.EVT_AUTH_ERROR:
                authErrorCount.incrementAndGet();
                break;
            case JournalClientEvents.EVT_TERMINATED:
                serverError.countDown();
                break;
            default:
                break;
        }
    });
    server.start();
    try {
        client.start();
        Assert.assertTrue(serverError.await(5, TimeUnit.SECONDS));
        Assert.assertFalse(client.isRunning());
        Assert.assertEquals(1, authErrorCount.get());
    } finally {
        server.halt();
    }
}
Also used : ServerConfig(com.questdb.net.ha.config.ServerConfig) FatalError(com.questdb.std.ex.FatalError) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

FatalError (com.questdb.std.ex.FatalError)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 Test (org.junit.Test)2 ServerConfig (com.questdb.net.ha.config.ServerConfig)1 Path (com.questdb.std.str.Path)1 AbstractTest (com.questdb.test.tools.AbstractTest)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GZIPInputStream (java.util.zip.GZIPInputStream)1