org.apache.pulsar.common.api.proto.PulsarApi Java Examples

The following examples show how to use org.apache.pulsar.common.api.proto.PulsarApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransactionMetaStoreHandler.java    From pulsar with Apache License 2.0 6 votes vote down vote up
void handleEndTxnResponse(PulsarApi.CommandEndTxnResponse response) {
    OpForVoidCallBack op = (OpForVoidCallBack) pendingRequests.remove(response.getRequestId());
    if (op == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got end txn response for timeout {} - {}", response.getTxnidMostBits(),
                    response.getTxnidLeastBits());
        }
        return;
    }
    if (!response.hasError()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got end txn response success for request {}", response.getRequestId());
        }
        op.callback.complete(null);
    } else {
        LOG.error("Got end txn response for request {} error {}", response.getRequestId(), response.getError());
        op.callback.completeExceptionally(getExceptionByServerError(response.getError(), response.getMessage()));
    }

    onResponse(op);
}
 
Example #2
Source File: KafkaProducerInterceptorWrapper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Delegate work to {@link org.apache.kafka.clients.producer.ProducerInterceptor#onAcknowledgement}
 * @param producer the producer which contains the interceptor.
 * @param message the message that application sends
 * @param msgId the message id that assigned by the broker; null if send failed.
 * @param exception the exception on sending messages, null indicates send has succeed.
 */
@Override
public void onSendAcknowledgement(Producer<byte[]> producer, Message<byte[]> message, MessageId msgId, Throwable exception) {
    try {
        PulsarApi.MessageMetadata.Builder messageMetadataBuilder = ((MessageImpl<byte[]>)message).getMessageBuilder();
        partitionID = getPartitionID(messageMetadataBuilder);
        TopicPartition topicPartition = new TopicPartition(topic, Integer.parseInt(partitionID));
        kafkaProducerInterceptor.onAcknowledgement(new RecordMetadata(topicPartition,
                                                            -1L,
                                                            -1L,
                                                            messageMetadataBuilder.getEventTime(),
                                                            -1L,
                                                            message.getKeyBytes().length,
                                                            message.getValue().length), new Exception(exception));
    } catch (NumberFormatException e) {
        String errorMessage = "Unable to convert partitionID to integer: " + e.getMessage();
        log.error(errorMessage);
        throw new RuntimeException(errorMessage);
    }
}
 
Example #3
Source File: ParserProxyHandler.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void logging(Channel conn, PulsarApi.BaseCommand.Type cmdtype, String info, List<RawMessage> messages) throws Exception{

        if (messages != null) {
            // lag
            for (int i=0; i<messages.size(); i++) {
                info = info + "["+ (System.currentTimeMillis() - messages.get(i).getPublishTime()) + "] " + new String(ByteBufUtil.getBytes((messages.get(i)).getData()), "UTF8");
            }
        }
        // log conn format is like from source to target
        switch (this.connType) {
            case ParserProxyHandler.FRONTEND_CONN:
                log.info(ParserProxyHandler.FRONTEND_CONN + ":{} cmd:{} msg:{}", "[" + conn.remoteAddress() + conn.localAddress() + "]", cmdtype, info);
                break;
            case ParserProxyHandler.BACKEND_CONN:
                log.info(ParserProxyHandler.BACKEND_CONN + ":{} cmd:{} msg:{}", "[" + conn.localAddress() + conn.remoteAddress() + "]", cmdtype, info);
                break;
        }
    }
 
Example #4
Source File: MessageIdImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static MessageId fromByteArray(byte[] data) throws IOException {
    checkNotNull(data);
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer(data, 0, data.length));
    PulsarApi.MessageIdData.Builder builder = PulsarApi.MessageIdData.newBuilder();

    PulsarApi.MessageIdData idData;
    try {
        idData = builder.mergeFrom(inputStream, null).build();
    } catch (UninitializedMessageException e) {
        throw new IOException(e);
    }

    MessageIdImpl messageId;
    if (idData.hasBatchIndex()) {
        messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(),
                idData.getBatchIndex());
    } else {
        messageId = new MessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition());
    }

    inputStream.recycle();
    builder.recycle();
    idData.recycle();
    return messageId;
}
 
