blob: 45b1a11b4affa54cddcab0c1df5ad63b9c317066 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.clientmetrics;
import com.yahoo.documentapi.messagebus.protocol.DocumentIgnoredReply;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.messagebus.Reply;
import com.yahoo.concurrent.SystemTimer;
import com.yahoo.messagebus.Error;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
/**
* @author thomasg
*/
public class MessageTypeMetricSet {
public long latency_total;
public long latency_min = Long.MAX_VALUE;
public long latency_max = Long.MIN_VALUE;
public long count = 0;
public long ignored = 0;
public long errorCount = 0;
private final Map<String, Long> errorCounts = new HashMap<>();
private final String msgName;
public MessageTypeMetricSet(String msgName) {
this.msgName = msgName;
}
public String getMessageName() {
return msgName;
}
public void addReply(Reply r) {
if (!r.hasErrors() || onlyTestAndSetConditionFailed(r.getErrors())) {
updateSuccessMetrics(r);
} else {
updateFailureMetrics(r);
}
}
private void updateFailureMetrics(Reply r) {
errorCount++;
String error = DocumentProtocol.getErrorName(r.getError(0).getCode());
Long s = errorCounts.get(error);
if (s == null) {
errorCounts.put(error, 1L);
} else {
errorCounts.put(error, s+1);
}
}
private void updateSuccessMetrics(Reply r) {
if (!(r instanceof DocumentIgnoredReply)) {
if (r.getMessage().getTimeReceived() != 0) {
long latency = (SystemTimer.INSTANCE.milliTime() - r.getMessage().getTimeReceived());
latency_max = Math.max(latency_max, latency);
latency_min = Math.min(latency_min, latency);
latency_total += latency;
}
count++;
} else {
ignored++;
}
}
/**
* Returns true if every error in a stream is a test and set condition failed
*/
private static boolean onlyTestAndSetConditionFailed(Stream<Error> errors) {
return errors.allMatch(e -> e.getCode() == DocumentProtocol.ERROR_TEST_AND_SET_CONDITION_FAILED);
}
}
|