-
Notifications
You must be signed in to change notification settings - Fork 1k
/
BlockingBatchedESOutput.java
190 lines (169 loc) · 7.6 KB
/
BlockingBatchedESOutput.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
/**
* 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.outputs;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.indexer.IndexSet;
import org.graylog2.indexer.cluster.Cluster;
import org.graylog2.indexer.messages.Messages;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.configuration.Configuration;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.shared.journal.Journal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import static com.codahale.metrics.MetricRegistry.name;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
// Singleton class
public class BlockingBatchedESOutput extends ElasticSearchOutput {
private static final Logger log = LoggerFactory.getLogger(BlockingBatchedESOutput.class);
private final Cluster cluster;
private final int maxBufferSize;
private final Timer processTime;
private final Histogram batchSize;
private final Meter bufferFlushes;
private final Meter bufferFlushesRequested;
private volatile List<Map.Entry<IndexSet, Message>> buffer;
private static final AtomicInteger activeFlushThreads = new AtomicInteger(0);
private final AtomicLong lastFlushTime = new AtomicLong();
private final int outputFlushInterval;
@AssistedInject
public BlockingBatchedESOutput(MetricRegistry metricRegistry,
Messages messages,
Cluster cluster,
org.graylog2.Configuration serverConfiguration,
Journal journal,
@Assisted Stream stream,
@Assisted Configuration configuration) {
this(metricRegistry, messages, cluster, serverConfiguration, journal);
}
@Inject
public BlockingBatchedESOutput(MetricRegistry metricRegistry,
Messages messages,
Cluster cluster,
org.graylog2.Configuration serverConfiguration,
Journal journal) {
super(metricRegistry, messages, journal);
this.cluster = cluster;
this.maxBufferSize = serverConfiguration.getOutputBatchSize();
outputFlushInterval = serverConfiguration.getOutputFlushInterval();
this.processTime = metricRegistry.timer(name(this.getClass(), "processTime"));
this.batchSize = metricRegistry.histogram(name(this.getClass(), "batchSize"));
this.bufferFlushes = metricRegistry.meter(name(this.getClass(), "bufferFlushes"));
this.bufferFlushesRequested = metricRegistry.meter(name(this.getClass(), "bufferFlushesRequested"));
buffer = Lists.newArrayListWithCapacity(maxBufferSize);
}
@Override
public void write(Message message) throws Exception {
for (IndexSet indexSet : message.getIndexSets()) {
writeMessageEntry(Maps.immutableEntry(indexSet, message));
}
}
public void writeMessageEntry(Map.Entry<IndexSet, Message> entry) throws Exception {
List<Map.Entry<IndexSet, Message>> flushBatch = null;
synchronized (this) {
buffer.add(entry);
if (buffer.size() >= maxBufferSize) {
flushBatch = buffer;
buffer = Lists.newArrayListWithCapacity(maxBufferSize);
}
}
// if the current thread found it had to flush any messages, it does so but blocks.
// this ensures we don't flush more than 'processorCount' in parallel.
// TODO this will still be time limited by the OutputBufferProcessor and thus be called more often than it should
if (flushBatch != null) {
flush(flushBatch);
}
}
private void flush(List<Map.Entry<IndexSet, Message>> messages) {
if (!cluster.isConnected() || !cluster.isDeflectorHealthy()) {
try {
cluster.waitForConnectedAndDeflectorHealthy();
} catch (TimeoutException | InterruptedException e) {
log.warn("Error while waiting for healthy Elasticsearch cluster. Not flushing.", e);
return;
}
}
// never try to flush an empty buffer
if (messages.size() == 0) {
return;
}
log.debug("Starting flushing {} messages, flush threads active {}",
messages.size(),
activeFlushThreads.incrementAndGet());
try (Timer.Context ignored = processTime.time()) {
lastFlushTime.set(System.nanoTime());
writeMessageEntries(messages);
batchSize.update(messages.size());
bufferFlushes.mark();
} catch (Exception e) {
log.error("Unable to flush message buffer", e);
}
activeFlushThreads.decrementAndGet();
log.debug("Flushing {} messages completed", messages.size());
}
public void forceFlushIfTimedout() {
if (!cluster.isConnected() || !cluster.isDeflectorHealthy()) {
// do not actually try to flush, because that will block until the cluster comes back.
// simply check and return.
log.debug("Cluster unavailable, but not blocking for periodic flush attempt. This will try again.");
return;
}
// if we shouldn't flush at all based on the last flush time, no need to synchronize on this.
if (lastFlushTime.get() != 0 &&
outputFlushInterval > NANOSECONDS.toSeconds(System.nanoTime() - lastFlushTime.get())) {
return;
}
// flip buffer quickly and initiate flush
final List<Map.Entry<IndexSet, Message>> flushBatch;
synchronized (this) {
flushBatch = buffer;
buffer = Lists.newArrayListWithCapacity(maxBufferSize);
}
if (flushBatch != null) {
bufferFlushesRequested.mark();
flush(flushBatch);
}
}
public interface Factory extends ElasticSearchOutput.Factory {
@Override
BlockingBatchedESOutput create(Stream stream, Configuration configuration);
@Override
Config getConfig();
@Override
Descriptor getDescriptor();
}
public static class Config extends ElasticSearchOutput.Config {
}
public static class Descriptor extends ElasticSearchOutput.Descriptor {
public Descriptor() {
super("Blocking Batched Elasticsearch Output", false, "", "Elasticsearch Output with Batching (blocking)");
}
}
}