-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathConnectionFactoryBuilder.java
502 lines (434 loc) · 13.3 KB
/
ConnectionFactoryBuilder.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package net.spy.memcached;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.metrics.MetricCollector;
import net.spy.memcached.metrics.MetricType;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationQueueFactory;
import net.spy.memcached.protocol.ascii.AsciiOperationFactory;
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.Transcoder;
/**
* Builder for more easily configuring a ConnectionFactory.
*/
public class ConnectionFactoryBuilder {
protected OperationQueueFactory opQueueFactory;
protected OperationQueueFactory readQueueFactory;
protected OperationQueueFactory writeQueueFactory;
protected Transcoder<Object> transcoder;
protected FailureMode failureMode;
protected Collection<ConnectionObserver> initialObservers =
Collections.emptyList();
protected OperationFactory opFact;
protected Locator locator = Locator.ARRAY_MOD;
protected long opTimeout = -1;
protected boolean isDaemon = false;
protected boolean shouldOptimize = false;
protected boolean useNagle = false;
protected boolean keepAlive = false;
protected long maxReconnectDelay =
DefaultConnectionFactory.DEFAULT_MAX_RECONNECT_DELAY;
protected int readBufSize = -1;
protected HashAlgorithm hashAlg;
protected AuthDescriptor authDescriptor = null;
protected long opQueueMaxBlockTime = -1;
protected int timeoutExceptionThreshold =
DefaultConnectionFactory.DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD;
protected MetricType metricType = null;
protected MetricCollector collector = null;
protected ExecutorService executorService = null;
protected long authWaitTime = DefaultConnectionFactory.DEFAULT_AUTH_WAIT_TIME;
/**
* Set the operation queue factory.
*/
public ConnectionFactoryBuilder() {
// empty
}
public ConnectionFactoryBuilder(ConnectionFactory cf) {
setAuthDescriptor(cf.getAuthDescriptor());
setDaemon(cf.isDaemon());
setFailureMode(cf.getFailureMode());
setHashAlg(cf.getHashAlg());
setInitialObservers(cf.getInitialObservers());
setMaxReconnectDelay(cf.getMaxReconnectDelay());
setOpQueueMaxBlockTime(cf.getOpQueueMaxBlockTime());
setOpTimeout(cf.getOperationTimeout());
setReadBufferSize(cf.getReadBufSize());
setShouldOptimize(cf.shouldOptimize());
setTimeoutExceptionThreshold(cf.getTimeoutExceptionThreshold());
setTranscoder(cf.getDefaultTranscoder());
setUseNagleAlgorithm(cf.useNagleAlgorithm());
setEnableMetrics(cf.enableMetrics());
setListenerExecutorService(cf.getListenerExecutorService());
setAuthWaitTime(cf.getAuthWaitTime());
}
public ConnectionFactoryBuilder setOpQueueFactory(OperationQueueFactory q) {
opQueueFactory = q;
return this;
}
/**
* Set the read queue factory.
*/
public ConnectionFactoryBuilder
setReadOpQueueFactory(OperationQueueFactory q) {
readQueueFactory = q;
return this;
}
/**
* Set the write queue factory.
*/
public ConnectionFactoryBuilder
setWriteOpQueueFactory(OperationQueueFactory q) {
writeQueueFactory = q;
return this;
}
/**
* Set the maximum amount of time (in milliseconds) a client is willing to
* wait for space to become available in an output queue.
*/
public ConnectionFactoryBuilder setOpQueueMaxBlockTime(long t) {
opQueueMaxBlockTime = t;
return this;
}
/**
* Set the default transcoder.
*/
public ConnectionFactoryBuilder setTranscoder(Transcoder<Object> t) {
transcoder = t;
return this;
}
/**
* Set the failure mode.
*/
public ConnectionFactoryBuilder setFailureMode(FailureMode fm) {
failureMode = fm;
return this;
}
/**
* Set the initial connection observers (will observe initial connection).
*/
public ConnectionFactoryBuilder setInitialObservers(
Collection<ConnectionObserver> obs) {
initialObservers = obs;
return this;
}
/**
* Set the operation factory.
*
* Note that the operation factory is used to also imply the type of nodes to
* create.
*
* @see MemcachedNode
*/
public ConnectionFactoryBuilder setOpFact(OperationFactory f) {
opFact = f;
return this;
}
/**
* Set the default operation timeout in milliseconds.
*/
public ConnectionFactoryBuilder setOpTimeout(long t) {
opTimeout = t;
return this;
}
/**
* Set the daemon state of the IO thread (defaults to true).
*/
public ConnectionFactoryBuilder setDaemon(boolean d) {
isDaemon = d;
return this;
}
/**
* Set to false if the default operation optimization is not desirable.
*/
public ConnectionFactoryBuilder setShouldOptimize(boolean o) {
shouldOptimize = o;
return this;
}
/**
* Set the read buffer size.
*/
public ConnectionFactoryBuilder setReadBufferSize(int to) {
readBufSize = to;
return this;
}
/**
* Set the hash algorithm.
*/
public ConnectionFactoryBuilder setHashAlg(HashAlgorithm to) {
hashAlg = to;
return this;
}
/**
* Set to true if you'd like to enable the Nagle algorithm.
*/
public ConnectionFactoryBuilder setUseNagleAlgorithm(boolean to) {
useNagle = to;
return this;
}
public ConnectionFactoryBuilder setKeepAlive(boolean on) {
keepAlive = on;
return this;
}
/**
* Convenience method to specify the protocol to use.
*/
public ConnectionFactoryBuilder setProtocol(Protocol prot) {
switch (prot) {
case TEXT:
opFact = new AsciiOperationFactory();
break;
case BINARY:
opFact = new BinaryOperationFactory();
break;
default:
assert false : "Unhandled protocol: " + prot;
}
return this;
}
/**
* Set the locator type.
*/
public ConnectionFactoryBuilder setLocatorType(Locator l) {
locator = l;
return this;
}
/**
* Set the maximum reconnect delay.
*/
public ConnectionFactoryBuilder setMaxReconnectDelay(long to) {
assert to > 0 : "Reconnect delay must be a positive number";
maxReconnectDelay = to;
return this;
}
/**
* Set the auth descriptor to enable authentication on new connections.
*/
public ConnectionFactoryBuilder setAuthDescriptor(AuthDescriptor to) {
authDescriptor = to;
return this;
}
/**
* Set the maximum timeout exception threshold.
*/
public ConnectionFactoryBuilder setTimeoutExceptionThreshold(int to) {
assert to > 1 : "Minimum timeout exception threshold is 2";
if (to > 1) {
timeoutExceptionThreshold = to - 2;
}
return this;
}
/**
* Enable or disable metric collection.
*
* @param type the metric type to use (or disable).
*/
public ConnectionFactoryBuilder setEnableMetrics(MetricType type) {
metricType = type;
return this;
}
/**
* Set a custom {@link MetricCollector}.
*
* @param collector the metric collector to use.
*/
public ConnectionFactoryBuilder setMetricCollector(MetricCollector collector) {
this.collector = collector;
return this;
}
/**
* Set a custom {@link ExecutorService} to execute the listener callbacks.
*
* Note that if a custom {@link ExecutorService} is passed in, it also needs to be properly
* shut down by the caller. The library itself treats it as a outside managed resource.
* Therefore, also make sure to not shut it down before all instances that use it are
* shut down.
*
* @param executorService the ExecutorService to use.
*/
public ConnectionFactoryBuilder setListenerExecutorService(ExecutorService executorService) {
this.executorService = executorService;
return this;
}
/**
* Set a custom wait time for the authentication on connect/reconnect.
*
* @param authWaitTime the time in milliseconds.
*/
public ConnectionFactoryBuilder setAuthWaitTime(long authWaitTime) {
this.authWaitTime = authWaitTime;
return this;
}
/**
* Get the ConnectionFactory set up with the provided parameters.
*/
public ConnectionFactory build() {
return new DefaultConnectionFactory() {
@Override
public BlockingQueue<Operation> createOperationQueue() {
return opQueueFactory == null ? super.createOperationQueue()
: opQueueFactory.create();
}
@Override
public BlockingQueue<Operation> createReadOperationQueue() {
return readQueueFactory == null ? super.createReadOperationQueue()
: readQueueFactory.create();
}
@Override
public BlockingQueue<Operation> createWriteOperationQueue() {
return writeQueueFactory == null ? super.createReadOperationQueue()
: writeQueueFactory.create();
}
@Override
public NodeLocator createLocator(List<MemcachedNode> nodes) {
switch (locator) {
case ARRAY_MOD:
return new ArrayModNodeLocator(nodes, getHashAlg());
case CONSISTENT:
return new KetamaNodeLocator(nodes, getHashAlg());
default:
throw new IllegalStateException("Unhandled locator type: " + locator);
}
}
@Override
public Transcoder<Object> getDefaultTranscoder() {
return transcoder == null ? super.getDefaultTranscoder() : transcoder;
}
@Override
public FailureMode getFailureMode() {
return failureMode == null ? super.getFailureMode() : failureMode;
}
@Override
public HashAlgorithm getHashAlg() {
return hashAlg == null ? super.getHashAlg() : hashAlg;
}
public Collection<ConnectionObserver> getInitialObservers() {
return initialObservers;
}
@Override
public OperationFactory getOperationFactory() {
return opFact == null ? super.getOperationFactory() : opFact;
}
@Override
public long getOperationTimeout() {
return opTimeout == -1 ? super.getOperationTimeout() : opTimeout;
}
@Override
public int getReadBufSize() {
return readBufSize == -1 ? super.getReadBufSize() : readBufSize;
}
@Override
public boolean isDaemon() {
return isDaemon;
}
@Override
public boolean shouldOptimize() {
return shouldOptimize;
}
@Override
public boolean useNagleAlgorithm() {
return useNagle;
}
@Override
public boolean getKeepAlive() {
return keepAlive;
}
@Override
public long getMaxReconnectDelay() {
return maxReconnectDelay;
}
@Override
public AuthDescriptor getAuthDescriptor() {
return authDescriptor;
}
@Override
public long getOpQueueMaxBlockTime() {
return opQueueMaxBlockTime > -1 ? opQueueMaxBlockTime
: super.getOpQueueMaxBlockTime();
}
@Override
public int getTimeoutExceptionThreshold() {
return timeoutExceptionThreshold;
}
@Override
public MetricType enableMetrics() {
return metricType == null ? super.enableMetrics() : metricType;
}
@Override
public MetricCollector getMetricCollector() {
return collector == null ? super.getMetricCollector() : collector;
}
@Override
public ExecutorService getListenerExecutorService() {
return executorService == null ? super.getListenerExecutorService() : executorService;
}
@Override
public boolean isDefaultExecutorService() {
return executorService == null;
}
@Override
public long getAuthWaitTime() {
return authWaitTime;
}
};
}
/**
* Type of protocol to use for connections.
*/
public static enum Protocol {
/**
* Use the text (ascii) protocol.
*/
TEXT,
/**
* Use the binary protocol.
*/
BINARY
}
/**
* Type of node locator to use.
*/
public static enum Locator {
/**
* Array modulus - the classic node location algorithm.
*/
ARRAY_MOD,
/**
* Consistent hash algorithm.
*
* This uses ketema's distribution algorithm, but may be used with any hash
* algorithm.
*/
CONSISTENT,
/**
* VBucket support.
*/
VBUCKET
}
}