Skip to content

Commit 554177b

Browse files
committed
(feat) Adds system property xmemcached.cached.data.size.max to control max data size, #127
1 parent 004bb44 commit 554177b

File tree

2 files changed

+58
-59
lines changed

2 files changed

+58
-59
lines changed

src/main/java/net/rubyeye/xmemcached/impl/Optimizer.java

+45-47
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import java.nio.ByteBuffer;
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
22-
import java.util.LinkedList;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.Queue;
2625
import java.util.concurrent.CountDownLatch;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
import com.google.code.yanf4j.buffer.IoBuffer;
29+
import com.google.code.yanf4j.core.impl.FutureImpl;
2730
import net.rubyeye.xmemcached.MemcachedOptimizer;
2831
import net.rubyeye.xmemcached.buffer.BufferAllocator;
2932
import net.rubyeye.xmemcached.command.AssocCommandAware;
@@ -43,15 +46,11 @@
4346
import net.rubyeye.xmemcached.utils.ByteUtils;
4447
import net.rubyeye.xmemcached.utils.OpaqueGenerater;
4548
import net.rubyeye.xmemcached.utils.Protocol;
46-
import org.slf4j.Logger;
47-
import org.slf4j.LoggerFactory;
48-
import com.google.code.yanf4j.buffer.IoBuffer;
49-
import com.google.code.yanf4j.core.impl.FutureImpl;
5049