Example #5
Source File: BatchMessageKeyBasedContainer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private ByteBuf getCompressedBatchMetadataAndPayload() {
    for (MessageImpl<?> msg : messages) {
        PulsarApi.MessageMetadata.Builder msgBuilder = msg.getMessageBuilder();
        batchedMessageMetadataAndPayload = Commands.serializeSingleMessageInBatchWithPayload(msgBuilder,
                msg.getDataBuffer(), batchedMessageMetadataAndPayload);
        msgBuilder.recycle();
    }
    int uncompressedSize = batchedMessageMetadataAndPayload.readableBytes();
    ByteBuf compressedPayload = compressor.encode(batchedMessageMetadataAndPayload);
    batchedMessageMetadataAndPayload.release();
    if (compressionType != PulsarApi.CompressionType.NONE) {
        messageMetadata.setCompression(compressionType);
        messageMetadata.setUncompressedSize(uncompressedSize);
    }

    // Update the current max batch size using the uncompressed size, which is what we need in any case to
    // accumulate the batch content
    maxBatchSize = Math.max(maxBatchSize, uncompressedSize);
    return compressedPayload;
}
 
Example #6
Source File: ServerCnx.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleAddPartitionToTxn(PulsarApi.CommandAddPartitionToTxn command) {
    TxnID txnID = new TxnID(command.getTxnidMostBits(), command.getTxnidLeastBits());
    if (log.isDebugEnabled()) {
        log.debug("Receive add published partition to txn request {} from {} with txnId {}", command.getRequestId(), remoteAddress, txnID);
    }
    service.pulsar().getTransactionMetadataStoreService().addProducedPartitionToTxn(txnID, command.getPartitionsList())
        .whenComplete(((v, ex) -> {
            if (ex == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Send response success for add published partition to txn request {}",  command.getRequestId());
                }
                ctx.writeAndFlush(Commands.newAddPartitionToTxnResponse(command.getRequestId(),
                        txnID.getLeastSigBits(), txnID.getMostSigBits()));
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Send response error for add published partition to txn request {}",  command.getRequestId(), ex);
                }
                ctx.writeAndFlush(Commands.newAddPartitionToTxnResponse(command.getRequestId(), txnID.getMostSigBits(),
                        BrokerServiceException.getClientErrorCode(ex), ex.getMessage()));
            }
        }));
}
 
Example #7
Source File: TransactionMetaStoreHandler.java    From pulsar with Apache License 2.0 6 votes vote down vote up
void handleNewTxnResponse(PulsarApi.CommandNewTxnResponse response) {
    OpForTxnIdCallBack op = (OpForTxnIdCallBack) pendingRequests.remove(response.getRequestId());
    if (op == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got new txn response for timeout {} - {}", response.getTxnidMostBits(),
                response.getTxnidLeastBits());
        }
        return;
    }
    if (!response.hasError()) {
        TxnID txnID = new TxnID(response.getTxnidMostBits(), response.getTxnidLeastBits());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got new txn response {} for request {}", txnID, response.getRequestId());
        }
        op.callback.complete(txnID);
    } else {
        LOG.error("Got new txn for request {} error {}", response.getRequestId(), response.getError());
        op.callback.completeExceptionally(getExceptionByServerError(response.getError(), response.getMessage()));
    }

    onResponse(op);
}
 
Example #8
Source File: CommandUtilsTests.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetadataFromCommandSubscribe() {
    Map<String, String> metadata = CommandUtils.metadataFromCommand(newCommandSubscribe(null, null));
    Assert.assertNotNull(metadata);
    Assert.assertTrue(metadata.isEmpty());

    final String key = "key";
    final String value = "value";

    PulsarApi.CommandSubscribe cmd = newCommandSubscribe(key, value);
    metadata = CommandUtils.metadataFromCommand(cmd);
    Assert.assertEquals(1, metadata.size());
    final Map.Entry<String, String> entry = metadata.entrySet().iterator().next();
    Assert.assertEquals(key, entry.getKey());
    Assert.assertEquals(value, entry.getValue());
}
 
