Skip to content

Commit 4f59651

Browse files
committed
Implement server-side tracing
Also implements flexible framing with ALT_RESPONSE_MAGIC (0x18) More info at: https://github.com/couchbase/kv_engine/blob/8ebe084593043fc00ee713c044129d6c8bdeaaba/docs/BinaryProtocol.md#response-header-with-flexible-framing-extras Change-Id: Iaeb2626a87731d4d480df0b8567a2a639d16c80e Reviewed-on: http://review.couchbase.org/89591 Reviewed-by: Mike Goldsmith <goldsmith.mike@gmail.com> Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
1 parent f9ccce0 commit 4f59651

29 files changed

+256
-209
lines changed

src/main/java/com/couchbase/mock/memcached/AppendPrependCommandExecutor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
class AppendPrependCommandExecutor implements CommandExecutor {
3333

3434
@Override
35-
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
35+
public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
3636
BinaryStoreCommand command = (BinaryStoreCommand) cmd;
3737
VBucketStore cache = server.getStorage().getCache(server, cmd.getVBucketId());
3838

3939
MutationStatus ms;
4040
Item existing = cache.get(command.getKeySpec());
4141
if (existing != null && existing.getValue().length + command.getItem(client.snappyMode()).getValue().length > Info.itemSizeMax()) {
42-
client.sendResponse(new BinaryResponse(cmd, ErrorCode.E2BIG));
43-
return;
42+
return new BinaryResponse(cmd, ErrorCode.E2BIG);
4443
}
4544

4645
switch (cmd.getComCode()) {
@@ -53,23 +52,23 @@ public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnecti
5352
ms = cache.prepend(command.getItem(client.snappyMode()), client.supportsXerror());
5453
break;
5554
default:
56-
return;
55+
throw new ProtocolException("invalid opcode for Append/Prepend handler: " + cmd.getComCode());
5756
}
5857

5958
if (ms.getStatus() == ErrorCode.SUCCESS) {
6059
switch (cmd.getComCode()) {
6160
case APPEND:
6261
case PREPEND:
63-
client.sendResponse(new BinaryStoreResponse(command, ms, client.getMutinfoWriter(), existing.getCas()));
62+
return new BinaryStoreResponse(command, ms, client.getMutinfoWriter(), existing.getCas());
6463
default:
65-
break;
64+
throw new ProtocolException("invalid opcode for Append/Prepend handler: " + cmd.getComCode());
6665
}
6766
} else {
6867
ErrorCode err = ms.getStatus();
6968
if (err == ErrorCode.KEY_ENOENT) {
7069
err = ErrorCode.NOT_STORED;
7170
}
72-
client.sendResponse(new BinaryResponse(cmd, err));
71+
return new BinaryResponse(cmd, err);
7372
}
7473
}
7574
}

src/main/java/com/couchbase/mock/memcached/ArithmeticCommandExecutor.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import com.couchbase.mock.memcached.protocol.Datatype;
2424
import com.couchbase.mock.memcached.protocol.ErrorCode;
2525

