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 a5f3dd6f3bb..7ffd1ed0514 100644 --- a/src/main/java/org/tikv/raw/RawKVClient.java +++ b/src/main/java/org/tikv/raw/RawKVClient.java @@ -151,6 +151,7 @@ public void put(ByteString key, ByteString value, long ttl) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -249,6 +250,7 @@ public void compareAndSet( } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -291,6 +293,7 @@ public void batchPut(Map kvPairs, long ttl) { RAW_REQUEST_SUCCESS.labels(label).inc(); } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -333,6 +336,7 @@ public Optional get(ByteString key) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -367,6 +371,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(); @@ -400,6 +405,7 @@ public void batchDelete(List keys) { return; } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -442,6 +448,7 @@ public Optional getKeyTTL(ByteString key) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration(); @@ -589,6 +596,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(); @@ -677,6 +685,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(); @@ -745,6 +754,7 @@ public void delete(ByteString key) { } } catch (Exception e) { RAW_REQUEST_FAILURE.labels(label).inc(); + slowLog.setError(e); throw e; } finally { requestTimer.observeDuration();