Search in sources :

Example 1 with IndexedJournalKey

use of com.questdb.net.ha.model.IndexedJournalKey in project questdb by bluestreak01.

the class SetKeyTest method testProducerConsumer.

@Test
public void testProducerConsumer() throws Exception {
    SetKeyRequestProducer producer = new SetKeyRequestProducer();
    SetKeyRequestConsumer consumer = new SetKeyRequestConsumer();
    IndexedJournalKey key = new IndexedJournalKey(0, new JournalKey<>(Quote.class, "loc1", PartitionBy.DAY, 100, true));
    producer.setValue(key);
    producer.write(channel);
    consumer.read(channel);
    Assert.assertEquals(key, consumer.getValue());
    IndexedJournalKey key2 = new IndexedJournalKey(1, new JournalKey<>(Quote.class, "longer_location", PartitionBy.DAY, 1000, true));
    producer.setValue(key2);
    producer.write(channel);
    consumer.read(channel);
    Assert.assertEquals(key2, consumer.getValue());
    IndexedJournalKey key3 = new IndexedJournalKey(2, new JournalKey<>(Quote.class, "shorter_loc", PartitionBy.DAY, 1000, true));
    producer.setValue(key3);
    producer.write(channel);
    consumer.read(channel);
    Assert.assertEquals(key3, consumer.getValue());
}
Also used : Quote(com.questdb.model.Quote) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey) SetKeyRequestProducer(com.questdb.net.ha.protocol.commands.SetKeyRequestProducer) SetKeyRequestConsumer(com.questdb.net.ha.protocol.commands.SetKeyRequestConsumer) Test(org.junit.Test)

Example 2 with IndexedJournalKey

use of com.questdb.net.ha.model.IndexedJournalKey in project questdb by bluestreak01.

the class JournalClient method unsubscribe.

private void unsubscribe(int index, JournalWriter writer, SubscriptionHolder holder, int reason) {
    JournalDeltaConsumer deltaConsumer = deltaConsumers.getQuiet(index);
    if (deltaConsumer != null) {
        deltaConsumer.free();
    }
    if (writer != null && writersToClose.remove(writer) > -1) {
        writer.close();
    }
    if (index < writers.size()) {
        writers.setQuick(index, null);
    }
    try {
        commandProducer.write(channel, Command.REMOVE_KEY_CMD);
        setKeyRequestProducer.write(channel, new IndexedJournalKey(index, holder.remote));
        checkAck();
    } catch (JournalNetworkException e) {
        LOG.error().$("Failed to unsubscribe journal ").$(holder.remote.getName()).$(e).$();
        notifyCallback(JournalClientEvents.EVT_UNSUB_REJECT);
    }
    if (reason == JournalEvents.EVT_JNL_INCOMPATIBLE) {
        // remove from duplicate check set
        subscribedJournals.remove(holder.local.getName());
        // remove from re-subscription list
        for (int i = 0, n = subscriptions.size(); i < n; i++) {
            SubscriptionHolder h = subscriptions.getQuick(i);
            if (h.local.getName().equals(holder.local.getName())) {
                subscriptions.remove(i);
                break;
            }
        }
    }
    if (holder.listener != null) {
        holder.listener.onEvent(reason);
    }
}
Also used : JournalDeltaConsumer(com.questdb.net.ha.comsumer.JournalDeltaConsumer) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey) JournalNetworkException(com.questdb.std.ex.JournalNetworkException)

Example 3 with IndexedJournalKey

use of com.questdb.net.ha.model.IndexedJournalKey in project questdb by bluestreak01.

the class JournalServerAgent method removeClientKey.

private void removeClientKey(ByteChannel channel) throws JournalNetworkException {
    setKeyRequestConsumer.read(channel);
    IndexedJournalKey indexedKey = setKeyRequestConsumer.getValue();
    LOG.debug().$(socketAddress).$(" RemoveKey command received. Index: ").$(indexedKey.getIndex()).$();
    JournalKey<?> readerKey = indexedKey.getKey();
    int index = indexedKey.getIndex();
    IndexedJournalKey augmentedReaderKey = server.getWriterIndex0(readerKey);
    if (augmentedReaderKey == null) {
        error(channel, "Requested key not exported: " + readerKey);
    } else {
        writerToReaderMap.put(augmentedReaderKey.getIndex(), JOURNAL_INDEX_NOT_FOUND);
        readerToWriterMap.extendAndSet(index, JOURNAL_INDEX_NOT_FOUND);
        removeReader(index);
        clientStates.setQuick(index, null);
        ok(channel);
    }
}
Also used : IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey)

Example 4 with IndexedJournalKey

use of com.questdb.net.ha.model.IndexedJournalKey in project questdb by bluestreak01.

the class JournalServerAgent method addClientKey.

