Search in sources :

Example 1 with Job

use of com.questdb.mp.Job in project questdb by bluestreak01.

the class BootstrapMain method main.

public static void main(String[] args) throws Exception {
    System.err.printf("QuestDB HTTP Server %s%nCopyright (C) Appsicle 2014-2018, all rights reserved.%n%n", getVersion());
    if (args.length < 1) {
        System.err.println("Root directory name expected");
        return;
    }
    if (Os.type == Os._32Bit) {
        System.err.println("QuestDB requires 64-bit JVM");
        return;
    }
    final CharSequenceObjHashMap<String> optHash = hashArgs(args);
    // expected flags:
    // -d <root dir> = sets root directory
    // -f = forces copy of site to root directory even if site exists
    // -n = disables handling of HUP signal
    String dir = optHash.get("-d");
    extractSite(dir, optHash.get("-f") != null);
    File conf = new File(dir, "conf/questdb.conf");
    if (!conf.exists()) {
        System.err.println("Configuration file does not exist: " + conf);
        return;
    }
    BootstrapEnv env = new BootstrapEnv();
    // main configuration
    env.configuration = new ServerConfiguration(conf);
    configureLoggers(env.configuration);
    env.dateFormatFactory = new DateFormatFactory();
    env.dateLocaleFactory = DateLocaleFactory.INSTANCE;
    env.typeProbeCollection = new TypeProbeCollection(new File(dir, "conf/date.formats").getAbsolutePath(), env.dateFormatFactory, env.dateLocaleFactory);
    // reader/writer factory and cache
    env.factory = new Factory(env.configuration.getDbPath().getAbsolutePath(), env.configuration.getDbPoolIdleTimeout(), env.configuration.getDbReaderPoolSize(), env.configuration.getDbPoolIdleCheckInterval());
    // URL matcher configuration
    env.matcher = new SimpleUrlMatcher();
    env.matcher.put("/imp", new ImportHandler(env));
    env.matcher.put("/exec", new QueryHandler(env));
    env.matcher.put("/exp", new CsvHandler(env));
    env.matcher.put("/chk", new ExistenceCheckHandler(env));
    env.matcher.setDefaultHandler(new StaticContentHandler(env));
    // server configuration
    // add all other jobs to server as it will be scheduling workers to do them
    final HttpServer server = new HttpServer(env);
    // monitoring setup
    final FactoryEventLogger factoryEventLogger = new FactoryEventLogger(env.factory, 10000000, 5000, MicrosecondClockImpl.INSTANCE);
    ObjHashSet<Job> jobs = server.getJobs();
    jobs.addAll(LogFactory.INSTANCE.getJobs());
    jobs.add(factoryEventLogger);
    env.factory.exportJobs(jobs);
    // welcome message
    CharSink welcome = Misc.getThreadLocalBuilder();
    if (!server.start()) {
        welcome.put("Could not bind socket ").put(env.configuration.getHttpIP()).put(':').put(env.configuration.getHttpPort());
        welcome.put(". Already running?");
        System.err.println(welcome);
        System.out.println(new Date() + " QuestDB failed to start");
    } else {
        welcome.put("Listening on ").put(env.configuration.getHttpIP()).put(':').put(env.configuration.getHttpPort());
        if (env.configuration.getSslConfig().isSecure()) {
            welcome.put(" [HTTPS]");
        } else {
            welcome.put(" [HTTP plain]");
        }
        System.err.println(welcome);
        System.out.println(new Date() + " QuestDB is running");
        if (Os.type != Os.WINDOWS && optHash.get("-n") == null) {
            // suppress HUP signal
            Signal.handle(new Signal("HUP"), signal -> {
            });
        }
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println(new Date() + " QuestDB is shutting down");
            server.halt();
            factoryEventLogger.close();
            env.factory.close();
        }));
    }
}
Also used : CharSink(com.questdb.std.str.CharSink) DateFormatFactory(com.questdb.std.time.DateFormatFactory) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) DateFormatFactory(com.questdb.std.time.DateFormatFactory) DateLocaleFactory(com.questdb.std.time.DateLocaleFactory) Date(java.util.Date) Signal(sun.misc.Signal) TypeProbeCollection(com.questdb.parser.typeprobe.TypeProbeCollection) HttpServer(com.questdb.net.http.HttpServer) SimpleUrlMatcher(com.questdb.net.http.SimpleUrlMatcher) Job(com.questdb.mp.Job) File(java.io.File)

Example 2 with Job

use of com.questdb.mp.Job in project questdb by bluestreak01.

the class LinuxLineProtoReceiverTest method assertReceive.

