/
elasticthrift_output.go
137 lines (127 loc) · 3.39 KB
/
elasticthrift_output.go
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
package main
import (
"net"
"sync"
"time"
"log"
"strings"
"git-wip-us.apache.org/repos/asf/thrift.git/lib/go/thrift"
"github.com/fayizk1/gen-go/elasticsearch" // generated code
)
const BUFFER_SIZE = 2048
type thriftClient struct {
sync.Mutex
client *elasticsearch.RestClient
host, thriftPort string
outputPipe chan []byte
flushInterval int
closeCh chan struct{}
}
func ThriftConnect(host string, thriftPort string) (*elasticsearch.RestClient, error) {
binaryProtocol := thrift.NewTBinaryProtocolFactoryDefault()
socket, err := thrift.NewTSocket(net.JoinHostPort(host, thriftPort))
if err != nil {
return nil, err
}
bufferedTransport := thrift.NewTBufferedTransport(socket, BUFFER_SIZE)
client := elasticsearch.NewRestClientFactory(bufferedTransport, binaryProtocol)
if err := bufferedTransport.Open(); err != nil {
return nil, err
}
return client, nil
}
func NewThriftClient(uri string, outputPipe chan []byte, flushInterval int) *thriftClient {
split_uri := strings.Split(uri, ":")
if len(split_uri) != 2 {
panic("Thrift Client : Unknow URI")
}
host, thriftPort := split_uri[0], split_uri[1]
client, err := ThriftConnect(host, thriftPort)
if err != nil {
panic("Thrift Client : Unable to connect")
}
closeCh := make(chan struct{})
return &thriftClient{client:client, host:host, thriftPort:thriftPort, outputPipe: outputPipe, flushInterval : flushInterval, closeCh : closeCh}
}
func (tc *thriftClient) Reconnect() {
tc.Lock()
defer tc.Unlock()
if tc.client.Transport != nil {
tc.client.Transport.Close()
}
connect:
log.Println("Reconnecting thrift client- ", tc.host, tc.thriftPort)
client, err := ThriftConnect(tc.host, tc.thriftPort)
if err != nil {
log.Println("Reconnecting failed, waiting 2 Sec before next try")
time.Sleep(2 * time.Second)
goto connect
}
tc.client = client
}
func (tc *thriftClient) SendData(request elasticsearch.RestRequest) error {
tc.Lock()
defer tc.Unlock()
_,err := tc.client.Execute(&request)
if err != nil {
go tc.Reconnect()
return err
}
return nil
}
func (tc *thriftClient) Run() error {
go func () {
var request = elasticsearch.RestRequest{
Method: elasticsearch.Method_POST,
Uri: "/_bulk",
}
log.Println(tc.flushInterval)
var ticker = time.NewTicker(time.Duration(tc.flushInterval) * time.Millisecond)
if ticker == nil {
panic("Ticker not initialized")
}
log.Println("added ticker", ticker)
var messages []byte
mainloop:
for {
select {
case <-ticker.C:
if len(messages) == 0 {
continue mainloop
}
request.Body = messages
send:
err := tc.SendData(request)
if err != nil {
log.Println("Failed to send message,", err)
time.Sleep(100 * time.Millisecond)
log.Println("sending data again")
goto send
}
messages = nil
case m := <- tc.outputPipe:
messages = append(messages, m...)
case <-tc.closeCh:
if len(messages) == 0 {
return
}
request.Body = messages
err := tc.SendData(request)
if err != nil {
log.Println("Failed to send message, Not retrying", err)
}
return
}
}
}()
return nil
}
func (tc *thriftClient) Close(pluginName string) {
tc.closeCh <-struct{}{}
log.Println("Closed plugin", pluginName)
}
func init() {
RegisterOutputPlugin("elasticthrift_output", func(uri string, outputPipe chan []byte, flushInterval int) interface{} {
return NewThriftClient(uri, outputPipe, flushInterval)
})
}