Permalink
Browse files

OperationStatus now has an error code

Having error codes will make it easier for users who get errors
to test for what error it is. Right now we only have the message
string and this can actually be different for the same error type.

Change-Id: I067e3145d5d95b09d7ca0ad0445bcfa5d5784d51
Reviewed-on: http://review.couchbase.org/15949
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
  • Loading branch information...
mikewied committed May 12, 2012
1 parent 82ad6cb commit cdc26cdfaeecc63714c1edcf297d6ffc2c9b8554
Showing with 104 additions and 45 deletions.
  1. +2 −1 src/main/java/net/spy/memcached/MemcachedClient.java
  2. +4 −2 src/main/java/net/spy/memcached/internal/BulkGetFuture.java
  3. +3 −1 src/main/java/net/spy/memcached/internal/OperationFuture.java
  4. +3 −2 src/main/java/net/spy/memcached/ops/CASOperationStatus.java
  5. +1 −1 src/main/java/net/spy/memcached/ops/CancelledOperationStatus.java
  6. +24 −1 src/main/java/net/spy/memcached/ops/OperationStatus.java
  7. +1 −1 src/main/java/net/spy/memcached/ops/TimedOutOperationStatus.java
  8. +2 −1 src/main/java/net/spy/memcached/protocol/BaseOperationImpl.java
  9. +3 −1 src/main/java/net/spy/memcached/protocol/GetCallbackWrapper.java
  10. +5 −3 src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java
  11. +2 −1 src/main/java/net/spy/memcached/protocol/ascii/BaseStoreOperationImpl.java
  12. +4 −3 src/main/java/net/spy/memcached/protocol/ascii/CASOperationImpl.java
  13. +3 −2 src/main/java/net/spy/memcached/protocol/ascii/DeleteOperationImpl.java
  14. +3 −1 src/main/java/net/spy/memcached/protocol/ascii/FlushOperationImpl.java
  15. +3 −2 src/main/java/net/spy/memcached/protocol/ascii/MutatorOperationImpl.java
  16. +7 −1 src/main/java/net/spy/memcached/protocol/ascii/OperationImpl.java
  17. +3 −1 src/main/java/net/spy/memcached/protocol/ascii/StatsOperationImpl.java
  18. +3 −2 src/main/java/net/spy/memcached/protocol/ascii/UnlockOperationImpl.java
  19. +3 −2 src/main/java/net/spy/memcached/protocol/ascii/VersionOperationImpl.java
  20. +3 −1 src/main/java/net/spy/memcached/protocol/binary/MutatorOperationImpl.java
  21. +7 −6 src/main/java/net/spy/memcached/protocol/binary/OperationImpl.java
  22. +7 −6 src/main/java/net/spy/memcached/protocol/binary/SASLBaseOperationImpl.java
  23. +3 −1 src/main/java/net/spy/memcached/protocol/binary/SASLMechsOperationImpl.java
  24. +3 −1 src/main/java/net/spy/memcached/protocol/binary/VersionOperationImpl.java
  25. +2 −1 src/test/java/net/spy/memcached/OperationFactoryTestBase.java
