Class: Message::PulsarClient

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb

Instance Method Summary collapse

Constructor Details

#initializePulsarClient

Returns a new instance of PulsarClient.



37
38
39
40
41
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 37

def initialize()
  @client_created_id = 1
  @request_id = 1
  @producer_id = 1
end

Instance Method Details

#ack(consumer_id, ledgerId, entryId) ⇒ Object



370
371
372
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 370

def ack(consumer_id, ledgerId, entryId)
  command_ack(consumer_id, ledgerId, entryId)
end

#closeObject



344
345
346
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 344

def close()
  close_socket()
end

#close_socketObject



49
50
51
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 49

def close_socket()
  @sock.close
end

#command_ack(consumer_id, ledgerId, entryId) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 318

def command_ack(consumer_id, ledgerId, entryId)
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::ACK
    )

  base_command.ack = Pulsar::Proto::CommandAck.new
  base_command.ack.consumer_id = consumer_id
  base_command.ack.ack_type = Pulsar::Proto::CommandAck::AckType::Individual
  base_command.ack.message_id = [Pulsar::Proto::MessageIdData.new(
      :ledgerId => ledgerId,
      :entryId => entryId
  )]
  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

end

#command_connectObject



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 53

def command_connect()
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::CONNECT
    )

  base_command.connect = Pulsar::Proto::CommandConnect.new
  base_command.connect.client_version  = "ruby-client-0.0.2"
  base_command.connect.protocol_version  = 6

  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)


  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::CONNECTED then
    return true
  else
    return false
  end
end

#command_flowObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 252

def command_flow()
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::FLOW
    )

  base_command.flow = Pulsar::Proto::CommandFlow.new
  base_command.flow.consumer_id = @client_created_id
  base_command.flow.messagePermits = 1000

  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

end

#command_lookup(topic) ⇒ Object



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 169

def command_lookup(topic)

  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::LOOKUP
    )

  base_command.lookupTopic = Pulsar::Proto::CommandLookupTopic.new
  base_command.lookupTopic.topic = topic
  base_command.lookupTopic.request_id = @request_id

  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)


  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::LOOKUP_RESPONSE then
    return true
  else
    print("type:" + recv_cmd.type.to_s + "\n")
    return false
  end
end

#command_messageObject



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 270

def command_message()
  
  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)

  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::MESSAGE then

    consumer_id = recv_cmd.message.consumer_id
    ledgerId = recv_cmd.message.message_id.ledgerId
    entryId = recv_cmd.message.message_id.entryId
    
    recv_meta_length = 0
    recv_meta_byte = nil

    recv_magic = @sock.read(2)
    if recv_magic == [0x0e, 0x01].pack('C*') then
      recv_crc = @sock.read(4)
    
      recv_meta_length = @sock.read(4).unpack('N')[0]
      recv_meta_byte = @sock.read(recv_meta_length)
    else
      recv_remain = @sock.read(2)
      recv_meta_length = (recv_magic + recv_remain).unpack('N')[0]
      recv_meta_byte = @sock.read(recv_meta_length)
    end

    payload_length = recv_length - (4 + recv_cmd_length + 4 + recv_meta_length)
    
    message = @sock.read(payload_length)  

    recv_message = PulsarMessage.new
    recv_message.client_created_id = consumer_id
    recv_message.message_ledger_id = ledgerId
    recv_message.message_entry_id = entryId
    recv_message.message = message

    return recv_message
  end

end

#command_producer(topic) ⇒ Object



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 86

def command_producer(topic)
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::PRODUCER
    )

  base_command.producer = Pulsar::Proto::CommandProducer.new
  base_command.producer.topic = topic
  base_command.producer.producer_id = @producer_id
  base_command.producer.request_id = @request_id


  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)

  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::PRODUCER_SUCCESS then
    return true 
  else
    return false  
  end
end

#command_send(producer_name, num_messages, message) ⇒ Object



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 120