26+
import java.net.ProtocolException;
27+
2628
/**
2729
* @author Trond Norbye
2830
*/
2931
public class ArithmeticCommandExecutor implements CommandExecutor {
3032

3133
@Override
32-
public void execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) {
34+
public BinaryResponse execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
3335
BinaryArithmeticCommand cmd = (BinaryArithmeticCommand) command;
3436
VBucketStore cache = server.getStorage().getCache(server, cmd.getVBucketId());
3537
Item item = cache.get(cmd.getKeySpec());
@@ -44,32 +46,30 @@ public void execute(BinaryCommand command, MemcachedServer server, MemcachedConn
4446

4547
switch (err) {
4648
case KEY_EEXISTS:
47-
execute(command, server, client);
48-
break;
49+
return execute(command, server, client);
4950
case SUCCESS:
5051
if (cc == CommandCode.INCREMENT || cc == CommandCode.DECREMENT) {
51-
client.sendResponse(new BinaryArithmeticResponse(cmd, cmd.getInitial(), item.getCas(), ms, miw));
52+
return new BinaryArithmeticResponse(cmd, cmd.getInitial(), item.getCas(), ms, miw);
53+
} else {
54+
throw new ProtocolException("invalid opcode for Arithmetic handler: " + cc);
5255
}
53-
break;
5456
default:
55-
client.sendResponse(new BinaryResponse(command, err));
57+
return new BinaryResponse(command, err);
5658
}
5759
} else {
58-
client.sendResponse(new BinaryResponse(command, ErrorCode.KEY_ENOENT));
60+
return new BinaryResponse(command, ErrorCode.KEY_ENOENT);
5961
}
6062
} else {
6163
long value;
6264

6365
if (!item.ensureUnlocked(command.getCas())) {
64-
client.sendResponse(new BinaryResponse(command, ErrorCode.ETMPFAIL));
65-
return;
66+
return new BinaryResponse(command, ErrorCode.ETMPFAIL);
6667
}
6768

6869
try {
6970
value = Long.parseLong(new String(item.getValue()));
7071
} catch (NumberFormatException ex) {
71-
client.sendResponse(new BinaryResponse(command, ErrorCode.DELTA_BADVAL));
72-
return;
72+
return new BinaryResponse(command, ErrorCode.DELTA_BADVAL);
7373
}
7474

7575
if (cc == CommandCode.INCREMENT || cc == CommandCode.INCREMENTQ) {
@@ -83,11 +83,12 @@ public void execute(BinaryCommand command, MemcachedServer server, MemcachedConn
8383
MutationStatus ms = cache.set(newValue, client.supportsXerror());
8484
if (ms.getStatus() == ErrorCode.SUCCESS) {
8585
if (cc == CommandCode.INCREMENT || cc == CommandCode.DECREMENT) {
86-
// return value
87-
client.sendResponse(new BinaryArithmeticResponse(cmd, value, newValue.getCas(), ms, miw));
86+
return new BinaryArithmeticResponse(cmd, value, newValue.getCas(), ms, miw);
87+
} else {
88+
throw new ProtocolException("invalid opcode for Arithmetic handler: " + cc);
8889
}
8990
} else {
90-
client.sendResponse(new BinaryResponse(command, ms.getStatus()));
91+
return new BinaryResponse(command, ms.getStatus());
9192
}
9293
}
9394
}

src/main/java/com/couchbase/mock/memcached/CommandExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.couchbase.mock.memcached;
1717

1818
import com.couchbase.mock.memcached.protocol.BinaryCommand;
19+
import com.couchbase.mock.memcached.protocol.BinaryResponse;
1920

2021
import java.net.ProtocolException;
2122

@@ -25,10 +26,9 @@
2526
interface CommandExecutor {
2627
/**
2728
* Execute a single command
28-
*
2929
* @param cmd The incoming command
3030
* @param server The server handling the command
3131
* @param client The client requesting the command
3232
*/
33-
void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException;
33+
BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException;
3434
}

src/main/java/com/couchbase/mock/memcached/ConfigCommandExecutor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
import com.couchbase.mock.memcached.protocol.BinaryCommand;
2020
import com.couchbase.mock.memcached.protocol.BinaryConfigResponse;
21+
import com.couchbase.mock.memcached.protocol.BinaryResponse;
2122

2223
/**
2324
* Created by mnunberg on 1/15/14.
2425
*/
2526
public class ConfigCommandExecutor implements CommandExecutor {
2627
@Override
27-
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
28-
client.sendResponse(BinaryConfigResponse.createGetConfig(cmd, server));
28+
public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
29+
return BinaryConfigResponse.createGetConfig(cmd, server);
2930
}
3031
}

src/main/java/com/couchbase/mock/memcached/DeleteCommandExecutor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@
2020
import com.couchbase.mock.memcached.protocol.CommandCode;
2121
import com.couchbase.mock.memcached.protocol.ErrorCode;
2222

