-
Notifications
You must be signed in to change notification settings - Fork 17
/
SentinelBasedJedisPoolWrapper.java
211 lines (169 loc) · 5.86 KB
/
SentinelBasedJedisPoolWrapper.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package pl.quaternion;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.util.Pool;
public class SentinelBasedJedisPoolWrapper extends Pool<Jedis> {
private class HostAndPort {
String host;
int port;
@Override
public boolean equals(Object obj) {
if (obj instanceof HostAndPort) {
final HostAndPort that = (HostAndPort) obj;
return this.port == that.port && this.host.equals(that.host);
}
return false;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
@Override
public String toString() {
return host + ":" + port;
}
}
public class JedisPubSubAdapter extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
}
@Override
public void onPMessage(String pattern, String channel, String message) {
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
}
}
private final Config poolConfig;
private final int timeout;
private final String password;
private final int database;
private volatile JedisPool poolToUse;
private volatile HostAndPort currentHostMaster;
public SentinelBasedJedisPoolWrapper(final Config poolConfig, int timeout, final String password, int database, String masterName, Set<String> sentinels) {
this.poolConfig = poolConfig;
this.timeout = timeout;
this.password = password;
this.database = database;
final HostAndPort master = initSentinels(sentinels, masterName);
initPool(master);
}
public void destroy() {
// TODO clean sentinels connections
poolToUse.destroy();
}
public HostAndPort getCurrentHostMaster() {
return currentHostMaster;
}
// Overwrite Pool<Jedis>
@SuppressWarnings("unchecked")
public Jedis getResource() {
return poolToUse.getResource();
}
private void initPool(HostAndPort master) {
if (!master.equals(currentHostMaster)) {
currentHostMaster = master;
log("Created pool: " + master);
poolToUse = new JedisPool(poolConfig, master.host, master.port, timeout, password, database);
}
}
private HostAndPort initSentinels(Set<String> sentinels, final String masterName) {
HostAndPort master = null;
final boolean running = true;
outer: while (running) {
for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
try {
final Jedis jedis = new Jedis(hap.host, hap.port);
if (master == null) {
master = toHostAndPort(jedis.sentinelGetMasterAddrByName(masterName));
jedis.disconnect();
break outer;
}
} catch (JedisConnectionException e) {
log("Cannot connect to sentinel running @ " + hap + ". Trying next one.");
}
}
try {
log("All sentinels down, cannot determinate where is " + masterName + " master is running... sleeping 1000ms.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log("Got master running at " + master + ". Starting sentinel listeners.");
for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
new Thread() {
public void run() {
boolean running = true;
while (running) {
final Jedis jedis = new Jedis(hap.host, hap.port);
try {
jedis.subscribe(new JedisPubSubAdapter() {
@Override
public void onMessage(String channel, String message) {
// System.out.println(channel + ": " + message);
// +switch-master: mymaster 127.0.0.1 6379 127.0.0.1 6380
log("Sentinel " + hap + " published: " + message + ".");
final String[] switchMasterMsg = message.split(" ");
if (masterName.equals(switchMasterMsg[0])) {
initPool(toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
}
}
}, "+switch-master");
} catch (JedisConnectionException e) {
log("Lost connection to " + hap + ". Sleeping 5000ms.");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
};
}.start();
}
return master;
}
// sophisticated logging
private void log(String msg) {
//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": " + msg);
}
public void returnBrokenResource(final Jedis resource) {
poolToUse.returnBrokenResource(resource);
}
public void returnResource(final Jedis resource) {
poolToUse.returnResource(resource);
}
public void returnResourceObject(final Object resource) {
poolToUse.returnResourceObject(resource);
}
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
final HostAndPort hap = new HostAndPort();
hap.host = getMasterAddrByNameResult.get(0);
hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1));
return hap;
}
}