Example #9
Source File: Commands.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static ByteBuf newAuthResponse(String authMethod,
                                       AuthData clientData,
                                       int clientProtocolVersion,
                                       String clientVersion) {
    CommandAuthResponse.Builder responseBuilder  = CommandAuthResponse.newBuilder();

    responseBuilder.setClientVersion(clientVersion != null ? clientVersion : "Pulsar Client");
    responseBuilder.setProtocolVersion(clientProtocolVersion);

    CommandAuthResponse response = responseBuilder
        .setResponse(PulsarApi.AuthData.newBuilder()
            .setAuthData(copyFrom(clientData.getBytes()))
            .setAuthMethodName(authMethod)
            .build())
        .build();

    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.AUTH_RESPONSE).setAuthResponse(response));
    response.recycle();
    responseBuilder.recycle();
    return res;
}
 
Example #10
Source File: MessageParser.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static void receiveIndividualMessagesFromBatch(ReferenceCountedObject<MessageMetadata> msgMetadata,
        ByteBuf uncompressedPayload, long ledgerId, long entryId, MessageProcessor processor) {
    int batchSize = msgMetadata.get().getNumMessagesInBatch();

    try {
        for (int i = 0; i < batchSize; ++i) {
            PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder = PulsarApi.SingleMessageMetadata
                    .newBuilder();
            ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload,
                    singleMessageMetadataBuilder, i, batchSize);

            if (singleMessageMetadataBuilder.getCompactedOut()) {
                // message has been compacted out, so don't send to the user
                singleMessagePayload.release();
                singleMessageMetadataBuilder.recycle();
                continue;
            }

            processor.process(RawMessageImpl.get(msgMetadata, singleMessageMetadataBuilder, singleMessagePayload,
                    ledgerId, entryId, i));
        }
    } catch (IOException e) {
        log.warn("Unable to obtain messages in batch", e);
    }
}
 
Example #11
Source File: TransactionMetaStoreHandler.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<Void> commitAsync(TxnID txnID) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Commit txn {}", txnID);
    }
    CompletableFuture<Void> callback = new CompletableFuture<>();

    if (!canSendRequest(callback)) {
        return callback;
    }
    long requestId = client.newRequestId();
    ByteBuf cmd = Commands.newEndTxn(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits(), PulsarApi.TxnAction.COMMIT);
    OpForVoidCallBack op = OpForVoidCallBack.create(cmd, callback);
    pendingRequests.put(requestId, op);
    timeoutQueue.add(new RequestTime(System.currentTimeMillis(), requestId));
    cmd.retain();
    cnx().ctx().writeAndFlush(cmd, cnx().ctx().voidPromise());
    return callback;
}
 
Example #12
Source File: SchemaInfoUtil.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static SchemaInfo newSchemaInfo(Schema schema) {
    SchemaInfo si = new SchemaInfo();
    si.setName(schema.getName());
    si.setSchema(schema.getSchemaData().toByteArray());
    si.setType(Commands.getSchemaType(schema.getType()));
    if (schema.getPropertiesCount() == 0) {
        si.setProperties(Collections.emptyMap());
    } else {
        si.setProperties(new TreeMap<>());
        for (int i = 0; i < schema.getPropertiesCount(); i++) {
            PulsarApi.KeyValue kv = schema.getProperties(i);
            si.getProperties().put(kv.getKey(), kv.getValue());
        }
    }
    return si;
}
 
Example #13
Source File: Commands.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static ByteBuf newAuthChallenge(String authMethod, AuthData brokerData, int clientProtocolVersion) {
    CommandAuthChallenge.Builder challengeBuilder = CommandAuthChallenge.newBuilder();

    // If the broker supports a newer version of the protocol, it will anyway advertise the max version that the
    // client supports, to avoid confusing the client.
    int currentProtocolVersion = getCurrentProtocolVersion();
    int versionToAdvertise = Math.min(currentProtocolVersion, clientProtocolVersion);

    challengeBuilder.setProtocolVersion(versionToAdvertise);

    byte[] authData = brokerData != null ? brokerData.getBytes() : new byte[0];

    CommandAuthChallenge challenge = challengeBuilder
        .setChallenge(PulsarApi.AuthData.newBuilder()
            .setAuthData(copyFrom(authData))
            .setAuthMethodName(authMethod)
            .build())
        .build();

    ByteBuf res = serializeWithSize(
        BaseCommand.newBuilder().setType(Type.AUTH_CHALLENGE).setAuthChallenge(challenge));
    challenge.recycle();
    challengeBuilder.recycle();
    return res;
}
 
