-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 1 commit
dcfa102
606808f
e6048bf
d589d70
1b0cf9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already logged in the metadata parsing method There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.