Examples of MessageSeqId


Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

        //
        // No topics acquired.
        //
        SubscribeRequest subRequest = SubscribeRequest.newBuilder().setSubscriberId(sub1).build();
        MessageSeqId msgId = MessageSeqId.newBuilder().setLocalComponent(100).build();

        sm.serveSubscribeRequest(topic1, subRequest, msgId, subDataCallback, null);

        Assert.assertEquals(ConcurrencyUtils.take(subDataCallbackQueue).right().getClass(),
                            PubSubException.ServerNotResponsibleForTopicException.class);

        sm.unsubscribe(topic1, sub1, voidCallback, null);

        Assert.assertEquals(ConcurrencyUtils.take(BooleanCallbackQueue).right().getClass(),
                            PubSubException.ServerNotResponsibleForTopicException.class);

        //
        // Acquire topic.
        //

        sm.acquiredTopic(topic1, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());

        Assert.assertTrue(sm.top2sub2seq.containsKey(topic1));
        Assert.assertEquals(0, sm.top2sub2seq.get(topic1).size());

        sm.unsubscribe(topic1, sub1, voidCallback, null);
        Assert.assertEquals(ConcurrencyUtils.take(BooleanCallbackQueue).right().getClass(),
                            PubSubException.ClientNotSubscribedException.class);

        //
        // Try to attach to a subscription.
        subRequest = SubscribeRequest.newBuilder().setCreateOrAttach(CreateOrAttach.ATTACH).setSubscriberId(sub1)
                     .build();

        sm.serveSubscribeRequest(topic1, subRequest, msgId, subDataCallback, null);
        Assert.assertEquals(ConcurrencyUtils.take(subDataCallbackQueue).right().getClass(),
                            PubSubException.ClientNotSubscribedException.class);

        // now create
        subRequest = SubscribeRequest.newBuilder().setCreateOrAttach(CreateOrAttach.CREATE).setSubscriberId(sub1)
                     .build();
        sm.serveSubscribeRequest(topic1, subRequest, msgId, subDataCallback, null);
        Assert.assertEquals(msgId.getLocalComponent(), ConcurrencyUtils.take(subDataCallbackQueue).left().getState().getMsgId().getLocalComponent());
        Assert.assertEquals(msgId.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getLastConsumeSeqId()
                            .getLocalComponent());

        // try to create again
        sm.serveSubscribeRequest(topic1, subRequest, msgId, subDataCallback, null);
        Assert.assertEquals(ConcurrencyUtils.take(subDataCallbackQueue).right().getClass(),
                            PubSubException.ClientAlreadySubscribedException.class);
        Assert.assertEquals(msgId.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getLastConsumeSeqId()
                            .getLocalComponent());

        sm.lostTopic(topic1);
        sm.acquiredTopic(topic1, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());

        // try to attach
        subRequest = SubscribeRequest.newBuilder().setCreateOrAttach(CreateOrAttach.ATTACH).setSubscriberId(sub1)
                     .build();
        MessageSeqId msgId1 = MessageSeqId.newBuilder().setLocalComponent(msgId.getLocalComponent() + 10).build();
        sm.serveSubscribeRequest(topic1, subRequest, msgId1, subDataCallback, null);
        Assert.assertEquals(msgId.getLocalComponent(), subDataCallbackQueue.take().left().getState().getMsgId().getLocalComponent());
        Assert.assertEquals(msgId.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getLastConsumeSeqId()
                            .getLocalComponent());

        // now manipulate the consume ptrs
        // dont give it enough to have it persist to ZK
        MessageSeqId msgId2 = MessageSeqId.newBuilder().setLocalComponent(
                                  msgId.getLocalComponent() + cfg.getConsumeInterval() - 1).build();
        sm.setConsumeSeqIdForSubscriber(topic1, sub1, msgId2, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());
        Assert.assertEquals(msgId2.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getLastConsumeSeqId()
                            .getLocalComponent());
        Assert.assertEquals(msgId.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getSubscriptionState().getMsgId()
                            .getLocalComponent());

        // give it more so that it will write to ZK
        MessageSeqId msgId3 = MessageSeqId.newBuilder().setLocalComponent(
                                  msgId.getLocalComponent() + cfg.getConsumeInterval() + 1).build();
        sm.setConsumeSeqIdForSubscriber(topic1, sub1, msgId3, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());

        sm.lostTopic(topic1);
        sm.acquiredTopic(topic1, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());

        Assert.assertEquals(msgId3.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getLastConsumeSeqId()
                            .getLocalComponent());
        Assert.assertEquals(msgId3.getLocalComponent(), sm.top2sub2seq.get(topic1).get(sub1).getSubscriptionState().getMsgId()
                            .getLocalComponent());

        // finally unsubscribe
        sm.unsubscribe(topic1, sub1, voidCallback, null);
        Assert.assertTrue(BooleanCallbackQueue.take().left());
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

     *
     * @param topic
     * @return
     */
    private MessageSeqId ensureSeqIdExistsForTopic(ByteString topic) {
        MessageSeqId presentSeqIdInMap = currTopicSeqIds.get(topic);

        if (presentSeqIdInMap != null) {
            return presentSeqIdInMap;
        }

        presentSeqIdInMap = MessageSeqId.newBuilder().setLocalComponent(0).build();
        MessageSeqId oldSeqIdInMap = currTopicSeqIds.putIfAbsent(topic, presentSeqIdInMap);

        if (oldSeqIdInMap != null) {
            return oldSeqIdInMap;
        }
        return presentSeqIdInMap;
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

     * @throws UnexpectedConditionException
     */
    private long adjustTopicSeqIdForPublish(ByteString topic, Message messageToPublish)
            throws UnexpectedConditionException {
        long retValue = 0;
        MessageSeqId oldId;
        MessageSeqId.Builder newIdBuilder = MessageSeqId.newBuilder();

        do {
            oldId = ensureSeqIdExistsForTopic(topic);

            // Increment our own component by 1
            retValue = oldId.getLocalComponent() + 1;
            newIdBuilder.setLocalComponent(retValue);

            if (messageToPublish.hasMsgId()) {
                // take a region-wise max
                MessageIdUtils.takeRegionMaximum(newIdBuilder, messageToPublish.getMsgId(), oldId);

            } else {
                newIdBuilder.addAllRemoteComponents(oldId.getRemoteComponentsList());
            }
        } while (!currTopicSeqIds.replace(topic, oldId, newIdBuilder.build()));

        return retValue;

View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

        }

        topicInfo.lastSeqIdPushed = builder.build();
        Message msgToSerialize = Message.newBuilder(request.message).setMsgId(topicInfo.lastSeqIdPushed).build();

        final MessageSeqId responseSeqId = msgToSerialize.getMsgId();
        topicInfo.currentLedgerRange.handle.asyncAddEntry(msgToSerialize.toByteArray(),
        new SafeAsynBKCallback.AddCallback() {
            AtomicBoolean processed = new AtomicBoolean(false);
            @Override
            public void safeAddComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

        // read last confirmed of the opened ledger
        try {
            List<LedgerRange> newLrs = new ArrayList<LedgerRange>();
            newLrs.addAll(lrs);
            lrs = newLrs;
            MessageSeqId lastSeqId;
            if (lrs.size() == 1) {
                lastSeqId = MessageSeqId.newBuilder().setLocalComponent(1).build();
            } else {
                lastSeqId = lrs.get(lrs.size() - 2).getEndSeqIdIncluded();
            }
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

     */
    LedgerRange refreshLastLedgerRange(MessageSeqId lastSeqId, LedgerRange oldRange)
        throws BKException, KeeperException, InterruptedException {
        LedgerHandle lh = bk.openLedgerNoRecovery(oldRange.getLedgerId(), DigestType.CRC32, passwd);
        long lastConfirmed = lh.readLastConfirmed();
        MessageSeqId newSeqId = MessageSeqId.newBuilder().mergeFrom(lastSeqId)
                                .setLocalComponent(lastSeqId.getLocalComponent() + lastConfirmed).build();
        return LedgerRange.newBuilder().mergeFrom(oldRange).setEndSeqIdIncluded(newSeqId).build();
    }
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

            return;
        }

        final ByteString topic = request.getTopic();

        MessageSeqId seqId;
        try {
            seqId = persistenceMgr.getCurrentSeqIdForTopic(topic);
        } catch (ServerNotResponsibleForTopicException e) {
            channel.write(PubSubResponseUtils.getResponseForException(e, request.getTxnId())).addListener(
                ChannelFutureListener.CLOSE);
            subStats.incrementFailedOps();
            ServerStats.getInstance().incrementRequestsRedirect();
            return;
        }

        final SubscribeRequest subRequest = request.getSubscribeRequest();
        final ByteString subscriberId = subRequest.getSubscriberId();

        MessageSeqId lastSeqIdPublished = MessageSeqId.newBuilder(seqId).setLocalComponent(seqId.getLocalComponent()).build();

        final long requestTime = System.currentTimeMillis();
        subMgr.serveSubscribeRequest(topic, subRequest, lastSeqIdPublished, new Callback<MessageSeqId>() {

            @Override
            public void operationFailed(Object ctx, PubSubException exception) {
                channel.write(PubSubResponseUtils.getResponseForException(exception, request.getTxnId())).addListener(
                    ChannelFutureListener.CLOSE);
                subStats.incrementFailedOps();
            }

            @Override
            public void operationFinished(Object ctx, MessageSeqId resultOfOperation) {

                TopicSubscriber topicSub = new TopicSubscriber(topic, subscriberId);

                // race with channel getting disconnected while we are adding it
                // to the 2 maps
                synchronized (channel) {
                    if (!channel.isConnected()) {
                        // channel got disconnected while we were processing the
                        // subscribe request,
                        // nothing much we can do in this case
                        subStats.incrementFailedOps();
                        return;
                    }

                    if (null != sub2Channel.putIfAbsent(topicSub, channel)) {
                        // there was another channel mapped to this sub
                        PubSubException pse = new PubSubException.TopicBusyException(
                            "subscription for this topic, subscriberId is already being served on a different channel");
                        channel.write(PubSubResponseUtils.getResponseForException(pse, request.getTxnId()))
                        .addListener(ChannelFutureListener.CLOSE);
                        subStats.incrementFailedOps();
                        return;
                    } else {
                        // channel2sub is just a cache, so we can add to it
                        // without synchronization
                        channel2sub.put(channel, topicSub);
                    }
                }
                // First write success and then tell the delivery manager,
                // otherwise the first message might go out before the response
                // to the subscribe
                channel.write(PubSubResponseUtils.getSuccessResponse(request.getTxnId()));
                subStats.updateLatency(System.currentTimeMillis() - requestTime);

                // want to start 1 ahead of the consume ptr
                MessageSeqId seqIdToStartFrom = MessageSeqId.newBuilder(resultOfOperation).setLocalComponent(
                                                    resultOfOperation.getLocalComponent() + 1).build();
                deliveryMgr.startServingSubscription(topic, subscriberId, seqIdToStartFrom,
                                                     new ChannelEndPoint(channel), TrueFilter.instance(), SubscriptionStateUtils
                                                     .isHubSubscriber(subRequest.getSubscriberId()));
            }
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

     *
     * @param topic
     * @return
     */
    private MessageSeqId ensureSeqIdExistsForTopic(ByteString topic) {
        MessageSeqId presentSeqIdInMap = currTopicSeqIds.get(topic);

        if (presentSeqIdInMap != null) {
            return presentSeqIdInMap;
        }

        presentSeqIdInMap = MessageSeqId.newBuilder().setLocalComponent(0).build();
        MessageSeqId oldSeqIdInMap = currTopicSeqIds.putIfAbsent(topic, presentSeqIdInMap);

        if (oldSeqIdInMap != null) {
            return oldSeqIdInMap;
        }
        return presentSeqIdInMap;
View Full Code Here

Examples of org.apache.hedwig.protocol.PubSubProtocol.MessageSeqId

     * @throws UnexpectedConditionException
     */
    private long adjustTopicSeqIdForPublish(ByteString topic, Message messageToPublish)
            throws UnexpectedConditionException {
        long retValue = 0;
        MessageSeqId oldId;
        MessageSeqId.Builder newIdBuilder = MessageSeqId.newBuilder();

        do {
            oldId = ensureSeqIdExistsForTopic(topic);

            // Increment our own component by 1
            retValue = oldId.getLocalComponent() + 1;
            newIdBuilder.setLocalComponent(retValue);

            if (messageToPublish.hasMsgId()) {
                // take a region-wise max
                MessageIdUtils.takeRegionMaximum(newIdBuilder, messageToPublish.getMsgId(), oldId);

            } else {
                newIdBuilder.addAllRemoteComponents(oldId.getRemoteComponentsList());
            }
        } while (!currTopicSeqIds.replace(topic, oldId, newIdBuilder.build()));

        return retValue;

View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.