/
MemcachedClient.java
616 lines (561 loc) · 17.7 KB
/
MemcachedClient.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright (c) 2006 Dustin Sallings <dustin@spy.net>
// arch-tag: 8F357832-263E-473E-BB88-3FFBC813180F
package net.spy.memcached;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import net.spy.SpyThread;
import net.spy.concurrent.SynchronizationObject;
import net.spy.memcached.ops.DeleteOperation;
import net.spy.memcached.ops.FlushOperation;
import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.StatsOperation;
import net.spy.memcached.ops.StoreOperation;
import net.spy.memcached.ops.VersionOperation;
/**
* Client to a memcached server.
*
* <h2>Basic usage</h2>
*
* <pre>
* MemcachedClient c=new MemcachedClient(
* new InetSocketAddress("hostname", portNum));
*
* // Store a value (async) for one hour
* c.set("someKey", 3600, someObject);
* // Retrieve a value.
* Object myObject=c.get("someKey");
* </pre>
*
* <h2>Advanced Usage</h2>
*
* <p>
* MemcachedClient may be processing a great deal of asynchronous messages or
* possibly dealing with an unreachable memcached, which may delay processing.
* If a memcached is disabled, for example, MemcachedConnection will continue
* to attempt to reconnect and replay pending operations until it comes back
* up. To prevent this from causing your application to hang, you can use
* one of the asynchronous mechanisms to time out a request and cancel the
* operation to the server.
* </p>
*
* <pre>
* // Try to get a value, for up to 5 seconds, and cancel if it doesn't return
* Object myObj=null;
* Future<Object> f=c.asyncGet("someKey");
* try {
* myObj=f.get(5, TimeUnit.SECONDS);
* } catch(TimeoutException e) {
* // Since we don't need this, go ahead and cancel the operation. This
* // is not strictly necessary, but it'll save some work on the server.
* f.cancel();
* // Do other timeout related stuff
* }
* </pre>
*/
public class MemcachedClient extends SpyThread {
private volatile boolean running=true;
private MemcachedConnection conn=null;
private Transcoder transcoder=null;
/**
* Get a memcache client operating on the specified memcached locations.
*
* @param ia the memcached locations
* @throws IOException if connections cannot be established
*/
public MemcachedClient(InetSocketAddress... ia) throws IOException {
super();
transcoder=new SerializingTranscoder();
conn=new MemcachedConnection(ia);
setName("Memcached IO over " + conn);
start();
}
/**
* Set the transcoder for managing the cache representations of objects
* going in and out of the cache.
*/
public void setTranscoder(Transcoder to) {
if(to == null) {
throw new NullPointerException("Can't use a null transcoder");
}
transcoder=to;
}
/**
* Get the current transcoder that's in use.
*/
public Transcoder getTranscoder() {
return transcoder;
}
private Operation addOp(int which, Operation op) {
assert isAlive() : "IO Thread is not running.";
conn.addOperation(which, op);
return op;
}
private Future<String> asyncStore(StoreOperation.StoreType storeType,
String key, int exp, Object value) {
CachedData co=transcoder.encode(value);
final SynchronizationObject<String> sync
=new SynchronizationObject<String>(null);
Operation op=new StoreOperation(storeType, key, co.getFlags(), exp,
co.getData(), new OperationCallback() {
public void receivedStatus(String val) {
sync.set(val);
}});
OperationFuture<String> rv=new OperationFuture<String>(sync, op);
addOp(getServerForKey(key), op);
return rv;
}
/**
* Add an object to the cache iff it does not exist already.
*
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @return a future representing the processing of this operation
*/
public Future<String> add(String key, int exp, Object o) {
return asyncStore(StoreOperation.StoreType.add, key, exp, o);
}
/**
* Set an object in the cache regardless of any existing value.
*
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @return a future representing the processing of this operation
*/
public Future<String> set(String key, int exp, Object o) {
return asyncStore(StoreOperation.StoreType.set, key, exp, o);
}
/**
* Replace an object with the given value iff there is already a value
* for the given key.
*
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @return a future representing the processing of this operation
*/
public Future<String> replace(String key, int exp, Object o) {
return asyncStore(StoreOperation.StoreType.replace, key, exp, o);
}
/**
* Get the given key asynchronously.
*
* @param key the key to fetch
* @return a future that will hold the return value of the fetch
*/
public Future<Object> asyncGet(final String key) {
// This marker object will be the default value of synchronization.
// When the synchronization object no longer contains this value, the
// get is complete.
final Object marker=new Object();
final SynchronizationObject<Object> sync
=new SynchronizationObject<Object>(marker);
Operation op=new GetOperation(key, new GetOperation.Callback() {
private Object val=null;
public void receivedStatus(String line) {
sync.set(val);
}
public void gotData(String k, int flags, byte[] data) {
assert key.equals(k) : "Wrong key returned";
val=transcoder.decode(new CachedData(flags, data));
}});
addOp(getServerForKey(key), op);
// Return an operation future that waits for completion
return new OperationFuture<Object>(sync, op) {
@Override
protected void waitForIt(long duration, TimeUnit units)
throws InterruptedException, TimeoutException {
sync.waitUntilTrue(
new SynchronizationObject.Predicate<Object>() {
public boolean evaluate(Object o) {
return o != marker;
}
}, duration, units);
}
};
}
/**
* Get with a single key.
*
* @param key the key to get
* @return the result from the cache (null if there is none)
*/
public Object get(String key) {
try {
return asyncGet(key).get();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for value", e);
} catch (ExecutionException e) {
throw new RuntimeException("Exception waiting for value", e);
}
}
/**
* Asynchronously get a bunch of objects from the cache.
*
* @param keys the keys to request
* @return a Future result of that fetch
*/
public Future<Map<String, Object>> asyncGetBulk(Collection<String> keys) {
final AtomicInteger requests=new AtomicInteger();
final Map<String, Object> m=new ConcurrentHashMap<String, Object>();
final SynchronizationObject<AtomicInteger> sync
=new SynchronizationObject<AtomicInteger>(requests);
GetOperation.Callback cb=new GetOperation.Callback() {
public void receivedStatus(String line) {
requests.decrementAndGet();
sync.set(requests);
}
public void gotData(String k, int flags, byte[] data) {
m.put(k, transcoder.decode(new CachedData(flags, data)));
}
};
// Break the gets down into groups by key
Map<Integer, Collection<String>> chunks
=new HashMap<Integer, Collection<String>>();
for(String key : keys) {
int which=getServerForKey(key);
Collection<String> ks=chunks.get(which);
if(ks == null) {
ks=new ArrayList<String>();
chunks.put(which, ks);
}
ks.add(key);
}
final Collection<Operation> ops=new ArrayList<Operation>();
for(Map.Entry<Integer, Collection<String>> me : chunks.entrySet()) {
requests.incrementAndGet();
ops.add(addOp(me.getKey(), new GetOperation(me.getValue(), cb)));
}
return new BulkGetFuture(requests, m, ops, sync);
}
/**
* Varargs wrapper for asynchronous bulk gets.
*
* @param keys one more more keys to get
* @return the future values of those keys
*/
public Future<Map<String, Object>> asyncGetBulk(String... keys) {
return asyncGetBulk(Arrays.asList(keys));
}
/**
* Get the values for multiple keys from the cache.
* @param keys the keys
* @return a map of the values (for each value that exists)
*/
public Map<String, Object> getBulk(String... keys) {
try {
return asyncGetBulk(Arrays.asList(keys)).get();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted getting bulk values", e);
} catch (ExecutionException e) {
throw new RuntimeException("Failed getting bulk values", e);
}
}
/**
* Get the versions of all of the connected memcacheds.
*/
public Map<SocketAddress, String> getVersions() {
final Map<SocketAddress, String>rv=
new ConcurrentHashMap<SocketAddress, String>();
final AtomicInteger ai=new AtomicInteger(0);
final SynchronizationObject<AtomicInteger> sync=
new SynchronizationObject<AtomicInteger>(ai);
for(int i=0; i<conn.getNumConnections(); i++) {
final SocketAddress sa=conn.getAddressOf(i);
ai.incrementAndGet();
addOp(i, new VersionOperation(
new OperationCallback() {
public void receivedStatus(String s) {
rv.put(sa, s);
ai.decrementAndGet();
sync.set(ai);
}
}));
}
waitForOperations(sync);
return rv;
}
/**
* Get all of the stats from all of the connections.
*/
public Map<SocketAddress, Map<String, String>> getStats() {
final Map<SocketAddress, Map<String, String>> rv
=new HashMap<SocketAddress, Map<String, String>>();
final AtomicInteger todo=new AtomicInteger(conn.getNumConnections());
final SynchronizationObject<AtomicInteger> sync
=new SynchronizationObject<AtomicInteger>(todo);
for(int i=0; i<conn.getNumConnections(); i++) {
final SocketAddress sa=conn.getAddressOf(i);
rv.put(sa, new HashMap<String, String>());
addOp(i, new StatsOperation(
new StatsOperation.Callback() {
public void gotStat(String name, String val) {
rv.get(sa).put(name, val);
}
public void receivedStatus(String line) {
todo.decrementAndGet();
sync.set(todo);
}}));
}
waitForOperations(sync);
return rv;
}
private long mutate(MutatorOperation.Mutator m, String key, int by) {
final SynchronizationObject<Long> sync=
new SynchronizationObject<Long>(null);
addOp(getServerForKey(key), new MutatorOperation(m, key, by,
new OperationCallback() {
public void receivedStatus(String val) {
sync.set(new Long(val==null?"-1":val));
}}));
try {
sync.waitUntilNotNull(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for mutation", e);
} catch (TimeoutException e) {
throw new RuntimeException("Timed out waiting forever.");
}
getLogger().debug("Mutation returned %s", sync.get());
return sync.get().longValue();
}
/**
* Increment the given key by the given amount.
*
* @param key the key
* @param by the amount to increment
* @return the new value (-1 if the key doesn't exist)
*/
public long incr(String key, int by) {
return mutate(MutatorOperation.Mutator.incr, key, by);
}
/**
* Decrement the given key by the given value.
*
* @param key the key
* @param by the value
* @return the new value (-1 if the key doesn't exist)
*/
public long decr(String key, int by) {
return mutate(MutatorOperation.Mutator.decr, key, by);
}
private long mutateWithDefault(MutatorOperation.Mutator t, String key,
int by, long def) {
long rv=mutate(t, key, by);
if(rv == -1) {
Future<String> f=asyncStore(StoreOperation.StoreType.add, key, 0,
String.valueOf(def));
try {
if(f.get().equals("STORED")) {
rv=def;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for store", e);
} catch (ExecutionException e) {
throw new RuntimeException("Failed waiting for store", e);
}
}
return rv;
}
/**
* Increment the given counter, returning the new value.
*
* @param key the key
* @param by the amount to increment
* @param def the default value (if the counter does not exist)
* @return the new value, or -1 if we were unable to increment or add
*/
public long incr(String key, int by, long def) {
return mutateWithDefault(MutatorOperation.Mutator.incr, key, by, def);
}
/**
* Decrement the given counter, returning the new value.
*
* @param key the key
* @param by the amount to decrement
* @param def the default value (if the counter does not exist)
* @return the new value, or -1 if we were unable to decrement or add
*/
public long decr(String key, int by, long def) {
return mutateWithDefault(MutatorOperation.Mutator.decr, key, by, def);
}
/**
* Delete the given key from the cache.
*
* @param key the key to delete
* @param when when the deletion should take effect
*/
public void delete(String key, int when) {
addOp(getServerForKey(key), new DeleteOperation(key, when));
}
/**
* Shortcut to delete that will immediately delete the item from the cache.
*/
public void delete(String key) {
delete(key, 0);
}
/**
* Flush all caches from all servers with a delay of application.
*/
public void flush(int delay) {
for(int i=0; i<conn.getNumConnections(); i++) {
addOp(i, new FlushOperation(delay));
}
}
/**
* Flush all caches from all servers immediately.
*/
public void flush() {
for(int i=0; i<conn.getNumConnections(); i++) {
addOp(i, new FlushOperation());
}
}
/**
* Infinitely loop processing IO.
*/
public void run() {
while(running) {
try {
conn.handleIO();
} catch(IOException e) {
getLogger().warn("Problem handling memcached IO", e);
}
}
getLogger().info("Shut down memcached client");
}
/**
* Shut down this client.
*/
public void shutdown() {
running=false;
try {
conn.shutdown();
} catch (IOException e) {
getLogger().warn("exception while shutting down", e);
}
}
private int getServerForKey(String key) {
return key.hashCode() % conn.getNumConnections();
}
private void waitForOperations(
final SynchronizationObject<AtomicInteger> sync) {
try {
sync.waitUntilTrue(
new SynchronizationObject.Predicate<AtomicInteger>() {
public boolean evaluate(AtomicInteger val) {
return val.get() == 0;
}},
Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for results", e);
} catch (TimeoutException e) {
throw new RuntimeException("Timed out waiting forever", e);
}
}
private static class BulkGetFuture implements Future<Map<String, Object>> {
private AtomicInteger requests;
private Map<String, Object> m;
private Collection<Operation> ops;
private SynchronizationObject<AtomicInteger> sync;
private boolean cancelled=false;
private BulkGetFuture(AtomicInteger requests, Map<String, Object> m,
Collection<Operation> ops,
SynchronizationObject<AtomicInteger> sync) {
super();
this.requests = requests;
this.m = m;
this.ops = ops;
this.sync = sync;
}
public boolean cancel(boolean ign) {
boolean rv=false;
for(Operation op : ops) {
rv |= op.getState() == Operation.State.WRITING;
op.cancel();
}
cancelled=true;
return rv;
}
public Map<String, Object> get()
throws InterruptedException, ExecutionException {
try {
return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
throw new RuntimeException("Timed out waiting forever", e);
}
}
public Map<String, Object> get(long timeout, TimeUnit unit)
throws InterruptedException,
ExecutionException, TimeoutException {
sync.waitUntilTrue(
new SynchronizationObject.Predicate<AtomicInteger>() {
public boolean evaluate(AtomicInteger i) {
return i.get() == 0;
}},
timeout, unit);
return m;
}
public boolean isCancelled() {
return cancelled;
}
public boolean isDone() {
return requests.get() == 0;
}
}
private static class OperationFuture<T> implements Future<T> {
private Operation op=null;
private SynchronizationObject<T> sync=null;
public OperationFuture(SynchronizationObject<T> so, Operation o) {
super();
sync=so;
op=o;
}
public boolean cancel(boolean ign) {
assert op != null : "No operation";
op.cancel();
// This isn't exactly correct, but it's close enough. If we're in
// a writing state, we *probably* haven't started.
return op.getState() == Operation.State.WRITING;
}
public T get() throws InterruptedException, ExecutionException {
try {
waitForIt(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
assert false : "Timed out waiting forever.";
}
return sync.get();
}
public T get(long duration, TimeUnit units)
throws InterruptedException, TimeoutException {
waitForIt(duration, units);
return sync.get();
}
protected void waitForIt(long duration, TimeUnit units)
throws InterruptedException, TimeoutException {
sync.waitUntilNotNull(duration, units);
}
public boolean isCancelled() {
assert op != null : "No operation";
return op.isCancelled();
}
public boolean isDone() {
assert op != null : "No operation";
return op.getState() == Operation.State.COMPLETE;
}
}
}