summaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/Server.java
blob: d0df5bf297260063a60de159b136c6559a8bd25a (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
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver;

import com.yahoo.io.FatalErrorHandler;
import com.yahoo.io.Listener;
import com.yahoo.log.LogLevel;
import com.yahoo.log.LogSetup;
import com.yahoo.log.event.Event;
import com.yahoo.logserver.handlers.HandlerThread;
import com.yahoo.logserver.handlers.LogHandler;
import com.yahoo.logserver.net.LogConnectionFactory;
import com.yahoo.logserver.net.control.Levels;
import com.yahoo.yolean.system.CatchSignals;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class implements the log server itself.  At present there is
 * no runtime configuration;  the server starts up, binds port 19081
 * and loads builtin plugins.
 *
 * @author Bjorn Borud
 * @author Stig Bakken
 */

public class Server implements Runnable {
    private final AtomicBoolean signalCaught = new AtomicBoolean(false);
    static final String APPNAME = "logserver";
    private static final Server instance = new Server();
    private static final Logger log = Logger.getLogger(Server.class.getName());
    private static final FatalErrorHandler fatalErrorHandler = new FatalErrorHandler();
    private static final HashMap<String, HandlerThread> handlerThreads = new HashMap<>();
    private static final HashMap<LogHandler, String> threadNameForHandler = new HashMap<>();

    static {
        LogSetup.initVespaLogging("ADM");
    }

    // the port is a String because we want to use it as the default
    // value of a System.getProperty().
    private static final String LISTEN_PORT = "19081";

    private int listenPort;
    private Listener listener;
    private final LogDispatcher dispatch;

    private final boolean isInitialized;

    private Server() {
        dispatch = new LogDispatcher();
        dispatch.setBatchedMode(true);
        isInitialized = false;
    }

    public static Server getInstance() {
        return instance;
    }

    private HandlerThread getHandlerThread(String threadName) {
        threadName += " handler thread";
        HandlerThread ht = handlerThreads.get(threadName);
        if (ht == null) {
            ht = new HandlerThread(threadName);
            handlerThreads.put(threadName, ht);
            ht.setFatalErrorHandler(fatalErrorHandler);
            dispatch.registerLogHandler(ht);
        }
        return ht;
    }

    private void registerPluginLoader(PluginLoader loader) {
        loader.loadPlugins();
    }

    public void registerLogHandler(LogHandler lh, String threadName) {
        HandlerThread ht = getHandlerThread(threadName);
        ht.registerHandler(lh);
        threadNameForHandler.put(lh, threadName);
    }

    public void unregisterLogHandler(LogHandler lh) {
        String threadName = threadNameForHandler.get(lh);
        unregisterLogHandler(lh, threadName);
    }

    private void unregisterLogHandler(LogHandler lh, String threadName) {
        HandlerThread ht = getHandlerThread(threadName);
        ht.unregisterHandler(lh);
        threadNameForHandler.remove(lh);
    }

    public void registerFlusher(LogHandler lh) {
        Flusher.register(lh);
    }

    /**
     * Included only for consistency
     */
    public void unregisterFlusher(LogHandler lh) {
        /* NOP */
    }

    /**
     * Initialize the server and start up all its plugins,
     *
     * @param listenPort The port on which the logserver accepts log
     *                   messages.
     */
    public void initialize(int listenPort) {
        if (isInitialized) {
            throw new IllegalStateException(APPNAME + " already initialized");
        }

        this.listenPort = listenPort;

        // plugins
        registerPluginLoader(new BuiltinPluginLoader());

        // main listener
        listener = new Listener(APPNAME);
        listener.addSelectLoopPostHook(dispatch);
        listener.setFatalErrorHandler(fatalErrorHandler);
    }

    /**
     * Sets up the listen port and starts the Listener.  Then waits for
     * Listener to exit.
     */
    public void run() {
        try {
            listener.listen(new LogConnectionFactory(dispatch), listenPort);
            log.log(LogLevel.CONFIG, APPNAME + ".listenport=" + listenPort);
        } catch (IOException e) {
            log.log(LogLevel.ERROR, "Unable to initialize", e);
            return;
        }

        log.fine("Starting listener...");
        listener.start();
        Event.started(APPNAME);
        try {
            listener.join();
            log.fine("listener thread exited");
        } catch (InterruptedException e) {
            log.log(Level.WARNING, "Server was interrupted", e);
        }
    }

    private void setupSignalHandler() {
        CatchSignals.setup(signalCaught); // catch termination and interrupt signals
    }

    private void waitForShutdown() {
        synchronized (signalCaught) {
            while (! signalCaught.get()) {
                try {
                    signalCaught.wait();
                } catch (InterruptedException e) {
                    // empty
                }
            }
        }
        Event.stopping(APPNAME, "shutdown");
        dispatch.close();
        Event.stopped(APPNAME, 0, 0);
        System.exit(0);
    }

    static HashMap<LogHandler, String> threadNameForHandler() {
        return threadNameForHandler;
    }

    static void help() {
        System.out.println();
        System.out.println("System properties:");
        System.out.println(" - " + APPNAME + ".listenport (" + LISTEN_PORT + ")");
        System.out.println(" - " + APPNAME + ".queue.size (" + HandlerThread.DEFAULT_QUEUESIZE + ")");
        System.out.println(" - logserver.default.loglevels (" + (new Levels()).toString() + ")");
        System.out.println();
    }

    public static void main(String[] args) {
        if (args.length > 0 && "-help".equals(args[0])) {
            help();
            System.exit(0);
        }

        String portString = System.getProperty(APPNAME + ".listenport", LISTEN_PORT);
        Server server = Server.getInstance();
        server.setupSignalHandler();
        server.initialize(Integer.parseInt(portString));

        Thread t = new Thread(server, "logserver main");
        t.start();
        server.waitForShutdown();
    }
}