-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRedisDB.java
282 lines (253 loc) · 10.5 KB
/
RedisDB.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.redis;
import redis.clients.jedis.ClientSetInfoConfig;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import sirius.kernel.async.CallContext;
import sirius.kernel.async.Operation;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Tuple;
import sirius.kernel.commons.Watch;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.Microtiming;
import sirius.kernel.settings.Extension;
import sirius.kernel.settings.PortMapper;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* Represents a connection pool to either a redis database or a set of sentinels which elect a master.
* <p>
* An instance is obtained via {@link Redis#getPool(String)} or {@link Redis#getSystem()}.
*/
public class RedisDB {
private Redis redisInstance;
private final String name;
private String host;
private int port;
private int connectTimeout;
private int readTimeout;
private String password;
private int db;
private int maxActive;
private int maxIdle;
private String masterName;
private String sentinels;
private boolean available = true;
/**
* Determines whether additional client information should be sent to the server when connecting.
* <p>
* This may be disabled for some redis servers that do not support this feature.
*/
private final boolean enableClientInfo;
protected JedisPool jedis;
protected JedisSentinelPool sentinelPool;
protected RedisDB(Redis redisInstance, Extension config) {
this.redisInstance = redisInstance;
this.name = config.getId();
this.host = config.getString("host");
this.port = config.getInt("port");
this.connectTimeout = config.getInt("connectTimeout");
this.readTimeout = config.getInt("readTimeout");
this.password = config.getString("password");
this.db = config.getInt("db");
this.maxActive = config.getInt("maxActive");
this.maxIdle = config.getInt("maxIdle");
this.masterName = config.getString("masterName");
this.sentinels = config.getString("sentinels");
this.enableClientInfo = config.get("enableClientInfo").asBoolean(false);
}
/**
* Determines if access to Redis is configured.
*
* @return <tt>true</tt> if at least a host is given or at least one sentinel is available, <tt>false</tt> otherwise
*/
public boolean isConfigured() {
return available && (Strings.isFilled(host) || Strings.isFilled(sentinels));
}
protected void close() {
available = false;
if (sentinelPool != null) {
JedisSentinelPool copy = this.sentinelPool;
this.sentinelPool = null;
copy.close();
}
if (jedis != null) {
JedisPool copy = this.jedis;
this.jedis = null;
copy.close();
}
}
/**
* Provides raw access to the underlying Redis connection.
* <p>
* Note that this method should be used with absolute care and calling {@link #query(Supplier, Function)}
* or {@link #exec(Supplier, Consumer)} is preferred as it ensures monitoring and proper connection handling.
*
* @return access to a Redis connection from the managed pool. Note that {@link Jedis#close()} has to be called
* in any case to ensure that the connection is returned to the pool.
*/
public Jedis getConnection() {
if (sentinelPool != null) {
return sentinelPool.getResource();
}
if (jedis != null) {
return jedis.getResource();
}
return setupConnection();
}
private synchronized Jedis setupConnection() {
if (sentinelPool == null && Strings.isFilled(sentinels)) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
sentinelPool = new JedisSentinelPool(masterName,
Arrays.stream(sentinels.split(","))
.map(String::trim)
.collect(Collectors.toSet()),
poolConfig,
connectTimeout,
readTimeout,
null,
db);
return sentinelPool.getResource();
}
if (sentinelPool != null) {
return sentinelPool.getResource();
}
if (jedis == null) {
if (Strings.isEmpty(host)) {
Redis.LOG.SEVERE(Strings.apply(
"Missing a Redis host for config '%s'! This might lead to undefined behaviour."
+ " Please specify redis.host or redis.sentinels!",
name));
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
Tuple<String, Integer> effectiveHostAndPort = PortMapper.mapPort(determineServiceName(), host, port);
HostAndPort hostAndPort =
new HostAndPort(effectiveHostAndPort.getFirst(), effectiveHostAndPort.getSecond());
DefaultJedisClientConfig jedisClientConfig = DefaultJedisClientConfig.builder()
.database(db)
.clientName(CallContext.getNodeName())
.connectionTimeoutMillis(connectTimeout)
.socketTimeoutMillis(readTimeout)
.clientSetInfoConfig(new ClientSetInfoConfig(
!enableClientInfo))
.password(Strings.isFilled(password) ?
password :
null)
.build();
jedis = new JedisPool(jedisPoolConfig, hostAndPort, jedisClientConfig);
}
return jedis.getResource();
}
private String determineServiceName() {
return Redis.POOL_SYSTEM.equals(name) ? "redis" : "redis-" + name;
}
/**
* Executes one or more Redis commands and returns a value of the given type.
*
* @param description a description of the actions performed used for debugging and tracing
* @param task the actual task to perform using redis
* @param <T> the generic type of the result
* @return a result computed by <tt>task</tt>
*/
public <T> T query(Supplier<String> description, Function<Jedis, T> task) {
Watch w = Watch.start();
try (Operation op = new Operation(description, Duration.ofSeconds(10)); Jedis redis = getConnection()) {
return task.apply(redis);
} catch (Exception e) {
throw Exceptions.handle(Redis.LOG, e);
} finally {
redisInstance.callDuration.addValue(w.elapsedMillis());
if (Microtiming.isEnabled()) {
w.submitMicroTiming("REDIS", description.get());
}
}
}
/**
* Executes one or more Redis commands without any return value.
*
* @param description a description of the actions performed used for debugging and tracing
* @param task the actual task to perform using redis
*/
public void exec(Supplier<String> description, Consumer<Jedis> task) {
query(description, r -> {
task.accept(r);
return null;
});
}
/**
* Pushes a piece of data to a queue in Redis.
*
* @param queue the name of the queue
* @param data the data to push
*/
public void pushToQueue(String queue, String data) {
exec(() -> "Push to Queue: " + queue, r -> {
r.lpush(queue, data);
});
}
/**
* Polls an element off a queue in Redis.
*
* @param queue the name of the queue
* @return the next entry in the queue or <tt>null</tt> if the queue is empty
*/
@Nullable
public String pollQueue(String queue) {
return query(() -> "Poll from Queue: " + queue, r -> {
String result = r.rpop(queue);
if (Strings.isEmpty(result)) {
return null;
} else {
return result;
}
});
}
/**
* Broadcasts a message to a pub-sub topic in redis.
*
* @param topic the name of the topic to broadcast to
* @param message the message to send
*/
public void publish(String topic, String message) {
exec(() -> "Publish to topic: " + topic, r -> {
r.publish(topic, message);
});
}
/**
* Returns a map of monitoring info about the redis server.
*
* @return a map containing statistical values supplied by the server
*/
public Map<String, String> getInfo() {
try {
return Arrays.stream(query(() -> "info", Jedis::info).split("\n"))
.map(l -> Strings.split(l, ":"))
.filter(t -> t.getFirst() != null && t.getSecond() != null)
.collect(Collectors.toMap(Tuple::getFirst, Tuple::getSecond));
} catch (Exception e) {
Exceptions.handle(Redis.LOG, e);
return Collections.emptyMap();
}
}
}