Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit e712891

Browse files
author
Brandon Tom
committed
added purbon's changes, (changed === to == everywhere, added .freeze to constants, removed Jruby check, reorganized private methods)
1 parent 0ccc9c2 commit e712891

File tree

2 files changed

+53
-59
lines changed

2 files changed

+53
-59
lines changed

lib/logstash/inputs/DynamoDBLogParser.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def parse_scan(log, new_image_size)
5858
@key_schema.each { |x|
5959
@hash_template["dynamodb"]["keys"][x] = data_hash[x]
6060
}
61-
unless @view_type === "keys_only"
61+
unless @view_type == "keys_only"
6262
size_bytes += new_image_size
6363
@hash_template["dynamodb"]["newImage"] = data_hash
6464
end
@@ -69,6 +69,11 @@ def parse_scan(log, new_image_size)
6969
return parse_view_type(@hash_template)
7070
end
7171

72+
public
73+
def parse_stream(log)
74+
return parse_view_type(JSON.parse(@mapper.writeValueAsString(log))["internalObject"])
75+
end
76+
7277
private
7378
def calculate_key_size_in_bytes(record)
7479
key_size = 0
@@ -88,14 +93,9 @@ def calculate_key_size_in_bytes(record)
8893
return key_size
8994
end
9095

91-
public
92-
def parse_stream(log)
93-
return parse_view_type(JSON.parse(@mapper.writeValueAsString(log))["internalObject"])
94-
end
95-
9696
private
9797
def parse_view_type(hash)
98-
if @log_format === LogStash::Inputs::DynamoDB::LF_PLAIN
98+
if @log_format == LogStash::Inputs::DynamoDB::LF_PLAIN
9999
return hash.to_json
100100
end
101101
case @view_type
@@ -110,7 +110,7 @@ def parse_view_type(hash)
110110

111111
private
112112
def parse_format(hash)
113-
if @log_format === LogStash::Inputs::DynamoDB::LF_DYNAMODB
113+
if @log_format == LogStash::Inputs::DynamoDB::LF_DYNAMODB
114114
return hash.to_json
115115
else
116116
return dynamodb_to_json(hash)
@@ -128,7 +128,7 @@ def formatAttributeValueMap(hash)
128128
hash.each do |k, v|
129129
dynamodb_key = v.keys.first
130130
dynamodb_value = v.values.first
131-
if @log_format === LogStash::Inputs::DynamoDB::LF_JSON_NO_BIN and (dynamodb_key === "BS" or dynamodb_key === "B")
131+
if @log_format == LogStash::Inputs::DynamoDB::LF_JSON_NO_BIN and (dynamodb_key == "BS" or dynamodb_key == "B")
132132
keys_to_delete.push(k) # remove binary values and binary sets
133133
next
134134
end

lib/logstash/inputs/dynamodb.rb

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ module KCL
7575
class LogStash::Inputs::DynamoDB < LogStash::Inputs::Base
7676
config_name "dynamodb"
7777

78-
LF_DYNAMODB = "dymamodb"
79-
LF_JSON_NO_BIN = "json_drop_binary"
80-
LF_PLAIN = "plain"
81-
LF_JSON_BIN_AS_TEXT = "json_binary_as_text"
82-
VT_KEYS_ONLY = "keys_only"
83-
VT_OLD_IMAGE = "old_image"
84-
VT_NEW_IMAGE = "new_image"
85-
VT_ALL_IMAGES = "new_and_old_images"
78+
LF_DYNAMODB = "dymamodb".freeze
79+
LF_JSON_NO_BIN = "json_drop_binary".freeze
80+
LF_PLAIN = "plain".freeze
81+
LF_JSON_BIN_AS_TEXT = "json_binary_as_text".freeze
82+
VT_KEYS_ONLY = "keys_only".freeze
83+
VT_OLD_IMAGE = "old_image".freeze
84+
VT_NEW_IMAGE = "new_image".freeze
85+
VT_ALL_IMAGES = "new_and_old_images".freeze
8686

8787
default :codec, 'json'
8888

@@ -129,7 +129,6 @@ class LogStash::Inputs::DynamoDB < LogStash::Inputs::Base
129129
# Number of threads to use when scanning the specified table
130130
config :number_of_scan_threads, :validate => :number, :default => 1
131131

132-
133132
# Number of threads to write to the logstash queue when scanning the table
134133
config :number_of_write_threads, :validate => :number, :default => 1
135134

@@ -155,11 +154,6 @@ def build_credentials
155154

156155
public
157156
def register
158-
if not LogStash::Environment.jruby?
159-
raise(LogStash::PluginLoadingError, "KCL requires JRuby for streams processing")
160-
end # if not LogStash::Environment.jruby?
161-
162-
163157
LogStash::Logger.setup_log4j(@logger)
164158

165159
@host = Socket.gethostname
@@ -168,10 +162,10 @@ def register
168162
@credentials = build_credentials()
169163
@logger.info("Checkpointer: " + @checkpointer)
170164

