aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/config/subscription/impl/JRTManagedConnectionPools.java
blob: 45e8be6131606c79951688f354cb76640424b66f (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
66
67
68
69
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription.impl;

import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.vespa.config.JRTConnectionPool;
import com.yahoo.vespa.config.TimingValues;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class JRTManagedConnectionPools {
    private static class JRTSourceThreadFactory implements ThreadFactory {
        @Override
        public Thread newThread(Runnable runnable) {
            Thread t = new Thread(runnable, String.format("jrt-config-requester-%d", System.currentTimeMillis()));
            // We want a daemon thread to avoid hanging threads in case something goes wrong in the config system
            t.setDaemon(true);
            return t;
        }
    }
    private static class CountedPool {
        final JRTConnectionPool pool;
        final ScheduledThreadPoolExecutor scheduler;
        long count;
        CountedPool(JRTConnectionPool requester) {
            pool = requester;
            scheduler = new ScheduledThreadPoolExecutor(1, new JRTSourceThreadFactory());
            count = 0;
            scheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        }
    }

    private final Map<ConfigSourceSet, CountedPool> pools = new HashMap<>();

    public JRTConfigRequester acquire(ConfigSourceSet sourceSet, TimingValues timingValues) {
        CountedPool countedPool;
        synchronized (pools) {
            countedPool = pools.get(sourceSet);
            if (countedPool == null) {
                countedPool = new CountedPool(new JRTConnectionPool(sourceSet));
                pools.put(sourceSet, countedPool);
            }
            countedPool.count++;
        }
        return new JRTConfigRequester(sourceSet, countedPool.scheduler, countedPool.pool, timingValues);
    }

    public synchronized void release(ConfigSourceSet sourceSet) {
        CountedPool countedPool;
        synchronized (pools) {
            countedPool = pools.get(sourceSet);
            if (countedPool != null)
                countedPool.count--;
            if (countedPool == null || countedPool.count > 0) return;
            pools.remove(sourceSet);
        }

        countedPool.pool.close();
        countedPool.scheduler.shutdownNow();
        try {
            countedPool.scheduler.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException("Failed shutting down scheduler:", e);
        }
    }
}