def command_send(producer_name, num_messages, message)
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::SEND
    )

  base_command.send = Pulsar::Proto::CommandSend.new
  base_command.send.producer_id = @producer_id
  base_command.send.sequence_id = 0
  base_command.send.num_messages = num_messages

  byte_cmd = base_command.serialize_to_string()


  magic_number= [0x0e, 0x01].pack('C*')
   = Pulsar::Proto::.new
  .producer_name = producer_name
  .sequence_id = 0
  .publish_time = Time.now.to_i * 1000

  byte_meta = .serialize_to_string()

  meta_payload = [byte_meta.length].pack('N') + byte_meta + message.bytes.pack('C*')

  crc = Digest::CRC32c.new
  crc << meta_payload
  checksum = crc.checksum

  total_length = 4 + byte_cmd.length + 6 + meta_payload.length

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd + magic_number + [checksum].pack('N') + meta_payload

  @sock.write(total_frame)

  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)

  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::PRODUCER_SUCCESS then
    return true
  else
    return false
  end
end

#command_subscribe(topic, subscription, sub_type) ⇒ Object



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
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 204

def command_subscribe(topic, subscription, sub_type)
  base_command = Pulsar::Proto::BaseCommand.new(
      :type => Pulsar::Proto::BaseCommand::Type::SUBSCRIBE
    )

  base_command.subscribe = Pulsar::Proto::CommandSubscribe.new
  base_command.subscribe.topic = topic
  base_command.subscribe.subscription = subscription

  if sub_type == 1 then
    base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Exclusive
  elsif sub_type == 2 then
    base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Shared
  elsif sub_type == 3 then
    base_command.subscribe.subType = Pulsar::Proto::CommandSubscribe::SubType::Failover
  end  

  base_command.subscribe.consumer_id = @client_created_id
  base_command.subscribe.request_id = @request_id


  byte_cmd = base_command.serialize_to_string()
  total_length = byte_cmd.length + 4

  total_frame = [total_length].pack('N') + [byte_cmd.length].pack('N') + byte_cmd

  @sock.write(total_frame)

  recv_length = @sock.read(4).unpack('N')[0]

  recv_cmd_length = @sock.read(4).unpack('N')[0]

  recv_cmd_byte = @sock.read(recv_cmd_length)
  
  recv_cmd = Pulsar::Proto::BaseCommand.new
  recv_cmd.parse_from_string(recv_cmd_byte)


  if recv_cmd.type == Pulsar::Proto::BaseCommand::Type::CONNECTED then
    return true
  elsif recv_cmd.type == Pulsar::Proto::BaseCommand::Type::SUCCESS then
    return true 
  else
    print("type:" + recv_cmd.type.to_s + "\n")
    return false
  end
end

#connect(host, port) ⇒ Object



339
340
341
342
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 339

def connect(host, port)
  connect_socket(host, port)
  command_connect()
end

#connect_socket(host, port) ⇒ Object



43
44
45
46
47
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 43

def connect_socket(host, port)
  @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  sockaddr = Socket.sockaddr_in(port, host)
  @sock.connect(sockaddr)  
end

#get_messageObject



365
366
367
368
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 365

def get_message()
  m = command_message()
  return m 
end

#send(topic, producer_name, num_messages, message) ⇒ Object



348
349
350
351
352
353
354
355
356
357
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 348

def send(topic, producer_name, num_messages, message)
  s = command_producer(topic)

  # try again
  if s == false then
    command_lookup(topic)
    command_producer(topic)
  end
  command_send(producer_name, num_messages, message)
end

#subscribe(topic, subscription, sub_type) ⇒ Object



359
360
361
362
363
# File 'lib/fluent/plugin/lib/pulsar_client/PulsarClient.rb', line 359

def subscribe(topic, subscription, sub_type)
  command_lookup(topic)
  command_subscribe(topic, subscription, sub_type)
  command_flow()
end