-
Notifications
You must be signed in to change notification settings - Fork 1k
/
RebuildIndexRangesJob.java
196 lines (165 loc) · 6.77 KB
/
RebuildIndexRangesJob.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
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.indexer.ranges;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.indexer.Deflector;
import org.graylog2.indexer.EmptyIndexException;
import org.graylog2.indexer.searches.Searches;
import org.graylog2.plugin.Tools;
import org.graylog2.shared.system.activities.Activity;
import org.graylog2.shared.system.activities.ActivityWriter;
import org.graylog2.system.jobs.SystemJob;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class RebuildIndexRangesJob extends SystemJob {
public interface Factory {
RebuildIndexRangesJob create(Deflector deflector);
}
private static final Logger LOG = LoggerFactory.getLogger(RebuildIndexRangesJob.class);
public static final int MAX_CONCURRENCY = 1;
private boolean cancelRequested = false;
private int indicesToCalculate = 0;
private int indicesCalculated = 0;
protected final Deflector deflector;
private final Searches searches;
private final ActivityWriter activityWriter;
protected final IndexRangeService indexRangeService;
@AssistedInject
public RebuildIndexRangesJob(@Assisted Deflector deflector,
Searches searches,
ActivityWriter activityWriter,
IndexRangeService indexRangeService) {
this.deflector = deflector;
this.searches = searches;
this.activityWriter = activityWriter;
this.indexRangeService = indexRangeService;
}
@Override
public void requestCancel() {
this.cancelRequested = true;
}
@Override
public int getProgress() {
if (indicesToCalculate <= 0) {
return 0;
}
// lolwtfbbqcasting
return (int) Math.floor(((float) indicesCalculated / (float) indicesToCalculate) * 100);
}
@Override
public String getDescription() {
return "Rebuilds index range information.";
}
@Override
public void execute() {
List<Map<String, Object>> ranges = Lists.newArrayList();
info("Re-calculating index ranges.");
String[] indices = deflector.getAllDeflectorIndexNames();
if (indices == null || indices.length == 0) {
info("No indices, nothing to calculate.");
return;
}
indicesToCalculate = indices.length;
Stopwatch sw = Stopwatch.createStarted();
for (String index : indices) {
if (cancelRequested) {
info("Stop requested. Not calculating next index range, not updating ranges.");
sw.stop();
return;
}
try {
ranges.add(calculateRange(index));
} catch (EmptyIndexException e) {
LOG.info("Index [{}] is empty, inserting dummy index range.", index);
Map<String, Object> emptyIndexRange = getDeflectorIndexRange(index);
if (deflector.getCurrentActualTargetIndex().equals(index)) {
LOG.info("Index [{}] is empty but it is the current deflector target. Inserting dummy index range.", index);
} else {
emptyIndexRange.put("start", 0);
emptyIndexRange.put("calculated_at", Tools.getUTCTimestamp());
}
ranges.add(emptyIndexRange);
} catch (Exception e) {
LOG.info("Could not calculate range of index [" + index + "]. Skipping.", e);
} finally {
indicesCalculated++;
}
}
// Now that all is calculated we can replace the whole collection at once.
updateCollection(ranges);
info("Done calculating index ranges for " + indices.length + " indices. Took " + sw.stop().elapsed(TimeUnit.MILLISECONDS) + "ms.");
}
protected Map<String, Object> getDeflectorIndexRange(String index) {
Map<String, Object> deflectorIndexRange = Maps.newHashMap();
deflectorIndexRange.put("index", index);
deflectorIndexRange.put("start", Tools.getUTCTimestamp());
return deflectorIndexRange;
}
protected Map<String, Object> calculateRange(String index) throws EmptyIndexException {
final Stopwatch x = Stopwatch.createStarted();
final DateTime timestamp = searches.findOldestMessageTimestampOfIndex(index);
if (timestamp == null) {
x.stop();
throw new EmptyIndexException();
}
final int rangeEnd = Ints.saturatedCast(timestamp.getMillis() / 1000L);
final int took = Ints.saturatedCast(x.stop().elapsed(TimeUnit.MILLISECONDS));
LOG.info("Calculated range of [{}] in [{}ms].", index, took);
return ImmutableMap.<String, Object>of(
"index", index,
"start", rangeEnd, // FIXME The name of the attribute is massively misleading and should be rectified some time
"calculated_at", Tools.getUTCTimestamp(),
"took_ms", took);
}
private void updateCollection(List<Map<String, Object>> ranges) {
indexRangeService.destroyAll();
for (Map<String, Object> range : ranges) {
IndexRange indexRange = indexRangeService.create(range);
indexRangeService.saveWithoutValidation(indexRange);
}
}
protected void info(String what) {
LOG.info(what);
activityWriter.write(new Activity(what, RebuildIndexRangesJob.class));
}
@Override
public boolean providesProgress() {
return true;
}
@Override
public boolean isCancelable() {
return true;
}
@Override
public int maxConcurrency() {
return MAX_CONCURRENCY;
}
@Override
public String getClassName() {
return this.getClass().getCanonicalName();
}
}