summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Musum <musum@oath.com>2018-04-17 08:42:13 +0200
committerGitHub <noreply@github.com>2018-04-17 08:42:13 +0200
commite05a40ea22ac94d63eeb054dbc20b47cbc3fce9c (patch)
tree0c425e558072b1118a2db6e3a9a34d11111df8a8
parente585045a1096c0c146d5871578610331acaecb40 (diff)
parent61c8f046d91ba48eb6803bd02e7e0bb757adba9d (diff)
Merge pull request #5585 from vespa-engine/hmusum/remove-logserver-code-for-getting-errors-in-logs
Remove code for getting errors in logs from log server
-rw-r--r--logserver/src/main/java/com/yahoo/logserver/BuiltinPluginLoader.java2
-rw-r--r--logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolder.java187
-rw-r--r--logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderConnection.java141
-rw-r--r--logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderPlugin.java51
-rw-r--r--logserver/src/test/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderTestCase.java122
5 files changed, 0 insertions, 503 deletions
diff --git a/logserver/src/main/java/com/yahoo/logserver/BuiltinPluginLoader.java b/logserver/src/main/java/com/yahoo/logserver/BuiltinPluginLoader.java
index 374b3fc6f8c..5c6569d9b9b 100644
--- a/logserver/src/main/java/com/yahoo/logserver/BuiltinPluginLoader.java
+++ b/logserver/src/main/java/com/yahoo/logserver/BuiltinPluginLoader.java
@@ -5,7 +5,6 @@ import java.util.logging.Logger;
import com.yahoo.log.LogLevel;
import com.yahoo.logserver.handlers.archive.ArchiverPlugin;
-import com.yahoo.logserver.handlers.lasterrorsholder.LastErrorsHolderPlugin;
import com.yahoo.logserver.handlers.logmetrics.LogMetricsPlugin;
import com.yahoo.logserver.handlers.replicator.ReplicatorPlugin;
@@ -23,7 +22,6 @@ public class BuiltinPluginLoader extends AbstractPluginLoader {
loadFromClass(ArchiverPlugin.class);
loadFromClass(ReplicatorPlugin.class);
loadFromClass(LogMetricsPlugin.class);
- loadFromClass(LastErrorsHolderPlugin.class);
log.log(LogLevel.DEBUG, "done loading builtin plugins");
}
diff --git a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolder.java b/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolder.java
deleted file mode 100644
index 927486bdd72..00000000000
--- a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolder.java
+++ /dev/null
@@ -1,187 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.logserver.handlers.lasterrorsholder;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.yahoo.io.Connection;
-import com.yahoo.io.ConnectionFactory;
-import com.yahoo.io.Listener;
-import com.yahoo.log.LogLevel;
-import com.yahoo.log.LogMessage;
-import com.yahoo.logserver.handlers.AbstractLogHandler;
-
-import java.io.IOException;
-import java.io.StringWriter;
-import java.nio.channels.SocketChannel;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Logger;
-
-/**
- * The LastErrorsHolder handler is used for holding the last n
- * messages at level error or higher. Connecting to this handler
- * will return a Json object with the last errors (default is last 100 errors)
- *
- * @author hmusum
- */
-public class LastErrorsHolder extends AbstractLogHandler implements ConnectionFactory {
-
- private static final Logger log = Logger.getLogger(LastErrorsHolder.class.getName());
- private static final int maxNumErrors = 100;
- private final Object lock = new Object();
-
- private int port;
- private Listener listener;
-
- private final ArrayList<LogMessage> errors = new ArrayList<>();
- private int numberOfErrors = 0;
-
- /**
- * @param port The port to which this handler listens to.
- */
- public LastErrorsHolder(int port) throws IOException {
- this.port = port;
- listen(port);
- }
-
- public void listen(int port) throws IOException {
- if (listener != null) {
- throw new IllegalStateException("already listening to port " + this.port);
- }
- listener = new Listener("last-errors-holder");
- listener.listen(this, port);
- listener.start();
- log.log(LogLevel.CONFIG, "port=" + port);
- }
-
- public boolean doHandle(LogMessage msg) {
- if (msg.getLevel().equals(LogLevel.ERROR) || msg.getLevel().equals(LogLevel.FATAL)) {
- synchronized (lock) {
- numberOfErrors++;
- if (errors.size() < maxNumErrors) {
- errors.add(msg);
- } else if (numberOfErrors == maxNumErrors) {
- log.log(LogLevel.DEBUG, String.format("Not storing errors, have reached maximum number of errors: %d, total number of errors received: %d",
- maxNumErrors, numberOfErrors));
- }
- }
- }
- return true;
- }
-
- public void close() {
- try {
- listener.interrupt();
- listener.join();
- log.log(LogLevel.DEBUG, "listener stopped");
- } catch (InterruptedException e) {
- log.log(LogLevel.WARNING, "listener was interrupted", e);
- }
- }
-
- public void flush() {
- }
-
- /**
- * Factory method for creating new connections. Since we just return a result
- * when client connection happens, we also write the result here
- *
- * @param socket The new SocketChannel
- * @param listener The Listener instance we want to use
- */
- public Connection newConnection(SocketChannel socket, Listener listener) {
- if (log.isLoggable(LogLevel.DEBUG)) {
- log.log(LogLevel.DEBUG, "New last-errors-holder connection: " + socket);
- }
- LastErrorsHolderConnection connection = new LastErrorsHolderConnection(socket);
- synchronized (lock) {
- Messages messages = new Messages();
- for (LogMessage error : errors) {
- messages.addMessage(
- new Message(error.getTime()/1000,
- error.getHost(),
- error.getService(),
- error.getLevel().getName(),
- error.getPayload()));
- }
- messages.setNumberOfErrors(numberOfErrors);
-
- try {
- ObjectMapper mapper = new ObjectMapper();
- StringWriter stringWriter = new StringWriter();
- mapper.writeValue(stringWriter, messages);
- connection.enqueue(StandardCharsets.UTF_8.encode(stringWriter.toString()));
- } catch (IOException e) {
- log.log(LogLevel.WARNING, "Could not enqueue log message", e);
- }
-
- errors.clear();
- numberOfErrors = 0;
- }
-
- return connection;
- }
-
- public String toString() {
- return LastErrorsHolder.class.getName();
- }
-
-
- static class Messages {
- private final List<Message> messages = new ArrayList<>();
- private long errorCount = 0; // There might be more errors than number of messages
-
- void addMessage(Message message) {
- messages.add(message);
- }
-
- void setNumberOfErrors(long errorCount) {
- this.errorCount = errorCount;
- }
-
- public List<Message> getMessages() {
- return messages;
- }
-
- public long getErrorCount() {
- return errorCount;
- }
- }
-
- static class Message {
- private final long time;
- private final String hostname;
- private final String service;
- private final String logLevel;
- private final String message;
-
- Message(long time, String hostname, String service, String logLevel, String message) {
- this.time = time;
- this.hostname = hostname;
- this.service = service;
- this.logLevel = logLevel;
- this.message = message;
- }
-
- public long getTime() {
- return time;
- }
-
- public String getMessage() {
- return message;
- }
-
- public String getLogLevel() {
- return logLevel;
- }
-
- public String getHostname() {
- return hostname;
- }
-
- public String getService() {
- return service;
- }
- }
-
-}
diff --git a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderConnection.java b/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderConnection.java
deleted file mode 100644
index c1d86859773..00000000000
--- a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderConnection.java
+++ /dev/null
@@ -1,141 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.logserver.handlers.lasterrorsholder;
-
-import com.yahoo.io.Connection;
-import com.yahoo.log.LogLevel;
-import com.yahoo.log.LogMessage;
-import com.yahoo.logserver.filter.LogFilter;
-import com.yahoo.logserver.filter.LogFilterManager;
-import com.yahoo.logserver.formatter.LogFormatter;
-
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.SocketChannel;
-import java.util.logging.Logger;
-
-/**
- * LastErrorsHandler client connection.
- *
- * @author hmusum
- */
-public class LastErrorsHolderConnection implements Connection, LogFilter {
- private static final Logger log = Logger.getLogger(LastErrorsHolderConnection.class.getName());
-
- private final SocketChannel socket;
- private ByteBuffer writeBuffer;
- private final ByteBuffer readBuffer = ByteBuffer.allocate(4096);
- private LogFilter filter = null;
- protected LogFormatter formatter = null;
- private static final String filterName = "system.mute";
-
- /**
- * Constructs a LastErrorsHolderConnection. Note that initially the
- * filter of this connection is set to MuteFilter, which mutes
- * all log messages.
- */
- public LastErrorsHolderConnection(SocketChannel socket) {
- this.socket = socket;
- this.filter = LogFilterManager.getLogFilter(filterName);
- }
-
- /**
- * Check if the message is wanted by this particular replicator
- * connection. The reason we provide this method is because we
- * want to be able to determine if a message is wanted by any
- * client before committing resources to creating a ByteBuffer to
- * serialize it into.
- *
- * @param msg The log message offered
- */
- public boolean isLoggable(LogMessage msg) {
- if (filter == null) {
- return true;
- }
- return filter.isLoggable(msg);
- }
-
- /**
- * Return the description of the currently active filter.
- */
- public String description() {
- if (filter == null) {
- return "No filter defined";
- }
- return filter.description();
- }
-
-
- /**
- * Enqueues a ByteBuffer containing the message destined
- * for the client.
- *
- * @param buffer the ByteBuffer into which the log message is
- * serialized.
- */
- public synchronized void enqueue(ByteBuffer buffer) throws IOException {
- writeBuffer = buffer;
- write();
- }
-
- public void read() throws IOException {
- if (!readBuffer.hasRemaining()) {
- log.warning("Log message too long. Message exceeds "
- + readBuffer.capacity()
- + " bytes. Connection dropped.");
- close();
- return;
- }
-
-
- int ret = socket.read(readBuffer);
- if (ret == -1) {
- close();
- return;
- }
-
- if (ret == 0) {
- if (log.isLoggable(LogLevel.INFO)) {
- log.log(LogLevel.INFO, "zero byte read occurred");
- }
- }
-
- readBuffer.flip();
- }
-
- public synchronized void write() throws IOException {
- if (!socket.isOpen()) {
- close();
- }
- do {
- try {
- socket.write(writeBuffer);
- } catch (IOException e) {
- log.log(LogLevel.WARNING, "Error writing", e);
- close();
- return;
- }
- } while (writeBuffer.hasRemaining());
- }
-
- public synchronized void close() throws IOException {
- socket.close();
- writeBuffer = null;
- }
-
- public int selectOps() {
- return SelectionKey.OP_READ;
- }
-
- public SocketChannel socketChannel() {
- return socket;
- }
-
- public void connect() {
- }
-
- void setFilter(LogFilter filter) {
- this.filter = filter;
- }
-}
-
diff --git a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderPlugin.java b/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderPlugin.java
deleted file mode 100644
index dda396b91db..00000000000
--- a/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderPlugin.java
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.logserver.handlers.lasterrorsholder;
-
-import com.yahoo.log.LogLevel;
-import com.yahoo.logserver.Server;
-import com.yahoo.plugin.Config;
-import com.yahoo.plugin.Plugin;
-
-import java.io.IOException;
-import java.util.logging.Logger;
-
-public class LastErrorsHolderPlugin implements Plugin {
- private static final String DEFAULT_PORT = "19082";
- private static final Logger log = Logger.getLogger(LastErrorsHolderPlugin.class.getName());
- private LastErrorsHolder lastErrorsHolder;
- private final Server server = Server.getInstance();
-
- public String getPluginName() {
- return "last-errors-holder";
- }
-
- /**
- * Initialize the plugin
- */
- public void initPlugin(Config config) {
- if (lastErrorsHolder != null) {
- throw new IllegalStateException("plugin already initialized: " + getPluginName());
- }
- int listenPort = config.getInt("port", DEFAULT_PORT);
- String threadName = config.get("thread", getPluginName());
- try {
- lastErrorsHolder = new LastErrorsHolder(listenPort);
- } catch (IOException e) {
- log.log(LogLevel.WARNING, "init failed: " + e);
- return;
- }
- server.registerLogHandler(lastErrorsHolder, threadName);
- }
-
- /**
- * Shut down the plugin.
- */
- public void shutdownPlugin() {
-
- if (lastErrorsHolder == null) {
- throw new IllegalStateException("plugin not initialized: " + getPluginName());
- }
- server.unregisterLogHandler(lastErrorsHolder);
- lastErrorsHolder = null;
- }
-}
diff --git a/logserver/src/test/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderTestCase.java b/logserver/src/test/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderTestCase.java
deleted file mode 100644
index 22fa7d5cf30..00000000000
--- a/logserver/src/test/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderTestCase.java
+++ /dev/null
@@ -1,122 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.logserver.handlers.lasterrorsholder;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.yahoo.log.InvalidLogFormatException;
-import com.yahoo.log.LogLevel;
-import com.yahoo.log.LogMessage;
-import com.yahoo.logserver.Server;
-import com.yahoo.text.Utf8;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.io.IOException;
-import java.io.StringWriter;
-import java.net.InetSocketAddress;
-import java.nio.ByteBuffer;
-import java.nio.channels.SocketChannel;
-import java.time.Duration;
-import java.time.Instant;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public class LastErrorsHolderTestCase {
-
- private static final int serverPort = 18324;
- private static final int lastErrorsHolderPort = 18326;
- private Server server;
- private Thread serverThread;
- private LastErrorsHolder lastErrorsHolder;
-
- @Before
- public void setUp() throws InterruptedException, IOException {
- server = Server.getInstance();
- server.initialize(serverPort);
- serverThread = new Thread(server);
- serverThread.start();
- lastErrorsHolder = new LastErrorsHolder(lastErrorsHolderPort);
- }
-
- @After
- public void tearDown() throws InterruptedException {
- if (serverThread != null) {
- serverThread.interrupt();
- serverThread.join();
- }
- if (lastErrorsHolder != null) lastErrorsHolder.close();
- }
-
- public String connectAndGetLogMessages() throws InterruptedException, IOException {
- SocketChannel socket = null;
- Instant start = Instant.now();
- while (Instant.now().isBefore(start.plus(Duration.ofMinutes(1)))) {
- try {
- InetSocketAddress address = new InetSocketAddress("localhost", lastErrorsHolderPort);
- socket = SocketChannel.open(address);
- break;
- } catch (Exception e) {
- Thread.sleep(100);
- }
- }
- if (socket == null) {
- throw new RuntimeException("Could not connect to server");
- }
-
- ByteBuffer buf = ByteBuffer.allocateDirect(10000);
- int bytesRead = socket.read(buf);
- byte[] bytes = new byte[bytesRead];
- buf.position(0);
- buf.get(bytes);
- socket.close();
-
- return Utf8.toString(bytes);
- }
-
-
- @Test
- public void testLastErrorsHolder() throws IOException, InvalidLogFormatException, InterruptedException {
- LastErrorsHolder.Message logMessage1 = new LastErrorsHolder.Message(1433996283, "host1.yahoo.com", "container", LogLevel.ERROR
- .getName(), "foo");
- LastErrorsHolder.Message logMessage2 = new LastErrorsHolder.Message(1433996284, "host2.yahoo.com", "container", LogLevel.ERROR
- .getName(), "bar");
- LastErrorsHolder.Message logMessage3 = new LastErrorsHolder.Message(1433996285, "host2.yahoo.com", "container", LogLevel.INFO
- .getName(), "bar");
-
- LastErrorsHolder.Messages messages = new LastErrorsHolder.Messages();
-
- // No log messages yet
- String logs = connectAndGetLogMessages();
- final ObjectMapper mapper = new ObjectMapper();
- StringWriter stringWriter = new StringWriter();
- mapper.writeValue(stringWriter, messages);
- assertThat(logs, is(stringWriter.toString()));
-
- // Three messages, one is at level INFO
- lastErrorsHolder.doHandle(createLogMessage(logMessage1));
- lastErrorsHolder.doHandle(createLogMessage(logMessage2));
- lastErrorsHolder.doHandle(createLogMessage(logMessage3));
- messages = new LastErrorsHolder.Messages();
- messages.addMessage(logMessage1);
- messages.addMessage(logMessage2);
- messages.setNumberOfErrors(2);
- // Not adding logMessage3, since it is at level INFO
-
- logs = connectAndGetLogMessages();
- stringWriter = new StringWriter();
- mapper.writeValue(stringWriter, messages);
- assertThat(logs, is(stringWriter.toString()));
- }
-
- private LogMessage createLogMessage(LastErrorsHolder.Message message) throws InvalidLogFormatException {
- return createLogMessage(message.getTime(), message.getHostname(), message.getService(), message.getLogLevel(), message
- .getMessage());
- }
-
- private LogMessage createLogMessage(long time, String hostname, String service, String logLevel, String message) throws InvalidLogFormatException {
- return LogMessage.parseNativeFormat(String.format("%d\t%s\t1/1\t%s\tcomponent\t%s\t%s", time, hostname, service, logLevel
- .toLowerCase(), message));
- }
-
-}