presto-main/src/main/java/io/prestosql/execution/resourcegroups/StochasticPriorityQueue.java/StochasticPriorityQueue/poll
class StochasticPriorityQueue:    @Override
    public E poll()
    {
        if (root == null) {
            return null;
        }

        long winningTicket = ThreadLocalRandom.current().nextLong(root.getTotalTickets());
        Node<E> candidate = root;
        while (!candidate.isLeaf()) {
            long leftTickets = candidate.getLeft().map(Node::getTotalTickets).orElse(0L);

            if (winningTicket < leftTickets) {
                candidate = candidate.getLeft().get();
                continue;
            }
            winningTicket -= leftTickets;

            if (winningTicket < candidate.getTickets()) {
                break;
            }
            winningTicket -= candidate.getTickets();

            checkState(candidate.getRight().isPresent(), "Expected right node to contain the winner, but it does not exist");
            candidate = candidate.getRight().get();
        }
        checkState(winningTicket < candidate.getTickets(), "Inconsistent winner");

        E value = candidate.getValue();
        remove(value);
        return value;
    }