aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/handlers/replicator/ReplicatorConnection.java
blob: 003d35e96d03a2cd5941facb214422d3cfd069d6 (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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// 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.replicator;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import com.yahoo.io.Connection;
import com.yahoo.io.IOUtils;
import com.yahoo.io.Listener;
import com.yahoo.io.ReadLine;
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 com.yahoo.logserver.formatter.LogFormatterManager;

/**
 * Replication client connection.
 *
 * @author Bjorn Borud
 */
public class ReplicatorConnection implements Connection, LogFilter {
    private static final Logger log = Logger.getLogger(ReplicatorConnection.class.getName());

    /**
     * The maximum number of queued messages before we start dropping
     */
    private static final int maxQueueLength;
    /**
     * The maximum number of times we go over maxQueueLength before we log a warning
     */
    private static final int maxRetriesBeforeWarning = 10;
    /**
     * Count of how many times we have received a message while the queue is full
     */
    private static int queueFullCount = 0;

    static {
        String maxQueue = System.getProperty("logserver.replicator.maxqueuelength",
                                             "5000");
        maxQueueLength = Integer.parseInt(maxQueue);
    }

    private final SocketChannel socket;
    private final String remoteHost;
    private final Listener listener;
    private final Replicator replicator;
    private final LinkedList<ByteBuffer> writeBufferList = new LinkedList<ByteBuffer>();
    private ByteBuffer writeBuffer;
    private final ByteBuffer readBuffer = ByteBuffer.allocate(4096);
    private LogFilter filter = null;
    protected LogFormatter formatter = null;
    private String filterName = "system.mute";
    private String formatterName = "system.nullformatter";
    private boolean droppingMode = false;
    private int numHandled = 0;
    private int numQueued = 0;
    private int numDropped = 0;
    private long totalBytesWritten = 0;

    /**
     * Constructs a ReplicatorConnection.  Note that initially the
     * filter of this connection is set to MuteFilter, which mutes
     * all log messages.
     */
    public ReplicatorConnection(SocketChannel socket, Listener listener, Replicator replicator) {
        this.socket = socket;
        this.listener = listener;
        this.replicator = replicator;
        this.filter = LogFilterManager.getLogFilter(filterName);
        this.formatter = LogFormatterManager.getLogFormatter(formatterName);

        // this might take some time
        remoteHost = socket.socket().getInetAddress().getHostName();

    }

    /**
     * Returns the remote hostname of this replicator connection.
     *
     * @return Returns the remote host name
     */
    public String getRemoteHost() {
        return remoteHost;
    }

    /**
     * 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 of 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 {
        if (writeBuffer == null) {
            writeBuffer = buffer;
        } else {
            // if we've reached the max we bail out
            if (writeBufferList.size() > maxQueueLength) {
                queueFullCount++;
                if (! droppingMode) {
                    droppingMode = true;
                    String message = "client at " + remoteHost + " can't keep up, dropping messages";
                    if (queueFullCount > maxRetriesBeforeWarning) {
                        log.log(LogLevel.WARNING, message);
                        queueFullCount = 0;
                    } else {
                        log.log(LogLevel.DEBUG, message);
                    }
                }
                numDropped++;
                return;
            }
            writeBufferList.addLast(buffer);
            listener.modifyInterestOps(this, SelectionKey.OP_WRITE, true);
            droppingMode = false;
            numQueued++;
        }
        numHandled++;
        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.DEBUG)) {
                log.fine("zero byte read occurred");
            }
        }

        readBuffer.flip();

        String s;
        while ((s = ReadLine.readLine(readBuffer)) != null) {
            onCommand(s);
        }

    }

    public synchronized void write() throws IOException {
        if (! socket.isOpen()) {
            // throw new IllegalStateException("SocketChannel not open in write()");
            close();
        }

        int bytesWritten;
        do {
            // if writeBufferList is not set we need to fetch the next buffer
            if (writeBuffer == null) {

                // if the list is empty, signal the selector we do not need
                // to do any writing for a while yet and bail
                if (writeBufferList.isEmpty()) {
                    listener.modifyInterestOpsBatch(this,
                                                    SelectionKey.OP_WRITE,
                                                    false);
                    return;
                }
                writeBuffer = writeBufferList.removeFirst();
            }


            // invariants: we have a writeBuffer

            // when the client drops off we actually need
            // to handle that here and close the connection
            // XXX: I am not sure why this works and the
            //      close method call on IOException in
            //      Listener doesn't.  this should be investigated!
            //
            try {
                bytesWritten = socket.write(writeBuffer);
            } catch (IOException e) {
                close();
                return;
            }
            totalBytesWritten += bytesWritten;

            // buffer drained so we forget it and see what happens when we
            // go around.  if indeed we go around
            if ((writeBuffer != null) && (! writeBuffer.hasRemaining())) {
                writeBuffer = null;
            }
        } while (bytesWritten > 0);
    }

    public synchronized void close() throws IOException {
        replicator.deRegisterConnection(this);
        socket.close();
        writeBuffer = null;
        writeBufferList.clear();
        log.log(LogLevel.DEBUG, "closing connection to " + remoteHost);
    }

    public int selectOps() {
        return SelectionKey.OP_READ;
    }

    public SocketChannel socketChannel() {
        return socket;
    }

    public void connect() {
    }


    // ========================================================
    // ==== command processing
    // ========================================================

    void onCommand(String s) {
        log.log(LogLevel.DEBUG, "COMMAND: '" + s + "' from " + remoteHost);
        StringTokenizer st = new StringTokenizer(s.toLowerCase());
        while (st.hasMoreTokens()) {
            String tok = st.nextToken();
            if ("ping".equals(tok)) {
                if (st.hasMoreTokens()) {
                    print("# 202 pong " + st.nextToken() + "\n");
                } else {
                    print("# 202 pong\n");
                }
                return;
            }

            if ("use".equals(tok)) {
                if (st.hasMoreTokens()) {
                    onUse(st.nextToken());
                }
                return;
            }

            if ("formatter".equals(tok)) {
                if (st.hasMoreTokens()) {
                    onFormatter(st.nextToken());
                }
                return;
            }

            if ("quit".equals(tok)) {
                print("# 201 bye\n");
                try { close(); } catch (IOException e) {e.printStackTrace();}
                return;
            }

            if ("list".equals(tok)) {
                onList();
                return;
            }

            if ("listformatters".equals(tok)) {
                onListFormatters();
                return;
            }

            if ("stats".equals(tok)) {
                onStats();
                return;
            }
        }
    }

    void onFormatter(String formatterName) {
        LogFormatter newFormatter = LogFormatterManager.getLogFormatter(formatterName);
        if (newFormatter == null) {
            print("# 405 formatter not found '" + formatterName + "'\n");
            return;
        }
        formatter = newFormatter;
        this.formatterName = formatterName;
        print("# 202 using '" + formatter + "'\n");
    }

    void onUse(String filterName) {
        LogFilter newFilter = LogFilterManager.getLogFilter(filterName);
        if (newFilter == null) {
            print("# 404 filter not found '" + filterName + "'\n");
            return;
        }
        filter = newFilter;
        this.filterName = filterName;
        print("# 200 using '" + filter + "'\n");
    }


    void onList() {
        print("# 203 filter list\n");
        String filterNames[] = LogFilterManager.getFilterNames();
        for (int i = 0; i < filterNames.length; i++) {
            LogFilter f = LogFilterManager.getLogFilter(filterNames[i]);
            print("# 204 " + filterNames[i] + " - " + f.description() + "\n");
        }
        print("# 205 end filter list\n");
    }

    void onListFormatters() {
        print("# 206 formatter list\n");
        String formatterNames[] = LogFormatterManager.getFormatterNames();
        for (int i = 0; i < formatterNames.length; i++) {
            LogFormatter fmt = LogFormatterManager.getLogFormatter(formatterNames[i]);
            print("# 207 " + formatterNames[i] + " - " + fmt.description() + "\n");
        }
        print("# 208 end formatter list\n");
    }

    private void print(String s) {
        try {
            enqueue(IOUtils.utf8ByteBuffer(s));
        } catch (IOException e) {
            log.log(LogLevel.WARNING, "error printing", e);
            try {
                close();
            } catch (IOException e2) {
                // ignore
            }
        }
    }

    void onStats() {

        print(new StringBuilder(80)
                      .append("# 206 stats start (this connection)\n")
                      .append("# 207 ").append(numHandled).append(" handled\n")
                      .append("# 208 ").append(numDropped).append(" dropped\n")
                      .append("# 209 ").append(numQueued)
                      .append(" handled and queued\n")
                      .append("# 210 ").append(totalBytesWritten)
                      .append(" total bytes written\n")
                      .append("# 211 stats end\n")
                      .toString()
        );
    }


    public int getNumHandled() {
        return numHandled;
    }

    public int getNumQueued() {
        return numQueued;
    }

    public int getNumDropped() {
        return numDropped;
    }

    public long getTotalBytesWritten() {
        return totalBytesWritten;
    }

    public String getLogFilterName() {
        return filterName;
    }

    void setFilter(LogFilter filter) {
        this.filter = filter;
    }

}