Example #14
Source File: CompressionCodecProvider.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static PulsarApi.CompressionType convertToWireProtocol(CompressionType compressionType) {
    switch (compressionType) {
    case NONE:
        return PulsarApi.CompressionType.NONE;
    case LZ4:
        return PulsarApi.CompressionType.LZ4;
    case ZLIB:
        return PulsarApi.CompressionType.ZLIB;
    case ZSTD:
        return PulsarApi.CompressionType.ZSTD;
    case SNAPPY:
        return PulsarApi.CompressionType.SNAPPY;

    default:
        throw new RuntimeException("Invalid compression type");
    }
}
 
Example #15
Source File: TransactionMetaStoreHandler.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<Void> abortAsync(TxnID txnID) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Abort txn {}", txnID);
    }
    CompletableFuture<Void> callback = new CompletableFuture<>();

    if (!canSendRequest(callback)) {
        return callback;
    }
    long requestId = client.newRequestId();
    ByteBuf cmd = Commands.newEndTxn(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits(), PulsarApi.TxnAction.ABORT);
    OpForVoidCallBack op = OpForVoidCallBack.create(cmd, callback);
    pendingRequests.put(requestId, op);
    timeoutQueue.add(new RequestTime(System.currentTimeMillis(), requestId));
    cmd.retain();
    cnx().ctx().writeAndFlush(cmd, cnx().ctx().voidPromise());
    return callback;
}
 
Example #16
Source File: PersistentMessageFinderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
    PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
    messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
    messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
    messageMetadataBuilder.setSequenceId(1);
    PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
    ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());

    int msgMetadataSize = messageMetadata.getSerializedSize();
    int payloadSize = data.readableBytes();
    int totalSize = 4 + msgMetadataSize + payloadSize;

    ByteBuf headers = PulsarByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);
    messageMetadata.writeTo(outStream);
    ByteBuf headersAndPayload = ByteBufPair.coalesce(ByteBufPair.get(headers, data));
    byte[] byteMessage = headersAndPayload.nioBuffer().array();
    headersAndPayload.release();
    return byteMessage;
}
 
Example #17
Source File: OffsetFinderTest.java    From kop with Apache License 2.0 6 votes vote down vote up
public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
    PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
    messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
    messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
    messageMetadataBuilder.setSequenceId(1);
    PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
    ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());

    int msgMetadataSize = messageMetadata.getSerializedSize();
    int payloadSize = data.readableBytes();
    int totalSize = 4 + msgMetadataSize + payloadSize;

    ByteBuf headers = PulsarByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);
    messageMetadata.writeTo(outStream);
    ByteBuf headersAndPayload = ByteBufPair.coalesce(ByteBufPair.get(headers, data));
    byte[] byteMessage = headersAndPayload.nioBuffer().array();
    headersAndPayload.release();
    return byteMessage;
}
 
Example #18
Source File: CompressionCodecProvider.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static CompressionType convertFromWireProtocol(PulsarApi.CompressionType compressionType) {
    switch (compressionType) {
    case NONE:
        return CompressionType.NONE;
    case LZ4:
        return CompressionType.LZ4;
    case ZLIB:
        return CompressionType.ZLIB;
    case ZSTD:
        return CompressionType.ZSTD;
    case SNAPPY:
        return CompressionType.SNAPPY;

    default:
        throw new RuntimeException("Invalid compression type");
    }
}
 
