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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;
import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.container.Server;
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 com.yahoo.io.Connection;
import java.io.IOException;
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 <a href="mailto:balder@yahoo-inc.com">Henning Baldersheim</a>
* @since 5.4.0
*/
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 int instanceId;
private final ListenerPool listeners;
private final Timer timer = new Timer(); // This is a timer for cleaning the closed connections
private Map<String, Backend> connectionPoolMap = new HashMap<>();
private final ExecutorService executor;
private final ScheduledExecutorService scheduledExecutor;
public FS4ResourcePool(Fs4Config fs4Config) {
instanceId = instanceCounter.getAndIncrement();
logger.log(Level.INFO, "Constructing an FS4ResourcePool with id '" + instanceId + "' with config '" + fs4Config.toString() + "'");
String name = "FS4-" + instanceId;
listeners = new ListenerPool(name, fs4Config.numlistenerthreads());
executor = Executors.newCachedThreadPool(ThreadFactoryFactory.getDaemonThreadFactory(name));
scheduledExecutor = Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getDaemonThreadFactory(name + ".scheduled"));
}
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, Server.get().getServerDiscriminator(), 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);
}
}
}
|