StatusUpdateTask.java
4.97 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
package com.dianping.cat.status;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.unidal.helper.Threads.Task;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.configuration.ClientConfigManager;
import com.dianping.cat.configuration.NetworkInterfaceManager;
import com.dianping.cat.message.Heartbeat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.MessageProducer;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.internal.MilliSecondTimer;
import com.dianping.cat.message.spi.MessageStatistics;
import com.dianping.cat.status.model.entity.Extension;
import com.dianping.cat.status.model.entity.StatusInfo;
public class StatusUpdateTask implements Task, Initializable {
@Inject
private MessageStatistics m_statistics;
@Inject
private ClientConfigManager m_manager;
private boolean m_active = true;
private String m_ipAddress;
private long m_interval = 60 * 1000; // 60 seconds
private String m_jars;
private void buildClasspath() {
ClassLoader loader = StatusUpdateTask.class.getClassLoader();
StringBuilder sb = new StringBuilder();
buildClasspath(loader, sb);
if (sb.length() > 0) {
m_jars = sb.substring(0, sb.length() - 1);
}
}
private void buildClasspath(ClassLoader loader, StringBuilder sb) {
if (loader instanceof URLClassLoader) {
URL[] urLs = ((URLClassLoader) loader).getURLs();
for (URL url : urLs) {
String jar = parseJar(url.toExternalForm());
if (jar != null) {
sb.append(jar).append(',');
}
}
ClassLoader parent = loader.getParent();
buildClasspath(parent, sb);
}
}
private void buildExtensionData(StatusInfo status) {
StatusExtensionRegister res = StatusExtensionRegister.getInstance();
List<StatusExtension> extensions = res.getStatusExtension();
for (StatusExtension extension : extensions) {
String id = extension.getId();
String des = extension.getDescription();
Map<String, String> propertis = extension.getProperties();
Extension item = status.findOrCreateExtension(id).setDescription(des);
for (Entry<String, String> entry : propertis.entrySet()) {
try {
double value = Double.parseDouble(entry.getValue());
item.findOrCreateExtensionDetail(entry.getKey()).setValue(value);
} catch (Exception e) {
Cat.logError("StatusExtension can only be double type", e);
}
}
}
}
@Override
public String getName() {
return "StatusUpdateTask";
}
@Override
public void initialize() throws InitializationException {
m_ipAddress = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
}
private String parseJar(String path) {
if (path.endsWith(".jar")) {
int index = path.lastIndexOf('/');
if (index > -1) {
return path.substring(index + 1);
}
}
return null;
}
@Override
public void run() {
// try to wait cat client init success
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
return;
}
while (true) {
Calendar cal = Calendar.getInstance();
int second = cal.get(Calendar.SECOND);
// try to avoid send heartbeat at 59-01 second
if (second < 2 || second > 58) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore it
}
} else {
break;
}
}
try {
buildClasspath();
} catch (Exception e) {
e.printStackTrace();
}
MessageProducer cat = Cat.getProducer();
Transaction reboot = cat.newTransaction("System", "Reboot");
reboot.setStatus(Message.SUCCESS);
cat.logEvent("Reboot", NetworkInterfaceManager.INSTANCE.getLocalHostAddress(), Message.SUCCESS, null);
reboot.complete();
while (m_active) {
long start = MilliSecondTimer.currentTimeMillis();
if (m_manager.isCatEnabled()) {
Transaction t = cat.newTransaction("System", "Status");
Heartbeat h = cat.newHeartbeat("Heartbeat", m_ipAddress);
StatusInfo status = new StatusInfo();
t.addData("dumpLocked", m_manager.isDumpLocked());
try {
StatusInfoCollector statusInfoCollector = new StatusInfoCollector(m_statistics, m_jars);
status.accept(statusInfoCollector.setDumpLocked(m_manager.isDumpLocked()));
buildExtensionData(status);
h.addData(status.toString());
h.setStatus(Message.SUCCESS);
} catch (Throwable e) {
h.setStatus(e);
cat.logError(e);
} finally {
h.complete();
}
t.setStatus(Message.SUCCESS);
t.complete();
}
long elapsed = MilliSecondTimer.currentTimeMillis() - start;
if (elapsed < m_interval) {
try {
Thread.sleep(m_interval - elapsed);
} catch (InterruptedException e) {
break;
}
}
}
}
public void setInterval(long interval) {
m_interval = interval;
}
@Override
public void shutdown() {
m_active = false;
}
}