StateAnalyzer.java
7.57 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
package com.dianping.cat.consumer.state;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicLong;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Constants;
import com.dianping.cat.analysis.AbstractMessageAnalyzer;
import com.dianping.cat.config.server.ServerFilterConfigManager;
import com.dianping.cat.configuration.NetworkInterfaceManager;
import com.dianping.cat.consumer.state.model.entity.Detail;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.Message;
import com.dianping.cat.consumer.state.model.entity.ProcessDomain;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.report.ReportManager;
import com.dianping.cat.report.DefaultReportManager.StoragePolicy;
import com.dianping.cat.statistic.ServerStatistic.Statistic;
import com.dianping.cat.statistic.ServerStatisticManager;
public class StateAnalyzer extends AbstractMessageAnalyzer<StateReport> implements LogEnabled {
public static final String ID = "state";
@Inject(ID)
private ReportManager<StateReport> m_reportManager;
@Inject
private ServerStatisticManager m_serverStateManager;
@Inject
private ServerFilterConfigManager m_serverFilterConfigManager;
@Inject
private String m_ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
private Machine buildStateInfo(Machine machine) {
long minute = 1000 * 60;
long start = m_startTime;
long end = m_startTime + minute * 60;
double maxTps = 0;
long current = System.currentTimeMillis();
int size = 0;
if (end > current) {
end = current;
}
for (; start < end; start += minute) {
Statistic state = m_serverStateManager.findOrCreateState(start);
Message temp = machine.findOrCreateMessage(start);
Map<String, AtomicLong> totals = state.getMessageTotals();
Map<String, AtomicLong> totalLosses = state.getMessageTotalLosses();
Map<String, AtomicLong> sizes = state.getMessageSizes();
for (Entry<String, AtomicLong> entry : totals.entrySet()) {
String domain = entry.getKey();
long value = entry.getValue().get();
ProcessDomain processDomain = machine.findOrCreateProcessDomain(domain);
Detail detail = processDomain.findOrCreateDetail(start);
processDomain.setTotal(value + processDomain.getTotal());
detail.setTotal(value + detail.getTotal());
}
for (Entry<String, AtomicLong> entry : totalLosses.entrySet()) {
String domain = entry.getKey();
long value = entry.getValue().get();
ProcessDomain processDomain = machine.findOrCreateProcessDomain(domain);
Detail detail = processDomain.findOrCreateDetail(start);
processDomain.setTotalLoss(value + processDomain.getTotalLoss());
detail.setTotalLoss(value + detail.getTotalLoss());
}
for (Entry<String, AtomicLong> entry : sizes.entrySet()) {
String domain = entry.getKey();
long value = entry.getValue().get();
ProcessDomain processDomain = machine.findOrCreateProcessDomain(domain);
Detail detail = processDomain.findOrCreateDetail(start);
processDomain.setSize(value + processDomain.getSize());
detail.setSize(value + detail.getSize());
}
long messageTotal = state.getMessageTotal();
long messageTotalLoss = state.getMessageTotalLoss();
long messageSize = state.getMessageSize();
long blockTotal = state.getBlockTotal();
long blockLoss = state.getBlockLoss();
long blockTime = state.getBlockTime();
long pigeonTimeError = state.getPigeonTimeError();
long networkTimeError = state.getNetworkTimeError();
long messageDump = state.getMessageDump();
long messageDumpLoss = state.getMessageDumpLoss();
int processDelayCount = state.getProcessDelayCount();
double processDelaySum = state.getProcessDelaySum();
temp.setTotal(messageTotal).setTotalLoss(messageTotalLoss).setSize(messageSize);
temp.setBlockTotal(blockTotal).setBlockLoss(blockLoss).setBlockTime(blockTime);
temp.setPigeonTimeError(pigeonTimeError).setNetworkTimeError(networkTimeError).setDump(messageDump);
temp.setDumpLoss(messageDumpLoss).setDelayCount(processDelayCount).setDelaySum(processDelaySum);
machine.setTotal(messageTotal + machine.getTotal()).setTotalLoss(messageTotalLoss + machine.getTotalLoss())
.setSize(messageSize + machine.getSize());
machine.setBlockTotal(machine.getBlockTotal() + blockTotal).setBlockLoss(machine.getBlockLoss() + blockLoss)
.setBlockTime(machine.getBlockTime() + blockTime);
machine.setPigeonTimeError(machine.getPigeonTimeError() + pigeonTimeError)
.setNetworkTimeError(machine.getNetworkTimeError() + networkTimeError)
.setDump(machine.getDump() + messageDump);
machine.setDumpLoss(machine.getDumpLoss() + messageDumpLoss)
.setDelayCount(machine.getDelayCount() + processDelayCount)
.setDelaySum(machine.getDelaySum() + processDelaySum);
double avg = 0;
long count = machine.getDelayCount();
if (count > 0) {
avg = machine.getDelaySum() / count;
machine.setDelayAvg(avg);
}
if (messageTotal > maxTps) {
maxTps = messageTotal;
}
temp.setTime(new Date(start));
size++;
}
double avgTps = 0;
if (size > 0) {
avgTps = machine.getTotal() / (double) size;
}
machine.setAvgTps(avgTps);
machine.setMaxTps(maxTps);
return machine;
}
@Override
public synchronized void doCheckpoint(boolean atEnd) {
long startTime = getStartTime();
StateReport stateReport = getReport(Constants.CAT);
Map<String, StateReport> reports = m_reportManager.getHourlyReports(startTime);
reports.put(Constants.CAT, stateReport);
if (atEnd && !isLocalMode()) {
m_reportManager.storeHourlyReports(startTime, StoragePolicy.FILE_AND_DB, m_index);
} else {
m_reportManager.storeHourlyReports(startTime, StoragePolicy.FILE, m_index);
}
if (atEnd) {
long minute = 1000 * 60;
long start = m_startTime - minute * 60 * 2;
long end = m_startTime - minute * 60;
for (; start < end; start += minute) {
m_serverStateManager.removeState(start);
}
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public StateReport getReport(String domain) {
StateReport report = new StateReport(Constants.CAT);
report.setStartTime(new Date(m_startTime));
report.setEndTime(new Date(m_startTime + MINUTE * 60 - 1));
Machine machine = buildStateInfo(report.findOrCreateMachine(m_ip));
StateReport stateReport = m_reportManager.getHourlyReport(getStartTime(), Constants.CAT, true);
Map<String, ProcessDomain> processDomains = stateReport.findOrCreateMachine(m_ip).getProcessDomains();
for (Map.Entry<String, ProcessDomain> entry : machine.getProcessDomains().entrySet()) {
ProcessDomain processDomain = processDomains.get(entry.getKey());
if (processDomain != null) {
entry.getValue().getIps().addAll(processDomain.getIps());
}
}
return report;
}
@Override
public ReportManager<StateReport> getReportManager() {
return m_reportManager;
}
@Override
protected void loadReports() {
// do nothing
}
@Override
protected void process(MessageTree tree) {
String domain = tree.getDomain();
if (m_serverFilterConfigManager.validateDomain(domain)) {
StateReport report = m_reportManager.getHourlyReport(getStartTime(), Constants.CAT, true);
String ip = tree.getIpAddress();
Machine machine = report.findOrCreateMachine(NetworkInterfaceManager.INSTANCE.getLocalHostAddress());
machine.findOrCreateProcessDomain(domain).addIp(ip);
}
}
}