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 17, 2024
1 parent c9473d9 commit 71a76b0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 24 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 @@ -42,34 +45,36 @@ public AsciiMemcachedNodeImpl(String name,

@Override
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());
}

}
57 changes: 57 additions & 0 deletions src/test/java/net/spy/memcached/OptimizeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.BeforeEach;
import org.junit.jupiter.api.Test;

class OptimizeTest {

private ArcusClientPool client;
private List<String> keys;

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

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);
}
}

@Test
void testParallelGet() throws Throwable {
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);
}
}

@Test
void testOptimizedOneOperation() throws Throwable {
List<Future<Object>> results = new ArrayList<>(10000);
for (int i = 0; i < 2; i++) {
results.add(client.asyncGet(keys.get(i)));
}
client.set(keys.get(0), 0, "value0");

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

0 comments on commit 71a76b0

Please sign in to comment.