/
CrawlerStatsHelper.java
301 lines (258 loc) · 9.95 KB
/
CrawlerStatsHelper.java
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
/*
* Copyright 2012-2024 CodeLibs Project and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.helper;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.fess.crawler.entity.UrlQueue;
import org.codelibs.fess.taglib.FessFunctions;
import org.codelibs.fess.util.ComponentUtil;
import org.dbflute.optional.OptionalThing;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* @author shinsuke
*
*/
public class CrawlerStatsHelper {
private static final Logger logger = LogManager.getLogger(CrawlerStatsHelper.class);
private static final String BEGIN_KEY = "begin";
protected Logger statsLogger = null;
protected String loggerName = "fess.log.crawler.stats";
protected long maxCacheSize = 1000;
protected long cacheExpireAfterWrite = 10 * 60 * 1000L;
protected LoadingCache<String, StatsObject> statsCache;
@PostConstruct
public void init() {
statsLogger = LogManager.getLogger(loggerName);
statsCache = CacheBuilder.newBuilder().maximumSize(maxCacheSize).expireAfterWrite(cacheExpireAfterWrite, TimeUnit.MILLISECONDS)
.build(new CacheLoader<String, StatsObject>() {
@Override
public StatsObject load(final String key) {
return new StatsObject();
}
});
}
@PreDestroy
public void destroy() {
if (logger.isDebugEnabled()) {
logger.debug("cache stats: {}", statsCache.stats());
}
statsCache.asMap().entrySet().stream().forEach(e -> {
final StatsObject data = e.getValue();
final Long begin = data.remove(BEGIN_KEY);
if (begin != null) {
printStats(e.getKey(), data, begin, false);
}
});
}
public void begin(final Object keyObj) {
getCacheKey(keyObj).ifPresent(key -> {
try {
statsCache.get(key);
} catch (final Exception e) {
final StringBuilder buf = createStringBuffer(keyObj, getCurrentTimeMillis());
buf.append('\t').append("action:begin");
buf.append('\t').append("error:").append(escapeValue(e.getLocalizedMessage()).replaceAll("\\s", " "));
log(buf);
}
});
}
public void record(final Object keyObj, final StatsAction action) {
record(keyObj, action.name().toLowerCase(Locale.ENGLISH));
}
public void record(final Object keyObj, final String action) {
getCacheKey(keyObj).ifPresent(key -> {
try {
final StatsObject data = statsCache.getIfPresent(key);
if (data != null) {
data.put(escapeValue(action), getCurrentTimeMillis());
}
} catch (final Exception e) {
final StringBuilder buf = createStringBuffer(keyObj, getCurrentTimeMillis());
buf.append('\t').append("action:record");
buf.append('\t').append("error:").append(escapeValue(e.getLocalizedMessage()).replaceAll("\\s", " "));
log(buf);
}
});
}
public void done(final Object keyObj) {
getCacheKey(keyObj).ifPresent(key -> {
try {
final StatsObject data = statsCache.getIfPresent(key);
if (data != null && data.decrement() <= 0) {
statsCache.invalidate(key);
final Long begin = data.remove(BEGIN_KEY);
if (begin != null) {
printStats(keyObj, data, begin, true);
}
}
} catch (final Exception e) {
final StringBuilder buf = createStringBuffer(keyObj, getCurrentTimeMillis());
buf.append('\t').append("action:done");
buf.append('\t').append("error:").append(escapeValue(e.getLocalizedMessage()).replaceAll("\\s", " "));
log(buf);
}
});
}
public void discard(final Object keyObj) {
getCacheKey(keyObj).ifPresent(key -> {
try {
final StatsObject data = statsCache.getIfPresent(key);
if (data != null) {
statsCache.invalidate(key);
}
} catch (final Exception e) {
final StringBuilder buf = createStringBuffer(keyObj, getCurrentTimeMillis());
buf.append('\t').append("action:done");
buf.append('\t').append("error:").append(escapeValue(e.getLocalizedMessage()).replaceAll("\\s", " "));
log(buf);
}
});
}
protected void printStats(final Object keyObj, final StatsObject data, final long begin, final boolean done) {
final StringBuilder buf = createStringBuffer(keyObj, begin);
if (done) {
buf.append('\t').append("done:").append(getCurrentTimeMillis() - begin);
}
data.entrySet().stream().map(e -> escapeValue(e.getKey()) + ":" + (e.getValue().longValue() - begin)).map(s -> "\t" + s)
.forEach(s -> buf.append(s));
log(buf);
}
public void runOnThread(final Object keyObj) {
getCacheKey(keyObj).ifPresent(key -> {
try {
final StatsObject data = statsCache.getIfPresent(key);
if (data != null) {
data.increment();
}
} catch (final Exception e) {
final StringBuilder buf = createStringBuffer(keyObj, getCurrentTimeMillis());
buf.append('\t').append("action:record");
buf.append('\t').append("error:").append(escapeValue(e.getLocalizedMessage()).replaceAll("\\s", " "));
log(buf);
}
});
}
protected long getCurrentTimeMillis() {
return ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
}
private StringBuilder createStringBuffer(final Object keyObj, final long time) {
final StringBuilder buf = new StringBuilder(1000);
buf.append("url:").append(getUrl(keyObj));
buf.append('\t');
buf.append("time:").append(FessFunctions.formatDate(new Date(time)));
return buf;
}
protected String getUrl(final Object keyObj) {
if (keyObj instanceof final UrlQueue<?> urlQueue) {
return escapeValue(urlQueue.getUrl());
}
if (keyObj instanceof final StatsKeyObject statsKey) {
return escapeValue(statsKey.getUrl());
}
if (keyObj instanceof final String key) {
return escapeValue(key);
}
if (keyObj instanceof final Number key) {
return key.toString();
}
return "-";
}
protected OptionalThing<String> getCacheKey(final Object keyObj) {
if (keyObj instanceof final UrlQueue<?> urlQueue) {
return OptionalThing.of(urlQueue.getId().toString());
}
if (keyObj instanceof final StatsKeyObject statsKey) {
return OptionalThing.of(statsKey.getId());
}
if (keyObj instanceof final String key) {
return OptionalThing.of(key);
}
if (keyObj instanceof final Number key) {
return OptionalThing.of(key.toString());
}
return OptionalThing.empty();
}
protected String escapeValue(final String action) {
return action.replace('\t', ' ');
}
protected void log(final StringBuilder buf) {
statsLogger.info(buf.toString());
}
public void setLoggerName(final String loggerName) {
this.loggerName = loggerName;
}
public void setMaxCacheSize(final long maxCacheSize) {
this.maxCacheSize = maxCacheSize;
}
public void setCacheExpireAfterWrite(final long cacheExpireAfterWrite) {
this.cacheExpireAfterWrite = cacheExpireAfterWrite;
}
public static class StatsKeyObject {
private final String id;
private String url;
public StatsKeyObject(final String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUrl(final String url) {
this.url = url;
}
protected String getUrl() {
if (url != null) {
return url;
}
return id;
}
}
public static class StatsObject extends LinkedHashMap<String, Long> {
private static final long serialVersionUID = 1L;
protected final AtomicInteger count;
public StatsObject() {
put(BEGIN_KEY, ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
count = new AtomicInteger(1);
}
public int increment() {
return count.incrementAndGet();
}
public int decrement() {
return count.decrementAndGet();
}
}
public enum StatsAction {
ACCESSED, //
ACCESS_EXCEPTION, //
CHILD_URL, //
CHILD_URLS, //
EVALUATED, //
EXCEPTION, //
FINISHED, //
PARSED, //
PREPARED, //
REDIRECTED, //
PROCESSED,//
}
}