@@ -57,6 +57,7 @@
import net.spy.memcached.ops.CancelledOperationStatus;
import net.spy.memcached.ops.ConcatenationType;
import net.spy.memcached.ops.DeleteOperation;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.GetAndTouchOperation;
import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.GetsOperation;
@@ -1909,7 +1910,7 @@ public boolean cancel(boolean ign) {
@Override
public Boolean get(long duration, TimeUnit units)
throws InterruptedException, TimeoutException, ExecutionException {
- status = new OperationStatus(true, "OK");
+ status = new OperationStatus(true, "OK", ErrorCode.SUCCESS);
return super.get(duration, units);
}
@@ -35,6 +35,7 @@
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.compat.log.LoggerFactory;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -73,7 +74,7 @@ public boolean cancel(boolean ign) {
v.cancel(ign);
}
cancelled = true;
- status = new OperationStatus(false, "Cancelled");
+ status = new OperationStatus(false, "Cancelled", ErrorCode.CANCELLED);
return rv;
}
@@ -167,7 +168,8 @@ public OperationStatus getStatus() {
try {
get();
} catch (InterruptedException e) {
- status = new OperationStatus(false, "Interrupted");
+ status = new OperationStatus(false, "Interrupted",
+ ErrorCode.EXCEPTION);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
return status;
@@ -32,6 +32,7 @@
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.compat.SpyObject;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -119,7 +120,8 @@ public OperationStatus getStatus() {
try {
get();
} catch (InterruptedException e) {
- status = new OperationStatus(false, "Interrupted");
+ status = new OperationStatus(false, "Interrupted",
+ ErrorCode.EXCEPTION);
Thread.currentThread().isInterrupted();
} catch (ExecutionException e) {
getLogger().warn("Error getting status of operation", e);
@@ -31,8 +31,9 @@
private final CASResponse casResponse;
- public CASOperationStatus(boolean success, String msg, CASResponse cres) {
- super(success, msg);
+ public CASOperationStatus(boolean success, String msg, ErrorCode ec,
+ CASResponse cres) {
+ super(success, msg, ec);
casResponse = cres;
}
@@ -28,6 +28,6 @@
public class CancelledOperationStatus extends OperationStatus {
public CancelledOperationStatus() {
- super(false, "cancelled");
+ super(false, "cancelled", ErrorCode.CANCELLED);
}
}
@@ -29,11 +29,20 @@
private final boolean isSuccess;
private final String message;
+ private final ErrorCode errorCode;
+ private final Throwable throwable;
- public OperationStatus(boolean success, String msg) {
+ public OperationStatus(boolean success, String msg, ErrorCode ec) {
+ this(success, msg, ec, null);
+ }
+
+ public OperationStatus(boolean success, String msg, ErrorCode ec,
+ Throwable t) {
super();
isSuccess = success;
message = msg;
+ errorCode = ec;
+ throwable = null;
}
/**
@@ -50,6 +59,20 @@ public String getMessage() {
return message;
}
+ /**
+ * Get the error code associated with this request.
+ */
+ public ErrorCode getErrorCode() {
+ return errorCode;
+ }
+
+ /**
+ * Get the throwable if one exists for this operation.
+ */
+ public Throwable getThrowable() {
+ return throwable;
+ }
+
@Override
public String toString() {
return "{OperationStatus success=" + isSuccess + ": " + message + "}";
@@ -28,6 +28,6 @@
public class TimedOutOperationStatus extends OperationStatus {
public TimedOutOperationStatus() {
- super(false, "timed out");
+ super(false, "timed out", ErrorCode.TIMED_OUT);
}
}
@@ -31,6 +31,7 @@
import net.spy.memcached.MemcachedNode;
import net.spy.memcached.compat.SpyObject;
import net.spy.memcached.ops.CancelledOperationStatus;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationErrorType;
@@ -170,7 +171,7 @@ protected void handleError(OperationErrorType eType, String line)
assert false;
}
callback.receivedStatus(new OperationStatus(false,
- exception.getMessage()));
+ exception.getMessage(), ErrorCode.EXCEPTION, exception));
transitionState(OperationState.COMPLETE);
throw exception;
}
@@ -22,6 +22,7 @@
package net.spy.memcached.protocol;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.OperationStatus;
@@ -30,7 +31,8 @@
*/
public class GetCallbackWrapper implements GetOperation.Callback {
- private static final OperationStatus END = new OperationStatus(true, "END");
+ private static final OperationStatus END = new OperationStatus(true, "END",
+ ErrorCode.SUCCESS);
private boolean completed = false;
private int remainingKeys = 0;
@@ -28,6 +28,7 @@
import java.util.Collections;
import net.spy.memcached.KeyUtil;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.GetAndTouchOperation;
import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.GetlOperation;
@@ -42,11 +43,12 @@
*/
abstract class BaseGetOpImpl extends OperationImpl {
- private static final OperationStatus END = new OperationStatus(true, "END");
+ private static final OperationStatus END = new OperationStatus(true, "END",
+ ErrorCode.SUCCESS);
private static final OperationStatus NOT_FOUND = new OperationStatus(false,
- "NOT_FOUND");
+ "NOT_FOUND", ErrorCode.ERR_NOT_FOUND);
private static final OperationStatus LOCK_ERROR = new OperationStatus(false,
- "LOCK_ERROR");
+ "LOCK_ERROR", ErrorCode.ERR_EXISTS);
private static final byte[] RN_BYTES = "\r\n".getBytes();
private final String cmd;
private final Collection<String> keys;
@@ -27,6 +27,7 @@
import java.util.Collections;
import net.spy.memcached.KeyUtil;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -38,7 +39,7 @@
private static final int OVERHEAD = 32;
private static final OperationStatus STORED = new OperationStatus(true,
- "STORED");
+ "STORED", ErrorCode.SUCCESS);
protected final String type;
protected final String key;
protected final int flags;
@@ -30,6 +30,7 @@
import net.spy.memcached.KeyUtil;
import net.spy.memcached.ops.CASOperation;
import net.spy.memcached.ops.CASOperationStatus;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -43,11 +44,11 @@
private static final int OVERHEAD = 64;
private static final OperationStatus STORED = new CASOperationStatus(true,
- "STORED", CASResponse.OK);
+ "STORED", ErrorCode.SUCCESS, CASResponse.OK);
private static final OperationStatus NOT_FOUND = new CASOperationStatus(
- false, "NOT_FOUND", CASResponse.NOT_FOUND);
+ false, "NOT_FOUND", ErrorCode.ERR_NOT_FOUND, CASResponse.NOT_FOUND);
private static final OperationStatus EXISTS = new CASOperationStatus(false,
- "EXISTS", CASResponse.EXISTS);
+ "EXISTS", ErrorCode.ERR_EXISTS, CASResponse.EXISTS);
private final String key;
private final long casValue;
@@ -28,6 +28,7 @@
import net.spy.memcached.KeyUtil;
import net.spy.memcached.ops.DeleteOperation;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -41,9 +42,9 @@
private static final int OVERHEAD = 32;
private static final OperationStatus DELETED = new OperationStatus(true,
- "DELETED");
+ "DELETED", ErrorCode.SUCCESS);
private static final OperationStatus NOT_FOUND = new OperationStatus(false,
- "NOT_FOUND");
+ "NOT_FOUND", ErrorCode.ERR_NOT_FOUND);
private final String key;
@@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.FlushOperation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
@@ -36,7 +37,8 @@
private static final byte[] FLUSH = "flush_all\r\n".getBytes();
- private static final OperationStatus OK = new OperationStatus(true, "OK");
+ private static final OperationStatus OK = new OperationStatus(true, "OK",
+ ErrorCode.SUCCESS);
private final int delay;
@@ -27,6 +27,7 @@
import java.util.Collections;
import net.spy.memcached.KeyUtil;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.OperationCallback;
@@ -42,7 +43,7 @@
public static final int OVERHEAD = 32;
private static final OperationStatus NOT_FOUND = new OperationStatus(false,
- "NOT_FOUND");
+ "NOT_FOUND", ErrorCode.ERR_NOT_FOUND);
private final Mutator mutator;
private final String key;
@@ -63,7 +64,7 @@ public void handleLine(String line) {
if (line.equals("NOT_FOUND")) {
found = NOT_FOUND;
} else {
- found = new OperationStatus(true, line);
+ found = new OperationStatus(true, line, ErrorCode.SUCCESS);
}
getCallback().receivedStatus(found);
transitionState(OperationState.COMPLETE);
@@ -27,6 +27,7 @@
import java.nio.ByteBuffer;
import net.spy.memcached.KeyUtil;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationErrorType;
@@ -72,7 +73,12 @@ protected final OperationStatus matchStatus(String line,
}
}
if (rv == null) {
- rv = new OperationStatus(false, line);
+ // TODO: (Mike) We need to standardize response messages in memcached
+ // and ep-engine in order for this operation status to be correctly
+ // returned in ASCII since ASCII doesn't return an error code and just
+ // returns a message. Invalid is probably the best since we will catch
+ // the most common responses before getting here anyways.
+ rv = new OperationStatus(false, line, ErrorCode.ERR_INVAL);
}
return rv;
}
@@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.StatsOperation;
@@ -33,7 +34,8 @@
*/
final class StatsOperationImpl extends OperationImpl implements StatsOperation {
- private static final OperationStatus END = new OperationStatus(true, "END");
+ private static final OperationStatus END = new OperationStatus(true, "END",
+ ErrorCode.SUCCESS);
private static final byte[] MSG = "stats\r\n".getBytes();
@@ -28,6 +28,7 @@
import java.util.Collections;
import net.spy.memcached.KeyUtil;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
@@ -42,9 +43,9 @@
private static final int OVERHEAD = 32;
private static final OperationStatus UNLOCKED = new OperationStatus(true,
- "UNLOCKED");
+ "UNLOCKED", ErrorCode.SUCCESS);
private static final OperationStatus NOT_FOUND = new OperationStatus(false,
- "NOT_FOUND");
+ "NOT_FOUND", ErrorCode.ERR_NOT_FOUND);
private static final String CMD = "unl";
private final String key;
@@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.NoopOperation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
@@ -45,8 +46,8 @@ public VersionOperationImpl(OperationCallback c) {
@Override
public void handleLine(String line) {
assert line.startsWith("VERSION ");
- getCallback().receivedStatus(
- new OperationStatus(true, line.substring("VERSION ".length())));
+ getCallback().receivedStatus(new OperationStatus(true,
+ line, ErrorCode.SUCCESS));
transitionState(OperationState.COMPLETE);
}
@@ -23,6 +23,7 @@
package net.spy.memcached.protocol.binary;
+import net.spy.memcached.ops.ErrorCode;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.OperationCallback;
@@ -66,8 +67,9 @@ public void initialize() {
@Override
protected void decodePayload(byte[] pl) {
+ ErrorCode ec = ErrorCode.getErrorCode((byte)errorCode);
getCallback().receivedStatus(
- new OperationStatus(true, String.valueOf(decodeLong(pl, 0))));
+ new OperationStatus(true, String.valueOf(decodeLong(pl, 0)), ec));
}
public long getBy() {
Oops, something went wrong.

0 comments on commit cdc26cd

Please sign in to comment.