blob: 64be0556cff6ae34a6dfa3924e12432ae491c41e (
plain) (
blame)
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.logging;
import com.yahoo.concurrent.DaemonThreadFactory;
import java.time.Clock;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/**
* Abstract class that deals with storing event entries on disk and making sure all stored
* entries are eventually sent. Note that the {@link #start()} method needs to be called by subclasses as
* the last statement in their constructor.
*
* @author hmusum
*/
public abstract class AbstractSpoolingLogger extends AbstractThreadedLogger implements Runnable {
protected static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(Spooler.class.getName());
private final ScheduledExecutorService executorService;
protected final Spooler spooler;
@SuppressWarnings("unused") // Used by subclasses
public AbstractSpoolingLogger() {
this(new Spooler(Clock.systemUTC()));
}
public AbstractSpoolingLogger(Spooler spooler) {
this.spooler = spooler;
this.executorService = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("AbstractSpoolingLogger-send-"));
}
/** Start processing files, must be called by subclasses */
public void start() {
this.executorService.scheduleWithFixedDelay(this, 0, 1L, TimeUnit.SECONDS);
}
public void run() {
try {
spooler.switchFileIfNeeded();
spooler.processFiles(this::transport);
} catch (Exception e) {
log.log(Level.WARNING, "Exception when processing files: " + e.getMessage());
}
}
@Override
public boolean send(LoggerEntry entry) {
log.log(Level.FINE, "Sending entry " + entry + " to spooler");
try {
executor.execute(() -> spooler.write(entry));
} catch (RejectedExecutionException e) {
return false;
}
return true;
}
@Deprecated
/*
@deprecated use {@link #deconstruct()} instead
*/
public void shutdown() { deconstruct(); }
@Override
public void deconstruct() {
super.deconstruct();
executorService.shutdown();
try {
if ( ! executorService.awaitTermination(10, TimeUnit.SECONDS))
log.log(Level.WARNING, "Timeout elapsed waiting for termination");
} catch (InterruptedException e) {
log.log(Level.WARNING, "Failure when waiting for termination: " + e.getMessage());
}
run(); // Run a last time to make sure all data is written to file
}
}
|