Example #19
Source File: TestPulsarConnector.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static List<Entry> getTopicEntries(String topicSchemaName) {
    List<Entry> entries = new LinkedList<>();

    long count = topicsToNumEntries.get(topicSchemaName);
    for (int i=0 ; i < count; i++) {

        Foo foo = new Foo();
        foo.field1 = (int) count;
        foo.field2 = String.valueOf(count);
        foo.field3 = count;
        foo.field4 = count;
        foo.field5 = count % 2 == 0;
        foo.field6 = count;
        foo.timestamp = System.currentTimeMillis();

        LocalTime now = LocalTime.now(ZoneId.systemDefault());
        foo.time = now.toSecondOfDay() * 1000;

        LocalDate localDate = LocalDate.now();
        LocalDate epoch = LocalDate.ofEpochDay(0);
        foo.date = Math.toIntExact(ChronoUnit.DAYS.between(epoch, localDate));

        PulsarApi.MessageMetadata messageMetadata = PulsarApi.MessageMetadata.newBuilder()
                .setProducerName("test-producer").setSequenceId(i)
                .setPublishTime(currentTimeMs + i).build();

        Schema schema = topicsToSchemas.get(topicSchemaName).getType() == SchemaType.AVRO ? AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build()) : JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());

        ByteBuf payload = io.netty.buffer.Unpooled
                .copiedBuffer(schema.encode(foo));

        ByteBuf byteBuf = serializeMetadataAndPayload(
                Commands.ChecksumType.Crc32c, messageMetadata, payload);

        Entry entry = EntryImpl.create(0, i, byteBuf);
        log.info("create entry: %s", entry.getEntryId());
        entries.add(entry);
    }
    return entries;
}
 
Example #20
Source File: PulsarProducerInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    if (allArguments[0] != null) {
        ProducerEnhanceRequiredInfo requiredInfo = (ProducerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        ContextCarrier contextCarrier = new ContextCarrier();
        String topicName = requiredInfo.getTopic();
        AbstractSpan activeSpan = ContextManager.createExitSpan(OPERATE_NAME_PREFIX + topicName + PRODUCER_OPERATE_NAME_SUFFIX, contextCarrier, requiredInfo
            .getServiceUrl());
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getServiceUrl());
        Tags.MQ_TOPIC.set(activeSpan, topicName);
        SpanLayer.asMQ(activeSpan);
        activeSpan.setComponent(ComponentsDefine.PULSAR_PRODUCER);
        CarrierItem next = contextCarrier.items();
        MessageImpl msg = (MessageImpl) allArguments[0];
        while (next.hasNext()) {
            next = next.next();
            msg.getMessageBuilder()
               .addProperties(PulsarApi.KeyValue.newBuilder()
                                                .setKey(next.getHeadKey())
                                                .setValue(next.getHeadValue()));
        }
        if (allArguments.length > 1) {
            EnhancedInstance callbackInstance = (EnhancedInstance) allArguments[1];
            if (callbackInstance != null) {
                ContextSnapshot snapshot = ContextManager.capture();
                if (null != snapshot) {
                    SendCallbackEnhanceRequiredInfo callbackRequiredInfo = new SendCallbackEnhanceRequiredInfo();
                    callbackRequiredInfo.setTopic(topicName);
                    callbackRequiredInfo.setContextSnapshot(snapshot);
                    callbackInstance.setSkyWalkingDynamicField(callbackRequiredInfo);
                }
            }
        }
    }
}
 
Example #21
Source File: MessageDuplicationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public ByteBuf getMessage(String producerName, long seqId) {
    PulsarApi.MessageMetadata messageMetadata = PulsarApi.MessageMetadata.newBuilder()
            .setProducerName(producerName).setSequenceId(seqId)
            .setPublishTime(System.currentTimeMillis()).build();

    ByteBuf byteBuf = serializeMetadataAndPayload(
            Commands.ChecksumType.Crc32c, messageMetadata, io.netty.buffer.Unpooled.copiedBuffer(new byte[0]));

    return byteBuf;
}
 