@SuppressWarnings("unchecked")
private void addClientKey(ByteChannel channel) throws JournalNetworkException {
    setKeyRequestConsumer.read(channel);
    IndexedJournalKey indexedKey = setKeyRequestConsumer.getValue();
    LOG.debug().$(socketAddress).$(" AddKey command received. Index: ").$(indexedKey.getIndex()).$();
    JournalKey<?> readerKey = indexedKey.getKey();
    int index = indexedKey.getIndex();
    IndexedJournalKey augmentedReaderKey = server.getWriterIndex0(readerKey);
    if (augmentedReaderKey == null) {
        error(channel, "Requested key not exported: " + readerKey);
    } else {
        writerToReaderMap.put(augmentedReaderKey.getIndex(), index);
        readerToWriterMap.extendAndSet(index, augmentedReaderKey.getIndex());
        try {
            createReader(index, augmentedReaderKey.getKey());
            ok(channel);
            sendMetadata(channel, index);
        } catch (JournalException e) {
            error(channel, "Could not created reader for key: " + readerKey, e);
        }
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey)

Example 5 with IndexedJournalKey

use of com.questdb.net.ha.model.IndexedJournalKey in project questdb by bluestreak01.

the class JournalServerAgentTest method testIncrementalInteraction.

@Test
public void testIncrementalInteraction() throws Exception {
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestUtils.generateQuoteData(origin, 200);
        server.start();
        try (JournalWriter<Quote> quoteClientWriter = getFactory().writer(Quote.class, "client")) {
            JournalDeltaConsumer quoteDeltaConsumer = new JournalDeltaConsumer(quoteClientWriter);
            // send quote journal key
            commandProducer.write(channel, Command.ADD_KEY_CMD);
            setKeyRequestProducer.write(channel, new IndexedJournalKey(0, quoteWriter.getMetadata().getKey()));
            agent.process(channel);
            charSequenceResponseConsumer.read(channel);
            TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
            hugeBufferConsumer.read(channel);
            // send quote state
            commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
            journalClientStateProducer.write(channel, new IndexedJournal(0, quoteClientWriter));
            agent.process(channel);
            charSequenceResponseConsumer.read(channel);
            TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
            quoteWriter.append(origin.query().all().asResultSet().subset(0, 100));
            quoteWriter.commit();
            commandProducer.write(channel, Command.CLIENT_READY_CMD);
            agent.process(channel);
            commandConsumer.read(channel);
            Assert.assertEquals(Command.JOURNAL_DELTA_CMD, commandConsumer.getCommand());
            Assert.assertEquals(0, intResponseConsumer.getValue(channel));
            quoteDeltaConsumer.read(channel);
            Assert.assertEquals(100, quoteClientWriter.size());
            commandConsumer.read(channel);
            Assert.assertEquals(Command.SERVER_READY_CMD, commandConsumer.getCommand());
            quoteWriter.append(origin.query().all().asResultSet().subset(100, 200));
            quoteWriter.commit();
            // send quote state
            commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
            journalClientStateProducer.write(channel, new IndexedJournal(0, quoteClientWriter));
            agent.process(channel);
            charSequenceResponseConsumer.read(channel);
            TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
            commandProducer.write(channel, Command.CLIENT_READY_CMD);
            agent.process(channel);
            commandConsumer.read(channel);
            Assert.assertEquals(Command.JOURNAL_DELTA_CMD, commandConsumer.getCommand());
            Assert.assertEquals(0, intResponseConsumer.getValue(channel));
            quoteDeltaConsumer.read(channel);
            Assert.assertEquals(200, quoteClientWriter.size());
            commandConsumer.read(channel);
            Assert.assertEquals(Command.SERVER_READY_CMD, commandConsumer.getCommand());
        }
    }
}
Also used : Quote(com.questdb.model.Quote) JournalDeltaConsumer(com.questdb.net.ha.comsumer.JournalDeltaConsumer) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey) IndexedJournal(com.questdb.net.ha.model.IndexedJournal) AbstractTest(com.questdb.test.tools.AbstractTest)

Aggregations

IndexedJournalKey (com.questdb.net.ha.model.IndexedJournalKey)8 Quote (com.questdb.model.Quote)3 JournalDeltaConsumer (com.questdb.net.ha.comsumer.JournalDeltaConsumer)3 IndexedJournal (com.questdb.net.ha.model.IndexedJournal)3 AbstractTest (com.questdb.test.tools.AbstractTest)3 JournalException (com.questdb.std.ex.JournalException)2 JournalNetworkException (com.questdb.std.ex.JournalNetworkException)2 IncompatibleJournalException (com.questdb.ex.IncompatibleJournalException)1 HugeBufferConsumer (com.questdb.net.ha.comsumer.HugeBufferConsumer)1 SetKeyRequestConsumer (com.questdb.net.ha.protocol.commands.SetKeyRequestConsumer)1 SetKeyRequestProducer (com.questdb.net.ha.protocol.commands.SetKeyRequestProducer)1 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)1 File (java.io.File)1 Test (org.junit.Test)1