TransactionAnalyzer.java
8.12 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.dianping.cat.consumer.transaction;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.lookup.annotation.Inject;
import org.unidal.tuple.Pair;
import com.dianping.cat.Cat;
import com.dianping.cat.Constants;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.config.server.ServerFilterConfigManager;
import com.dianping.cat.consumer.transaction.model.entity.Duration;
import com.dianping.cat.consumer.transaction.model.entity.Range;
import com.dianping.cat.consumer.transaction.model.entity.Range2;
import com.dianping.cat.consumer.transaction.model.entity.TransactionName;
import com.dianping.cat.consumer.transaction.model.entity.TransactionReport;
import com.dianping.cat.consumer.transaction.model.entity.TransactionType;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.ReportManager;
import com.dianping.cat.report.DefaultReportManager.StoragePolicy;
public class TransactionAnalyzer extends AbstractMessageAnalyzer<TransactionReport> implements LogEnabled {
@Inject
private TransactionDelegate m_delegate;
@Inject(ID)
private ReportManager<TransactionReport> m_reportManager;
@Inject
private ServerFilterConfigManager m_serverFilterConfigManager;
private TransactionStatisticsComputer m_computer = new TransactionStatisticsComputer();
public static final String ID = "transaction";
private Pair<Boolean, Long> checkForTruncatedMessage(MessageTree tree, Transaction t) {
Pair<Boolean, Long> pair = new Pair<Boolean, Long>(true, t.getDurationInMicros());
List<Message> children = t.getChildren();
int size = children.size();
if (tree.getMessage() == t && size > 0) { // root transaction with children
Message last = children.get(size - 1);
if (last instanceof Event) {
String type = last.getType();
String name = last.getName();
if (type.equals("RemoteCall") && name.equals("Next")) {
pair.setKey(false);
} else if (type.equals("TruncatedTransaction") && name.equals("TotalDuration")) {
try {
long delta = Long.parseLong(last.getData().toString());
pair.setValue(delta);
} catch (Exception e) {
Cat.logError(e);
}
}
}
}
return pair;
}
private double computeDuration(double duration) {
if (duration < 20) {
return duration;
} else if (duration < 200) {
return duration - duration % 5;
} else if (duration < 2000) {
return duration - duration % 50;
} else {
return duration - duration % 500;
}
}
@Override
public synchronized void doCheckpoint(boolean atEnd) {
if (atEnd && !isLocalMode()) {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE_AND_DB, m_index);
} else {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public int getAnanlyzerCount() {
return 2;
}
public Set<String> getDomains() {
return m_reportManager.getDomains(getStartTime());
}
public TransactionReport getRawReport(String domain) {
TransactionReport report = m_reportManager.getHourlyReport(getStartTime(), domain, false);
return report;
}
@Override
public TransactionReport getReport(String domain) {
if (!Constants.ALL.equals(domain)) {
try {
return queryReport(domain);
} catch (Exception e) {
try {
return queryReport(domain);
// for concurrent modify exception
} catch (ConcurrentModificationException ce) {
Cat.logEvent("ConcurrentModificationException", domain, Event.SUCCESS, null);
return new TransactionReport(domain);
}
}
} else {
Map<String, TransactionReport> reports = m_reportManager.getHourlyReports(getStartTime());
return m_delegate.createAggregatedReport(reports);
}
}
@Override
public ReportManager<TransactionReport> getReportManager() {
return m_reportManager;
}
@Override
protected void loadReports() {
m_reportManager.loadHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
@Override
public void process(MessageTree tree) {
String domain = tree.getDomain();
TransactionReport report = m_reportManager.getHourlyReport(getStartTime(), domain, true);
Message message = tree.getMessage();
report.addIp(tree.getIpAddress());
if (message instanceof Transaction) {
Transaction root = (Transaction) message;
processTransaction(report, tree, root);
}
}
private void processNameGraph(Transaction t, TransactionName name, int min, double d) {
int dk = 1;
if (d > 65536) {
dk = 65536;
} else {
if (dk > 256) {
dk = 256;
}
while (dk < d) {
dk <<= 1;
}
}
Duration duration = name.findOrCreateDuration(dk);
Range range = name.findOrCreateRange(min);
duration.incCount();
range.incCount();
if (!t.isSuccess()) {
range.incFails();
}
range.setSum(range.getSum() + d);
}
protected void processTransaction(TransactionReport report, MessageTree tree, Transaction t) {
String type = t.getType();
String name = t.getName();
if (m_serverFilterConfigManager.discardTransaction(type, name)) {
return;
} else {
Pair<Boolean, Long> pair = checkForTruncatedMessage(tree, t);
if (pair.getKey().booleanValue()) {
String ip = tree.getIpAddress();
TransactionType transactionType = report.findOrCreateMachine(ip).findOrCreateType(type);
TransactionName transactionName = transactionType.findOrCreateName(name);
String messageId = tree.getMessageId();
processTypeAndName(t, transactionType, transactionName, messageId, pair.getValue().doubleValue() / 1000d);
}
List<Message> children = t.getChildren();
for (Message child : children) {
if (child instanceof Transaction) {
processTransaction(report, tree, (Transaction) child);
}
}
}
}
protected void processTypeAndName(Transaction t, TransactionType type, TransactionName name, String messageId,
double duration) {
type.incTotalCount();
name.incTotalCount();
if (t.isSuccess()) {
if (type.getSuccessMessageUrl() == null) {
type.setSuccessMessageUrl(messageId);
}
if (name.getSuccessMessageUrl() == null) {
name.setSuccessMessageUrl(messageId);
}
} else {
type.incFailCount();
name.incFailCount();
if (type.getFailMessageUrl() == null) {
type.setFailMessageUrl(messageId);
}
if (name.getFailMessageUrl() == null) {
name.setFailMessageUrl(messageId);
}
}
int allDuration = ((int) computeDuration(duration));
double sum = duration * duration;
name.setMax(Math.max(name.getMax(), duration));
name.setMin(Math.min(name.getMin(), duration));
name.setSum(name.getSum() + duration);
name.setSum2(name.getSum2() + sum);
name.findOrCreateAllDuration(allDuration).incCount();
type.setMax(Math.max(type.getMax(), duration));
type.setMin(Math.min(type.getMin(), duration));
type.setSum(type.getSum() + duration);
type.setSum2(type.getSum2() + sum);
type.findOrCreateAllDuration(allDuration).incCount();
long current = t.getTimestamp() / 1000 / 60;
int min = (int) (current % (60));
processNameGraph(t, name, min, duration);
processTypeRange(t, type, min, duration);
}
private void processTypeRange(Transaction t, TransactionType type, int min, double d) {
Range2 range = type.findOrCreateRange2(min);
if (!t.isSuccess()) {
range.incFails();
}
range.incCount();
range.setSum(range.getSum() + d);
}
private TransactionReport queryReport(String domain) {
long period = getStartTime();
long timestamp = System.currentTimeMillis();
long remainder = timestamp % ONE_HOUR;
long current = timestamp - remainder;
TransactionReport report = m_reportManager.getHourlyReport(period, domain, false);
report.getDomainNames().addAll(m_reportManager.getDomains(period));
if (period == current) {
report.accept(m_computer.setDuration(remainder / 1000));
} else if (period < current) {
report.accept(m_computer.setDuration(3600));
}
return report;
}
}