summaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderConnection.java
blob: 0dc89e95d11b31d3da717ebbba7f6ab8dd023964 (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
// Copyright 2016 Yahoo Inc. 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 musum
 */
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;
    }
}