fluentd-plugins-pulsar 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,377 @@
1
+ # Copyright 2017 Hiroaki Kawata
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'socket'
16
+ require 'fluent/plugin/lib/pulsar_client/PulsarApi.pb'
17
+ require 'digest/crc32c'
18
+
19
+ module Message
20
+
21
+ class PulsarMessage
22
+ attr_accessor :client_created_id
23
+ attr_accessor :message_ledger_id
24
+ attr_accessor :message_entry_id
25
+ attr_accessor :message
26
+
27
+ def initialize()
28
+ @client_created_id = 0
29
+ @message_ledger_id = 0
30
+ @message_entry_id = 0
31
+ @message = ""
32
+ end
33
+ end
34
+
35
+ class PulsarClient
36
+
37
+ def initialize()
38
+ @client_created_id = 1
39
+ @request_id = 1
40
+ @producer_id = 1
41
+ end
42
+
43
+ def connect_socket(host, port)
44
+ @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
45
+ sockaddr = Socket.sockaddr_in(port, host)
46
+ @sock.connect(sockaddr)
47
+ end
48
+
49
+ def close_socket()
50
+ @sock.close
51
+ end
52
+
53
+ def command_connect()
54
+ base_command = Pulsar::Proto::BaseCommand.new(
55
+ :type => Pulsar::Proto::BaseCommand::Type::CONNECT
56
+ )
57
+
58
+ base_command.connect = Pulsar::Proto::CommandConnect.new
59
+ base_command.connect.client_version = "ruby-client-0.0.2"
60
+ base_command.connect.protocol_version = 6
61
+
62
+ byte_cmd = base_command.serialize_to_string()
63
+ total_length = byte_cmd.length + 4
64
+
65
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
66
+
67
+ @sock.write(total_frame)
68
+
69
+ recv_length = @sock.read(4).unpack('N')[0]
70
+
71
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
72
+
73
+ recv_cmd_byte = @sock.read(recv_cmd_length)
74
+
75
+ recv_cmd = Pulsar::Proto::BaseCommand.new
76
+ recv_cmd.parse_from_string(recv_cmd_byte)
77
+
78
+
79
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::CONNECTED then
80
+ return true
81
+ else
82
+ return false
83
+ end
84
+ end
85
+
86
+ def command_producer(topic)
87
+ base_command = Pulsar::Proto::BaseCommand.new(
88
+ :type => Pulsar::Proto::BaseCommand::Type::PRODUCER
89
+ )
90
+
91
+ base_command.producer = Pulsar::Proto::CommandProducer.new
92
+ base_command.producer.topic = topic
93
+ base_command.producer.producer_id = @producer_id
94
+ base_command.producer.request_id = @request_id
95
+
96
+
97
+ byte_cmd = base_command.serialize_to_string()
98
+ total_length = byte_cmd.length + 4
99
+
100
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
101
+
102
+ @sock.write(total_frame)
103
+
104
+ recv_length = @sock.read(4).unpack('N')[0]
105
+
106
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
107
+
108
+ recv_cmd_byte = @sock.read(recv_cmd_length)
109
+
110
+ recv_cmd = Pulsar::Proto::BaseCommand.new
111
+ recv_cmd.parse_from_string(recv_cmd_byte)
112
+
113
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::PRODUCER_SUCCESS then
114
+ return true
115
+ else
116
+ return false
117
+ end
118
+ end
119
+
120
+ def command_send(producer_name, num_messages, message)
121
+ base_command = Pulsar::Proto::BaseCommand.new(
122
+ :type => Pulsar::Proto::BaseCommand::Type::SEND
123
+ )
124
+
125
+ base_command.send = Pulsar::Proto::CommandSend.new
126
+ base_command.send.producer_id = @producer_id
127
+ base_command.send.sequence_id = 0
128
+ base_command.send.num_messages = num_messages
129
+
130
+ byte_cmd = base_command.serialize_to_string()
131
+
132
+
133
+ magic_number= [0x0e, 0x01].pack('C*')
134
+ metadata = Pulsar::Proto::MessageMetadata.new
135
+ metadata.producer_name = producer_name
136
+ metadata.sequence_id = 0
137
+ metadata.publish_time = Time.now.to_i * 1000
138
+
139
+ byte_meta = metadata.serialize_to_string()
140
+
141
+ meta_payload = [byte_meta.length].pack('N') + byte_meta + message.bytes.pack('C*')
142
+
143
+ crc = Digest::CRC32c.new
144
+ crc << meta_payload
145
+ checksum = crc.checksum
146
+
147
+ total_length = 4 + byte_cmd.length + 6 + meta_payload.length
148
+
149
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd + magic_number + [checksum].pack('N') + meta_payload
150
+
151
+ @sock.write(total_frame)
152
+
153
+ recv_length = @sock.read(4).unpack('N')[0]
154
+
155
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
156
+
157
+ recv_cmd_byte = @sock.read(recv_cmd_length)
158
+
159
+ recv_cmd = Pulsar::Proto::BaseCommand.new
160
+ recv_cmd.parse_from_string(recv_cmd_byte)
161
+
162
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::PRODUCER_SUCCESS then
163
+ return true
164
+ else
165
+ return false
166
+ end
167
+ end
168
+
169
+ def command_lookup(topic)
170
+
171
+ base_command = Pulsar::Proto::BaseCommand.new(
172
+ :type => Pulsar::Proto::BaseCommand::Type::LOOKUP
173
+ )
174
+
175
+ base_command.lookupTopic = Pulsar::Proto::CommandLookupTopic.new
176
+ base_command.lookupTopic.topic = topic
177
+ base_command.lookupTopic.request_id = @request_id
178
+
179
+ byte_cmd = base_command.serialize_to_string()
180
+ total_length = byte_cmd.length + 4
181
+
182
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
183
+
184
+ @sock.write(total_frame)
185
+
186
+ recv_length = @sock.read(4).unpack('N')[0]
187
+
188
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
189
+
190
+ recv_cmd_byte = @sock.read(recv_cmd_length)
191
+
192
+ recv_cmd = Pulsar::Proto::BaseCommand.new
193
+ recv_cmd.parse_from_string(recv_cmd_byte)
194
+
195
+
196
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::LOOKUP_RESPONSE then
197
+ return true
198
+ else
199
+ print("type:" + recv_cmd.type.to_s + "\n")
200
+ return false
201
+ end
202
+ end
203
+
204
+ def command_subscribe(topic, subscription, sub_type)
205
+ base_command = Pulsar::Proto::BaseCommand.new(
206
+ :type => Pulsar::Proto::BaseCommand::Type::SUBSCRIBE
207
+ )
208
+
209
+ base_command.subscribe = Pulsar::Proto::CommandSubscribe.new
210
+ base_command.subscribe.topic = topic
211
+ base_command.subscribe.subscription = subscription
212
+
213
+ if sub_type == 1 then
214
+ base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Exclusive
215
+ elsif sub_type == 2 then
216
+ base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Shared
217
+ elsif sub_type == 3 then
218
+ base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Failover
219
+ end
220
+
221
+ base_command.subscribe.consumer_id = @client_created_id
222
+ base_command.subscribe.request_id = @request_id
223
+
224
+
225
+ byte_cmd = base_command.serialize_to_string()
226
+ total_length = byte_cmd.length + 4
227
+
228
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
229
+
230
+ @sock.write(total_frame)
231
+
232
+ recv_length = @sock.read(4).unpack('N')[0]
233
+
234
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
235
+
236
+ recv_cmd_byte = @sock.read(recv_cmd_length)
237
+
238
+ recv_cmd = Pulsar::Proto::BaseCommand.new
239
+ recv_cmd.parse_from_string(recv_cmd_byte)
240
+
241
+
242
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::CONNECTED then
243
+ return true
244
+ elsif recv_cmd.type == Pulsar::Proto::BaseCommand::Type::SUCCESS then
245
+ return true
246
+ else
247
+ print("type:" + recv_cmd.type.to_s + "\n")
248
+ return false
249
+ end
250
+ end
251
+
252
+ def command_flow()
253
+ base_command = Pulsar::Proto::BaseCommand.new(
254
+ :type => Pulsar::Proto::BaseCommand::Type::FLOW
255
+ )
256
+
257
+ base_command.flow = Pulsar::Proto::CommandFlow.new
258
+ base_command.flow.consumer_id = @client_created_id
259
+ base_command.flow.messagePermits = 1000
260
+
261
+ byte_cmd = base_command.serialize_to_string()
262
+ total_length = byte_cmd.length + 4
263
+
264
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
265
+
266
+ @sock.write(total_frame)
267
+
268
+ end
269
+
270
+ def command_message()
271
+
272
+ recv_length = @sock.read(4).unpack('N')[0]
273
+
274
+ recv_cmd_length = @sock.read(4).unpack('N')[0]
275
+
276
+ recv_cmd_byte = @sock.read(recv_cmd_length)
277
+
278
+ recv_cmd = Pulsar::Proto::BaseCommand.new
279
+ recv_cmd.parse_from_string(recv_cmd_byte)
280
+
281
+ if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::MESSAGE then
282
+
283
+ consumer_id = recv_cmd.message.consumer_id
284
+ ledgerId = recv_cmd.message.message_id.ledgerId
285
+ entryId = recv_cmd.message.message_id.entryId
286
+
287
+ recv_meta_length = 0
288
+ recv_meta_byte = nil
289
+
290
+ recv_magic = @sock.read(2)
291
+ if recv_magic == [0x0e, 0x01].pack('C*') then
292
+ recv_crc = @sock.read(4)
293
+
294
+ recv_meta_length = @sock.read(4).unpack('N')[0]
295
+ recv_meta_byte = @sock.read(recv_meta_length)
296
+ else
297
+ recv_remain = @sock.read(2)
298
+ recv_meta_length = (recv_magic + recv_remain).unpack('N')[0]
299
+ recv_meta_byte = @sock.read(recv_meta_length)
300
+ end
301
+
302
+ payload_length = recv_length - (4 + recv_cmd_length + 4 + recv_meta_length)
303
+
304
+ message = @sock.read(payload_length)
305
+
306
+ recv_message = PulsarMessage.new
307
+ recv_message.client_created_id = consumer_id
308
+ recv_message.message_ledger_id = ledgerId
309
+ recv_message.message_entry_id = entryId
310
+ recv_message.message = message
311
+
312
+ return recv_message
313
+ end
314
+
315
+ end
316
+
317
+
318
+ def command_ack(consumer_id, ledgerId, entryId)
319
+ base_command = Pulsar::Proto::BaseCommand.new(
320
+ :type => Pulsar::Proto::BaseCommand::Type::ACK
321
+ )
322
+
323
+ base_command.ack = Pulsar::Proto::CommandAck.new
324
+ base_command.ack.consumer_id = consumer_id
325
+ base_command.ack.ack_type = Pulsar::Proto::CommandAck::AckType::Individual
326
+ base_command.ack.message_id = [Pulsar::Proto::MessageIdData.new(
327
+ :ledgerId => ledgerId,
328
+ :entryId => entryId
329
+ )]
330
+ byte_cmd = base_command.serialize_to_string()
331
+ total_length = byte_cmd.length + 4
332
+
333
+ total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd
334
+
335
+ @sock.write(total_frame)
336
+
337
+ end
338
+
339
+ def connect(host, port)
340
+ connect_socket(host, port)
341
+ command_connect()
342
+ end
343
+
344
+ def close()
345
+ close_socket()
346
+ end
347
+
348
+ def send(topic, producer_name, num_messages, message)
349
+ s = command_producer(topic)
350
+
351
+ # try again
352
+ if s == false then
353
+ command_lookup(topic)
354
+ command_producer(topic)
355
+ end
356
+ command_send(producer_name, num_messages, message)
357
+ end
358
+
359
+ def subscribe(topic, subscription, sub_type)
360
+ command_lookup(topic)
361
+ command_subscribe(topic, subscription, sub_type)
362
+ command_flow()
363
+ end
364
+
365
+ def get_message()
366
+ m = command_message()
367
+ return m
368
+ end
369
+
370
+ def ack(consumer_id, ledgerId, entryId)
371
+ command_ack(consumer_id, ledgerId, entryId)
372
+ end
373
+
374
+ end
375
+
376
+ end
377
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluentd-plugins-pulsar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sai Theja K
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " "
14
+ email: sai.theja1008@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/fluent/plugin/lib/in_pulsar.rb
20
+ - lib/fluent/plugin/lib/out_pulsar.rb
21
+ - lib/fluent/plugin/lib/pulsar_client/PulsarApi.pb.rb
22
+ - lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb
23
+ homepage: https://rubygems.org/gems/fluentd-plugins-pulsar
24
+ licenses:
25
+ - Nonstandard
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.1.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Fluentd Input and output plugins for pulsar
46
+ test_files: []