Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

feat: add more exception info on synchronous APIs #62

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public enum error_types {

// ERROR_CODE defined by client
ERR_SESSION_RESET,
ERR_THREAD_INTERRUPTED,
};

public error_types errno;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/xiaomi/infra/pegasus/client/PException.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;

import com.xiaomi.infra.pegasus.base.error_code;
import com.xiaomi.infra.pegasus.rpc.ReplicationException;
import java.util.concurrent.TimeoutException;

/**
* The generic type of exception thrown by all of the Pegasus APIs.
*
Expand All @@ -27,4 +31,20 @@ public PException(String message) {
public PException(Throwable cause) {
super(cause);
}

static PException threadInterrupted(String tableName, InterruptedException e) {
return new PException(
new ReplicationException(
error_code.error_types.ERR_THREAD_INTERRUPTED,
String.format("[table=%s] Thread was interrupted: %s", tableName, e.getMessage())));
}

static PException timeout(String tableName, int timeout, TimeoutException e) {
return new PException(
new ReplicationException(
error_code.error_types.ERR_TIMEOUT,
String.format(
"[table=%s, timeout=%dms] Timeout on Future await: %s",
tableName, timeout, e.getMessage())));
}
}
84 changes: 42 additions & 42 deletions src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,9 @@ public boolean exist(byte[] hashKey, byte[] sortKey, int timeout) throws PExcept
try {
return asyncExist(hashKey, sortKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -876,9 +876,9 @@ public long sortKeyCount(byte[] hashKey, int timeout) throws PException {
try {
return asyncSortKeyCount(hashKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -890,9 +890,9 @@ public byte[] get(byte[] hashKey, byte[] sortKey, int timeout) throws PException
try {
return asyncGet(hashKey, sortKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -969,9 +969,9 @@ public MultiGetResult multiGet(
return asyncMultiGet(hashKey, sortKeys, maxFetchCount, maxFetchSize, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -984,9 +984,9 @@ public MultiGetResult multiGet(byte[] hashKey, List<byte[]> sortKeys, int timeou
try {
return asyncMultiGet(hashKey, sortKeys, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1008,9 +1008,9 @@ public MultiGetResult multiGet(
hashKey, startSortKey, stopSortKey, options, maxFetchCount, maxFetchSize, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1029,9 +1029,9 @@ public MultiGetResult multiGet(
return asyncMultiGet(hashKey, startSortKey, stopSortKey, options, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1112,9 +1112,9 @@ public MultiGetSortKeysResult multiGetSortKeys(
return asyncMultiGetSortKeys(hashKey, maxFetchCount, maxFetchSize, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1126,9 +1126,9 @@ public MultiGetSortKeysResult multiGetSortKeys(byte[] hashKey, int timeout) thro
try {
return asyncMultiGetSortKeys(hashKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1141,9 +1141,9 @@ public void set(byte[] hashKey, byte[] sortKey, byte[] value, int ttlSeconds, in
try {
asyncSet(hashKey, sortKey, value, ttlSeconds, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1155,9 +1155,9 @@ public void set(byte[] hashKey, byte[] sortKey, byte[] value, int timeout) throw
try {
asyncSet(hashKey, sortKey, value, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1215,9 +1215,9 @@ public void multiSet(
try {
asyncMultiSet(hashKey, values, ttlSeconds, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1230,9 +1230,9 @@ public void multiSet(byte[] hashKey, List<Pair<byte[], byte[]>> values, int time
try {
asyncMultiSet(hashKey, values, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1298,9 +1298,9 @@ public void del(byte[] hashKey, byte[] sortKey, int timeout) throws PException {
try {
asyncDel(hashKey, sortKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1362,9 +1362,9 @@ public void multiDel(byte[] hashKey, List<byte[]> sortKeys, int timeout) throws
try {
asyncMultiDel(hashKey, sortKeys, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1517,9 +1517,9 @@ public long incr(byte[] hashKey, byte[] sortKey, long increment, int ttlSeconds,
return asyncIncr(hashKey, sortKey, increment, ttlSeconds, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1531,9 +1531,9 @@ public long incr(byte[] hashKey, byte[] sortKey, long increment, int timeout) th
try {
return asyncIncr(hashKey, sortKey, increment, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down Expand Up @@ -1563,9 +1563,9 @@ public CheckAndSetResult checkAndSet(
timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1587,9 +1587,9 @@ public CheckAndMutateResult checkAndMutate(
hashKey, checkSortKey, checkType, checkOperand, mutations, options, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1610,9 +1610,9 @@ public CompareExchangeResult compareExchange(
hashKey, sortKey, expectedValue, desiredValue, ttlSeconds, timeout)
.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand All @@ -1624,9 +1624,9 @@ public int ttl(byte[] hashKey, byte[] sortKey, int timeout) throws PException {
try {
return asyncTTL(hashKey, sortKey, timeout).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.threadInterrupted(table.getTableName(), e);
} catch (TimeoutException e) {
throw new PException(new ReplicationException(error_code.error_types.ERR_TIMEOUT));
throw PException.timeout(table.getTableName(), timeout, e);
} catch (ExecutionException e) {
throw new PException(e);
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/xiaomi/infra/pegasus/client/TestPException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2019, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

package com.xiaomi.infra.pegasus.client;

import java.util.concurrent.TimeoutException;
import org.junit.Assert;
import org.junit.Test;

public class TestPException {
@Test
public void testThreadInterrupted() throws Exception {
PException ex = PException.threadInterrupted("test", new InterruptedException("intxxx"));
Assert.assertEquals(
"com.xiaomi.infra.pegasus.rpc.ReplicationException: ERR_THREAD_INTERRUPTED: [table=test] Thread was interrupted: intxxx",
ex.getMessage());
}

@Test
public void testTimeout() throws Exception {
PException ex = PException.timeout("test", 1000, new TimeoutException("tmxxx"));
Assert.assertEquals(
"com.xiaomi.infra.pegasus.rpc.ReplicationException: ERR_TIMEOUT: [table=test, timeout=1000ms] Timeout on Future await: tmxxx",
ex.getMessage());
}
}