23+
import java.net.ProtocolException;
24+
2325
/**
2426
* @author Trond Norbye
2527
*/
2628
public class DeleteCommandExecutor implements CommandExecutor {
2729

2830
@Override
29-
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
31+
public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
3032
VBucketStore cache = server.getStorage().getCache(server, cmd.getVBucketId());
3133
MutationStatus ms = cache.delete(cmd.getKeySpec(), cmd.getCas(), client.supportsXerror());
3234
ErrorCode err = ms.getStatus();
3335

3436
if (!(cmd.getComCode() == CommandCode.DELETEQ && err == ErrorCode.SUCCESS)) {
35-
client.sendResponse(new BinaryResponse(cmd, ms, client.getMutinfoWriter(), 0));
37+
return new BinaryResponse(cmd, ms, client.getMutinfoWriter(), 0);
38+
} else {
39+
throw new ProtocolException("invalid opcode for Delete handler: " + cmd.getComCode());
3640
}
3741
}
3842
}

src/main/java/com/couchbase/mock/memcached/EvictCommandExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
public class EvictCommandExecutor implements CommandExecutor {
2323
@Override
24-
public void execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) {
24+
public BinaryResponse execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) {
2525
VBucketStore cache = server.getStorage().getCache(server, command.getVBucketId());
2626

2727
if (cache.get(command.getKeySpec()) == null) {
28-
client.sendResponse(new BinaryResponse(command, ErrorCode.KEY_ENOENT));
28+
return new BinaryResponse(command, ErrorCode.KEY_ENOENT);
2929
} else {
30-
client.sendResponse(new BinaryResponse(command, ErrorCode.SUCCESS));
30+
return new BinaryResponse(command, ErrorCode.SUCCESS);
3131
}
3232
}
3333
}

src/main/java/com/couchbase/mock/memcached/FlushCommandExecutor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.couchbase.mock.memcached.protocol.CommandCode;
2121
import com.couchbase.mock.memcached.protocol.ErrorCode;
2222

23+
import java.net.ProtocolException;
24+
2325
/**
2426
* Implementation of the callback function for a flush command.
2527
*
@@ -28,10 +30,12 @@
2830
public class FlushCommandExecutor implements CommandExecutor {
2931

3032
@Override
31-
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
33+
public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
3234
server.flushAll();
3335
if (cmd.getComCode() == CommandCode.FLUSH) {
34-
client.sendResponse(new BinaryResponse(cmd, ErrorCode.SUCCESS));
36+
return new BinaryResponse(cmd, ErrorCode.SUCCESS);
37+
} else {
38+
throw new ProtocolException("invalid opcode for Flush handler: " + cmd.getComCode());
3539
}
3640
}
3741
}

src/main/java/com/couchbase/mock/memcached/GetCommandExecutor.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.couchbase.mock.memcached.protocol.CommandCode;
2323
import com.couchbase.mock.memcached.protocol.ErrorCode;
2424

25+
import java.net.ProtocolException;
2526
import java.security.AccessControlException;
2627

2728
/**
@@ -30,7 +31,7 @@
3031
public class GetCommandExecutor implements CommandExecutor {
3132

3233
@Override
33-
public void execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) {
34+
public BinaryResponse execute(BinaryCommand command, MemcachedServer server, MemcachedConnection client) throws ProtocolException {
3435
BinaryGetCommand cmd = (BinaryGetCommand) command;
3536
VBucketStore cache;
3637
CommandCode cc = cmd.getComCode();
@@ -48,33 +49,31 @@ public void execute(BinaryCommand command, MemcachedServer server, MemcachedConn
4849

4950
if (item == null) {
5051
if (cc != CommandCode.GETKQ && cc != CommandCode.GETQ && cc != CommandCode.GATQ) {
51-
client.sendResponse(new BinaryGetResponse(cmd, ErrorCode.KEY_ENOENT,
52-
server.isEnhancedErrorsEnabled() ? "Failed to lookup item" : null));
52+
return new BinaryGetResponse(cmd, ErrorCode.KEY_ENOENT,
53+
server.isEnhancedErrorsEnabled() ? "Failed to lookup item" : null);
54+
} else {
55+
throw new ProtocolException("invalid opcode for Get handler: " + cmd.getComCode());
5356
}
54-
return;
5557
}
5658

5759
if (cc == CommandCode.GETL) {
5860
ErrorCode ec = cache.lock(item, cmd.getExpiration());
5961
if (ec != ErrorCode.SUCCESS) {
60-
client.sendResponse(new BinaryResponse(cmd, ec,
61-
server.isEnhancedErrorsEnabled() ? "Failed to lock item" : null));
62-
return;
62+
return new BinaryResponse(cmd, ec,
63+
server.isEnhancedErrorsEnabled() ? "Failed to lock item" : null);
6364
}
64-
client.sendResponse(new BinaryGetResponse(cmd, item, item.getCasReal(), client.snappyMode()));
65-
return;
65+
return new BinaryGetResponse(cmd, item, item.getCasReal(), client.snappyMode());
6666
} else if (cc == CommandCode.TOUCH || cc == CommandCode.GAT || cc == CommandCode.GATQ) {
6767
ErrorCode ec = cache.touch(item, cmd.getExpiration(), client.supportsXerror());
6868
if (ec != ErrorCode.SUCCESS) {
69-
client.sendResponse(new BinaryResponse(cmd, ec));
70-
return;
69+
return new BinaryResponse(cmd, ec);
7170
}
7271
}
7372

7473
if (cc == CommandCode.TOUCH) {
75-
client.sendResponse(new BinaryGetResponse(cmd, ErrorCode.SUCCESS));
74+
return new BinaryGetResponse(cmd, ErrorCode.SUCCESS);
7675
} else {
77-
client.sendResponse(new BinaryGetResponse(cmd, item, client.snappyMode()));
76+
return new BinaryGetResponse(cmd, item, client.snappyMode());
7877
}
7978
}
8079
}

src/main/java/com/couchbase/mock/memcached/GetErrmapCommandExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public class GetErrmapCommandExecutor implements CommandExecutor {
3939
}
4040

4141
@Override
42-
public void execute(BinaryCommand cmdBase, MemcachedServer server, MemcachedConnection client) {
42+
public BinaryResponse execute(BinaryCommand cmdBase, MemcachedServer server, MemcachedConnection client) {
4343
BinaryGetErrmapCommand cmd = (BinaryGetErrmapCommand)cmdBase;
4444
// Get the version:
4545
short version = cmd.getVersion();
4646
if (version < 1) {
47-
client.sendResponse(new BinaryResponse(cmd, ErrorCode.KEY_ENOENT));
47+
return new BinaryResponse(cmd, ErrorCode.KEY_ENOENT);
4848
} else {
49-
client.sendResponse(new BinaryGetErrmapResponse(cmd, ERRMAP_V1));
49+
return new BinaryGetErrmapResponse(cmd, ERRMAP_V1);
5050
}
5151
}
5252
}

src/main/java/com/couchbase/mock/memcached/GetRandomCommandExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
public class GetRandomCommandExecutor implements CommandExecutor {
2525
@Override
26-
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
26+
public BinaryResponse execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
2727
Item itm = server.getStorage().getRandomItem();
2828
if (itm != null) {
29-
client.sendResponse(new BinaryGetResponse(cmd, itm, client.snappyMode()));
29+
return new BinaryGetResponse(cmd, itm, client.snappyMode());
3030
} else {
31-
client.sendResponse(new BinaryResponse(cmd, ErrorCode.KEY_ENOENT));
31+
return new BinaryResponse(cmd, ErrorCode.KEY_ENOENT);
3232
}
3333
}
3434
}

0 commit comments

Comments
 (0)