From a0ed8205a2c0da234b2257af96f0f54e93bbc86c Mon Sep 17 00:00:00 2001 From: Shuo Date: Thu, 7 Nov 2019 19:14:11 +0800 Subject: [PATCH] fix: fix bug where timeout in exception info is 0 (#65) --- .../infra/pegasus/client/PegasusTable.java | 5 +-- .../infra/pegasus/client/TestPException.java | 33 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java index f99c52c4..bb0af94f 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java @@ -1730,8 +1730,9 @@ public List getUnorderedScanners(int maxSplitCount, Sca return ret; } - static void handleReplicaException( + public void handleReplicaException( DefaultPromise promise, client_operator op, Table table, int timeout) { + if (timeout <= 0) timeout = defaultTimeout; gpid gPid = op.get_gpid(); ReplicaConfiguration replicaConfiguration = ((TableHandler) table).getReplicaConfig(gPid.get_pidx()); @@ -1761,7 +1762,7 @@ static void handleReplicaException( message = " Disconnected from the replica-server due to internal error!"; break; case ERR_TIMEOUT: - message = " The operationTimeout is " + timeout + "ms!"; + message = " The operation timeout is " + timeout + "ms!"; break; case ERR_OBJECT_NOT_FOUND: message = " The replica server doesn't serve this partition!"; diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestPException.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestPException.java index 90db903a..f395fe61 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestPException.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestPException.java @@ -7,6 +7,7 @@ import com.xiaomi.infra.pegasus.apps.update_request; import com.xiaomi.infra.pegasus.base.blob; import com.xiaomi.infra.pegasus.base.error_code; +import com.xiaomi.infra.pegasus.base.error_code.error_types; import com.xiaomi.infra.pegasus.base.gpid; import com.xiaomi.infra.pegasus.operator.rrdb_put_operator; import com.xiaomi.infra.pegasus.rpc.KeyHasher; @@ -58,7 +59,8 @@ public void testHandleReplicationException() throws Exception { op.rpc_error.errno = error_code.error_types.ERR_OBJECT_NOT_FOUND; // set failure in promise, the exception is thrown as ExecutionException. - PegasusTable.handleReplicaException(promise, op, table, 1000); + PegasusTable pegasusTable = new PegasusTable(null, table); + pegasusTable.handleReplicaException(promise, op, table, 1000); try { promise.get(); } catch (ExecutionException e) { @@ -76,4 +78,33 @@ public void testHandleReplicationException() throws Exception { } Assert.fail(); } + + @Test + public void testTimeOutIsZero() throws Exception { + // ensure "PException ERR_TIMEOUT" is thrown with the real timeout value, when user given + // timeout is 0. + String[] metaList = {"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"}; + ClusterManager manager = new ClusterManager(1000, 1, false, null, 60, metaList); + TableHandler table = manager.openTable("temp", KeyHasher.DEFAULT); + DefaultPromise promise = table.newPromise(); + update_request req = new update_request(new blob(), new blob(), 100); + gpid gpid = table.getGpidByHash(1); + rrdb_put_operator op = new rrdb_put_operator(gpid, table.getTableName(), req, 0); + op.rpc_error.errno = error_types.ERR_TIMEOUT; + + PegasusTable pegasusTable = new PegasusTable(null, table); + pegasusTable.handleReplicaException(promise, op, table, 0); + try { + promise.get(); + } catch (Exception e) { + TableHandler.ReplicaConfiguration replicaConfig = table.getReplicaConfig(gpid.get_pidx()); + String server = replicaConfig.primary.get_ip() + ":" + replicaConfig.primary.get_port(); + + String msg = + String.format( + "com.xiaomi.infra.pegasus.client.PException: {version}: com.xiaomi.infra.pegasus.rpc.ReplicationException: ERR_TIMEOUT: [table=temp,operation=put,replicaServer=%s,gpid=(%s)] The operation timeout is 1000ms!", + server, gpid.toString()); + Assert.assertEquals(e.getMessage(), msg); + } + } }