Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved entries filtering from consumer to dispatcher #4329

Merged
merged 5 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.pulsar.broker.service;

import static com.google.common.base.Preconditions.checkArgument;

import io.netty.buffer.ByteBuf;
import io.netty.util.concurrent.FastThreadLocal;

import java.util.Collections;
import java.util.List;

import org.apache.bookkeeper.mledger.Entry;
import org.apache.bookkeeper.mledger.impl.PositionImpl;
import org.apache.pulsar.common.api.Commands;
import org.apache.pulsar.common.api.proto.PulsarApi.CommandAck.AckType;
import org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata;

public abstract class AbstractBaseDispatcher {

protected final Subscription subscription;

protected AbstractBaseDispatcher(Subscription subscription) {
this.subscription = subscription;
}

/**
* Filter messages that are being sent to a consumers.
* <p>
* Messages can be filtered out for multiple reasons:
* <ul>
* <li>Checksum or metadata corrupted
* <li>Message is an internal marker
* <li>Message is not meant to be delivered immediately
* </ul>
*
* @param entries
* a list of entries as read from storage
*
* @param batchSizes
* an array where the batch size for each entry (the number of messages within an entry) is stored. This
* array needs to be of at least the same size as the entries list
*
* @param sendMessageInfo
* an object where the total size in messages and bytes will be returned back to the caller
* @param subscription
* the subscription object
*/
public void filterEntriesForConsumer(List<Entry> entries, int[] batchSizes, SendMessageInfo sendMessageInfo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on what bases are we going to filter the entry? do we have plan to pass Consumer to apply function for filtering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current plan is to have a general way to filter out messages for (corrupted, delayed messages, internal markers).

It could be also used, in future, to do some other filtering as well, though that's not the intention here to generalize for that.

checkArgument(batchSizes.length >= entries.size());
int totalMessages = 0;
long totalBytes = 0;

for (int i = 0, entriesSize = entries.size(); i < entriesSize; i++) {
Entry entry = entries.get(i);
ByteBuf metadataAndPayload = entry.getDataBuffer();
PositionImpl pos = (PositionImpl) entry.getPosition();

MessageMetadata msgMetadata = Commands.peekMessageMetadata(metadataAndPayload, subscription.toString(), -1);

try {
if (msgMetadata == null) {
// Message metadata was corrupted
entries.set(i, null);
entry.release();
subscription.acknowledgeMessage(Collections.singletonList(pos), AckType.Individual,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add debug log or may be info log because it shouldn't happen often.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already logged in the metadata parsing method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need to do this on subscription? The operation is to tell the managed ledger to not give us this message again. dispatchers have a handle on the managed ledger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely, yes, though technically it's only true for PersistentDispatcher. This asbtract method it's shared across all the types of dispatchers, hence it goes though the Subscription abstraction.

Collections.emptyMap());
continue;
}

int batchSize = msgMetadata.getNumMessagesInBatch();
totalMessages += batchSize;
totalBytes += metadataAndPayload.readableBytes();
batchSizes[i] = batchSize;
} finally {
msgMetadata.recycle();
}
}

sendMessageInfo.setTotalMessages(totalMessages);
sendMessageInfo.setTotalBytes(totalBytes);
}

private static final FastThreadLocal<int[]> threadLocalBatchSizes = new FastThreadLocal<int[]>() {
protected int[] initialValue() throws Exception {
return new int[100];
};
};

protected int[] getThreadLocalBatchSizes(int entries) {
int[] a = threadLocalBatchSizes.get();
if (a.length < entries) {
a = new int[entries];
threadLocalBatchSizes.set(a);
}

return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
*/
public abstract class AbstractDispatcherMultipleConsumers {
public abstract class AbstractDispatcherMultipleConsumers extends AbstractBaseDispatcher {

protected final CopyOnWriteArrayList<Consumer> consumerList = new CopyOnWriteArrayList<>();
protected final ObjectSet<Consumer> consumerSet = new ObjectHashSet<>();
Expand All @@ -39,6 +39,11 @@ public abstract class AbstractDispatcherMultipleConsumers {
protected static final AtomicIntegerFieldUpdater<AbstractDispatcherMultipleConsumers> IS_CLOSED_UPDATER = AtomicIntegerFieldUpdater
.newUpdater(AbstractDispatcherMultipleConsumers.class, "isClosed");
private volatile int isClosed = FALSE;

protected AbstractDispatcherMultipleConsumers(Subscription subscription) {
super(subscription);
}

public boolean isConsumerConnected() {
return !consumerList.isEmpty();
}
Expand Down Expand Up @@ -127,7 +132,7 @@ public Consumer getNextConsumer() {

/**
* Finds index of first available consumer which has higher priority then given targetPriority
*
*
* @param targetPriority
* @return -1 if couldn't find any available consumer
*/
Expand Down Expand Up @@ -187,7 +192,7 @@ private int getNextConsumerFromSameOrLowerLevel(int currentRoundRobinIndex) {

/**
* Finds index of first consumer in list which has same priority as given targetPriority
*
*
* @param targetPriority
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractDispatcherSingleActiveConsumer {
public abstract class AbstractDispatcherSingleActiveConsumer extends AbstractBaseDispatcher {

protected final String topicName;
protected static final AtomicReferenceFieldUpdater<AbstractDispatcherSingleActiveConsumer, Consumer> ACTIVE_CONSUMER_UPDATER =
Expand All @@ -53,7 +53,8 @@ public abstract class AbstractDispatcherSingleActiveConsumer {
private volatile int isClosed = FALSE;

public AbstractDispatcherSingleActiveConsumer(SubType subscriptionType, int partitionIndex,
String topicName) {
String topicName, Subscription subscription) {
super(subscription);
this.topicName = topicName;
this.consumers = new CopyOnWriteArrayList<>();
this.partitionIndex = partitionIndex;
Expand Down
Loading