Search in sources :

Example 1 with JournalDeltaConsumer

use of com.questdb.net.ha.comsumer.JournalDeltaConsumer 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 2 with JournalDeltaConsumer

use of com.questdb.net.ha.comsumer.JournalDeltaConsumer 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)

Example 3 with JournalDeltaConsumer

use of com.questdb.net.ha.comsumer.JournalDeltaConsumer in project questdb by bluestreak01.

the class AbstractJournalTest method setUp.

@Before
public void setUp() throws Exception {
    origin = getFactory().writer(Quote.class, "origin");
    slave = getFactory().writer(Quote.class, "slave");
    master = getFactory().writer(Quote.class, "master");
    journalClientStateProducer = new JournalClientStateProducer();
    journalClientStateConsumer = new JournalClientStateConsumer();
    this.masterReader = getFactory().reader(Quote.class, "master");
    journalDeltaProducer = new JournalDeltaProducer(masterReader);
    journalDeltaConsumer = new JournalDeltaConsumer(slave);
    channel = new MockByteChannel();
}
Also used : Quote(com.questdb.model.Quote) JournalClientStateProducer(com.questdb.net.ha.producer.JournalClientStateProducer) JournalDeltaProducer(com.questdb.net.ha.producer.JournalDeltaProducer) JournalDeltaConsumer(com.questdb.net.ha.comsumer.JournalDeltaConsumer) JournalClientStateConsumer(com.questdb.net.ha.comsumer.JournalClientStateConsumer) Before(org.junit.Before)

Example 4 with JournalDeltaConsumer

use of com.questdb.net.ha.comsumer.JournalDeltaConsumer in project questdb by bluestreak01.

the class JournalClient method subscribeOne.

private void subscribeOne(int index, SubscriptionHolder holder, String name, boolean newSubscription) {
    if (newSubscription) {
        SubscriptionHolder sub = new SubscriptionHolder();
        sub.local = holder.local;
        sub.remote = holder.remote;
        sub.listener = holder.listener;
        sub.writer = holder.writer;
        subscriptions.add(sub);
    }
    JournalWriter<?> writer = writers.getQuiet(index);
    try {
        commandProducer.write(channel, Command.ADD_KEY_CMD);
        setKeyRequestProducer.write(channel, new IndexedJournalKey(index, holder.remote));
        checkAck();
        // todo: do we really have to use file here?
        JournalMetadata<?> metadata;
        File file = Files.makeTempFile();
        try {
            try (HugeBufferConsumer h = new HugeBufferConsumer(file)) {
                h.read(channel);
                metadata = new JournalMetadata(h.getHb(), name);
            } catch (JournalException e) {
                throw new JournalNetworkException(e);
            }
        } finally {
            Files.delete(file);
        }
        boolean validate = true;
        if (writer == null) {
            if (holder.writer == null) {
                try {
                    writer = factory.writer(metadata);
                } catch (JournalException e) {
                    LOG.error().$("Failed to create writer: ").$(e).$();
                    unsubscribe(index, null, holder, JournalEvents.EVT_JNL_INCOMPATIBLE);
                    return;
                }
                writersToClose.add(writer);
                validate = false;
            } else {
                writer = holder.writer;
            }
            writer.disableCommitOnClose();
            statusSentList.extendAndSet(index, 0);
            deltaConsumers.extendAndSet(index, new JournalDeltaConsumer(writer));
            writers.extendAndSet(index, writer);
            writer.setJournalListener(holder.listener);
        } else {
            statusSentList.setQuick(index, 0);
        }
        if (validate && !metadata.isCompatible(writer.getMetadata(), false)) {
            LOG.error().$("Journal ").$(holder.local.getName()).$(" is not compatible with ").$(holder.remote.getName()).$("(remote)").$();
            unsubscribe(index, writer, holder, JournalEvents.EVT_JNL_INCOMPATIBLE);
            return;
        }
        commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
        journalClientStateProducer.write(channel, new IndexedJournal(index, writer));
        checkAck();
        statusSentList.setQuick(index, 1);
        if (holder.listener != null) {
            holder.listener.onEvent(JournalEvents.EVT_JNL_SUBSCRIBED);
        }
        LOG.info().$("Subscribed ").$(name).$(" to ").$(holder.remote.getName()).$("(remote)").$();
    } catch (JournalNetworkException e) {
        LOG.error().$("Failed to subscribe ").$(name).$(" to ").$(holder.remote.getName()).$("(remote)").$();
        unsubscribe(index, writer, holder, JournalEvents.EVT_JNL_SERVER_ERROR);
    }
}
Also used : JournalMetadata(com.questdb.store.factory.configuration.JournalMetadata) IncompatibleJournalException(com.questdb.ex.IncompatibleJournalException) JournalException(com.questdb.std.ex.JournalException) JournalDeltaConsumer(com.questdb.net.ha.comsumer.JournalDeltaConsumer) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey) JournalNetworkException(com.questdb.std.ex.JournalNetworkException) HugeBufferConsumer(com.questdb.net.ha.comsumer.HugeBufferConsumer) IndexedJournal(com.questdb.net.ha.model.IndexedJournal) File(java.io.File)

Aggregations

JournalDeltaConsumer (com.questdb.net.ha.comsumer.JournalDeltaConsumer)4 IndexedJournalKey (com.questdb.net.ha.model.IndexedJournalKey)3 Quote (com.questdb.model.Quote)2 IndexedJournal (com.questdb.net.ha.model.IndexedJournal)2 JournalNetworkException (com.questdb.std.ex.JournalNetworkException)2 IncompatibleJournalException (com.questdb.ex.IncompatibleJournalException)1 HugeBufferConsumer (com.questdb.net.ha.comsumer.HugeBufferConsumer)1 JournalClientStateConsumer (com.questdb.net.ha.comsumer.JournalClientStateConsumer)1 JournalClientStateProducer (com.questdb.net.ha.producer.JournalClientStateProducer)1 JournalDeltaProducer (com.questdb.net.ha.producer.JournalDeltaProducer)1 JournalException (com.questdb.std.ex.JournalException)1 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)1 AbstractTest (com.questdb.test.tools.AbstractTest)1 File (java.io.File)1 Before (org.junit.Before)1