1
1
package org .logstash .beats ;
2
2
3
- import io .netty .channel .ChannelHandler ;
4
3
import io .netty .channel .ChannelHandlerContext ;
5
4
import io .netty .channel .SimpleChannelInboundHandler ;
6
- import io .netty .handler .timeout .IdleState ;
7
- import io .netty .handler .timeout .IdleStateEvent ;
8
5
import io .netty .util .AttributeKey ;
9
6
import org .apache .logging .log4j .LogManager ;
10
7
import org .apache .logging .log4j .Logger ;
11
- import org .apache .logging .log4j .ThreadContext ;
12
8
13
9
import java .net .InetSocketAddress ;
14
- import java .net .SocketAddress ;
15
- import java .util .Map ;
16
- import java .util .UUID ;
17
- import java .util .concurrent .atomic .AtomicBoolean ;
18
10
import javax .net .ssl .SSLHandshakeException ;
19
11
20
- public class BeatsHandler extends SimpleChannelInboundHandler <Batch > {
12
+ public class BeatsHandler extends SimpleChannelInboundHandler <NewBatch > {
21
13
private final static Logger logger = LogManager .getLogger (BeatsHandler .class );
22
14
public static AttributeKey <Boolean > PROCESSING_BATCH = AttributeKey .valueOf ("processing-batch" );
23
15
private final IMessageListener messageListener ;
24
16
private ChannelHandlerContext context ;
25
17
26
18
27
-
28
19
public BeatsHandler (IMessageListener listener ) {
29
20
messageListener = listener ;
30
21
}
31
22
32
23
@ Override
33
- public void handlerAdded (ChannelHandlerContext ctx ) throws Exception {
24
+ public void channelActive (final ChannelHandlerContext ctx ) throws Exception {
25
+ if (logger .isTraceEnabled ()){
26
+ logger .trace (format ("Channel Active" ));
27
+ }
28
+ ctx .channel ().attr (BeatsHandler .PROCESSING_BATCH ).set (false );
29
+
30
+ super .channelActive (ctx );
34
31
context = ctx ;
35
32
messageListener .onNewConnection (ctx );
36
- // Give some breathing room on new clients to receive the keep alive.
37
- ctx .channel ().attr (PROCESSING_BATCH ).set (false );
38
33
}
39
34
40
35
@ Override
41
- public void handlerRemoved (ChannelHandlerContext ctx ) throws Exception {
42
- ctx .channel ().attr (PROCESSING_BATCH ).set (false );
36
+ public void channelInactive (ChannelHandlerContext ctx ) throws Exception {
37
+ super .channelInactive (ctx );
38
+ if (logger .isTraceEnabled ()){
39
+ logger .trace (format ("Channel Inactive" ));
40
+ }
41
+ ctx .channel ().attr (BeatsHandler .PROCESSING_BATCH ).set (false );
43
42
messageListener .onConnectionClose (ctx );
44
43
}
45
44
46
45
47
46
@ Override
48
47
public void channelRead0 (ChannelHandlerContext ctx , Batch batch ) throws Exception {
49
- if (logger .isDebugEnabled ()) {
50
- logger .debug (format ("Received a new payload" ));
48
+ if (logger .isTraceEnabled ()) {
49
+ logger .trace (format ("Received a new payload" ));
51
50
}
52
51
53
- ctx .channel ().attr (PROCESSING_BATCH ).set (true );
52
+ ctx .channel ().attr (BeatsHandler . PROCESSING_BATCH ).set (true );
54
53
for (Message message : batch .getMessages ()) {
55
- if (logger .isDebugEnabled ()) {
56
- logger .debug (format ("Sending a new message for the listener, sequence: " + message .getSequence ()));
54
+ if (logger .isTraceEnabled ()) {
55
+ logger .trace (format ("Sending a new message for the listener, sequence: " + message .getSequence ()));
57
56
}
58
57
messageListener .onNewMessage (ctx , message );
59
58
@@ -65,6 +64,33 @@ public void channelRead0(ChannelHandlerContext ctx, Batch batch) throws Exceptio
65
64
ctx .channel ().attr (PROCESSING_BATCH ).set (false );
66
65
}
67
66
67
+ @ Override
68
+ public void channelRead0 (ChannelHandlerContext ctx , NewBatch batch ) throws Exception {
69
+ if (logger .isDebugEnabled ()) {
70
+ logger .debug (format ("Received a new payload" ));
71
+ }
72
+
73
+ ctx .channel ().attr (PROCESSING_BATCH ).set (true );
74
+ batch .getMessageStream ().forEach (e -> {
75
+ messageListener .onNewMessage (ctx , e );
76
+ if (needAck (e )){
77
+ ack (ctx , e );
78
+ }
79
+ });
80
+ // for(Message message : batch.getMessages()) {
81
+ // if(logger.isDebugEnabled()) {
82
+ // logger.debug(format("Sending a new message for the listener, sequence: " + message.getSequence()));
83
+ // }
84
+ // messageListener.onNewMessage(ctx, message);
85
+ //
86
+ // if(needAck(message)) {
87
+ // ack(ctx, message);
88
+ // }
89
+ // }
90
+ ctx .flush ();
91
+ ctx .channel ().attr (PROCESSING_BATCH ).set (false );
92
+ }
93
+
68
94
/*
69
95
* Do not propagate the SSL handshake exception down to the ruby layer handle it locally instead and close the connection
70
96
* if the channel is still active. Calling `onException` will flush the content of the codec's buffer, this call
@@ -75,26 +101,33 @@ public void channelRead0(ChannelHandlerContext ctx, Batch batch) throws Exceptio
75
101
* overlap Filebeat transmission; we were recommending multiline at the source in v5 and in v6 we enforce it.
76
102
*/
77
103
@ Override
78
- public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) {
79
- ctx .close ();
80
-
81
- if (!(cause instanceof SSLHandshakeException )) {
82
- messageListener .onException (ctx , cause );
83
- }
84
-
85
- String causeMessage = cause .getMessage () == null ? cause .getClass ().toString () : cause .getMessage ();
104
+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
105
+ try {
106
+ if (!(cause instanceof SSLHandshakeException )) {
107
+ messageListener .onException (ctx , cause );
108
+ }
109
+ String causeMessage = cause .getMessage () == null ? cause .getClass ().toString () : cause .getMessage ();
86
110
87
- if (logger .isDebugEnabled ()){
88
- logger .debug (format ("Handling exception: " + causeMessage ), cause );
111
+ if (logger .isDebugEnabled ()){
112
+ logger .debug (format ("Handling exception: " + causeMessage ), cause );
113
+ }
114
+ logger .info (format ("Handling exception: " + causeMessage ));
115
+ } finally {
116
+ super .exceptionCaught (ctx , cause );
117
+ ctx .flush ();
118
+ ctx .close ();
89
119
}
90
- logger .info (format ("Handling exception: " + causeMessage ));
91
120
}
92
121
93
122
private boolean needAck (Message message ) {
94
- return message .getSequence () == message .getBatch ().getBatchSize ();
123
+ return message .getSequence () == message .getNewBatch ().getBatchSize ();
95
124
}
96
125
97
126
private void ack (ChannelHandlerContext ctx , Message message ) {
127
+ logger .warn (format ("Acking message number " + message .getSequence ()));
128
+ if (logger .isTraceEnabled ()){
129
+ logger .trace (format ("Acking message number " + message .getSequence ()));
130
+ }
98
131
writeAck (ctx , message .getBatch ().getProtocol (), message .getSequence ());
99
132
}
100
133
0 commit comments