private void assertReceive(ReceiverConfiguration receiverCfg, ReceiverFactory factory) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final String expected = "colour\tshape\tsize\ttimestamp\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n";
        CairoConfiguration cairoCfg = new DefaultCairoConfiguration(root);
        try (WriterPool pool = new WriterPool(cairoCfg)) {
            Job receiver = factory.createReceiver(receiverCfg, cairoCfg, pool);
            try {
                CountDownLatch workerHaltLatch = new CountDownLatch(1);
                try (TableModel model = new TableModel(configuration, "tab", PartitionBy.NONE).col("colour", ColumnType.SYMBOL).col("shape", ColumnType.SYMBOL).col("size", ColumnType.DOUBLE).timestamp()) {
                    CairoTestUtils.create(model);
                }
                // warm writer up
                try (TableWriter w = pool.get("tab")) {
                    w.warmUp();
                }
                ObjHashSet<Job> jobs = new ObjHashSet<>();
                jobs.add(receiver);
                Worker worker = new Worker(jobs, workerHaltLatch);
                worker.start();
                try (LineProtoSender sender = new LineProtoSender("127.0.0.1", receiverCfg.getPort(), 1400)) {
                    for (int i = 0; i < 10; i++) {
                        sender.metric("tab").tag("colour", "blue").tag("shape", "square").field("size", 3.4, 4).$(100000000);
                    }
                    sender.flush();
                }
                try (TableReader reader = new TableReader(cairoCfg, "tab")) {
                    int count = 1000000;
                    while (true) {
                        if (count-- > 0 && reader.size() < 10) {
                            reader.reload();
                            LockSupport.parkNanos(1);
                        } else {
                            break;
                        }
                    }
                    Assert.assertTrue(count > 0);
                    worker.halt();
                    Assert.assertTrue(workerHaltLatch.await(3, TimeUnit.SECONDS));
                    StringSink sink = new StringSink();
                    RecordSourcePrinter printer = new RecordSourcePrinter(sink);
                    printer.print(reader.getCursor(), true, reader.getMetadata());
                    TestUtils.assertEquals(expected, sink);
                }
            } finally {
                Misc.free(receiver);
            }
        }
    });
}
Also used : RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) StringSink(com.questdb.std.str.StringSink) WriterPool(com.questdb.cairo.pool.WriterPool) CountDownLatch(java.util.concurrent.CountDownLatch) Worker(com.questdb.mp.Worker) Job(com.questdb.mp.Job) LineProtoSender(com.questdb.cutlass.client.LineProtoSender)

Example 3 with Job

use of com.questdb.mp.Job in project questdb by bluestreak01.

the class EngineTest method testExpiry.

@Test
public void testExpiry() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        createX();
        final ObjHashSet<Job> jobs = new ObjHashSet<>();
        class MyListener implements PoolListener {

            int count = 0;

            @Override
            public void onEvent(byte factoryType, long thread, CharSequence name, short event, short segment, short position) {
                if (event == PoolListener.EV_EXPIRE) {
                    count++;
                }
            }
        }
        MyListener listener = new MyListener();
        try (Engine engine = new Engine(configuration)) {
            engine.setPoolListener(listener);
            assertWriter(engine, "x");
            assertReader(engine, "x");
            engine.exportJobs(jobs);
            Assert.assertEquals(1, jobs.size());
            Job job = jobs.get(0);
            Assert.assertNotNull(job);
            Assert.assertTrue(job.run());
            Assert.assertFalse(job.run());
            Assert.assertEquals(2, listener.count);
        }
    });
}
Also used : ObjHashSet(com.questdb.std.ObjHashSet) PoolListener(com.questdb.cairo.pool.PoolListener) Job(com.questdb.mp.Job) Test(org.junit.Test)

Aggregations

Job (com.questdb.mp.Job)3 PoolListener (com.questdb.cairo.pool.PoolListener)1 WriterPool (com.questdb.cairo.pool.WriterPool)1 LineProtoSender (com.questdb.cutlass.client.LineProtoSender)1 LogFactory (com.questdb.log.LogFactory)1 Worker (com.questdb.mp.Worker)1 HttpServer (com.questdb.net.http.HttpServer)1 SimpleUrlMatcher (com.questdb.net.http.SimpleUrlMatcher)1 TypeProbeCollection (com.questdb.parser.typeprobe.TypeProbeCollection)1 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)1 ObjHashSet (com.questdb.std.ObjHashSet)1 CharSink (com.questdb.std.str.CharSink)1 StringSink (com.questdb.std.str.StringSink)1 DateFormatFactory (com.questdb.std.time.DateFormatFactory)1 DateLocaleFactory (com.questdb.std.time.DateLocaleFactory)1 Factory (com.questdb.store.factory.Factory)1 File (java.io.File)1 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1