171-
if @perform_scan and @view_type === VT_OLD_IMAGE
165+
if @perform_scan and @view_type == VT_OLD_IMAGE
172166
raise(LogStash::ConfigurationError, "Cannot perform scan with view type: " + @view_type + " configuration")
173167
end
174-
if @view_type === VT_ALL_IMAGES and (not @log_format === LF_PLAIN)
168+
if @view_type == VT_ALL_IMAGES and (not @log_format == LF_PLAIN)
175169
raise(LogStash::ConfigurationError, "Cannot show view_type: " + @view_type + ", with log_format: " + @log_format)
176170
end
177171

@@ -198,6 +192,34 @@ def register
198192
end # unless @perform_stream
199193
end # def register
200194

195+
public
196+
def run(logstash_queue)
197+
begin
198+
run_with_catch(logstash_queue)
199+
rescue LogStash::ShutdownSignal
200+
exit_threads
201+
until @queue.empty?
202+
@logger.info("Flushing rest of events in logstash queue")
203+
event = @queue.pop()
204+
queue_event(@parser.parse_stream(event), logstash_queue, @host)
205+
end # until [email protected]?
206+
end # begin
207+
end # def run(logstash_queue)
208+
209+
# Starts KCL app in a background thread
210+
# Starts parallel scan if need be in a background thread
211+
private
212+
def run_with_catch(logstash_queue)
213+
if @perform_scan
214+
scan(logstash_queue)
215+
end # if @perform_scan
216+
217+
# Once scan is finished, start kcl thread to read from streams
218+
if @perform_stream
219+
stream(logstash_queue)
220+
end # unless @perform_stream
221+
end # def run
222+
201223
private
202224
def setup_stream
203225
worker_id = SecureRandom.uuid()
@@ -222,21 +244,21 @@ def setup_stream
222244
stream_status = stream_description.getStreamStatus()
223245

224246
stream_view_type = stream_description.getStreamViewType().to_s.downcase
225-
unless (stream_view_type === @view_type or @view_type === VT_KEYS_ONLY or stream_view_type === VT_ALL_IMAGES)
247+
unless (stream_view_type == @view_type or @view_type == VT_KEYS_ONLY or stream_view_type == VT_ALL_IMAGES)
226248
raise(LogStash::ConfigurationError, "Cannot stream " + @view_type + " when stream is setup for " + stream_view_type)
227249
end
228250

229-
while stream_status === "ENABLING"
230-
if(stream_status === "ENABLING")
251+
while stream_status == "ENABLING"
252+
if(stream_status == "ENABLING")
231253
@logger.info("Sleeping until stream is enabled")
232254
sleep(1)
233-
end # if stream_status === "ENABLING"
255+
end # if stream_status == "ENABLING"
234256
stream_description = dynamodb_streams_client.describeStream(AmazonDynamoDB::DescribeStreamRequest.new() \
235257
.withStreamArn(stream_arn)).getStreamDescription()
236258
stream_status = stream_description.getStreamStatus()
237259
end # while not active
238260

239-
if not stream_status === "ENABLED"
261+
if not stream_status == "ENABLED"
240262
raise(LogStash::PluginLoadingError, "No streams are enabled")
241263
end # if not active
242264
@logger.info("Stream Id: " + stream_arn)
@@ -259,34 +281,6 @@ def setup_stream
259281
@worker = KCL::Worker.new(LogStashRecordProcessorFactory.new(@queue), kcl_config, adapter, @dynamodb_client, cloudwatch_client)
260282
end # def setup_stream
261283

262-
public
263-
def run(logstash_queue)
264-
begin
265-
run_with_catch(logstash_queue)
266-
rescue LogStash::ShutdownSignal
267-
exit_threads
268-
until @queue.empty?
269-
@logger.info("Flushing rest of events in logstash queue")
270-
event = @queue.pop()
271-
queue_event(@parser.parse_stream(event), logstash_queue, @host)
272-
end # until [email protected]?
273-
end # begin
274-
end # def run(logstash_queue)
275-
276-
# Starts KCL app in a background thread
277-
# Starts parallel scan if need be in a background thread
278-
private
279-
def run_with_catch(logstash_queue)
280-
if @perform_scan
281-
scan(logstash_queue)
282-
end # if @perform_scan
283-
284-
# Once scan is finished, start kcl thread to read from streams
285-
if @perform_stream
286-
stream(logstash_queue)
287-
end # unless @perform_stream
288-
end # def run
289-
290284
private
291285
def scan(logstash_queue)
292286
@logger.info("Starting scan...")
@@ -298,7 +292,7 @@ def scan(logstash_queue)
298292
scan_queue = @logstash_writer.getQueue()
299293
while true
300294
event = scan_queue.take()
301-
if event.getEntry().nil? and event.getSize() === -1
295+
if event.getEntry().nil? and event.getSize() == -1
302296
break
303297
end # if event.isEmpty()
304298
queue_event(@parser.parse_scan(event.getEntry(), event.getSize()), logstash_queue, @host)

0 commit comments

Comments
 (0)