aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/FS4ResourcePool.java
blob: f85a4019b78636a43af42bd81325cbf28b666a57 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.container.QrConfig;
import com.yahoo.container.search.Fs4Config;
import com.yahoo.fs4.mplex.Backend;
import com.yahoo.fs4.mplex.ConnectionPool;
import com.yahoo.fs4.mplex.ListenerPool;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Provider for {@link com.yahoo.fs4.mplex.ListenerPool}. All users will get the same pool instance.
 *
 * @author baldersheim
 */
public class FS4ResourcePool extends AbstractComponent {

    private static final Logger logger = Logger.getLogger(FS4ResourcePool.class.getName());
    private static final AtomicInteger instanceCounter = new AtomicInteger(0);
    private final String serverId;
    private final int instanceId;
    private final ListenerPool listeners;
    private final Timer timer = new Timer();  // This is a timer for cleaning the closed connections
    private final Map<String, Backend> connectionPoolMap = new HashMap<>();
    private final ExecutorService executor;
    private final ScheduledExecutorService scheduledExecutor;

    @Inject
    public FS4ResourcePool(Fs4Config fs4Config, QrConfig config) {
        this(config.discriminator(), fs4Config.numlistenerthreads());
    }
    
    public FS4ResourcePool(String serverId, int listenerThreads) {
        this.serverId = serverId;
        instanceId = instanceCounter.getAndIncrement();
        String name = "FS4-" + instanceId;
        listeners = new ListenerPool(name, listenerThreads);
        executor = Executors.newCachedThreadPool(ThreadFactoryFactory.getDaemonThreadFactory(name));
        scheduledExecutor = Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getDaemonThreadFactory(name + ".scheduled"));
    }

    /** Returns an unique identifier of the server this runs in */
    public String getServerId() { return serverId; }
    public ExecutorService getExecutor() { return executor; }
    public ScheduledExecutorService getScheduledExecutor() { return scheduledExecutor; }

    public Backend getBackend(String host, int port) {
        String key = host + ":" + port;
        synchronized (connectionPoolMap) {
            Backend pool = connectionPoolMap.get(key);
            if (pool == null) {
                pool = new Backend(host, port, serverId, listeners, new ConnectionPool(timer));
                connectionPoolMap.put(key, pool);
            }
            return pool;
        }
    }

    @Override
    public void deconstruct() {
        logger.log(Level.INFO, "Deconstructing FS4ResourcePool with id '" + instanceId + "'.");
        super.deconstruct();
        listeners.close();
        timer.cancel();
        for (Backend backend : connectionPoolMap.values()) {
            backend.shutdown();
            backend.close();
        }
        executor.shutdown();
        scheduledExecutor.shutdown();
        try {
            executor.awaitTermination(10, TimeUnit.SECONDS);
            scheduledExecutor.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            logger.warning("Executors failed terminating within timeout of 10 seconds : " + e);
        }
    }

}