presto-raptor-legacy/src/main/java/io/prestosql/plugin/raptor/legacy/metadata/ShardCleaner.java/ShardCleaner/drain
class ShardCleaner:    private static <T> Collection<T> drain(BlockingQueue<T> queue, int desired, long timeout, TimeUnit unit)
    {
        long start = System.nanoTime();
        Collection<T> result = new ArrayList<>();

        while (true) {
            queue.drainTo(result);
            if (result.size() >= desired) {
                return result;
            }

            long elapsedNanos = System.nanoTime() - start;
            long remainingNanos = unit.toNanos(timeout) - elapsedNanos;
            if (remainingNanos <= 0) {
                return result;
            }

            try {
                T value = queue.poll(remainingNanos, NANOSECONDS);
                if (value != null) {
                    result.add(value);
                }
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return result;
            }
        }
    }