aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/FS4ResourcePool.java
blob: ed9eb72d7ddbc27be5fd6d1b7ece7028321582be (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
// 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 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;

/**
 * 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 ExecutorService executor;
    private final ScheduledExecutorService scheduledExecutor;

    @Inject
    public FS4ResourcePool(QrConfig config) {
        this(config.discriminator());
    }
    
    public FS4ResourcePool(String serverId) {
        this.serverId = serverId;
        instanceId = instanceCounter.getAndIncrement();
        String name = "FS4-" + instanceId;
        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; }

    @Override
    public void deconstruct() {
        logger.log(Level.INFO, "Deconstructing FS4ResourcePool with id '" + instanceId + "'.");
        super.deconstruct();
        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);
        }
    }

}