forked from cloudera/flume
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThriftEventSink.java
196 lines (170 loc) · 6 KB
/
ThriftEventSink.java
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
/**
* Licensed to Cloudera, Inc. under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Cloudera, Inc. licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cloudera.flume.handlers.thrift;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.flume.conf.SinkFactory.SinkBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.handlers.thrift.ThriftFlumeEventServer.Client;
import com.cloudera.flume.reporter.ReportEvent;
/**
* This is a sink that sends events to a remote host/port.
*/
public class ThriftEventSink extends EventSink.Base {
static Logger LOG = Logger.getLogger(ThriftEventSink.class);
final public static String A_SERVERHOST = "serverHost";
final public static String A_SERVERPORT = "serverPort";
final public static String A_SENTBYTES = "sentBytes";
String host;
int port;
Client client;
TTransport transport;
TStatsTransport stats;
boolean nonblocking;
AtomicLong sentBytes = new AtomicLong();
public ThriftEventSink(String host, int port, boolean nonblocking) {
this.host = host;
this.port = port;
this.nonblocking = nonblocking;
}
public ThriftEventSink(String host, int port) {
this(host, port, false);
}
@Override
public void append(Event e) throws IOException {
ThriftFlumeEvent tfe = ThriftEventAdaptor.convert(e);
try {
client.append(tfe);
sentBytes.set(stats.getBytesWritten());
super.append(e);
} catch (TException e1) {
throw new IOException("Append failed " + e1.getMessage(), e1);
}
}
@Override
public void close() throws IOException {
if (transport != null) {
transport.close();
transport = null;
LOG.info("ThriftEventSink on port " + port + " closed");
}
}
public void open(boolean secured) throws IOException {
try {
// TODO: will "TFramedTransport" handle nonblocking SSLSockets correctly?
if(secured) {
if (nonblocking) {
// non blocking must use "Framed transport"
transport = new TSocket(SSLSocketFactory.getDefault().createSocket(host, port));
stats = new TStatsTransport(transport);
transport = new TFramedTransport(stats);
} else {
transport = new TSocket(SSLSocketFactory.getDefault().createSocket(host, port));
stats = new TStatsTransport(transport);
transport = stats;
}
} else {
if (nonblocking) {
// non blocking must use "Framed transport"
transport = new TSocket(host, port);
stats = new TStatsTransport(transport);
transport = new TFramedTransport(stats);
} else {
transport = new TSocket(host, port);
stats = new TStatsTransport(transport);
transport = stats;
}
}
TProtocol protocol = new TBinaryProtocol(transport);
if (!transport.isOpen()) {
transport.open();
}
client = new Client(protocol);
LOG.info("ThriftEventSink open on port " + port + " opened");
} catch (TTransportException e) {
throw new IOException("Failed to open thrift event sink at " + host + ":"
+ port + " : " + e.getMessage());
}
}
@Override
public void open() throws IOException {
FlumeConfiguration conf = FlumeConfiguration.get();
boolean secured = conf.getIsSecureSSLTransport();
open(secured);
}
@Override
public ReportEvent getReport() {
ReportEvent rpt = super.getReport();
rpt.setStringMetric(A_SERVERHOST, host);
rpt.setLongMetric(A_SERVERPORT, port);
rpt.setLongMetric(A_SENTBYTES, sentBytes.get());
return rpt;
}
public static void main(String argv[]) {
FlumeConfiguration conf = FlumeConfiguration.get();
ThriftEventSink sink = new ThriftEventSink("localhost", conf
.getCollectorPort());
try {
sink.open();
for (int i = 0; i < 100; i++) {
Event e = new EventImpl(("This is a test " + i).getBytes());
sink.append(e);
Thread.sleep(200);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static SinkBuilder builder() {
return new SinkBuilder() {
@Override
public EventSink build(Context context, String... args) {
if (args.length > 2) {
throw new IllegalArgumentException(
"usage: thrift([hostname, [portno]]) ");
}
String host = FlumeConfiguration.get().getCollectorHost();
int port = FlumeConfiguration.get().getCollectorPort();
if (args.length >= 1) {
host = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
return new ThriftEventSink(host, port);
}
};
}
}