diff --git a/src/main/java/org/tikv/common/log/SlowLog.java b/src/main/java/org/tikv/common/log/SlowLog.java index e172a003462..007f3f1d736 100644 --- a/src/main/java/org/tikv/common/log/SlowLog.java +++ b/src/main/java/org/tikv/common/log/SlowLog.java @@ -22,5 +22,7 @@ public interface SlowLog { SlowLogSpan start(String name); + void setError(Throwable err); + void log(); } diff --git a/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java b/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java index 37e68237db5..5e76031f7c4 100644 --- a/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java +++ b/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java @@ -30,6 +30,9 @@ public SlowLogSpan start(String name) { return SlowLogSpanEmptyImpl.INSTANCE; } + @Override + public void setError(Throwable err) {} + @Override public void log() {} } diff --git a/src/main/java/org/tikv/common/log/SlowLogImpl.java b/src/main/java/org/tikv/common/log/SlowLogImpl.java index 76ed4c922bf..3a722b90476 100644 --- a/src/main/java/org/tikv/common/log/SlowLogImpl.java +++ b/src/main/java/org/tikv/common/log/SlowLogImpl.java @@ -35,6 +35,7 @@ public class SlowLogImpl implements SlowLog { public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS"); private final List slowLogSpans = new ArrayList<>(); + private Throwable error = null; private final long startMS; private final long slowThresholdMS; @@ -63,10 +64,15 @@ public synchronized SlowLogSpan start(String name) { return slowLogSpan; } + @Override + public void setError(Throwable err) { + this.error = err; + } + @Override public void log() { long currentMS = System.currentTimeMillis(); - if (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS) { + if (error != null || (slowThresholdMS >= 0 && currentMS - startMS > slowThresholdMS)) { logger.warn("SlowLog:" + getSlowLogString(currentMS)); } } @@ -77,6 +83,9 @@ private String getSlowLogString(long currentMS) { jsonObject.addProperty("start", DATE_FORMAT.format(startMS)); jsonObject.addProperty("end", DATE_FORMAT.format(currentMS)); jsonObject.addProperty("duration", (currentMS - startMS) + "ms"); + if (error != null) { + jsonObject.addProperty("error", error.getMessage()); + } for (Map.Entry entry : properties.entrySet()) { jsonObject.addProperty(entry.getKey(), entry.getValue()); diff --git a/src/main/java/org/tikv/raw/RawKVClient.java b/src/main/java/org/tikv/raw/RawKVClient.java index d68e162912d..76ef92581a1 100644 --- a/src/main/java/org/tikv/raw/RawKVClient.java +++ b/src/main/java/org/tikv/raw/RawKVClient.java @@ -158,6 +158,7 @@ private void put(ByteString key, ByteString value, long ttl, boolean atomic) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -214,6 +215,7 @@ public ByteString putIfAbsent(ByteString key, ByteString value, long ttl) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -279,6 +281,7 @@ private void batchPut(Map kvPairs, long ttl, boolean ato RAW_REQUEST_SUCCESS.labels(label).inc(); } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -321,6 +324,7 @@ public ByteString get(ByteString key) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -355,6 +359,7 @@ public List batchGet(List keys) { return result; } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -401,6 +406,7 @@ private void batchDelete(List keys, boolean atomic) { return; } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -443,6 +449,7 @@ public Long getKeyTTL(ByteString key) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -547,6 +554,7 @@ public List scan(ByteString startKey, ByteString endKey, int limit, bool return result; } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -635,6 +643,7 @@ public List scan(ByteString startKey, ByteString endKey, boolean keyOnly return result; } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -717,6 +726,7 @@ private void delete(ByteString key, boolean atomic) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration();