/** * Remove an existing entry if found * * @param item1 * @return true if removed or false if item was not present */ public boolean remove(long item1, long item2) { checkBiggerEqualZero(item1); long h = hash(item1, item2); return getSection(h).remove(item1, item2, (int) h); }
/** * @return a new list of all keys (makes a copy) */ public Set<LongPair> items() { Set<LongPair> items = new HashSet<>(); forEach((item1, item2) -> items.add(new LongPair(item1, item2))); return items; }
private static void insertKeyValueNoLock(long[] table, int capacity, long item1, long item2) { int bucket = signSafeMod(hash(item1, item2), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item1; table[bucket + 1] = item2; return; } bucket = (bucket + 2) & (table.length - 1); } } }
if (!messagesToReplay.isEmpty()) { if (havePendingReplayRead) { log.debug("[{}] Skipping replay while awaiting previous read to complete", name); Set<PositionImpl> messagesToReplayNow = messagesToReplay.items(messagesToRead).stream() .map(pair -> new PositionImpl(pair.first, pair.second)).collect(toSet()); deletedMessages.forEach(position -> messagesToReplay.remove(((PositionImpl) position).getLedgerId(), ((PositionImpl) position).getEntryId()));
@Override public int hashCode() { return (int) hash(first, second); }
@Override public synchronized void redeliverUnacknowledgedMessages(Consumer consumer) { consumer.getPendingAcks().forEach((ledgerId, entryId, batchSize, none) -> { messagesToReplay.add(ledgerId, entryId); }); if (log.isDebugEnabled()) { log.debug("[{}-{}] Redelivering unacknowledged messages for consumer {}", name, consumer, messagesToReplay); } readMoreEntries(); }
@Override public synchronized void addConsumer(Consumer consumer) throws BrokerServiceException { if (IS_CLOSED_UPDATER.get(this) == TRUE) { log.warn("[{}] Dispatcher is already closed. Closing consumer ", name, consumer); consumer.disconnect(); return; } if (consumerList.isEmpty()) { if (havePendingRead || havePendingReplayRead) { // There is a pending read from previous run. We must wait for it to complete and then rewind shouldRewindBeforeReadingOrReplaying = true; } else { cursor.rewind(); shouldRewindBeforeReadingOrReplaying = false; } messagesToReplay.clear(); } if (isConsumersExceededOnTopic()) { log.warn("[{}] Attempting to add consumer to topic which reached max consumers limit", name); throw new ConsumerBusyException("Topic reached max consumers limit"); } if (isConsumersExceededOnSubscription()) { log.warn("[{}] Attempting to add consumer to subscription which reached max consumers limit", name); throw new ConsumerBusyException("Subscription reached max consumers limit"); } consumerList.add(consumer); consumerList.sort((c1, c2) -> c1.getPriorityLevel() - c2.getPriorityLevel()); consumerSet.add(consumer); }
public PersistentDispatcherMultipleConsumers(PersistentTopic topic, ManagedCursor cursor) { this.cursor = cursor; this.name = topic.getName() + " / " + Codec.decode(cursor.getName()); this.topic = topic; this.messagesToReplay = new ConcurrentLongPairSet(512, 2); this.redeliveryTracker = new InMemoryRedeliveryTracker(); this.readBatchSize = MaxReadBatchSize; this.maxUnackedMessages = topic.getBrokerService().pulsar().getConfiguration() .getMaxUnackedMessagesPerSubscription(); this.serviceConfig = topic.getBrokerService().pulsar().getConfiguration(); this.dispatchRateLimiter = null; }
private static void insertKeyValueNoLock(long[] table, int capacity, long item1, long item2) { int bucket = signSafeMod(hash(item1, item2), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item1; table[bucket + 1] = item2; return; } bucket = (bucket + 2) & (table.length - 1); } } }
private int removeIf(LongPairPredicate filter) { Objects.requireNonNull(filter); int removedItems = 0; // Go through all the buckets for this section for (int bucket = 0; bucket < table.length; bucket += 2) { long storedItem1 = table[bucket]; long storedItem2 = table[bucket + 1]; if (storedItem1 != DeletedItem && storedItem1 != EmptyItem) { if (filter.test(storedItem1, storedItem2)) { long h = hash(storedItem1, storedItem2); if (remove(storedItem1, storedItem2, (int) h)) { removedItems++; } } } } return removedItems; }
@Override public synchronized void redeliverUnacknowledgedMessages(Consumer consumer, List<PositionImpl> positions) { positions.forEach(position -> { messagesToReplay.add(position.getLedgerId(), position.getEntryId()); redeliveryTracker.incrementAndGetRedeliveryCount(position); }); if (log.isDebugEnabled()) { log.debug("[{}-{}] Redelivering unacknowledged messages for consumer {}", name, consumer, positions); } readMoreEntries(); }
public boolean add(long item1, long item2) { checkBiggerEqualZero(item1); long h = hash(item1, item2); return getSection(h).add(item1, item2, (int) h); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append('{'); final AtomicBoolean first = new AtomicBoolean(true); forEach((item1, item2) -> { if (!first.getAndSet(false)) { sb.append(", "); } sb.append('['); sb.append(item1); sb.append(':'); sb.append(item2); sb.append(']'); }); sb.append('}'); return sb.toString(); } }
@Override public int hashCode() { return (int) hash(first, second); }
public boolean contains(long item1, long item2) { checkBiggerEqualZero(item1); long h = hash(item1, item2); return getSection(h).contains(item1, item2, (int) h); }
/** * @return a new list of all keys (makes a copy) */ public Set<LongPair> items() { Set<LongPair> items = new HashSet<>(); forEach((item1, item2) -> items.add(new LongPair(item1, item2))); return items; }
private int removeIf(LongPairPredicate filter) { Objects.requireNonNull(filter); int removedItems = 0; // Go through all the buckets for this section for (int bucket = 0; bucket < table.length; bucket += 2) { long storedItem1 = table[bucket]; long storedItem2 = table[bucket + 1]; if (storedItem1 != DeletedItem && storedItem1 != EmptyItem) { if (filter.test(storedItem1, storedItem2)) { long h = hash(storedItem1, storedItem2); if (remove(storedItem1, storedItem2, (int) h)) { removedItems++; } } } } return removedItems; }
/** * Remove an existing entry if found * * @param item1 * @return true if removed or false if item was not present */ public boolean remove(long item1, long item2) { checkBiggerEqualZero(item1); long h = hash(item1, item2); return getSection(h).remove(item1, item2, (int) h); }