Skip to content

Commit

Permalink
INTERNAL: Limit bulk get keys size
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust committed Dec 10, 2024
1 parent c9473d9 commit b559ee2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* Memcached node for the ASCII protocol.
*/
public final class AsciiMemcachedNodeImpl extends TCPMemcachedNodeImpl {

private static final int GET_BULK_CHUNK_SIZE = 200;

public AsciiMemcachedNodeImpl(String name,
SocketAddress sa,
int bufSize, BlockingQueue<Operation> rq,
Expand All @@ -45,31 +48,35 @@ protected void optimize() {
// make sure there are at least two get operations in a row before
// attempting to optimize them.
Operation nxtOp = writeQ.peek();
if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) {
optimizedOp = writeQ.remove();
nxtOp = writeQ.peek();
if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) {
OptimizedGetImpl og = new OptimizedGetImpl(
(GetOperation) optimizedOp);
optimizedOp = og;
if (!(nxtOp instanceof GetOperation) || nxtOp.getAPIType() == APIType.MGET ||
((GetOperation) nxtOp).getKeys().size() > GET_BULK_CHUNK_SIZE) {
return;
}

do {
GetOperationImpl o = (GetOperationImpl) writeQ.remove();
if (!o.isCancelled()) {
og.addOperation(o);
}
nxtOp = writeQ.peek();
} while (nxtOp instanceof GetOperation &&
nxtOp.getAPIType() != APIType.MGET);
int cnt = ((GetOperation) nxtOp).getKeys().size();
optimizedOp = new OptimizedGetImpl((GetOperation) writeQ.remove());
nxtOp = writeQ.peek();
OptimizedGetImpl og = null;

// Initialize the new mega get
optimizedOp.initialize();
assert optimizedOp.getState() == OperationState.WRITE_QUEUED;
ProxyCallback pcb = (ProxyCallback) og.getCallback();
getLogger().debug("Set up %s with %s keys and %s callbacks",
this, pcb.numKeys(), pcb.numCallbacks());
while (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) {
if (og == null) {
og = (OptimizedGetImpl) optimizedOp;
}
cnt += ((GetOperation) nxtOp).getKeys().size();
if (cnt > GET_BULK_CHUNK_SIZE) {
break;
}
GetOperationImpl currentOp = (GetOperationImpl) writeQ.remove();
if (!currentOp.isCancelled()) {
og.addOperation(currentOp);
}
nxtOp = writeQ.peek();
}
// Initialize the new mega get
optimizedOp.initialize();
assert optimizedOp.getState() == OperationState.WRITE_QUEUED;
ProxyCallback pcb = (ProxyCallback) optimizedOp.getCallback();
getLogger().debug("Set up %s with %s keys and %s callbacks",
this, pcb.numKeys(), pcb.numCallbacks());
}

}
38 changes: 38 additions & 0 deletions src/test/java/net/spy/memcached/OptimizeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.spy.memcached;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


class OptimizeTest {

@Test
void testParallelGet() throws Throwable {
ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
builder.setShouldOptimize(true);
// Get a connection with the get optimization.
ArcusClientPool client =
ArcusClient.createArcusClientPool("127.0.0.1:2181", "test", builder, 1);

final List<String> keys = new ArrayList<>(10000);
for (int i = 0; i < 100; i++) {
keys.add("k" + i);
Boolean b = client.set(keys.get(i), 0, "value" + i).get();
Assertions.assertEquals(true, b);
}

List<Future<Object>> results = new ArrayList<>(10000);
for (int i = 0; i < 100; i++) {
results.add(client.asyncGet(keys.get(i)));
}

for (int i = 0; i < 100; i++) {
Object o = results.get(i).get();
Assertions.assertEquals("value" + i, o);
}
}
}

0 comments on commit b559ee2

Please sign in to comment.