use of org.apache.pulsar.client.api.Range in project pulsar by apache.
the class HashRangeExclusiveStickyKeyConsumerSelectorTest method testGetConsumerKeyHashRangesWithSameConsumerName.
@Test
public void testGetConsumerKeyHashRangesWithSameConsumerName() throws Exception {
HashRangeExclusiveStickyKeyConsumerSelector selector = new HashRangeExclusiveStickyKeyConsumerSelector(10);
final String consumerName = "My-consumer";
List<int[]> range = Arrays.asList(new int[] { 0, 2 }, new int[] { 3, 7 }, new int[] { 9, 12 });
List<Consumer> consumers = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Consumer consumer = mock(Consumer.class);
KeySharedMeta keySharedMeta = new KeySharedMeta().setKeySharedMode(KeySharedMode.STICKY);
keySharedMeta.addHashRange().setStart(range.get(i)[0]).setEnd(range.get(i)[1]);
when(consumer.getKeySharedMeta()).thenReturn(keySharedMeta);
when(consumer.consumerName()).thenReturn(consumerName);
Assert.assertEquals(consumer.getKeySharedMeta(), keySharedMeta);
selector.addConsumer(consumer);
consumers.add(consumer);
}
List<Range> prev = null;
for (Consumer consumer : consumers) {
List<Range> ranges = selector.getConsumerKeyHashRanges().get(consumer);
Assert.assertEquals(ranges.size(), 1);
if (prev != null) {
Assert.assertNotEquals(prev, ranges);
}
prev = ranges;
}
}
use of org.apache.pulsar.client.api.Range in project pulsar by apache.
the class ReaderBuilderImpl method keyHashRange.
@Override
public ReaderBuilder<T> keyHashRange(Range... ranges) {
checkArgument(ranges != null && ranges.length > 0, "Cannot specify a null ofr an empty key hash ranges for a reader");
for (int i = 0; i < ranges.length; i++) {
Range range1 = ranges[i];
if (range1.getStart() < 0 || range1.getEnd() > DEFAULT_HASH_RANGE_SIZE) {
throw new IllegalArgumentException("Ranges must be [0, 65535] but provided range is " + range1);
}
for (int j = 0; j < ranges.length; j++) {
Range range2 = ranges[j];
if (i != j && range1.intersect(range2) != null) {
throw new IllegalArgumentException("Key hash ranges with overlap between " + range1 + " and " + range2);
}
}
}
conf.setKeyHashRanges(Arrays.asList(ranges));
return this;
}
use of org.apache.pulsar.client.api.Range in project pulsar by apache.
the class Commands method newSubscribe.
public static ByteBuf newSubscribe(String topic, String subscription, long consumerId, long requestId, SubType subType, int priorityLevel, String consumerName, boolean isDurable, MessageIdData startMessageId, Map<String, String> metadata, boolean readCompacted, boolean isReplicated, InitialPosition subscriptionInitialPosition, long startMessageRollbackDurationInSec, SchemaInfo schemaInfo, boolean createTopicIfDoesNotExist, KeySharedPolicy keySharedPolicy, Map<String, String> subscriptionProperties, long consumerEpoch) {
BaseCommand cmd = localCmd(Type.SUBSCRIBE);
CommandSubscribe subscribe = cmd.setSubscribe().setTopic(topic).setSubscription(subscription).setSubType(subType).setConsumerId(consumerId).setConsumerName(consumerName).setRequestId(requestId).setPriorityLevel(priorityLevel).setDurable(isDurable).setReadCompacted(readCompacted).setInitialPosition(subscriptionInitialPosition).setReplicateSubscriptionState(isReplicated).setForceTopicCreation(createTopicIfDoesNotExist).setConsumerEpoch(consumerEpoch);
if (subscriptionProperties != null && !subscriptionProperties.isEmpty()) {
List<KeyValue> keyValues = new ArrayList<>();
subscriptionProperties.forEach((key, value) -> {
KeyValue keyValue = new KeyValue();
keyValue.setKey(key);
keyValue.setValue(value);
keyValues.add(keyValue);
});
subscribe.addAllSubscriptionProperties(keyValues);
}
if (keySharedPolicy != null) {
KeySharedMeta keySharedMeta = subscribe.setKeySharedMeta();
keySharedMeta.setAllowOutOfOrderDelivery(keySharedPolicy.isAllowOutOfOrderDelivery());
keySharedMeta.setKeySharedMode(convertKeySharedMode(keySharedPolicy.getKeySharedMode()));
if (keySharedPolicy instanceof KeySharedPolicy.KeySharedPolicySticky) {
List<Range> ranges = ((KeySharedPolicy.KeySharedPolicySticky) keySharedPolicy).getRanges();
for (Range range : ranges) {
IntRange r = keySharedMeta.addHashRange();
r.setStart(range.getStart());
r.setEnd(range.getEnd());
}
}
}
if (startMessageId != null) {
subscribe.setStartMessageId().copyFrom(startMessageId);
}
if (startMessageRollbackDurationInSec > 0) {
subscribe.setStartMessageRollbackDurationSec(startMessageRollbackDurationInSec);
}
if (!metadata.isEmpty()) {
metadata.entrySet().forEach(e -> subscribe.addMetadata().setKey(e.getKey()).setValue(e.getValue()));
}
if (schemaInfo != null) {
if (subscribe.hasSchema()) {
throw new IllegalStateException();
}
if (subscribe.setSchema().getPropertiesCount() > 0) {
throw new IllegalStateException();
}
convertSchema(schemaInfo, subscribe.setSchema());
}
return serializeWithSize(cmd);
}
use of org.apache.pulsar.client.api.Range in project pulsar-flink by streamnative.
the class PulsarMetadataReader method buildRange.
private SerializableRange buildRange(Map<String, String> caseInsensitiveParams) {
if (numParallelSubtasks <= 0 || indexOfThisSubtask < 0) {
return SerializableRange.ofFullRange();
}
if (caseInsensitiveParams == null || caseInsensitiveParams.isEmpty() || !caseInsensitiveParams.containsKey(ENABLE_KEY_HASH_RANGE_KEY)) {
return SerializableRange.ofFullRange();
}
final String enableKeyHashRange = caseInsensitiveParams.get(ENABLE_KEY_HASH_RANGE_KEY);
if (!Boolean.parseBoolean(enableKeyHashRange)) {
return SerializableRange.ofFullRange();
}
final Range range = SourceSinkUtils.distributeRange(numParallelSubtasks, indexOfThisSubtask);
return SerializableRange.of(range);
}
use of org.apache.pulsar.client.api.Range in project pulsar-flink by streamnative.
the class SourceSinkTest method testRange.
private void testRange(int countOfSubTasks) {
// log.info("test pulsar range {}", countOfSubTasks);
List<Range> ranges = new ArrayList<>(countOfSubTasks);
for (int i = 0; i < countOfSubTasks; i++) {
ranges.add(SourceSinkUtils.distributeRange(countOfSubTasks, i));
}
// log.info(ranges.toString());
Collections.sort(ranges, Comparator.comparingInt(Range::getStart));
Assert.assertEquals(ranges.get(0).getStart(), 0);
Assert.assertEquals(ranges.get(ranges.size() - 1).getEnd(), SerializableRange.FULL_RANGE_END);
for (int i = 1; i < ranges.size(); i++) {
final Range range = ranges.get(i - 1);
final Range currentRange = ranges.get(i);
final String message = MessageFormat.format("countOfSubTasks {0} / indexOfSubTasks {1} \n all range {2}", countOfSubTasks, i, ranges);
Assert.assertEquals(message, range.getEnd() + 1, currentRange.getStart());
}
}
Aggregations