|
| 1 | +/* Unless explicitly stated otherwise all files in this repository are |
| 2 | + * licensed under the Apache 2.0 License. |
| 3 | + * |
| 4 | + * This product includes software developed at Datadog |
| 5 | + * (https://www.datadoghq.com/) Copyright 2026 Datadog, Inc. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.datadoghq.dogstatsd.http.forwarder; |
| 9 | + |
| 10 | +import java.util.Map; |
| 11 | +import java.util.TreeMap; |
| 12 | +import java.util.concurrent.locks.Condition; |
| 13 | +import java.util.concurrent.locks.Lock; |
| 14 | +import java.util.concurrent.locks.ReentrantLock; |
| 15 | + |
| 16 | +class BoundedQueue { |
| 17 | + // Key represents a tuple of integers (tries, clock). |
| 18 | + static class Key implements Comparable<Key> { |
| 19 | + final long tries; |
| 20 | + final long clock; |
| 21 | + |
| 22 | + Key(long clock) { |
| 23 | + this.tries = 0; |
| 24 | + this.clock = clock; |
| 25 | + } |
| 26 | + |
| 27 | + private Key(long tries, long clock) { |
| 28 | + this.tries = tries; |
| 29 | + this.clock = clock; |
| 30 | + } |
| 31 | + |
| 32 | + Key next() { |
| 33 | + return new Key(tries + 1, clock); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public int compareTo(Key o) { |
| 38 | + // Keys are ordered such first we try items with fewer |
| 39 | + // attempts, and then with a newer (larger) clock value. |
| 40 | + if (tries == o.tries) { |
| 41 | + return Long.compare(o.clock, clock); |
| 42 | + } |
| 43 | + return Long.compare(tries, o.tries); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + long clock = Long.MIN_VALUE; |
| 48 | + long bytes; |
| 49 | + final long maxBytes; |
| 50 | + final long maxTries; |
| 51 | + final WhenFull whenFull; |
| 52 | + |
| 53 | + final TreeMap<Key, byte[]> items = new TreeMap<>(); |
| 54 | + |
| 55 | + long droppedItems; |
| 56 | + long droppedBytes; |
| 57 | + |
| 58 | + Lock lock = new ReentrantLock(); |
| 59 | + Condition notEmpty = lock.newCondition(); |
| 60 | + Condition notFull = lock.newCondition(); |
| 61 | + |
| 62 | + BoundedQueue(long maxBytes, long maxTries, WhenFull whenFull) { |
| 63 | + this.maxBytes = maxBytes; |
| 64 | + this.maxTries = maxTries; |
| 65 | + this.whenFull = whenFull; |
| 66 | + } |
| 67 | + |
| 68 | + void add(byte[] item) throws InterruptedException { |
| 69 | + put(null, item, whenFull); |
| 70 | + } |
| 71 | + |
| 72 | + void requeue(Map.Entry<Key, byte[]> item) throws InterruptedException { |
| 73 | + Key nextKey = item.getKey().next(); |
| 74 | + if (nextKey.tries > maxTries) { |
| 75 | + droppedItems++; |
| 76 | + droppedBytes += item.getValue().length; |
| 77 | + return; |
| 78 | + } |
| 79 | + put(nextKey, item.getValue(), WhenFull.DROP); |
| 80 | + } |
| 81 | + |
| 82 | + // Must be called when lock is held. |
| 83 | + private Key newKey() { |
| 84 | + clock++; |
| 85 | + return new Key(clock); |
| 86 | + } |
| 87 | + |
| 88 | + private void put(Key key, byte[] item, WhenFull whenFull) throws InterruptedException { |
| 89 | + lock.lock(); |
| 90 | + try { |
| 91 | + if (key == null) { |
| 92 | + key = newKey(); |
| 93 | + } |
| 94 | + ensureSpace(item.length, whenFull); |
| 95 | + items.put(key, item); |
| 96 | + bytes += item.length; |
| 97 | + notEmpty.signal(); |
| 98 | + } finally { |
| 99 | + lock.unlock(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void ensureSpace(int length, WhenFull whenFull) throws InterruptedException { |
| 104 | + if (length > maxBytes) { |
| 105 | + throw new IllegalArgumentException("item length is larger than maxBytes"); |
| 106 | + } |
| 107 | + while (bytes + length > maxBytes) { |
| 108 | + switch (whenFull) { |
| 109 | + case DROP: |
| 110 | + Map.Entry<Key, byte[]> last = items.pollLastEntry(); |
| 111 | + droppedItems++; |
| 112 | + droppedBytes += last.getValue().length; |
| 113 | + bytes -= last.getValue().length; |
| 114 | + break; |
| 115 | + case BLOCK: |
| 116 | + notFull.await(); |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + Map.Entry<Key, byte[]> next() throws InterruptedException { |
| 123 | + lock.lock(); |
| 124 | + try { |
| 125 | + while (items.size() == 0) { |
| 126 | + notEmpty.await(); |
| 127 | + } |
| 128 | + Map.Entry<Key, byte[]> item = items.pollFirstEntry(); |
| 129 | + bytes -= item.getValue().length; |
| 130 | + notFull.signalAll(); |
| 131 | + return item; |
| 132 | + } finally { |
| 133 | + lock.unlock(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments