|
| 1 | +package tech.ydb.yoj.repository.ydb.spliterator; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import tech.ydb.yoj.ExperimentalApi; |
| 7 | +import tech.ydb.yoj.repository.db.exception.DeadlineExceededException; |
| 8 | +import tech.ydb.yoj.repository.db.exception.QueryInterruptedException; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.ArrayDeque; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.concurrent.locks.Condition; |
| 14 | +import java.util.concurrent.locks.Lock; |
| 15 | +import java.util.concurrent.locks.ReentrantLock; |
| 16 | + |
| 17 | +@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/42") |
| 18 | +/* package */ final class YdbSpliteratorQueue<V> { |
| 19 | + private static final Logger log = LoggerFactory.getLogger(YdbSpliteratorQueue.class); |
| 20 | + |
| 21 | + private static final SupplierStatus UNDONE_SUPPLIER_STATUS = () -> false; |
| 22 | + |
| 23 | + private final int maxQueueSize; |
| 24 | + private final ArrayDeque<V> queue; |
| 25 | + private final long streamWorkDeadlineNanos; |
| 26 | + |
| 27 | + private final Lock lock = new ReentrantLock(); |
| 28 | + private final Condition newElement = lock.newCondition(); |
| 29 | + private final Condition queueIsNotFull = lock.newCondition(); |
| 30 | + |
| 31 | + private SupplierStatus supplierStatus = UNDONE_SUPPLIER_STATUS; |
| 32 | + private boolean closed = false; |
| 33 | + |
| 34 | + public YdbSpliteratorQueue(int maxQueueSize, Duration streamWorkTimeout) { |
| 35 | + Preconditions.checkArgument(maxQueueSize > 0, "maxQueueSize must be greater than 0"); |
| 36 | + this.maxQueueSize = maxQueueSize; |
| 37 | + this.queue = new ArrayDeque<>(maxQueueSize); |
| 38 | + this.streamWorkDeadlineNanos = System.nanoTime() + TimeUnit.NANOSECONDS.toNanos(saturatedToNanos(streamWorkTimeout)); |
| 39 | + } |
| 40 | + |
| 41 | + public boolean onNext(V value) { |
| 42 | + Preconditions.checkState(supplierStatus.equals(UNDONE_SUPPLIER_STATUS), |
| 43 | + "can't call onNext after supplierDone" |
| 44 | + ); |
| 45 | + |
| 46 | + lock.lock(); |
| 47 | + try { |
| 48 | + if (!awaitFreeSpaceLocked()) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + queue.add(value); |
| 53 | + |
| 54 | + newElement.signal(); |
| 55 | + } finally { |
| 56 | + lock.unlock(); |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + public boolean awaitFreeSpace() { |
| 63 | + Preconditions.checkState(supplierStatus.equals(UNDONE_SUPPLIER_STATUS), |
| 64 | + "can't call onNext after supplierDone" |
| 65 | + ); |
| 66 | + |
| 67 | + lock.lock(); |
| 68 | + try { |
| 69 | + return awaitFreeSpaceLocked(); |
| 70 | + } finally { |
| 71 | + lock.unlock(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private boolean awaitFreeSpaceLocked() { |
| 76 | + if (closed) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (queue.size() != maxQueueSize) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + if (!queueIsNotFull.await(calculateTimeout(), TimeUnit.NANOSECONDS)) { |
| 86 | + throw new OfferDeadlineExceededException(); |
| 87 | + } |
| 88 | + } catch (InterruptedException e) { |
| 89 | + Thread.currentThread().interrupt(); |
| 90 | + throw new QueryInterruptedException("Supplier thread interrupted", e); |
| 91 | + } |
| 92 | + |
| 93 | + return !closed; |
| 94 | + } |
| 95 | + |
| 96 | + // (supplier thread) Send knowledge to stream when data is over. |
| 97 | + public void supplierDone(SupplierStatus status) { |
| 98 | + lock.lock(); |
| 99 | + try { |
| 100 | + if (closed) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + supplierStatus = status; |
| 105 | + |
| 106 | + newElement.signal(); |
| 107 | + } finally { |
| 108 | + lock.unlock(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public boolean isClosed() { |
| 113 | + lock.lock(); |
| 114 | + try { |
| 115 | + return closed; |
| 116 | + } finally { |
| 117 | + lock.unlock(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public V poll() { |
| 122 | + lock.lock(); |
| 123 | + try { |
| 124 | + if (closed) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + if (queue.isEmpty()) { |
| 129 | + if (supplierStatus.isDone()) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + if (!newElement.await(calculateTimeout(), TimeUnit.NANOSECONDS)) { |
| 135 | + log.warn("Supplier thread was closed because consumer didn't poll an element of stream on timeout"); |
| 136 | + throw new DeadlineExceededException("Stream deadline exceeded on poll"); |
| 137 | + } |
| 138 | + } catch (InterruptedException e) { |
| 139 | + Thread.currentThread().interrupt(); |
| 140 | + throw new QueryInterruptedException("Consumer thread interrupted", e); |
| 141 | + } |
| 142 | + |
| 143 | + if (closed || supplierStatus.isDone()) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + V value = queue.pop(); |
| 149 | + |
| 150 | + queueIsNotFull.signal(); |
| 151 | + |
| 152 | + return value; |
| 153 | + } finally { |
| 154 | + lock.unlock(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public void close() { |
| 159 | + lock.lock(); |
| 160 | + try { |
| 161 | + if (closed) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + closed = true; |
| 166 | + |
| 167 | + queueIsNotFull.signal(); |
| 168 | + newElement.signalAll(); |
| 169 | + } finally { |
| 170 | + lock.unlock(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private long calculateTimeout() { |
| 175 | + return TimeUnit.NANOSECONDS.toNanos(streamWorkDeadlineNanos - System.nanoTime()); |
| 176 | + } |
| 177 | + |
| 178 | + public static final class OfferDeadlineExceededException extends RuntimeException { |
| 179 | + } |
| 180 | + |
| 181 | + // copy-paste from com.google.common.util.concurrent.Uninterruptibles |
| 182 | + private static long saturatedToNanos(Duration duration) { |
| 183 | + try { |
| 184 | + return duration.toNanos(); |
| 185 | + } catch (ArithmeticException ignore) { |
| 186 | + return duration.isNegative() ? -9223372036854775808L : 9223372036854775807L; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @FunctionalInterface |
| 191 | + public interface SupplierStatus { |
| 192 | + boolean isDone(); |
| 193 | + } |
| 194 | +} |
0 commit comments