|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.service; |
| 20 | + |
| 21 | +import org.apache.pulsar.broker.service.BrokerServiceException.ConsumerAssignException; |
| 22 | +import org.apache.pulsar.common.util.Murmur3_32Hash; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Map.Entry; |
| 27 | +import java.util.concurrent.ConcurrentSkipListMap; |
| 28 | + |
| 29 | +/** |
| 30 | + * This is a consumer selector based fixed hash range. |
| 31 | + * |
| 32 | + * 1.Each consumer serves a fixed range of hash value |
| 33 | + * 2.The whole range of hash value could be covered by all the consumers. |
| 34 | + * 3.Once a consumer is removed, the left consumers could still serve the whole range. |
| 35 | + * |
| 36 | + * Initializing with a fixed hash range, by default 2 << 5. |
| 37 | + * First consumer added, hash range looks like: |
| 38 | + * |
| 39 | + * 0 -> 65536(consumer-1) |
| 40 | + * |
| 41 | + * Second consumer added, will find a biggest range to split: |
| 42 | + * |
| 43 | + * 0 -> 32768(consumer-2) -> 65536(consumer-1) |
| 44 | + * |
| 45 | + * While a consumer removed, The range for this consumer will be taken over |
| 46 | + * by other consumer, consumer-2 removed: |
| 47 | + * |
| 48 | + * 0 -> 65536(consumer-1) |
| 49 | + * |
| 50 | + * In this approach use skip list map to maintain the hash range and consumers. |
| 51 | + * |
| 52 | + * Select consumer will return the ceiling key of message key hashcode % range size. |
| 53 | + * |
| 54 | + */ |
| 55 | +public class HashRangeStickyKeyConsumerSelector implements StickyKeyConsumerSelector { |
| 56 | + |
| 57 | + public static final int DEFAULT_RANGE_SIZE = 2 << 15; |
| 58 | + |
| 59 | + private final int rangeSize; |
| 60 | + |
| 61 | + private final ConcurrentSkipListMap<Integer, Consumer> rangeMap; |
| 62 | + private final Map<Consumer, Integer> consumerRange; |
| 63 | + |
| 64 | + public HashRangeStickyKeyConsumerSelector() { |
| 65 | + this(DEFAULT_RANGE_SIZE); |
| 66 | + } |
| 67 | + |
| 68 | + public HashRangeStickyKeyConsumerSelector(int rangeSize) { |
| 69 | + if (rangeSize < 2) { |
| 70 | + throw new IllegalArgumentException("range size must greater than 2"); |
| 71 | + } |
| 72 | + if (!is2Power(rangeSize)) { |
| 73 | + throw new IllegalArgumentException("range size must be nth power with 2"); |
| 74 | + } |
| 75 | + this.rangeMap = new ConcurrentSkipListMap<>(); |
| 76 | + this.consumerRange = new HashMap<>(); |
| 77 | + this.rangeSize = rangeSize; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public synchronized void addConsumer(Consumer consumer) throws ConsumerAssignException { |
| 82 | + if (rangeMap.size() == 0) { |
| 83 | + rangeMap.put(rangeSize, consumer); |
| 84 | + consumerRange.put(consumer, rangeSize); |
| 85 | + } else { |
| 86 | + splitRange(findBiggestRange(), consumer); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public synchronized void removeConsumer(Consumer consumer) { |
| 92 | + Integer removeRange = consumerRange.get(consumer); |
| 93 | + if (removeRange != null) { |
| 94 | + if (removeRange == rangeSize && rangeMap.size() > 1) { |
| 95 | + Consumer lowerConsumer = rangeMap.lowerEntry(removeRange).getValue(); |
| 96 | + rangeMap.put(removeRange, lowerConsumer); |
| 97 | + consumerRange.put(lowerConsumer, removeRange); |
| 98 | + } else { |
| 99 | + rangeMap.remove(removeRange); |
| 100 | + consumerRange.remove(consumer); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Consumer select(byte[] stickyKey) { |
| 107 | + if (rangeMap.size() > 0) { |
| 108 | + int slot = Murmur3_32Hash.getInstance().makeHash(stickyKey) % rangeSize; |
| 109 | + return rangeMap.ceilingEntry(slot).getValue(); |
| 110 | + } else { |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private int findBiggestRange() { |
| 116 | + int slots = 0; |
| 117 | + int busiestRange = rangeSize; |
| 118 | + for (Entry<Integer, Consumer> entry : rangeMap.entrySet()) { |
| 119 | + Integer lowerKey = rangeMap.lowerKey(entry.getKey()); |
| 120 | + if (lowerKey == null) { |
| 121 | + lowerKey = 0; |
| 122 | + } |
| 123 | + if (entry.getKey() - lowerKey > slots) { |
| 124 | + slots = entry.getKey() - lowerKey; |
| 125 | + busiestRange = entry.getKey(); |
| 126 | + } |
| 127 | + } |
| 128 | + return busiestRange; |
| 129 | + } |
| 130 | + |
| 131 | + private void splitRange(int range, Consumer targetConsumer) throws ConsumerAssignException { |
| 132 | + Integer lowerKey = rangeMap.lowerKey(range); |
| 133 | + if (lowerKey == null) { |
| 134 | + lowerKey = 0; |
| 135 | + } |
| 136 | + if (range - lowerKey <= 1) { |
| 137 | + throw new ConsumerAssignException("No more range can assigned to new consumer, assigned consumers " |
| 138 | + + rangeMap.size()); |
| 139 | + } |
| 140 | + int splitRange = range - ((range - lowerKey) >> 1); |
| 141 | + rangeMap.put(splitRange, targetConsumer); |
| 142 | + consumerRange.put(targetConsumer, splitRange); |
| 143 | + } |
| 144 | + |
| 145 | + private boolean is2Power(int num) { |
| 146 | + if(num < 2) return false; |
| 147 | + return (num & num - 1) == 0; |
| 148 | + } |
| 149 | +} |
0 commit comments