HdfsMessageBucketManager.java
4.78 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
package com.dianping.cat.hadoop.hdfs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.unidal.helper.Threads;
import org.unidal.helper.Threads.Task;
import org.unidal.lookup.ContainerHolder;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.config.server.ServerConfigManager;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.PathBuilder;
import com.dianping.cat.message.MessageProducer;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.internal.MessageId;
import com.dianping.cat.message.spi.MessageTree;
import com.dianping.cat.message.storage.MessageBucket;
import com.dianping.cat.message.storage.MessageBucketManager;
public class HdfsMessageBucketManager extends ContainerHolder implements MessageBucketManager, Initializable {
public static final String ID = "hdfs";
@Inject
private FileSystemManager m_manager;
@Inject
private PathBuilder m_pathBuilder;
@Inject
private ServerConfigManager m_serverConfigManager;
private Map<String, HdfsMessageBucket> m_buckets = new ConcurrentHashMap<String, HdfsMessageBucket>();
private void closeIdleBuckets() throws IOException {
long now = System.currentTimeMillis();
long hour = 3600 * 1000L;
Set<String> closed = new HashSet<String>();
for (Map.Entry<String, HdfsMessageBucket> entry : m_buckets.entrySet()) {
HdfsMessageBucket bucket = entry.getValue();
if (now - bucket.getLastAccessTime() >= hour) {
try {
bucket.close();
closed.add(entry.getKey());
} catch (Exception e) {
Cat.logError(e);
}
}
}
for (String close : closed) {
HdfsMessageBucket bucket = m_buckets.remove(close);
release(bucket);
}
}
@Override
public void initialize() throws InitializationException {
if (m_serverConfigManager.isHdfsOn()) {
Threads.forGroup("cat").start(new IdleChecker());
}
}
@Override
public MessageTree loadMessage(String messageId) {
if (!m_serverConfigManager.isHdfsOn()) {
return null;
}
MessageProducer cat = Cat.getProducer();
Transaction t = cat.newTransaction("BucketService", getClass().getSimpleName());
t.setStatus(Message.SUCCESS);
try {
MessageId id = MessageId.parse(messageId);
final String path = m_pathBuilder.getLogviewPath(new Date(id.getTimestamp()), "");
final StringBuilder sb = new StringBuilder();
FileSystem fs = m_manager.getFileSystem(ServerConfigManager.DUMP_DIR, sb);
sb.append('/').append(path);
final String key = id.getDomain() + '-' + id.getIpAddress();
final String str = sb.toString();
final Path basePath = new Path(str);
final List<String> paths = new ArrayList<String>();
fs.listStatus(basePath, new PathFilter() {
@Override
public boolean accept(Path p) {
String name = p.getName();
if (name.contains(key) && !name.endsWith(".idx")) {
paths.add(path + name);
}
return false;
}
});
t.addData(paths.toString());
for (String dataFile : paths) {
try {
Cat.getProducer().logEvent("HDFSBucket", dataFile);
HdfsMessageBucket bucket = m_buckets.get(dataFile);
if (bucket == null) {
bucket = (HdfsMessageBucket) lookup(MessageBucket.class, HdfsMessageBucket.ID);
bucket.initialize(dataFile);
m_buckets.put(dataFile, bucket);
}
if (bucket != null) {
MessageTree tree = bucket.findById(messageId);
if (tree != null && tree.getMessageId().equals(messageId)) {
t.addData("path", dataFile);
return tree;
}
}
} catch (Exception e) {
t.setStatus(e);
Cat.logError(e);
}
}
} catch (IOException e) {
t.setStatus(e);
cat.logError(e);
} catch (RuntimeException e) {
t.setStatus(e);
cat.logError(e);
throw e;
} finally {
t.complete();
}
return null;
}
@Override
public void storeMessage(MessageTree tree, MessageId id) {
throw new UnsupportedOperationException("Not supported by HDFS!");
}
class IdleChecker implements Task {
@Override
public String getName() {
return "HdfsMessageBucketManager-IdleChecker";
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(60 * 1000L); // 1 minute
try {
closeIdleBuckets();
} catch (IOException e) {
Cat.logError(e);
}
}
} catch (InterruptedException e) {
// ignore it
}
}
@Override
public void shutdown() {
}
}
}