-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathredis.rb
427 lines (362 loc) · 11.9 KB
/
redis.rb
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
197
198
199
200
201
202
203
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# encoding: utf-8
require "logstash/namespace"
require "logstash/inputs/base"
require "logstash/inputs/threadable"
require 'redis'
# This input will read events from a Redis instance; it supports both Redis channels, lists and sortedsets.
#
# The list command (BLPOP) used by Logstash is supported in Redis v1.3.1+, and
# the channel commands used by Logstash are found in Redis v1.3.8+.
# While you may be able to make these Redis versions work, the best performance
# and stability will be found in more recent stable versions. Versions 2.6.0+
# are recommended.
#
# The sortedset commands (ZREM, ZREMRANGEBYRANK, ZRANGE, ZREVRANGE) used by Logstash are supported
# in Redis v2.0.0+
#
# For more information about Redis, see <http://redis.io/>
#
# `batch_count` note: If you use the `batch_count` setting, you *must* use a Redis version 2.6.0 or
# newer. Anything older does not support the operations used by batching.
#
module LogStash module Inputs class Redis < LogStash::Inputs::Threadable
BATCH_EMPTY_SLEEP = 0.25
config_name "redis"
default :codec, "json"
# The hostname of your Redis server.
config :host, :validate => :string, :default => "127.0.0.1"
# The port to connect on.
config :port, :validate => :number, :default => 6379
# The Redis database number.
config :db, :validate => :number, :default => 0
# Initial connection timeout in seconds.
config :timeout, :validate => :number, :default => 5
# Password to authenticate with. There is no authentication by default.
config :password, :validate => :password
# The name of a Redis list or channel.
config :key, :validate => :string, :required => true
# Pop high scores item first in sortedset. No effect for other data types
config :priority_reverse, :validate => :boolean, :default => false
# Specify either list or channel. If `redis\_type` is `list`, then we will BLPOP
# the key. If `redis\_type` is `channel`, then we will SUBSCRIBE to the key.
# If `redis\_type` is `pattern_channel`, then we will PSUBSCRIBE to the key.
# If `redis\_type` is `sortedset`, then we will ZRANGE/ZREVRANGE
config :data_type, :validate => [ "list", "channel", "pattern_channel", "sortedset" ], :required => true
# The number of events to return from Redis using EVAL.
config :batch_count, :validate => :number, :default => 125
public
# public API
# use to store a proc that can provide a redis instance or mock
def add_external_redis_builder(builder) #callable
@redis_builder = builder
self
end
# use to apply an instance directly and bypass the builder
def use_redis(instance)
@redis = instance
self
end
def new_redis_instance
@redis_builder.call
end
def register
@redis_url = "redis://#{@password}@#{@host}:#{@port}/#{@db}"
@redis_builder ||= method(:internal_redis_builder)
# just switch on data_type once
if @data_type == 'list' || @data_type == 'dummy'
@run_method = method(:list_runner)
@stop_method = method(:list_stop)
elsif @data_type == 'sortedset'
@run_method = method(:sortedset_runner)
@stop_method = method(:sortedset_stop)
elsif @data_type == 'channel'
@run_method = method(:channel_runner)
@stop_method = method(:subscribe_stop)
elsif @data_type == 'pattern_channel'
@run_method = method(:pattern_channel_runner)
@stop_method = method(:subscribe_stop)
end
#TODO voir à terme comment fusionner ces deux méthodes
@list_method = batched? ? method(:list_batch_listener) : method(:list_single_listener)
@sortedset_method = batched? ? method(:sortedset_batch_listener) : method(:sortedset_single_listener)
@identity = "#{@redis_url} #{@data_type}:#{@key}"
@logger.info("Registering Redis", :identity => @identity)
end # def register
def run(output_queue)
@run_method.call(output_queue)
rescue LogStash::ShutdownSignal
# ignore and quit
end # def run
def stop
@stop_method.call
end
# private methods -----------------------------
private
def batched?
@batch_count > 1
end
# private
def is_list_type?
@data_type == 'list'
end
# private
def is_sortedset_type?
@data_type == 'sortedset'
end
# private
def redis_params
{
:host => @host,
:port => @port,
:timeout => @timeout,
:db => @db,
:password => @password.nil? ? nil : @password.value
}
end
# private
def internal_redis_builder
::Redis.new(redis_params)
end
# private
def connect
redis = new_redis_instance
list_load_batch_script(redis) if batched? && is_list_type?
sortedset_load_batch_script(redis) if batched? && is_sortedset_type?
redis
end # def connect
# private
def list_load_batch_script(redis)
#A Redis Lua EVAL script to fetch a count of keys
redis_script = <<EOF
local batchsize = tonumber(ARGV[1])
local result = redis.call('lrange', KEYS[1], 0, batchsize)
redis.call('ltrim', KEYS[1], batchsize + 1, -1)
return result
EOF
@redis_script_sha = redis.script(:load, redis_script)
end
# private
def sortedset_load_batch_script(redis)
#A Redis Lua EVAL script to fetch a count of keys
redis_script = "local batchsize = tonumber(ARGV[1])\n"
redis_script << "local zcard = tonumber(redis.call('zcard', KEYS[1]))\n"
redis_script << "local result = redis.call('"
if @priority_reverse then
redis_script << 'zrevrange'
else
redis_script << 'zrange'
end
redis_script << "', KEYS[1], 0, batchsize)\n"
redis_script << "redis.call('"
redis_script << "zremrangebyrank', KEYS[1], "
if @priority_reverse then
redis_script << "zcard - batchsize - 1, zcard"
else
redis_script << "0, batchsize"
end
redis_script << ")\nreturn result\n"
@redis_script_sha = redis.script(:load, redis_script)
end
# private
def queue_event(msg, output_queue)
begin
@codec.decode(msg) do |event|
decorate(event)
output_queue << event
end
rescue => e # parse or event creation error
@logger.error("Failed to create event", :message => msg, :exception => e, :backtrace => e.backtrace);
end
end
# private
def list_stop
return if @redis.nil? || [email protected]?
@redis.quit rescue nil
@redis = nil
end
# private
def list_runner(output_queue)
while !stop?
begin
@redis ||= connect
@list_method.call(@redis, output_queue)
rescue ::Redis::BaseError => e
@logger.warn("Redis connection problem", :exception => e)
# Reset the redis variable to trigger reconnect
@redis = nil
# this sleep does not need to be stoppable as its
# in a while !stop? loop
sleep 1
end
end
end
def list_batch_listener(redis, output_queue)
begin
results = redis.evalsha(@redis_script_sha, [@key], [@batch_count-1])
results.each do |item|
queue_event(item, output_queue)
end
if results.size.zero?
sleep BATCH_EMPTY_SLEEP
end
# Below is a commented-out implementation of 'batch fetch'
# using pipelined LPOP calls. This in practice has been observed to
# perform exactly the same in terms of event throughput as
# the evalsha method. Given that the EVALSHA implementation uses
# one call to Redis instead of N (where N == @batch_count) calls,
# I decided to go with the 'evalsha' method of fetching N items
# from Redis in bulk.
#redis.pipelined do
#error, item = redis.lpop(@key)
#(@batch_count-1).times { redis.lpop(@key) }
#end.each do |item|
#queue_event(item, output_queue) if item
#end
# --- End commented out implementation of 'batch fetch'
# further to the above, the LUA script now uses lrange and trim
# which should further improve the efficiency of the script
rescue ::Redis::CommandError => e
if e.to_s =~ /NOSCRIPT/ then
@logger.warn("Redis may have been restarted, reloading Redis batch EVAL script", :exception => e);
list_load_batch_script(redis)
retry
else
raise e
end
end
end
def list_single_listener(redis, output_queue)
item = redis.blpop(@key, 0, :timeout => 1)
return unless item # from timeout or other conditions
# blpop returns the 'key' read from as well as the item result
# we only care about the result (2nd item in the list).
queue_event(item.last, output_queue)
end
# private
def sortedset_stop
return if @redis.nil? || [email protected]?
@redis.quit rescue nil
@redis = nil
end
# private
def sortedset_runner(output_queue)
while !stop?
begin
@redis ||= connect
@sortedset_method.call(@redis, output_queue)
rescue ::Redis::BaseError => e
@logger.warn("Redis connection problem", :exception => e)
# Reset the redis variable to trigger reconnect
@redis = nil
# this sleep does not need to be stoppable as its
# in a while !stop? loop
sleep 1
end
end
end
def sortedset_batch_listener(redis, output_queue)
begin
results = redis.evalsha(@redis_script_sha, [@key], [@batch_count-1])
results.each do |item|
queue_event(item, output_queue)
end
if results.size.zero?
sleep BATCH_EMPTY_SLEEP
end
rescue ::Redis::CommandError => e
if e.to_s =~ /NOSCRIPT/ then
@logger.warn("Redis may have been restarted, reloading Redis batch EVAL script", :exception => e);
sortedset_load_batch_script(redis)
retry
else
raise e
end
end
end
def sortedset_single_listener(redis, output_queue)
redis.watch(@key) do
if @priority_reverse then
item = redis.zrevrange(@key, 0, 0, :timeout => 1)
else
item = redis.zrange(@key, 0, 0, :timeout => 1)
end
if item.size.zero?
sleep BATCH_EMPTY_SLEEP
end
return unless item.size > 0
redis.multi do |multi|
redis.zrem(@key, item)
end
queue_event(item.first, output_queue)
end
end
# private
def subscribe_stop
return if @redis.nil? || [email protected]?
# if its a SubscribedClient then:
# it does not have a disconnect method (yet)
if @redis.client.is_a?(::Redis::SubscribedClient)
if @data_type == 'pattern_channel'
@redis.client.punsubscribe
else
@redis.client.unsubscribe
end
else
@redis.client.disconnect
end
@redis = nil
end
# private
def redis_runner
begin
@redis ||= connect
yield
rescue ::Redis::BaseError => e
@logger.warn("Redis connection problem", :exception => e)
# Reset the redis variable to trigger reconnect
@redis = nil
Stud.stoppable_sleep(1) { stop? }
retry if !stop?
end
end
# private
def channel_runner(output_queue)
redis_runner do
channel_listener(output_queue)
end
end
# private
def channel_listener(output_queue)
@redis.subscribe(@key) do |on|
on.subscribe do |channel, count|
@logger.info("Subscribed", :channel => channel, :count => count)
end
on.message do |channel, message|
queue_event(message, output_queue)
end
on.unsubscribe do |channel, count|
@logger.info("Unsubscribed", :channel => channel, :count => count)
end
end
end
def pattern_channel_runner(output_queue)
redis_runner do
pattern_channel_listener(output_queue)
end
end
# private
def pattern_channel_listener(output_queue)
@redis.psubscribe @key do |on|
on.psubscribe do |channel, count|
@logger.info("Subscribed", :channel => channel, :count => count)
end
on.pmessage do |pattern, channel, message|
queue_event(message, output_queue)
end
on.punsubscribe do |channel, count|
@logger.info("Unsubscribed", :channel => channel, :count => count)
end
end
end
# end
end end end # Redis Inputs LogStash