-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEventSendBuffer.java
More file actions
368 lines (313 loc) · 15 KB
/
EventSendBuffer.java
File metadata and controls
368 lines (313 loc) · 15 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package fi.helsinki.cs.tmc.spyware;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.NbTmcSettings;
import fi.helsinki.cs.tmc.model.ServerAccess;
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
import fi.helsinki.cs.tmc.utilities.Cooldown;
import fi.helsinki.cs.tmc.utilities.ExceptionUtils;
import fi.helsinki.cs.tmc.utilities.SingletonTask;
import fi.helsinki.cs.tmc.utilities.TmcRequestProcessor;
import fi.helsinki.cs.tmc.core.communication.HttpResult;
import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.Exceptions;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Buffers {@link LoggableEvent}s and sends them to the server and/or syncs them
* to the disk periodically.
*/
public class EventSendBuffer implements EventReceiver {
private static final Logger log = Logger.getLogger(EventSendBuffer.class.getName());
public static final long DEFAULT_SEND_INTERVAL = 3 * 60 * 1000;
public static final long DEFAULT_SAVE_INTERVAL = 3 * 60 * 1000;
public static final int DEFAULT_MAX_EVENTS = 64 * 1024;
public static final int DEFAULT_AUTOSEND_THREHSOLD = DEFAULT_MAX_EVENTS / 2;
public static final int DEFAULT_AUTOSEND_COOLDOWN = 30 * 1000;
private Random random = new Random();
private SpywareSettings settings;
private ServerAccess serverAccess;
private CourseDb courseDb;
private EventStore eventStore;
// The following variables must only be accessed with a lock on sendQueue.
private final ArrayDeque<LoggableEvent> sendQueue = new ArrayDeque<LoggableEvent>();
private int eventsToRemoveAfterSend = 0;
private int maxEvents = DEFAULT_MAX_EVENTS;
private int autosendThreshold = DEFAULT_AUTOSEND_THREHSOLD;
private Cooldown autosendCooldown;
public EventSendBuffer(
SpywareSettings settings,
ServerAccess serverAccess,
CourseDb courseDb,
EventStore eventStore) {
this.settings = settings;
this.serverAccess = serverAccess;
this.courseDb = courseDb;
this.eventStore = eventStore;
this.autosendCooldown = new Cooldown(DEFAULT_AUTOSEND_COOLDOWN);
try {
List<LoggableEvent> initialEvents = Arrays.asList(eventStore.load());
initialEvents = initialEvents.subList(0, Math.min(maxEvents, initialEvents.size()));
this.sendQueue.addAll(initialEvents);
} catch (IOException ex) {
log.log(Level.WARNING, "Failed to read events from event store", ex);
} catch (RuntimeException ex) {
log.log(Level.WARNING, "Failed to read events from event store", ex);
}
this.sendingTask.setInterval(DEFAULT_SEND_INTERVAL);
this.savingTask.setInterval(DEFAULT_SAVE_INTERVAL);
}
public void setSendingInterval(long interval) {
this.sendingTask.setInterval(interval);
}
public void setSavingInterval(long interval) {
this.savingTask.setInterval(interval);
}
public void setMaxEvents(int newMaxEvents) {
if (newMaxEvents <= 0) {
throw new IllegalArgumentException();
}
synchronized (sendQueue) {
if (newMaxEvents < maxEvents) {
int diff = newMaxEvents - maxEvents;
for (int i = 0; i < diff; ++i) {
sendQueue.pop();
}
eventsToRemoveAfterSend -= diff;
}
maxEvents = newMaxEvents;
}
}
public void setAutosendThreshold(int autosendThreshold) {
synchronized (sendQueue) {
if (autosendThreshold <= 0) {
throw new IllegalArgumentException();
}
this.autosendThreshold = autosendThreshold;
maybeAutosend();
}
}
public void setAutosendCooldown(long durationMillis) {
this.autosendCooldown.setDurationMillis(durationMillis);
}
public void sendNow() {
sendingTask.start();
}
public void saveNow(long timeout) throws TimeoutException, InterruptedException {
savingTask.start();
savingTask.waitUntilFinished(timeout);
}
public void waitUntilCurrentSendingFinished(long timeout)
throws TimeoutException, InterruptedException {
sendingTask.waitUntilFinished(timeout);
}
@Override
public void receiveEvent(LoggableEvent event) {
if (!settings.isSpywareEnabled()) {
return;
}
synchronized (sendQueue) {
if (sendQueue.size() >= maxEvents) {
sendQueue.pop();
eventsToRemoveAfterSend--;
}
sendQueue.add(event);
maybeAutosend();
}
}
private void maybeAutosend() {
if (sendQueue.size() >= autosendThreshold && autosendCooldown.isExpired()) {
autosendCooldown.start();
sendNow();
}
}
/**
* Stops sending any more events.
*
* Buffer manipulation methods may still be called.
*/
@Override
public void close() {
long delayPerWait = 2000;
try {
sendingTask.unsetInterval();
savingTask.unsetInterval();
savingTask.waitUntilFinished(delayPerWait);
savingTask.start();
savingTask.waitUntilFinished(delayPerWait);
sendingTask.waitUntilFinished(delayPerWait);
} catch (TimeoutException ex) {
log.log(Level.WARNING, "Time out when closing EventSendBuffer", ex);
} catch (InterruptedException ex) {
log.log(Level.WARNING, "Closing EventSendBuffer interrupted", ex);
}
}
private SingletonTask sendingTask =
new SingletonTask(
new Runnable() {
// Sending too many at once may go over the server's POST size limit.
private static final int MAX_EVENTS_PER_SEND = 500;
@Override
public void run() {
boolean shouldSendMore;
do {
ArrayList<LoggableEvent> eventsToSend = copyEventsToSendFromQueue();
if (eventsToSend.isEmpty()) {
return;
}
synchronized (sendQueue) {
shouldSendMore = sendQueue.size() > eventsToSend.size();
}
String url = pickDestinationUrl();
if (url == null) {
return;
}
log.log(
Level.INFO,
"Sending {0} events to {1}",
new Object[] {eventsToSend.size(), url});
doSend(eventsToSend, url);
} while (shouldSendMore);
}
private ArrayList<LoggableEvent> copyEventsToSendFromQueue() {
synchronized (sendQueue) {
ArrayList<LoggableEvent> eventsToSend =
new ArrayList<LoggableEvent>(sendQueue.size());
Iterator<LoggableEvent> i = sendQueue.iterator();
while (i.hasNext() && eventsToSend.size() < MAX_EVENTS_PER_SEND) {
eventsToSend.add(i.next());
}
eventsToRemoveAfterSend = eventsToSend.size();
return eventsToSend;
}
}
private String pickDestinationUrl() {
Course course = courseDb.getCurrentCourse();
if (course == null) {
log.log(
Level.FINE,
"Not sending events because no course selected");
return null;
}
List<URI> urls = course.getSpywareUrls();
if (urls == null || urls.isEmpty()) {
log.log(
Level.INFO,
"Not sending events because no URL provided by server");
return null;
}
String url = urls.get(random.nextInt(urls.size())).toString();
return url;
}
/**
* Converts events to data[] and sends it to defined url.
*/
private void doSend(
final ArrayList<LoggableEvent> eventsToSend, final String url) {
NbTmcSettings settings = NbTmcSettings.getDefault();
Optional<Course> currentCourse = settings.getCurrentCourse();
if (!currentCourse.isPresent()) {
return;
}
addCorrectSpywareUrl(url, currentCourse);
final ProgressHandle progress =
ProgressHandleFactory.createSystemHandle("Sending stats");
progress.start();
try {
byte[] data = convertEventsToByteArray(eventsToSend);
ListenableFuture<List<HttpResult>> spywareSending =
TmcCoreSingleton.getInstance().sendSpywareDiffs(data);
Futures.addCallback(
spywareSending,
new FutureCallback<List<HttpResult>>() {
@Override
public void onSuccess(List<HttpResult> success) {
clearAfterSend(success);
}
@Override
public void onFailure(Throwable thrwbl) {
clearAfterSend(new ArrayList<HttpResult>());
System.err.println(thrwbl.getMessage());
}
private void clearAfterSend(List<HttpResult> success) {
progress.finish();
log.log(
Level.INFO,
"Sent {0} events successfully to {1}",
new Object[] {eventsToSend.size(), url});
removeSentEventsFromQueue();
// If saving fails now (or is already running and fails later)
// then we may end up sending duplicate events later.
// This will hopefully be very rare.
savingTask.start();
}
});
} catch (TmcCoreException ex) {
progress.finish();
Exceptions.printStackTrace(ex);
}
}
private void addCorrectSpywareUrl(
final String url, Optional<Course> currentCourse) {
List<URI> spywareUrls = new ArrayList<URI>();
URI finalUrl = URI.create(serverAccess.addApiCallQueryParameters(url));
spywareUrls.add(finalUrl);
currentCourse.get().setSpywareUrls(spywareUrls);
}
private byte[] convertEventsToByteArray(
final ArrayList<LoggableEvent> eventsToSend)
throws RuntimeException {
byte[] data;
try {
data = serverAccess.eventListToPostBody(eventsToSend);
} catch (IOException ex) {
throw ExceptionUtils.toRuntimeException(ex);
}
return data;
}
private void removeSentEventsFromQueue() {
synchronized (sendQueue) {
assert (eventsToRemoveAfterSend <= sendQueue.size());
while (eventsToRemoveAfterSend > 0) {
sendQueue.pop();
eventsToRemoveAfterSend--;
}
}
}
},
TmcRequestProcessor.instance);
private SingletonTask savingTask =
new SingletonTask(
new Runnable() {
@Override
public void run() {
try {
LoggableEvent[] eventsToSave;
synchronized (sendQueue) {
eventsToSave =
Iterables.toArray(sendQueue, LoggableEvent.class);
}
eventStore.save(eventsToSave);
} catch (IOException ex) {
log.log(Level.WARNING, "Failed to save events", ex);
}
}
},
TmcRequestProcessor.instance);
}