CrossAnalyzer.java
8.88 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.dianping.cat.consumer.cross;
import java.util.List;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.util.StringUtils;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.config.server.ServerConfigManager;
import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
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 CrossAnalyzer extends AbstractMessageAnalyzer<CrossReport> implements LogEnabled {
public static final String ID = "cross";
@Inject(ID)
protected ReportManager<CrossReport> m_reportManager;
@Inject
protected IpConvertManager m_ipConvertManager;
private int m_discardLogs = 0;
private int m_errorAppName;
public static final String DEFAULT = "unknown";
public CrossInfo convertCrossInfo(String client, CrossInfo crossInfo) {
String localAddress = crossInfo.getLocalAddress();
String remoteAddress = crossInfo.getRemoteAddress();
int index = remoteAddress.indexOf(":");
if (index > 0) {
remoteAddress = remoteAddress.substring(0, index);
}
CrossInfo info = new CrossInfo();
info.setLocalAddress(remoteAddress);
String clientPort = crossInfo.getClientPort();
if (clientPort == null) {
info.setRemoteAddress(localAddress);
} else {
info.setRemoteAddress(localAddress + ":" + clientPort);
}
info.setRemoteRole("Pigeon.Caller");
info.setDetailType("PigeonCall");
info.setApp(client);
return info;
}
@Override
public synchronized void doCheckpoint(boolean atEnd) {
if (atEnd && !isLocalMode()) {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE_AND_DB, m_index);
m_logger.info("discard server logview count " + m_discardLogs + ", errorAppName " + m_errorAppName);
} else {
m_reportManager.storeHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public int getAnanlyzerCount() {
return 2;
}
@Override
public CrossReport getReport(String domain) {
CrossReport report = m_reportManager.getHourlyReport(getStartTime(), domain, false);
report.getDomainNames().addAll(m_reportManager.getDomains(getStartTime()));
return report;
}
@Override
public ReportManager<CrossReport> getReportManager() {
return m_reportManager;
}
@Override
protected void loadReports() {
m_reportManager.loadHourlyReports(getStartTime(), StoragePolicy.FILE, m_index);
}
public CrossInfo parseCorssTransaction(Transaction t, MessageTree tree) {
String type = t.getType();
if (m_serverConfigManager.isRpcClient(type)) {
return parsePigeonClientTransaction(t, tree);
} else if (m_serverConfigManager.isRpcServer(type)) {
return parsePigeonServerTransaction(t, tree);
}
return null;
}
private CrossInfo parsePigeonClientTransaction(Transaction t, MessageTree tree) {
CrossInfo crossInfo = new CrossInfo();
String localAddress = tree.getIpAddress();
List<Message> messages = t.getChildren();
for (Message message : messages) {
if (message instanceof Event) {
String type = message.getType();
if (type.equals("PigeonCall.server") || type.equals("Call.server")) {
crossInfo.setRemoteAddress(message.getName());
}
if (type.equals("PigeonCall.app") || type.equals("Call.app")) {
crossInfo.setApp(message.getName());
}
if (type.equals("PigeonCall.port") || type.equals("Call.port")) {
crossInfo.setClientPort(message.getName());
}
}
}
crossInfo.setLocalAddress(localAddress);
crossInfo.setRemoteRole("Pigeon.Server");
crossInfo.setDetailType("PigeonCall");
return crossInfo;
}
private CrossInfo parsePigeonServerTransaction(Transaction t, MessageTree tree) {
CrossInfo crossInfo = new CrossInfo();
String localAddress = tree.getIpAddress();
List<Message> messages = t.getChildren();
for (Message message : messages) {
if (message instanceof Event) {
String type = message.getType();
if (type.equals("PigeonService.client") || type.equals("Service.client")) {
crossInfo.setRemoteAddress(message.getName());
}
if (type.equals("PigeonService.app") || type.equals("Service.app")) {
crossInfo.setApp(message.getName());
}
}
}
crossInfo.setLocalAddress(localAddress);
crossInfo.setRemoteRole("Pigeon.Client");
crossInfo.setDetailType("PigeonService");
return crossInfo;
}
@Override
public void process(MessageTree tree) {
String domain = tree.getDomain();
CrossReport report = m_reportManager.getHourlyReport(getStartTime(), domain, true);
Message message = tree.getMessage();
report.addIp(tree.getIpAddress());
if (message instanceof Transaction) {
processTransaction(report, tree, (Transaction) message);
}
}
private void processTransaction(CrossReport report, MessageTree tree, Transaction t) {
CrossInfo crossInfo = parseCorssTransaction(t, tree);
if (crossInfo != null && crossInfo.validate()) {
updateCrossReport(report, t, crossInfo);
String targetDomain = crossInfo.getApp();
if (m_serverConfigManager.isRpcClient(t.getType()) && !DEFAULT.equals(targetDomain)) {
CrossInfo serverCrossInfo = convertCrossInfo(tree.getDomain(), crossInfo);
if (serverCrossInfo != null) {
CrossReport serverReport = m_reportManager.getHourlyReport(getStartTime(), targetDomain, true);
updateCrossReport(serverReport, t, serverCrossInfo);
}
} else {
m_errorAppName++;
}
}
List<Message> children = t.getChildren();
for (Message child : children) {
if (child instanceof Transaction) {
processTransaction(report, tree, (Transaction) child);
}
}
}
public void setIpConvertManager(IpConvertManager ipConvertManager) {
m_ipConvertManager = ipConvertManager;
}
public void setReportManager(ReportManager<CrossReport> reportManager) {
m_reportManager = reportManager;
}
public void setServerConfigManager(ServerConfigManager serverConfigManager) {
m_serverConfigManager = serverConfigManager;
}
private void updateCrossReport(CrossReport report, Transaction t, CrossInfo info) {
synchronized (report) {
String localIp = info.getLocalAddress();
String remoteIp = info.getRemoteAddress();
String role = info.getRemoteRole();
String transactionName = t.getName();
Local local = report.findOrCreateLocal(localIp);
String remoteId = remoteIp + ":" + role;
Remote remote = local.findOrCreateRemote(remoteId);
report.addIp(localIp);
if (StringUtils.isEmpty(remote.getIp())) {
remote.setIp(remoteIp);
}
if (StringUtils.isEmpty(remote.getRole())) {
remote.setRole(role);
}
if (StringUtils.isEmpty(remote.getApp())) {
remote.setApp(info.getApp());
}
Type type = remote.getType();
if (type == null) {
type = new Type();
type.setId(info.getDetailType());
remote.setType(type);
}
Name name = type.findOrCreateName(transactionName);
type.incTotalCount();
name.incTotalCount();
if (!t.isSuccess()) {
type.incFailCount();
name.incFailCount();
}
double duration = t.getDurationInMicros() / 1000d;
type.setSum(type.getSum() + duration);
name.setSum(name.getSum() + duration);
}
}
public static class CrossInfo {
private String m_remoteRole;
private String m_localAddress;
private String m_remoteAddress;
private String m_detailType;
private String m_app;
private String m_clientPort;
public String getApp() {
if (StringUtils.isEmpty(m_app)) {
return DEFAULT;
} else {
return m_app;
}
}
public String getClientPort() {
return m_clientPort;
}
public String getDetailType() {
return m_detailType;
}
public String getLocalAddress() {
return m_localAddress;
}
public String getRemoteAddress() {
return m_remoteAddress;
}
public String getRemoteRole() {
return m_remoteRole;
}
public void setApp(String app) {
m_app = app;
}
public void setClientPort(String clientPort) {
m_clientPort = clientPort;
}
public void setDetailType(String detailType) {
m_detailType = detailType;
}
public void setLocalAddress(String localAddress) {
m_localAddress = localAddress;
}
public void setRemoteAddress(String remoteAddress) {
m_remoteAddress = remoteAddress;
}
public void setRemoteRole(String remoteRole) {
m_remoteRole = remoteRole;
}
public boolean validate() {
if (m_localAddress != null && m_remoteAddress != null) {
return true;
} else {
return false;
}
}
}
}