Example #22
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private List<Message<byte[]>> getIndividualMsgsFromBatch(String topic, String msgId, byte[] data,
                             Map<String, String> properties, PulsarApi.MessageMetadata.Builder msgMetadataBuilder) {
    List<Message<byte[]>> ret = new ArrayList<>();
    int batchSize = Integer.parseInt(properties.get(BATCH_HEADER));
    ByteBuf buf = Unpooled.wrappedBuffer(data);
    for (int i = 0; i < batchSize; i++) {
        String batchMsgId = msgId + ":" + i;
        PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder = PulsarApi.SingleMessageMetadata
                .newBuilder();
        try {
            ByteBuf singleMessagePayload =
                    Commands.deSerializeSingleMessageInBatch(buf, singleMessageMetadataBuilder, i, batchSize);
            SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.build();
            if (singleMessageMetadata.getPropertiesCount() > 0) {
                for (KeyValue entry : singleMessageMetadata.getPropertiesList()) {
                    properties.put(entry.getKey(), entry.getValue());
                }
            }
            ret.add(new MessageImpl<>(topic, batchMsgId, properties, singleMessagePayload,
                    Schema.BYTES, msgMetadataBuilder));
        } catch (Exception ex) {
            log.error("Exception occured while trying to get BatchMsgId: {}", batchMsgId, ex);
        }
        singleMessageMetadataBuilder.recycle();
    }
    buf.release();
    return ret;
}
 
Example #23
Source File: ClientCnx.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleEndTxnResponse(PulsarApi.CommandEndTxnResponse command) {
    TransactionMetaStoreHandler handler = checkAndGetTransactionMetaStoreHandler(command.getTxnidMostBits());
    if (handler != null) {
        handler.handleEndTxnResponse(command);
    }
}
 
Example #24
Source File: ClientCnx.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleNewTxnResponse(PulsarApi.CommandNewTxnResponse command) {
    TransactionMetaStoreHandler handler = checkAndGetTransactionMetaStoreHandler(command.getTxnidMostBits());
    if (handler != null) {
        handler.handleNewTxnResponse(command);
    }
}
 
Example #25
Source File: ClientCnx.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleAddPartitionToTxnResponse(PulsarApi.CommandAddPartitionToTxnResponse command) {
    TransactionMetaStoreHandler handler = checkAndGetTransactionMetaStoreHandler(command.getTxnidMostBits());
    if (handler != null) {
        handler.handleAddPublishPartitionToTxnResponse(command);
    }
}
 
Example #26
Source File: MockBrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleCloseConsumer(PulsarApi.CommandCloseConsumer closeConsumer) {
    if (handleCloseConsumer != null) {
        handleCloseConsumer.apply(ctx, closeConsumer);
        return;
    }
    // default
    ctx.writeAndFlush(Commands.newSuccess(closeConsumer.getRequestId()));
}
 
Example #27
Source File: MockBrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleCloseProducer(PulsarApi.CommandCloseProducer closeProducer) {
    if (handleCloseProducer != null) {
        handleCloseProducer.apply(ctx, closeProducer);
        return;
    }
    // default
    ctx.writeAndFlush(Commands.newSuccess(closeProducer.getRequestId()));
}
 
Example #28
Source File: MockBrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleUnsubscribe(PulsarApi.CommandUnsubscribe unsubscribe) {
    if (handleUnsubscribe != null) {
        handleUnsubscribe.apply(ctx, unsubscribe);
        return;
    }
    // default
    ctx.writeAndFlush(Commands.newSuccess(unsubscribe.getRequestId()));
}
 
Example #29
Source File: MockBrokerService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleFlow(PulsarApi.CommandFlow flow) {
    if (handleFlow != null) {
        handleFlow.apply(ctx, flow);
    }
    // default: do nothing
}
 
Example #30
Source File: MessageIdImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName) throws IOException {
    checkNotNull(data);
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer(data, 0, data.length));
    PulsarApi.MessageIdData.Builder builder = PulsarApi.MessageIdData.newBuilder();

    PulsarApi.MessageIdData idData;
    try {
        idData = builder.mergeFrom(inputStream, null).build();
    } catch (UninitializedMessageException e) {
        throw new IOException(e);
    }

    MessageId messageId;
    if (idData.hasBatchIndex()) {
        messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(),
                idData.getBatchIndex());
    } else {
        messageId = new MessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition());
    }
    if (idData.getPartition() > -1 && topicName != null) {
        messageId = new TopicMessageIdImpl(
                topicName.getPartition(idData.getPartition()).toString(), topicName.toString(), messageId);
    }

    inputStream.recycle();
    builder.recycle();
    idData.recycle();
    return messageId;
}