aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/PortRangeAllocator.java
blob: 49bb7a8860a91b92a9d5870e31148fd106ff96f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;

/**
 * Allocates port ranges for all configserver tests.
 *
 * @author Ulf Lilleengen
 */
public class PortRangeAllocator {
    private final static PortRange portRange = new PortRange();

    // Get the next port from a pre-allocated range
    public static int findAvailablePort() throws InterruptedException {
        return portRange.next();
    }

    public static void releasePort(int port) {
        portRange.release(port);
    }

    private static class PortRange {
        private final Set<Integer> takenPorts = new HashSet<>();
        private final Deque<Integer> freePorts = new ArrayDeque<>();
        private static final int first = 18651;
        private static final int last = 18899; // see: factory/doc/port-ranges

        PortRange() {
            freePorts.addAll(ContiguousSet.create(Range.closed(first, last), DiscreteDomain.integers()));
        }

        synchronized int next() throws InterruptedException {
            if (freePorts.isEmpty()) {
                wait(600_000);
                if (freePorts.isEmpty()) {
                    throw new RuntimeException("no more ports in range " + first + "-" + last);
                }
            }
            int port = freePorts.pop();
            takenPorts.add(port);
            return port;
        }

        synchronized void release(int port) {
            if (port < first || port > last) {
                throw new RuntimeException("trying to release port outside valid range " + port);
            }
            if (!takenPorts.contains(port)) {
                throw new RuntimeException("trying to release port never acquired " + port);
            }
            takenPorts.remove(port);
            freePorts.push(port);
            notify();
        }
    }

}