forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIP-51] Introduce sticky consumer (apache#5388)
### Motivation Introduce sticky consumer, users can enable it by ```java client.newConsumer() .keySharedPolicy(KeySharedPolicy.exclusiveHashRange().hashRangeTotal(10).ranges(Range.of(0, 10))) .subscribe(); ``` ### Modifications Add a new consumer selector named HashRangeExclusiveStickyKeyConsumerSelector to support sticky consumer. This change added tests and can be verified as follows: Add new unit tests.
- Loading branch information
1 parent
c460f22
commit 951664c
Showing
31 changed files
with
5,688 additions
and
3,583 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...in/java/org/apache/pulsar/broker/service/HashRangeExclusiveStickyKeyConsumerSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* 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 org.apache.pulsar.common.api.proto.PulsarApi; | ||
import org.apache.pulsar.common.util.Murmur3_32Hash; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
|
||
public class HashRangeExclusiveStickyKeyConsumerSelector implements StickyKeyConsumerSelector { | ||
|
||
private final int rangeSize; | ||
private final ConcurrentSkipListMap<Integer, Consumer> rangeMap; | ||
|
||
public HashRangeExclusiveStickyKeyConsumerSelector() { | ||
this(DEFAULT_RANGE_SIZE); | ||
} | ||
|
||
public HashRangeExclusiveStickyKeyConsumerSelector(int rangeSize) { | ||
super(); | ||
if (rangeSize < 1) { | ||
throw new IllegalArgumentException("range size must greater than 0"); | ||
} | ||
this.rangeSize = rangeSize; | ||
this.rangeMap = new ConcurrentSkipListMap<>(); | ||
} | ||
|
||
@Override | ||
public void addConsumer(Consumer consumer) throws BrokerServiceException.ConsumerAssignException { | ||
validateKeySharedMeta(consumer); | ||
for (PulsarApi.IntRange intRange : consumer.getKeySharedMeta().getHashRangesList()) { | ||
rangeMap.put(intRange.getStart(), consumer); | ||
rangeMap.put(intRange.getEnd(), consumer); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeConsumer(Consumer consumer) { | ||
rangeMap.entrySet().removeIf(entry -> entry.getValue().equals(consumer)); | ||
} | ||
|
||
@Override | ||
public Consumer select(byte[] stickyKey) { | ||
return select(Murmur3_32Hash.getInstance().makeHash(stickyKey)); | ||
} | ||
|
||
public Consumer select(int hash) { | ||
if (rangeMap.size() > 0) { | ||
int slot = hash % rangeSize; | ||
Map.Entry<Integer, Consumer> ceilingEntry = rangeMap.ceilingEntry(slot); | ||
Map.Entry<Integer, Consumer> floorEntry = rangeMap.floorEntry(slot); | ||
Consumer ceilingConsumer = ceilingEntry != null ? ceilingEntry.getValue() : null; | ||
Consumer floorConsumer = floorEntry != null ? floorEntry.getValue() : null; | ||
if (floorConsumer != null && floorConsumer.equals(ceilingConsumer)) { | ||
return ceilingConsumer; | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private void validateKeySharedMeta(Consumer consumer) throws BrokerServiceException.ConsumerAssignException { | ||
if (consumer.getKeySharedMeta() == null) { | ||
throw new BrokerServiceException.ConsumerAssignException("Must specify key shared meta for consumer."); | ||
} | ||
List<PulsarApi.IntRange> ranges = consumer.getKeySharedMeta().getHashRangesList(); | ||
if (ranges.isEmpty()) { | ||
throw new BrokerServiceException.ConsumerAssignException("Ranges for KeyShared policy must not be empty."); | ||
} | ||
for (PulsarApi.IntRange intRange : ranges) { | ||
|
||
if (intRange.getStart() > intRange.getEnd()) { | ||
throw new BrokerServiceException.ConsumerAssignException("Fixed hash range start > end"); | ||
} | ||
|
||
Map.Entry<Integer, Consumer> ceilingEntry = rangeMap.ceilingEntry(intRange.getStart()); | ||
Map.Entry<Integer, Consumer> floorEntry = rangeMap.floorEntry(intRange.getEnd()); | ||
|
||
if (floorEntry != null && floorEntry.getKey() >= intRange.getStart()) { | ||
throw new BrokerServiceException.ConsumerAssignException("Range conflict with consumer " + floorEntry.getValue()); | ||
} | ||
|
||
if (ceilingEntry != null && ceilingEntry.getKey() <= intRange.getEnd()) { | ||
throw new BrokerServiceException.ConsumerAssignException("Range conflict with consumer " + ceilingEntry.getValue()); | ||
} | ||
|
||
if (ceilingEntry != null && floorEntry != null && ceilingEntry.getValue().equals(floorEntry.getValue())) { | ||
throw new BrokerServiceException.ConsumerAssignException("Range conflict with consumer " + ceilingEntry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
Map<Integer, Consumer> getRangeConsumer() { | ||
return Collections.unmodifiableMap(rangeMap); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.