5150
/**
52-
* Memcached command optimizer,merge single-get comands to multi-get command,merge ByteBuffers to
53-
* fit the socket's sendBufferSize etc.
54-
*
51+
* Memcached command optimizer,merge single-get comands to multi-get command,
52+
* merge ByteBuffers to fit the socket's sendBufferSize etc.
53+
*
5554
* @author dennis
5655
*/
5756
public class Optimizer implements OptimizerMBean, MemcachedOptimizer {
@@ -64,21 +63,21 @@ public class Optimizer implements OptimizerMBean, MemcachedOptimizer {
6463
private static final Logger log = LoggerFactory.getLogger(Optimizer.class);
6564
private Protocol protocol = Protocol.Binary;
6665

67-
public Optimizer(Protocol protocol) {
66+
public Optimizer(final Protocol protocol) {
6867
XMemcachedMbeanServer.getInstance().registMBean(this, this.getClass().getPackage().getName()
6968
+ ":type=" + this.getClass().getSimpleName() + "-" + MemcachedClientNameHolder.getName());
7069
this.protocol = protocol;
7170
}
7271

73-
public void setBufferAllocator(BufferAllocator bufferAllocator) {
72+
public void setBufferAllocator(final BufferAllocator bufferAllocator) {
7473

7574
}
7675

7776
public int getMergeFactor() {
7877
return this.mergeFactor;
7978
}
8079

81-
public void setMergeFactor(int mergeFactor) {
80+
public void setMergeFactor(final int mergeFactor) {
8281
if (this.mergeFactor != mergeFactor) {
8382
log.warn("change mergeFactor from " + this.mergeFactor + " to " + mergeFactor);
8483
}
@@ -90,7 +89,7 @@ public boolean isOptimizeGet() {
9089
return this.optimiezeGet;
9190
}
9291

93-
public void setOptimizeGet(boolean optimiezeGet) {
92+
public void setOptimizeGet(final boolean optimiezeGet) {
9493
log.warn(optimiezeGet ? "Enable merge get commands" : "Disable merge get commands");
9594
this.optimiezeGet = optimiezeGet;
9695
}
@@ -99,47 +98,45 @@ public boolean isOptimizeMergeBuffer() {
9998
return this.optimiezeMergeBuffer;
10099
}
101100

102-
public void setOptimizeMergeBuffer(boolean optimiezeMergeBuffer) {
101+
public void setOptimizeMergeBuffer(final boolean optimiezeMergeBuffer) {
103102
log.warn(optimiezeMergeBuffer ? "Enable merge buffers" : "Disable merge buffers");
104103
this.optimiezeMergeBuffer = optimiezeMergeBuffer;
105104
}
106105

107106
@SuppressWarnings("unchecked")
108107
public Command optimize(final Command currentCommand, final Queue writeQueue,
109-
final Queue<Command> executingCmds, int sendBufferSize) {
108+
final Queue<Command> executingCmds, final int sendBufferSize) {
110109
Command optimiezeCommand = currentCommand;
111-
optimiezeCommand = this.optimiezeGet(writeQueue, executingCmds, optimiezeCommand);
110+
optimiezeCommand = optimiezeGet(writeQueue, executingCmds, optimiezeCommand);
111+
optimiezeCommand = optimiezeSet(writeQueue, executingCmds, optimiezeCommand, sendBufferSize);
112112
optimiezeCommand =
113-
this.optimiezeSet(writeQueue, executingCmds, optimiezeCommand, sendBufferSize);
114-
optimiezeCommand =
115-
this.optimiezeMergeBuffer(optimiezeCommand, writeQueue, executingCmds, sendBufferSize);
113+
optimiezeMergeBuffer(optimiezeCommand, writeQueue, executingCmds, sendBufferSize);
116114
return optimiezeCommand;
117115
}
118116

119117
/**
120118
* merge buffers to fit socket's send buffer size
121-
*
119+
*
122120
* @param currentCommand
123121
* @return
124122
* @throws InterruptedException
125123
*/
126124
@SuppressWarnings("unchecked")
127125
public final Command optimiezeMergeBuffer(Command optimiezeCommand, final Queue writeQueue,
128-
final Queue<Command> executingCmds, int sendBufferSize) {
126+
final Queue<Command> executingCmds, final int sendBufferSize) {
129127
if (log.isDebugEnabled()) {
130128
log.debug("Optimieze merge buffer:" + optimiezeCommand.toString());
131129
}
132130
if (this.optimiezeMergeBuffer
133131
&& optimiezeCommand.getIoBuffer().remaining() < sendBufferSize - 24) {
134-
optimiezeCommand =
135-
this.mergeBuffer(optimiezeCommand, writeQueue, executingCmds, sendBufferSize);
132+
optimiezeCommand = mergeBuffer(optimiezeCommand, writeQueue, executingCmds, sendBufferSize);
136133
}
137134
return optimiezeCommand;
138135
}
139136

140137
/**
141138
* Merge get operation to multi-get operation
142-
*
139+
*
143140
* @param currentCmd
144141
* @param mergeCommands
145142
* @return
@@ -151,18 +148,18 @@ public final Command optimiezeGet(final Queue writeQueue, final Queue<Command> e
151148
if (optimiezeCommand.getCommandType() == CommandType.GET_ONE
152149
|| optimiezeCommand.getCommandType() == CommandType.GETS_ONE) {
153150
if (this.optimiezeGet) {
154-
optimiezeCommand = this.mergeGetCommands(optimiezeCommand, writeQueue, executingCmds,
151+
optimiezeCommand = mergeGetCommands(optimiezeCommand, writeQueue, executingCmds,
155152
optimiezeCommand.getCommandType());
156153
}
157154
}
158155
return optimiezeCommand;
159156
}
160157

161158
public final Command optimiezeSet(final Queue writeQueue, final Queue<Command> executingCmds,
162-
Command optimiezeCommand, int sendBufferSize) {
159+
Command optimiezeCommand, final int sendBufferSize) {
163160
if (this.optimiezeSet && optimiezeCommand.getCommandType() == CommandType.SET
164161
&& !optimiezeCommand.isNoreply() && this.protocol == Protocol.Binary) {
165-
optimiezeCommand = this.mergeSetCommands(optimiezeCommand, writeQueue, executingCmds,
162+
optimiezeCommand = mergeSetCommands(optimiezeCommand, writeQueue, executingCmds,
166163
optimiezeCommand.getCommandType(), sendBufferSize);
167164
}
168165
return optimiezeCommand;
@@ -177,7 +174,7 @@ private final Command mergeBuffer(final Command firstCommand, final Queue writeQ
177174
return lastCommand;
178175
}
179176

180-
final List<Command> commands = this.getLocalList();
177+
final List<Command> commands = getLocalList();
181178
final ByteBuffer firstBuffer = firstCommand.getIoBuffer().buf();
182179
int totalBytes = firstBuffer.remaining();
183180
commands.add(firstCommand);
@@ -201,8 +198,7 @@ private final Command mergeBuffer(final Command firstCommand, final Queue writeQ
201198
// if it is get_one command,try to merge get commands
202199
if ((nextCmd.getCommandType() == CommandType.GET_ONE
203200
|| nextCmd.getCommandType() == CommandType.GETS_ONE) && this.optimiezeGet) {
204-
nextCmd =
205-
this.mergeGetCommands(nextCmd, writeQueue, executingCmds, nextCmd.getCommandType());
201+
nextCmd = mergeGetCommands(nextCmd, writeQueue, executingCmds, nextCmd.getCommandType());
206202
}
207203

208204
commands.add(nextCmd);
@@ -269,17 +265,17 @@ public Object getResult() {
269265
return new String(this.buf, 0, this.count);
270266
}
271267

272-
public void visit(Command command) {
268+
public void visit(final Command command) {
273269
if (this.wasFirst) {
274-
this.append(command.getKey());
270+
append(command.getKey());
275271
this.wasFirst = false;
276272
} else {
277-
this.append(" ");
278-
this.append(command.getKey());
273+
append(" ");
274+
append(command.getKey());
279275
}
280276
}
281277

282-
private void expandCapacity(int minimumCapacity) {
278+
private void expandCapacity(final int minimumCapacity) {
283279
int newCapacity = (this.buf.length + 1) * 2;
284280
if (newCapacity < 0) {
285281
newCapacity = Integer.MAX_VALUE;
@@ -291,14 +287,14 @@ private void expandCapacity(int minimumCapacity) {
291287
this.buf = copy;
292288
}
293289

294-
private void append(String str) {
290+
private void append(final String str) {
295291
int len = str.length();
296292
if (len == 0) {
297293
return;
298294
}
299295
int newCount = this.count + len;
300296
if (newCount > this.buf.length) {
301-
this.expandCapacity(newCount);
297+
expandCapacity(newCount);
302298
}
303299
str.getChars(0, len, this.buf, this.count);
304300
this.count = newCount;
@@ -341,7 +337,7 @@ public Object getResult() {
341337
return resultCommand;
342338
}
343339

344-
public void visit(Command command) {
340+
public void visit(final Command command) {
345341

346342
// Encode prev command
347343
if (this.prevCommand != null) {
@@ -422,7 +418,7 @@ public Object getResult() {
422418
return resultCommand;
423419
}
424420

425-
public void visit(Command command) {
421+
public void visit(final Command command) {
426422
// Encode prev command
427423
if (this.prevCommand != null) {
428424
// first n-1 send getq command
@@ -450,10 +446,10 @@ public void finish() {
450446

451447
@SuppressWarnings("unchecked")
452448
private final Command mergeGetCommands(final Command currentCmd, final Queue writeQueue,
453-
final Queue<Command> executingCmds, CommandType expectedCommandType) {
449+
final Queue<Command> executingCmds, final CommandType expectedCommandType) {
454450
Map<Object, Command> mergeCommands = null;
455451
int mergeCount = 1;
456-
final CommandCollector commandCollector = this.createGetCommandCollector();
452+
final CommandCollector commandCollector = createGetCommandCollector();
457453
currentCmd.setStatus(OperationStatus.WRITING);
458454

459455
commandCollector.visit(currentCmd);
@@ -480,10 +476,12 @@ private final Command mergeGetCommands(final Command currentCmd, final Queue wri
480476
if (mergeCommands.containsKey(removedCommand.getKey())) {
481477
final AssocCommandAware mergedGetCommand =
482478
(AssocCommandAware) mergeCommands.get(removedCommand.getKey());
483-
if (mergedGetCommand.getAssocCommands() == null) {
484-
mergedGetCommand.setAssocCommands(new ArrayList<Command>(5));
479+
List<Command> assocCommands = mergedGetCommand.getAssocCommands();
480+
if (assocCommands == null) {
481+
assocCommands = new ArrayList<Command>(5);
482+
mergedGetCommand.setAssocCommands(assocCommands);
485483
}
486-
mergedGetCommand.getAssocCommands().add(removedCommand);
484+
assocCommands.add(removedCommand);
487485
} else {
488486
commandCollector.visit(nextCmd);
489487
mergeCommands.put(removedCommand.getKey(), removedCommand);
@@ -500,8 +498,7 @@ private final Command mergeGetCommands(final Command currentCmd, final Queue wri
500498
if (log.isDebugEnabled()) {
501499
log.debug("Merge optimieze:merge " + mergeCount + " get commands");
502500
}
503-
return this.newMergedCommand(mergeCommands, mergeCount, commandCollector,
504-
expectedCommandType);
501+
return newMergedCommand(mergeCommands, mergeCount, commandCollector, expectedCommandType);
505502
}
506503
}
507504

@@ -516,7 +513,8 @@ protected BinarySetQCollector initialValue() {
516513
};
517514

518515
private final Command mergeSetCommands(final Command currentCmd, final Queue writeQueue,
519-
final Queue<Command> executingCmds, CommandType expectedCommandType, int sendBufferSize) {
516+
final Queue<Command> executingCmds, final CommandType expectedCommandType,
517+
final int sendBufferSize) {
520518
int mergeCount = 1;
521519
final CommandCollector commandCollector = BIN_SET_CMD_COLLECTOR_THREAD_LOCAL.get().reset();
522520
currentCmd.setStatus(OperationStatus.WRITING);
@@ -582,7 +580,7 @@ private CommandCollector createGetCommandCollector() {
582580
}
583581
}
584582

585-
private Command newMergedCommand(final Map<Object, Command> mergeCommands, int mergeCount,
583+
private Command newMergedCommand(final Map<Object, Command> mergeCommands, final int mergeCount,
586584
final CommandCollector commandCollector, final CommandType commandType) {
587585
if (this.protocol == Protocol.Text) {
588586
String resultKey = (String) commandCollector.getResult();

src/main/java/net/rubyeye/xmemcached/transcoders/CachedData.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public final class CachedData {
1313
/**
1414
* Maximum data size allowed by memcached.
1515
*/
16-
public final static int MAX_SIZE = 1024 * 1024;
16+
public final static int MAX_SIZE =
17+
Integer.valueOf(System.getProperty("xmemcached.cached.data.size.max", "1048576"));
1718

1819
protected int flag;
1920
protected long cas;
@@ -34,12 +35,12 @@ public final int getSize() {
3435
return this.size;
3536
}
3637

37-
public final void fillData(ByteBuffer buffer, int offset, int length) {
38+
public final void fillData(final ByteBuffer buffer, final int offset, final int length) {
3839
buffer.get(this.data, offset, length);
3940
this.size += length;
4041
}
4142

42-
public final void fillData(ByteBuffer buffer, int length) {
43+
public final void fillData(final ByteBuffer buffer, final int length) {
4344
buffer.get(this.data, this.size, length);
4445
this.size += length;
4546
}
@@ -48,31 +49,31 @@ public final int getCapacity() {
4849
return this.capacity;
4950
}
5051

51-
public final void setSize(int size) {
52+
public final void setSize(final int size) {
5253
this.size = size;
5354
}
5455

55-
public final void setCapacity(int dataLen) {
56+
public final void setCapacity(final int dataLen) {
5657
this.capacity = dataLen;
5758
}
5859

5960
public static final int getMAX_SIZE() {
6061
return MAX_SIZE;
6162
}
6263

63-
public final void setFlag(int flags) {
64+
public final void setFlag(final int flags) {
6465
this.flag = flags;
6566
}
6667

67-
public final void setData(byte[] data) {
68+
public final void setData(final byte[] data) {
6869
if (data.length > this.capacity) {
6970
throw new IllegalArgumentException("Cannot cache data larger than 1MB (you tried to cache a "
7071
+ data.length + " byte object)");
7172
}
7273
this.data = data;
7374
}
7475

75-
public final void setCas(long cas) {
76+
public final void setCas(final long cas) {
7677
this.cas = cas;
7778
}
7879

@@ -86,12 +87,12 @@ public CachedData() {
8687

8788
/**
8889
* Get a CachedData instance for the given flags and byte array.
89-
*
90+
*
9091
* @param f the flags
9192
* @param d the data
9293
* @param capacity the maximum allowable size.
9394
*/
94-
public CachedData(int f, byte[] d, int capacity, long casId) {
95+
public CachedData(final int f, final byte[] d, final int capacity, final long casId) {
9596
super();
9697
this.capacity = capacity;
9798
this.size = d != null ? d.length : 0;
@@ -106,11 +107,11 @@ public CachedData(int f, byte[] d, int capacity, long casId) {
106107

107108
/**
108109
* Get a CachedData instance for the given flags and byte array.
109-
*
110+
*
110111
* @param f the flags
111112
* @param d the data
112113
*/
113-
public CachedData(int f, byte[] d) {
114+
public CachedData(final int f, final byte[] d) {
114115
this(f, d, MAX_SIZE, -1);
115116
}
116117

0 commit comments

Comments
 (0)