Skip to content
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
2 changes: 2 additions & 0 deletions src/main/java/org/tikv/common/log/SlowLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public interface SlowLog {

SlowLogSpan start(String name);

void setError(Throwable err);

void log();
}
3 changes: 3 additions & 0 deletions src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public SlowLogSpan start(String name) {
return SlowLogSpanEmptyImpl.INSTANCE;
}

@Override
public void setError(Throwable err) {}

@Override
public void log() {}
}
11 changes: 10 additions & 1 deletion src/main/java/org/tikv/common/log/SlowLogImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SlowLogImpl implements SlowLog {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");

private final List<SlowLogSpan> slowLogSpans = new ArrayList<>();
private Throwable error = null;

private final long startMS;
private final long slowThresholdMS;
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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<String, String> entry : properties.entrySet()) {
jsonObject.addProperty(entry.getKey(), entry.getValue());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/tikv/raw/RawKVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -249,6 +250,7 @@ public void compareAndSet(
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
Expand Down Expand Up @@ -291,6 +293,7 @@ public void batchPut(Map<ByteString, ByteString> 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();
Expand Down Expand Up @@ -333,6 +336,7 @@ public Optional<ByteString> get(ByteString key) {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
Expand Down Expand Up @@ -367,6 +371,7 @@ public List<KvPair> batchGet(List<ByteString> keys) {
return result;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
Expand Down Expand Up @@ -400,6 +405,7 @@ public void batchDelete(List<ByteString> keys) {
return;
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
Expand Down Expand Up @@ -442,6 +448,7 @@ public Optional<Long> getKeyTTL(ByteString key) {
}
} catch (Exception e) {
RAW_REQUEST_FAILURE.labels(label).inc();
slowLog.setError(e);
throw e;
} finally {
requestTimer.observeDuration();
Expand Down Expand Up @@ -589,6 +596,7 @@ public List<KvPair> 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();
Expand Down Expand Up @@ -677,6 +685,7 @@ public List<KvPair> 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();
Expand Down Expand Up @@ -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();
Expand Down