use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.
the class Consumer method individualAckWithTransaction.
// this method is for individual ack carry the transaction
private CompletableFuture<Void> individualAckWithTransaction(CommandAck ack) {
// Individual ack
List<MutablePair<PositionImpl, Integer>> positionsAcked = new ArrayList<>();
if (!isTransactionEnabled()) {
return FutureUtil.failedFuture(new BrokerServiceException.NotAllowedException("Server don't support transaction ack!"));
}
for (int i = 0; i < ack.getMessageIdsCount(); i++) {
MessageIdData msgId = ack.getMessageIdAt(i);
PositionImpl position;
long ackedCount = 0;
long batchSize = getBatchSize(msgId);
Consumer ackOwnerConsumer = getAckOwnerConsumer(msgId.getLedgerId(), msgId.getEntryId());
if (msgId.getAckSetsCount() > 0) {
long[] ackSets = new long[msgId.getAckSetsCount()];
for (int j = 0; j < msgId.getAckSetsCount(); j++) {
ackSets[j] = msgId.getAckSetAt(j);
}
position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), ackSets);
ackedCount = getAckedCountForTransactionAck(batchSize, ackSets);
} else {
position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
ackedCount = batchSize;
}
positionsAcked.add(new MutablePair<>(position, (int) batchSize));
addAndGetUnAckedMsgs(ackOwnerConsumer, -(int) ackedCount);
checkCanRemovePendingAcksAndHandle(position, msgId);
checkAckValidationError(ack, position);
}
CompletableFuture<Void> completableFuture = transactionIndividualAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked);
if (Subscription.isIndividualAckMode(subType)) {
completableFuture.whenComplete((v, e) -> positionsAcked.forEach(positionLongMutablePair -> {
if (positionLongMutablePair.getLeft().getAckSet() != null) {
if (((PersistentSubscription) subscription).checkIsCanDeleteConsumerPendingAck(positionLongMutablePair.left)) {
removePendingAcks(positionLongMutablePair.left);
}
}
}));
}
return completableFuture;
}
use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.
the class Commands method newMultiMessageAckCommon.
private static BaseCommand newMultiMessageAckCommon(List<Triple<Long, Long, ConcurrentBitSetRecyclable>> entries) {
BaseCommand cmd = localCmd(Type.ACK);
CommandAck ack = cmd.setAck();
int entriesCount = entries.size();
for (int i = 0; i < entriesCount; i++) {
long ledgerId = entries.get(i).getLeft();
long entryId = entries.get(i).getMiddle();
ConcurrentBitSetRecyclable bitSet = entries.get(i).getRight();
MessageIdData msgId = ack.addMessageId().setLedgerId(ledgerId).setEntryId(entryId);
if (bitSet != null) {
long[] ackSet = bitSet.toLongArray();
for (int j = 0; j < ackSet.length; j++) {
msgId.addAckSet(ackSet[j]);
}
bitSet.recycle();
}
}
return cmd;
}
use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.
the class Consumer method individualAckNormal.
// this method is for individual ack not carry the transaction
private CompletableFuture<Void> individualAckNormal(CommandAck ack, Map<String, Long> properties) {
List<Position> positionsAcked = new ArrayList<>();
for (int i = 0; i < ack.getMessageIdsCount(); i++) {
MessageIdData msgId = ack.getMessageIdAt(i);
PositionImpl position;
long ackedCount = 0;
long batchSize = getBatchSize(msgId);
Consumer ackOwnerConsumer = getAckOwnerConsumer(msgId.getLedgerId(), msgId.getEntryId());
if (msgId.getAckSetsCount() > 0) {
long[] ackSets = new long[msgId.getAckSetsCount()];
for (int j = 0; j < msgId.getAckSetsCount(); j++) {
ackSets[j] = msgId.getAckSetAt(j);
}
position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), ackSets);
ackedCount = getAckedCountForBatchIndexLevelEnabled(position, batchSize, ackSets);
if (isTransactionEnabled()) {
// sync the batch position bit set point, in order to delete the position in pending acks
if (Subscription.isIndividualAckMode(subType)) {
((PersistentSubscription) subscription).syncBatchPositionBitSetForPendingAck(position);
}
}
} else {
position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
ackedCount = getAckedCountForMsgIdNoAckSets(batchSize, position);
}
addAndGetUnAckedMsgs(ackOwnerConsumer, -(int) ackedCount);
positionsAcked.add(position);
checkCanRemovePendingAcksAndHandle(position, msgId);
checkAckValidationError(ack, position);
}
subscription.acknowledgeMessage(positionsAcked, AckType.Individual, properties);
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
completableFuture.complete(null);
if (isTransactionEnabled() && Subscription.isIndividualAckMode(subType)) {
completableFuture.whenComplete((v, e) -> positionsAcked.forEach(position -> {
// the bit set is empty in pending ack handle.
if (((PositionImpl) position).getAckSet() != null) {
if (((PersistentSubscription) subscription).checkIsCanDeleteConsumerPendingAck((PositionImpl) position)) {
removePendingAcks((PositionImpl) position);
}
}
}));
}
return completableFuture;
}
use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.
the class Commands method newAck.
public static ByteBuf newAck(long consumerId, long ledgerId, long entryId, BitSetRecyclable ackSet, AckType ackType, ValidationError validationError, Map<String, Long> properties, long txnIdLeastBits, long txnIdMostBits, long requestId, int batchSize) {
BaseCommand cmd = localCmd(Type.ACK);
CommandAck ack = cmd.setAck().setConsumerId(consumerId).setAckType(ackType);
MessageIdData messageIdData = ack.addMessageId().setLedgerId(ledgerId).setEntryId(entryId);
if (ackSet != null) {
long[] as = ackSet.toLongArray();
for (int i = 0; i < as.length; i++) {
messageIdData.addAckSet(as[i]);
}
}
if (batchSize >= 0) {
messageIdData.setBatchSize(batchSize);
}
if (validationError != null) {
ack.setValidationError(validationError);
}
if (txnIdMostBits >= 0) {
ack.setTxnidMostBits(txnIdMostBits);
}
if (txnIdLeastBits >= 0) {
ack.setTxnidLeastBits(txnIdLeastBits);
}
if (requestId >= 0) {
ack.setRequestId(requestId);
}
if (!properties.isEmpty()) {
properties.forEach((k, v) -> {
ack.addProperty().setKey(k).setValue(v);
});
}
return serializeWithSize(cmd);
}
use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.
the class BrokerInterceptorWithClassLoaderTest method testClassLoaderSwitcher.
@Test
public void testClassLoaderSwitcher() throws Exception {
NarClassLoader narLoader = mock(NarClassLoader.class);
BrokerInterceptor interceptor = new BrokerInterceptor() {
@Override
public void beforeSendMessage(Subscription subscription, Entry entry, long[] ackSet, MessageMetadata msgMetadata) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onConnectionCreated(ServerCnx cnx) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void producerCreated(ServerCnx cnx, Producer producer, Map<String, String> metadata) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void consumerCreated(ServerCnx cnx, Consumer consumer, Map<String, String> metadata) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void messageProduced(ServerCnx cnx, Producer producer, long startTimeNs, long ledgerId, long entryId, Topic.PublishContext publishContext) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void messageDispatched(ServerCnx cnx, Consumer consumer, long ledgerId, long entryId, ByteBuf headersAndPayload) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void messageAcked(ServerCnx cnx, Consumer consumer, CommandAck ackCmd) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onPulsarCommand(BaseCommand command, ServerCnx cnx) throws InterceptException {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onConnectionClosed(ServerCnx cnx) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onWebserviceRequest(ServletRequest request) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onWebserviceResponse(ServletRequest request, ServletResponse response) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void onFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void initialize(PulsarService pulsarService) throws Exception {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
@Override
public void close() {
assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
}
};
BrokerInterceptorWithClassLoader brokerInterceptorWithClassLoader = new BrokerInterceptorWithClassLoader(interceptor, narLoader);
ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
// test class loader
assertEquals(brokerInterceptorWithClassLoader.getClassLoader(), narLoader);
// test initialize
brokerInterceptorWithClassLoader.initialize(mock(PulsarService.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onFilter
brokerInterceptorWithClassLoader.onFilter(mock(ServletRequest.class), mock(ServletResponse.class), mock(FilterChain.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onWebserviceResponse
brokerInterceptorWithClassLoader.onWebserviceResponse(mock(ServletRequest.class), mock(ServletResponse.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onWebserviceRequest
brokerInterceptorWithClassLoader.onWebserviceRequest(mock(ServletRequest.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onConnectionClosed
brokerInterceptorWithClassLoader.onConnectionClosed(mock(ServerCnx.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onPulsarCommand
brokerInterceptorWithClassLoader.onPulsarCommand(null, mock(ServerCnx.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test messageAcked
brokerInterceptorWithClassLoader.messageAcked(mock(ServerCnx.class), mock(Consumer.class), null);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test messageDispatched
brokerInterceptorWithClassLoader.messageDispatched(mock(ServerCnx.class), mock(Consumer.class), 1, 1, null);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test messageProduced
brokerInterceptorWithClassLoader.messageProduced(mock(ServerCnx.class), mock(Producer.class), 1, 1, 1, null);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test consumerCreated
brokerInterceptorWithClassLoader.consumerCreated(mock(ServerCnx.class), mock(Consumer.class), Maps.newHashMap());
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test producerCreated
brokerInterceptorWithClassLoader.producerCreated(mock(ServerCnx.class), mock(Producer.class), Maps.newHashMap());
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test onConnectionCreated
brokerInterceptorWithClassLoader.onConnectionCreated(mock(ServerCnx.class));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test beforeSendMessage
brokerInterceptorWithClassLoader.beforeSendMessage(mock(Subscription.class), mock(Entry.class), null, null);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
// test close
brokerInterceptorWithClassLoader.close();
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Aggregations