From 436fd3176e4f5807781ed609050bb742ca149222 Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 7 Feb 2017 10:57:40 +0100 Subject: [PATCH 01/16] Support for REDIS priority queues This commit adds support for ZSET queues in REDIS with data_type 'list'. Batch is supported. Priority is based on a field of the message containing a number. If the field is absent or not a number a default priority is then used. It adds the following options to the configuration: * **priority**: *boolean*, default false, enable priority mode * **priority_field**: *string*, default '@timestamp', Priority field to use, if field doesn't exist, priority will be priority_default * **priority_default**: *number*, default '0', Default priority when priority field is not found in the event Usage example: output { redis { host => ["127.0.0.1"] port => 6379 data_type => "list" key => "syslog" priority => true priority_field => "epoch" } } --- lib/logstash/outputs/redis.rb | 64 ++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index aa289a8..416437b 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -3,13 +3,17 @@ require "logstash/namespace" require "stud/buffer" -# This output will send events to a Redis queue using RPUSH. +# This output will send events to a Redis queue using RPUSH +# or ZADD (in priority mode). +# # The RPUSH command is supported in Redis v0.0.7+. Using # PUBLISH to a channel requires at least 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 ZADD command is supported in Redis v1.2.0+. +# # For more information, see http://redis.io/[the Redis homepage] # class LogStash::Outputs::Redis < LogStash::Outputs::Base @@ -51,7 +55,7 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Password to authenticate with. There is no authentication by default. config :password, :validate => :password - # The name of the Redis queue (we'll use RPUSH on this). Dynamic names are + # The name of the Redis queue (we'll use RPUSH or ZADD on this). Dynamic names are # valid here, for example `logstash-%{type}` config :queue, :validate => :string, :deprecated => true @@ -60,22 +64,22 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :key, :validate => :string, :required => false # Either list or channel. If `redis_type` is list, then we will set - # RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. + # RPUSH or ZADD to key. If `redis_type` is channel, then we will PUBLISH to `key`. config :data_type, :validate => [ "list", "channel" ], :required => false - # Set to true if you want Redis to batch up values and send 1 RPUSH command + # Set to true if you want Redis to batch up values and send 1 RPUSH or 1 ZADD command # instead of one command per value to push on the list. Note that this only # works with `data_type="list"` mode right now. # - # If true, we send an RPUSH every "batch_events" events or + # If true, we send an RPUSH or ZADD every "batch_events" events or # "batch_timeout" seconds (whichever comes first). # Only supported for `data_type` is "list". config :batch, :validate => :boolean, :default => false - # If batch is set to true, the number of events we queue up for an RPUSH. + # If batch is set to true, the number of events we queue up for an RPUSH or ZADD. config :batch_events, :validate => :number, :default => 50 - # If batch is set to true, the maximum amount of time between RPUSH commands + # If batch is set to true, the maximum amount of time between RPUSH or ZADD commands # when there are pending events to flush. config :batch_timeout, :validate => :number, :default => 5 @@ -95,6 +99,15 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Zero means to check on every event. config :congestion_interval, :validate => :number, :default => 1 + # Enable priority mode + config :priority, :validate => :boolean, :default => false + + # Priority field to use, if field doesn't exist, priority will be priority_default + config :priority_field, :validate => :string, :default => "@timestamp" + + # Default priority when priority field is not found in the event + config :priority_default, :validate => :number, :default => 0 + def register require 'redis' @@ -176,8 +189,13 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - @redis.rpush(key, events) + if @priority then + @redis.zadd(key, events.map{ |event| [priorize(event), event] }) + else + @redis.rpush(key, events) + end end + # called from Stud::Buffer#buffer_flush when an error occurs def on_flush_error(e) @logger.warn("Failed to send backlog of events to Redis", @@ -222,6 +240,30 @@ def connect Redis.new(params) end # def connect + private + def priorize(event) + if event.is_a?(String) then + begin + @codec.decode(event) do |event_decoded| + event = event_decoded + end + rescue => e # parse or event creation error + @logger.warn("Default priority [" << @priority_default.to_s << "] used, can't decode event [" << @event << "]") + return @priority_default + end + end + + priority_value=event.get(@priority_field) + # Is it a number ? + if priority_value.to_s =~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then + return priority_value.to_f + else + @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or is not a number") + return @priority_default + end + + end + # A string used to identify a Redis instance in log messages def identity @name || "redis://#{@password}@#{@current_host}:#{@current_port}/#{@db} #{@data_type}:#{@key}" @@ -241,7 +283,11 @@ def send_to_redis(event, payload) @redis ||= connect if @data_type == 'list' congestion_check(key) - @redis.rpush(key, payload) + if @priority then + @redis.zadd(key, priorize(event), payload) + else + @redis.rpush(key, payload) + end else @redis.publish(key, payload) end From aa9f18af58b55069610e7be48a4fc385743c6e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Wed, 8 Mar 2017 16:57:06 +0100 Subject: [PATCH 02/16] Added batch_timeout to redis batch unit testing Sometimes redis didn't have the time to ingest all messages and the test fails randomly. It appeared it was due to redis too short timeout. --- spec/unit/outputs/redis_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/unit/outputs/redis_spec.rb b/spec/unit/outputs/redis_spec.rb index 7ed1220..4366acf 100644 --- a/spec/unit/outputs/redis_spec.rb +++ b/spec/unit/outputs/redis_spec.rb @@ -19,6 +19,7 @@ "data_type" => "list", "batch" => true, "batch_events" => 50, + "batch_timeout" => 60 } } let(:redis) { described_class.new(config) } From 68295b7d4dd9b62f225a53f228f168111e32435b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Wed, 8 Mar 2017 17:09:13 +0100 Subject: [PATCH 03/16] Sorted set: Spec and support sortedset data_type As sorted sets are a native redis data type, it's best to support ZSET through data_type option instead to boolean priority option. A first group of integration tests (batched and unbatched) for sorted sets is also added with this commit. --- lib/logstash/outputs/redis.rb | 40 ++++++++++----------- spec/integration/outputs/redis_spec.rb | 50 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index 416437b..f5c110c 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -4,7 +4,7 @@ require "stud/buffer" # This output will send events to a Redis queue using RPUSH -# or ZADD (in priority mode). +# or ZADD or PUBLISH. # # The RPUSH command is supported in Redis v0.0.7+. Using # PUBLISH to a channel requires at least v1.3.8+. @@ -55,7 +55,7 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Password to authenticate with. There is no authentication by default. config :password, :validate => :password - # The name of the Redis queue (we'll use RPUSH or ZADD on this). Dynamic names are + # The name of the Redis queue/sortedset (using RPUSH/ZADD cmds). Dynamic names are # valid here, for example `logstash-%{type}` config :queue, :validate => :string, :deprecated => true @@ -64,16 +64,18 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :key, :validate => :string, :required => false # Either list or channel. If `redis_type` is list, then we will set - # RPUSH or ZADD to key. If `redis_type` is channel, then we will PUBLISH to `key`. - config :data_type, :validate => [ "list", "channel" ], :required => false + # RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. + # If `redis_type` is sortedset, then we will ZADD to `key` with weight set + # to content of `priority_field` + config :data_type, :validate => [ "list", "channel", "sortedset" ], :required => false # Set to true if you want Redis to batch up values and send 1 RPUSH or 1 ZADD command - # instead of one command per value to push on the list. Note that this only - # works with `data_type="list"` mode right now. + # instead of one command per value to push on the list or set. Note that this only + # works with `data_type="list"` and `data_type="sortedset"` mode right now. # # If true, we send an RPUSH or ZADD every "batch_events" events or # "batch_timeout" seconds (whichever comes first). - # Only supported for `data_type` is "list". + # Only supported for `data_type` "list" or "sortedset". config :batch, :validate => :boolean, :default => false # If batch is set to true, the number of events we queue up for an RPUSH or ZADD. @@ -86,24 +88,21 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Interval for reconnecting to failed Redis connections config :reconnect_interval, :validate => :number, :default => 1 - # In case Redis `data_type` is `list` and has more than `@congestion_threshold` items, + # In case Redis `data_type` is `list` or `sortedset` and has more than `@congestion_threshold` items, # block until someone consumes them and reduces congestion, otherwise if there are # no consumers Redis will run out of memory, unless it was configured with OOM protection. # But even with OOM protection, a single Redis list can block all other users of Redis, # until Redis CPU consumption reaches the max allowed RAM size. # A default value of 0 means that this limit is disabled. - # Only supported for `list` Redis `data_type`. + # Only supported for `list` and `sortedset` Redis `data_type`. config :congestion_threshold, :validate => :number, :default => 0 # How often to check for congestion. Default is one second. # Zero means to check on every event. config :congestion_interval, :validate => :number, :default => 1 - # Enable priority mode - config :priority, :validate => :boolean, :default => false - # Priority field to use, if field doesn't exist, priority will be priority_default - config :priority_field, :validate => :string, :default => "@timestamp" + config :priority_field, :validate => :string, :default => "epoch" # Default priority when priority field is not found in the event config :priority_default, :validate => :number, :default => 0 @@ -131,7 +130,7 @@ def register if @batch - if @data_type != "list" + if @data_type != "list" and @data_type != "sortedset" raise RuntimeError.new( "batch is not supported with data_type #{@data_type}" ) @@ -189,7 +188,7 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - if @priority then + if @data_type == 'sortedset' then @redis.zadd(key, events.map{ |event| [priorize(event), event] }) else @redis.rpush(key, events) @@ -273,7 +272,7 @@ def send_to_redis(event, payload) # How can I do this sort of thing with codecs? key = event.sprintf(@key) - if @batch && @data_type == 'list' # Don't use batched method for pubsub. + if @batch && (@data_type == 'list' or @data_type == 'sortedset') # Don't use batched method for pubsub. # Stud::Buffer buffer_receive(payload, key) return @@ -283,11 +282,10 @@ def send_to_redis(event, payload) @redis ||= connect if @data_type == 'list' congestion_check(key) - if @priority then - @redis.zadd(key, priorize(event), payload) - else - @redis.rpush(key, payload) - end + @redis.rpush(key, payload) + elsif @data_type == 'sortedset' + congestion_check(key) + @redis.zadd(key, priorize(event), payload) else @redis.publish(key, payload) end diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 4d531f7..ffde9d4 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -72,6 +72,56 @@ end end end + + + shared_examples_for "writing to redis sortedset" do |extra_config| + let(:key) { 10.times.collect { rand(10).to_s }.join("") } + let(:event_count) { Flores::Random.integer(0..10000) } + #let(:message) { Flores::Random.text(0..100) } + let(:default_config) { + { + "key" => key, + "data_type" => "sortedset", + "host" => "localhost" + } + } + let(:redis_config) { + default_config.merge(extra_config || {}) + } + let(:redis_output) { described_class.new(redis_config) } + + before do + redis_output.register + event_count.times do |i| + event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) + redis_output.receive(event) + end + redis_output.close + end + end + + context "when batch_mode is false" do + include_examples "writing to redis sortedset" + end + + context "when batch_mode is true" do + batch_events = Flores::Random.integer(1..1000) + batch_settings = { + "batch" => true, + "batch_events" => batch_events + } + + include_examples "writing to redis sortedset", batch_settings do + + # A canary to make sure we're actually enabling batch mode + # in this shared example. + it "should have batch mode enabled" do + expect(redis_config).to include("batch") + expect(redis_config["batch"]).to be_truthy + end + end + end + end end From 33ae643570048db77dec5a159745c475820b10ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Tue, 14 Mar 2017 09:35:37 +0100 Subject: [PATCH 04/16] ZADD integration spec --- spec/integration/outputs/redis_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index ffde9d4..729e7f8 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -90,8 +90,10 @@ } let(:redis_output) { described_class.new(redis_config) } - before do + it "should call zadd" do redis_output.register + expect(redis_output).not_to receive(:rpush) + expect(redis_output).to receive(:zadd).exactly(1).time event_count.times do |i| event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) redis_output.receive(event) @@ -100,6 +102,7 @@ end end + context "when batch_mode is false" do include_examples "writing to redis sortedset" end From 7dc212b5237f1f25cc76b98d30a4b2b7a5df34e5 Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:17:26 +0200 Subject: [PATCH 05/16] Priority in sortedset is a string --- lib/logstash/outputs/redis.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index f5c110c..1c22cce 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -102,10 +102,12 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :congestion_interval, :validate => :number, :default => 1 # Priority field to use, if field doesn't exist, priority will be priority_default + # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) config :priority_field, :validate => :string, :default => "epoch" # Default priority when priority field is not found in the event - config :priority_default, :validate => :number, :default => 0 + # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) + config :priority_default, :validate => :number, :default => "-1" def register require 'redis' @@ -188,7 +190,7 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - if @data_type == 'sortedset' then + if @data_type == 'sortedset' then @redis.zadd(key, events.map{ |event| [priorize(event), event] }) else @redis.rpush(key, events) @@ -248,19 +250,19 @@ def priorize(event) end rescue => e # parse or event creation error @logger.warn("Default priority [" << @priority_default.to_s << "] used, can't decode event [" << @event << "]") - return @priority_default + return @priority_default.to_s end end - + + priority_value=event.get(@priority_field) - # Is it a number ? - if priority_value.to_s =~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then - return priority_value.to_f - else - @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or is not a number") - return @priority_default + + if priority_value.nil? || priority_value.to_s !~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then + @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or doesn't contain a number") + priority_value=@priority_default end + return priority_value.to_s end # A string used to identify a Redis instance in log messages @@ -285,7 +287,7 @@ def send_to_redis(event, payload) @redis.rpush(key, payload) elsif @data_type == 'sortedset' congestion_check(key) - @redis.zadd(key, priorize(event), payload) + @redis.zadd(key, priorize(event), payload) else @redis.publish(key, payload) end From 214a50453208b36c6a7d5d209cf079abd0ceffda Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:18:50 +0200 Subject: [PATCH 06/16] Sorted test integration test non batch ok --- spec/integration/outputs/redis_spec.rb | 87 +++++++++++++++++++------- 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 729e7f8..56a0351 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -3,14 +3,15 @@ require "logstash/json" require "redis" require "flores/random" +require 'securerandom' describe LogStash::Outputs::Redis do context "integration tests", :integration => true do shared_examples_for "writing to redis list" do |extra_config| - let(:key) { 10.times.collect { rand(10).to_s }.join("") } + let(:key) { SecureRandom.hex } let(:event_count) { Flores::Random.integer(0..10000) } - let(:message) { Flores::Random.text(0..100) } + let(:message) { SecureRandom.hex } # We use hex generation to avoid escaping issues on Windows let(:default_config) { { "key" => key, @@ -75,14 +76,14 @@ shared_examples_for "writing to redis sortedset" do |extra_config| - let(:key) { 10.times.collect { rand(10).to_s }.join("") } - let(:event_count) { Flores::Random.integer(0..10000) } - #let(:message) { Flores::Random.text(0..100) } + let(:key) { SecureRandom.hex } + let(:event_count) { Flores::Random.integer(12..1000) } # Minimum 12 to test two digits cases let(:default_config) { { "key" => key, "data_type" => "sortedset", - "host" => "localhost" + "host" => "localhost", + "priority_field" => "epoch" } } let(:redis_config) { @@ -90,16 +91,54 @@ } let(:redis_output) { described_class.new(redis_config) } - it "should call zadd" do + before do + redis = Redis.new(:host => "127.0.0.1") + insist { redis.zcard(key) } == 0 + redis.close() + redis_output.register - expect(redis_output).not_to receive(:rpush) - expect(redis_output).to receive(:zadd).exactly(1).time - event_count.times do |i| - event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) + + event_count_1 = event_count / 2 + event_count_2 = event_count - event_count_1 + + # Add a half of events in non reverse order + event_count_1.times do |i| + event = LogStash::Event.new("message" => { "i" => i }, "epoch" => i ) + redis_output.receive(event) + end + # And add a half of events in reverse order to verify that events are sorted + event_count_2.times do |j| + i = event_count - j - 1 + event = LogStash::Event.new("message" => { "i" => i }, "epoch" => i ) redis_output.receive(event) end + redis_output.close end + + it "should successfully send all events to redis" do + redis = Redis.new(:host => "127.0.0.1") + + # The sorted set should contain the number of elements our agent pushed up. + insist { redis.zcard(key) } == event_count + + # Now check all events for order and correctness. + event_count.times do |i| + # Non reverse order + item = redis.zrange(key, i, i).first + event = LogStash::Event.new(LogStash::Json.load(item)) + insist { event.get("[message][i]") } == i + insist { event.get("[epoch]") } == i + end + end + + after "should clear the sortedset" do + redis = Redis.new(:host => "127.0.0.1") + + redis.zremrangebyrank(key, 0, -1) + # The list should now be empty + insist { redis.zcard(key) } == 0 + end end @@ -107,23 +146,23 @@ include_examples "writing to redis sortedset" end - context "when batch_mode is true" do - batch_events = Flores::Random.integer(1..1000) - batch_settings = { - "batch" => true, - "batch_events" => batch_events - } + #context "when batch_mode is true" do + # batch_events = Flores::Random.integer(1..1000) + # batch_settings = { + # "batch" => true, + # "batch_events" => batch_events + # } - include_examples "writing to redis sortedset", batch_settings do + # include_examples "writing to redis sortedset", batch_settings do # A canary to make sure we're actually enabling batch mode # in this shared example. - it "should have batch mode enabled" do - expect(redis_config).to include("batch") - expect(redis_config["batch"]).to be_truthy - end - end - end + # it "should have batch mode enabled" do + # expect(redis_config).to include("batch") + # expect(redis_config["batch"]).to be_truthy + # end + # end + #end end end From 6cf35166598a9730df9b597e2d1a34519778e685 Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:22:40 +0200 Subject: [PATCH 07/16] Sortedset batch integration testing done --- spec/integration/outputs/redis_spec.rb | 27 +++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 56a0351..525200e 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -146,24 +146,23 @@ include_examples "writing to redis sortedset" end - #context "when batch_mode is true" do - # batch_events = Flores::Random.integer(1..1000) - # batch_settings = { - # "batch" => true, - # "batch_events" => batch_events - # } + context "when batch_mode is true" do + batch_events = Flores::Random.integer(1..1000) + batch_settings = { + "batch" => true, + "batch_events" => batch_events + } - # include_examples "writing to redis sortedset", batch_settings do + include_examples "writing to redis sortedset", batch_settings do # A canary to make sure we're actually enabling batch mode # in this shared example. - # it "should have batch mode enabled" do - # expect(redis_config).to include("batch") - # expect(redis_config["batch"]).to be_truthy - # end - # end - #end - + it "should have batch mode enabled" do + expect(redis_config).to include("batch") + expect(redis_config["batch"]).to be_truthy + end + end + end end end From ddb633cd35ef25bae5ec9116534f92ee229f7c05 Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 7 Feb 2017 10:57:40 +0100 Subject: [PATCH 08/16] Support for REDIS priority queues This commit adds support for ZSET queues in REDIS with data_type 'list'. Batch is supported. Priority is based on a field of the message containing a number. If the field is absent or not a number a default priority is then used. It adds the following options to the configuration: * **priority**: *boolean*, default false, enable priority mode * **priority_field**: *string*, default '@timestamp', Priority field to use, if field doesn't exist, priority will be priority_default * **priority_default**: *number*, default '0', Default priority when priority field is not found in the event Usage example: output { redis { host => ["127.0.0.1"] port => 6379 data_type => "list" key => "syslog" priority => true priority_field => "epoch" } } --- lib/logstash/outputs/redis.rb | 64 ++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index aa289a8..416437b 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -3,13 +3,17 @@ require "logstash/namespace" require "stud/buffer" -# This output will send events to a Redis queue using RPUSH. +# This output will send events to a Redis queue using RPUSH +# or ZADD (in priority mode). +# # The RPUSH command is supported in Redis v0.0.7+. Using # PUBLISH to a channel requires at least 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 ZADD command is supported in Redis v1.2.0+. +# # For more information, see http://redis.io/[the Redis homepage] # class LogStash::Outputs::Redis < LogStash::Outputs::Base @@ -51,7 +55,7 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Password to authenticate with. There is no authentication by default. config :password, :validate => :password - # The name of the Redis queue (we'll use RPUSH on this). Dynamic names are + # The name of the Redis queue (we'll use RPUSH or ZADD on this). Dynamic names are # valid here, for example `logstash-%{type}` config :queue, :validate => :string, :deprecated => true @@ -60,22 +64,22 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :key, :validate => :string, :required => false # Either list or channel. If `redis_type` is list, then we will set - # RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. + # RPUSH or ZADD to key. If `redis_type` is channel, then we will PUBLISH to `key`. config :data_type, :validate => [ "list", "channel" ], :required => false - # Set to true if you want Redis to batch up values and send 1 RPUSH command + # Set to true if you want Redis to batch up values and send 1 RPUSH or 1 ZADD command # instead of one command per value to push on the list. Note that this only # works with `data_type="list"` mode right now. # - # If true, we send an RPUSH every "batch_events" events or + # If true, we send an RPUSH or ZADD every "batch_events" events or # "batch_timeout" seconds (whichever comes first). # Only supported for `data_type` is "list". config :batch, :validate => :boolean, :default => false - # If batch is set to true, the number of events we queue up for an RPUSH. + # If batch is set to true, the number of events we queue up for an RPUSH or ZADD. config :batch_events, :validate => :number, :default => 50 - # If batch is set to true, the maximum amount of time between RPUSH commands + # If batch is set to true, the maximum amount of time between RPUSH or ZADD commands # when there are pending events to flush. config :batch_timeout, :validate => :number, :default => 5 @@ -95,6 +99,15 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Zero means to check on every event. config :congestion_interval, :validate => :number, :default => 1 + # Enable priority mode + config :priority, :validate => :boolean, :default => false + + # Priority field to use, if field doesn't exist, priority will be priority_default + config :priority_field, :validate => :string, :default => "@timestamp" + + # Default priority when priority field is not found in the event + config :priority_default, :validate => :number, :default => 0 + def register require 'redis' @@ -176,8 +189,13 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - @redis.rpush(key, events) + if @priority then + @redis.zadd(key, events.map{ |event| [priorize(event), event] }) + else + @redis.rpush(key, events) + end end + # called from Stud::Buffer#buffer_flush when an error occurs def on_flush_error(e) @logger.warn("Failed to send backlog of events to Redis", @@ -222,6 +240,30 @@ def connect Redis.new(params) end # def connect + private + def priorize(event) + if event.is_a?(String) then + begin + @codec.decode(event) do |event_decoded| + event = event_decoded + end + rescue => e # parse or event creation error + @logger.warn("Default priority [" << @priority_default.to_s << "] used, can't decode event [" << @event << "]") + return @priority_default + end + end + + priority_value=event.get(@priority_field) + # Is it a number ? + if priority_value.to_s =~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then + return priority_value.to_f + else + @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or is not a number") + return @priority_default + end + + end + # A string used to identify a Redis instance in log messages def identity @name || "redis://#{@password}@#{@current_host}:#{@current_port}/#{@db} #{@data_type}:#{@key}" @@ -241,7 +283,11 @@ def send_to_redis(event, payload) @redis ||= connect if @data_type == 'list' congestion_check(key) - @redis.rpush(key, payload) + if @priority then + @redis.zadd(key, priorize(event), payload) + else + @redis.rpush(key, payload) + end else @redis.publish(key, payload) end From 76da9d59cc7fe138ecb82cffd4eb27fde1f0d576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Wed, 8 Mar 2017 16:57:06 +0100 Subject: [PATCH 09/16] Added batch_timeout to redis batch unit testing Sometimes redis didn't have the time to ingest all messages and the test fails randomly. It appeared it was due to redis too short timeout. --- spec/unit/outputs/redis_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/unit/outputs/redis_spec.rb b/spec/unit/outputs/redis_spec.rb index 7ed1220..4366acf 100644 --- a/spec/unit/outputs/redis_spec.rb +++ b/spec/unit/outputs/redis_spec.rb @@ -19,6 +19,7 @@ "data_type" => "list", "batch" => true, "batch_events" => 50, + "batch_timeout" => 60 } } let(:redis) { described_class.new(config) } From 4ce53ac83516ce11ba9d3a2bfae0606674c3bfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Wed, 8 Mar 2017 17:09:13 +0100 Subject: [PATCH 10/16] Sorted set: Spec and support sortedset data_type As sorted sets are a native redis data type, it's best to support ZSET through data_type option instead to boolean priority option. A first group of integration tests (batched and unbatched) for sorted sets is also added with this commit. --- lib/logstash/outputs/redis.rb | 40 ++++++++++----------- spec/integration/outputs/redis_spec.rb | 50 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index 416437b..f5c110c 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -4,7 +4,7 @@ require "stud/buffer" # This output will send events to a Redis queue using RPUSH -# or ZADD (in priority mode). +# or ZADD or PUBLISH. # # The RPUSH command is supported in Redis v0.0.7+. Using # PUBLISH to a channel requires at least v1.3.8+. @@ -55,7 +55,7 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Password to authenticate with. There is no authentication by default. config :password, :validate => :password - # The name of the Redis queue (we'll use RPUSH or ZADD on this). Dynamic names are + # The name of the Redis queue/sortedset (using RPUSH/ZADD cmds). Dynamic names are # valid here, for example `logstash-%{type}` config :queue, :validate => :string, :deprecated => true @@ -64,16 +64,18 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :key, :validate => :string, :required => false # Either list or channel. If `redis_type` is list, then we will set - # RPUSH or ZADD to key. If `redis_type` is channel, then we will PUBLISH to `key`. - config :data_type, :validate => [ "list", "channel" ], :required => false + # RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. + # If `redis_type` is sortedset, then we will ZADD to `key` with weight set + # to content of `priority_field` + config :data_type, :validate => [ "list", "channel", "sortedset" ], :required => false # Set to true if you want Redis to batch up values and send 1 RPUSH or 1 ZADD command - # instead of one command per value to push on the list. Note that this only - # works with `data_type="list"` mode right now. + # instead of one command per value to push on the list or set. Note that this only + # works with `data_type="list"` and `data_type="sortedset"` mode right now. # # If true, we send an RPUSH or ZADD every "batch_events" events or # "batch_timeout" seconds (whichever comes first). - # Only supported for `data_type` is "list". + # Only supported for `data_type` "list" or "sortedset". config :batch, :validate => :boolean, :default => false # If batch is set to true, the number of events we queue up for an RPUSH or ZADD. @@ -86,24 +88,21 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Interval for reconnecting to failed Redis connections config :reconnect_interval, :validate => :number, :default => 1 - # In case Redis `data_type` is `list` and has more than `@congestion_threshold` items, + # In case Redis `data_type` is `list` or `sortedset` and has more than `@congestion_threshold` items, # block until someone consumes them and reduces congestion, otherwise if there are # no consumers Redis will run out of memory, unless it was configured with OOM protection. # But even with OOM protection, a single Redis list can block all other users of Redis, # until Redis CPU consumption reaches the max allowed RAM size. # A default value of 0 means that this limit is disabled. - # Only supported for `list` Redis `data_type`. + # Only supported for `list` and `sortedset` Redis `data_type`. config :congestion_threshold, :validate => :number, :default => 0 # How often to check for congestion. Default is one second. # Zero means to check on every event. config :congestion_interval, :validate => :number, :default => 1 - # Enable priority mode - config :priority, :validate => :boolean, :default => false - # Priority field to use, if field doesn't exist, priority will be priority_default - config :priority_field, :validate => :string, :default => "@timestamp" + config :priority_field, :validate => :string, :default => "epoch" # Default priority when priority field is not found in the event config :priority_default, :validate => :number, :default => 0 @@ -131,7 +130,7 @@ def register if @batch - if @data_type != "list" + if @data_type != "list" and @data_type != "sortedset" raise RuntimeError.new( "batch is not supported with data_type #{@data_type}" ) @@ -189,7 +188,7 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - if @priority then + if @data_type == 'sortedset' then @redis.zadd(key, events.map{ |event| [priorize(event), event] }) else @redis.rpush(key, events) @@ -273,7 +272,7 @@ def send_to_redis(event, payload) # How can I do this sort of thing with codecs? key = event.sprintf(@key) - if @batch && @data_type == 'list' # Don't use batched method for pubsub. + if @batch && (@data_type == 'list' or @data_type == 'sortedset') # Don't use batched method for pubsub. # Stud::Buffer buffer_receive(payload, key) return @@ -283,11 +282,10 @@ def send_to_redis(event, payload) @redis ||= connect if @data_type == 'list' congestion_check(key) - if @priority then - @redis.zadd(key, priorize(event), payload) - else - @redis.rpush(key, payload) - end + @redis.rpush(key, payload) + elsif @data_type == 'sortedset' + congestion_check(key) + @redis.zadd(key, priorize(event), payload) else @redis.publish(key, payload) end diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 4d531f7..ffde9d4 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -72,6 +72,56 @@ end end end + + + shared_examples_for "writing to redis sortedset" do |extra_config| + let(:key) { 10.times.collect { rand(10).to_s }.join("") } + let(:event_count) { Flores::Random.integer(0..10000) } + #let(:message) { Flores::Random.text(0..100) } + let(:default_config) { + { + "key" => key, + "data_type" => "sortedset", + "host" => "localhost" + } + } + let(:redis_config) { + default_config.merge(extra_config || {}) + } + let(:redis_output) { described_class.new(redis_config) } + + before do + redis_output.register + event_count.times do |i| + event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) + redis_output.receive(event) + end + redis_output.close + end + end + + context "when batch_mode is false" do + include_examples "writing to redis sortedset" + end + + context "when batch_mode is true" do + batch_events = Flores::Random.integer(1..1000) + batch_settings = { + "batch" => true, + "batch_events" => batch_events + } + + include_examples "writing to redis sortedset", batch_settings do + + # A canary to make sure we're actually enabling batch mode + # in this shared example. + it "should have batch mode enabled" do + expect(redis_config).to include("batch") + expect(redis_config["batch"]).to be_truthy + end + end + end + end end From b5038bc391de5ff55e131ce6854f07c9bae63073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Tue, 14 Mar 2017 09:35:37 +0100 Subject: [PATCH 11/16] ZADD integration spec --- spec/integration/outputs/redis_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index ffde9d4..729e7f8 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -90,8 +90,10 @@ } let(:redis_output) { described_class.new(redis_config) } - before do + it "should call zadd" do redis_output.register + expect(redis_output).not_to receive(:rpush) + expect(redis_output).to receive(:zadd).exactly(1).time event_count.times do |i| event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) redis_output.receive(event) @@ -100,6 +102,7 @@ end end + context "when batch_mode is false" do include_examples "writing to redis sortedset" end From 7f0e33c56cdb7baded0afc9b5ee931f28cbf41ec Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:17:26 +0200 Subject: [PATCH 12/16] Priority in sortedset is a string --- lib/logstash/outputs/redis.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index f5c110c..1c22cce 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -102,10 +102,12 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base config :congestion_interval, :validate => :number, :default => 1 # Priority field to use, if field doesn't exist, priority will be priority_default + # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) config :priority_field, :validate => :string, :default => "epoch" # Default priority when priority field is not found in the event - config :priority_default, :validate => :number, :default => 0 + # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) + config :priority_default, :validate => :number, :default => "-1" def register require 'redis' @@ -188,7 +190,7 @@ def flush(events, key, close=false) # we should not block due to congestion on close # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value. congestion_check(key) unless close - if @data_type == 'sortedset' then + if @data_type == 'sortedset' then @redis.zadd(key, events.map{ |event| [priorize(event), event] }) else @redis.rpush(key, events) @@ -248,19 +250,19 @@ def priorize(event) end rescue => e # parse or event creation error @logger.warn("Default priority [" << @priority_default.to_s << "] used, can't decode event [" << @event << "]") - return @priority_default + return @priority_default.to_s end end - + + priority_value=event.get(@priority_field) - # Is it a number ? - if priority_value.to_s =~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then - return priority_value.to_f - else - @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or is not a number") - return @priority_default + + if priority_value.nil? || priority_value.to_s !~ /\A[-+]?[0-9]+(\.[0-9]+)?\z/ then + @logger.debug("Default priority [" << @priority_default.to_s << "] used, field [" << @priority_field << "] doesn't exist or doesn't contain a number") + priority_value=@priority_default end + return priority_value.to_s end # A string used to identify a Redis instance in log messages @@ -285,7 +287,7 @@ def send_to_redis(event, payload) @redis.rpush(key, payload) elsif @data_type == 'sortedset' congestion_check(key) - @redis.zadd(key, priorize(event), payload) + @redis.zadd(key, priorize(event), payload) else @redis.publish(key, payload) end From 4a90aeb16ea7faf75e958f6ade2d7923d742252c Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:18:50 +0200 Subject: [PATCH 13/16] Sorted test integration test non batch ok --- spec/integration/outputs/redis_spec.rb | 87 +++++++++++++++++++------- 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 729e7f8..56a0351 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -3,14 +3,15 @@ require "logstash/json" require "redis" require "flores/random" +require 'securerandom' describe LogStash::Outputs::Redis do context "integration tests", :integration => true do shared_examples_for "writing to redis list" do |extra_config| - let(:key) { 10.times.collect { rand(10).to_s }.join("") } + let(:key) { SecureRandom.hex } let(:event_count) { Flores::Random.integer(0..10000) } - let(:message) { Flores::Random.text(0..100) } + let(:message) { SecureRandom.hex } # We use hex generation to avoid escaping issues on Windows let(:default_config) { { "key" => key, @@ -75,14 +76,14 @@ shared_examples_for "writing to redis sortedset" do |extra_config| - let(:key) { 10.times.collect { rand(10).to_s }.join("") } - let(:event_count) { Flores::Random.integer(0..10000) } - #let(:message) { Flores::Random.text(0..100) } + let(:key) { SecureRandom.hex } + let(:event_count) { Flores::Random.integer(12..1000) } # Minimum 12 to test two digits cases let(:default_config) { { "key" => key, "data_type" => "sortedset", - "host" => "localhost" + "host" => "localhost", + "priority_field" => "epoch" } } let(:redis_config) { @@ -90,16 +91,54 @@ } let(:redis_output) { described_class.new(redis_config) } - it "should call zadd" do + before do + redis = Redis.new(:host => "127.0.0.1") + insist { redis.zcard(key) } == 0 + redis.close() + redis_output.register - expect(redis_output).not_to receive(:rpush) - expect(redis_output).to receive(:zadd).exactly(1).time - event_count.times do |i| - event = LogStash::Event.new("sequence" => i, "message" => { "data" => Flores::Random.text(0..100), "@timestamp" => i } ) + + event_count_1 = event_count / 2 + event_count_2 = event_count - event_count_1 + + # Add a half of events in non reverse order + event_count_1.times do |i| + event = LogStash::Event.new("message" => { "i" => i }, "epoch" => i ) + redis_output.receive(event) + end + # And add a half of events in reverse order to verify that events are sorted + event_count_2.times do |j| + i = event_count - j - 1 + event = LogStash::Event.new("message" => { "i" => i }, "epoch" => i ) redis_output.receive(event) end + redis_output.close end + + it "should successfully send all events to redis" do + redis = Redis.new(:host => "127.0.0.1") + + # The sorted set should contain the number of elements our agent pushed up. + insist { redis.zcard(key) } == event_count + + # Now check all events for order and correctness. + event_count.times do |i| + # Non reverse order + item = redis.zrange(key, i, i).first + event = LogStash::Event.new(LogStash::Json.load(item)) + insist { event.get("[message][i]") } == i + insist { event.get("[epoch]") } == i + end + end + + after "should clear the sortedset" do + redis = Redis.new(:host => "127.0.0.1") + + redis.zremrangebyrank(key, 0, -1) + # The list should now be empty + insist { redis.zcard(key) } == 0 + end end @@ -107,23 +146,23 @@ include_examples "writing to redis sortedset" end - context "when batch_mode is true" do - batch_events = Flores::Random.integer(1..1000) - batch_settings = { - "batch" => true, - "batch_events" => batch_events - } + #context "when batch_mode is true" do + # batch_events = Flores::Random.integer(1..1000) + # batch_settings = { + # "batch" => true, + # "batch_events" => batch_events + # } - include_examples "writing to redis sortedset", batch_settings do + # include_examples "writing to redis sortedset", batch_settings do # A canary to make sure we're actually enabling batch mode # in this shared example. - it "should have batch mode enabled" do - expect(redis_config).to include("batch") - expect(redis_config["batch"]).to be_truthy - end - end - end + # it "should have batch mode enabled" do + # expect(redis_config).to include("batch") + # expect(redis_config["batch"]).to be_truthy + # end + # end + #end end end From 6144e82ced6febff1437656454f04a0d7edfa2e6 Mon Sep 17 00:00:00 2001 From: Amaury Decreme Date: Tue, 30 May 2017 16:22:40 +0200 Subject: [PATCH 14/16] Sortedset batch integration testing done --- spec/integration/outputs/redis_spec.rb | 27 +++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/spec/integration/outputs/redis_spec.rb b/spec/integration/outputs/redis_spec.rb index 56a0351..525200e 100644 --- a/spec/integration/outputs/redis_spec.rb +++ b/spec/integration/outputs/redis_spec.rb @@ -146,24 +146,23 @@ include_examples "writing to redis sortedset" end - #context "when batch_mode is true" do - # batch_events = Flores::Random.integer(1..1000) - # batch_settings = { - # "batch" => true, - # "batch_events" => batch_events - # } + context "when batch_mode is true" do + batch_events = Flores::Random.integer(1..1000) + batch_settings = { + "batch" => true, + "batch_events" => batch_events + } - # include_examples "writing to redis sortedset", batch_settings do + include_examples "writing to redis sortedset", batch_settings do # A canary to make sure we're actually enabling batch mode # in this shared example. - # it "should have batch mode enabled" do - # expect(redis_config).to include("batch") - # expect(redis_config["batch"]).to be_truthy - # end - # end - #end - + it "should have batch mode enabled" do + expect(redis_config).to include("batch") + expect(redis_config["batch"]).to be_truthy + end + end + end end end From f40492097e9bf123183d5b47d301c97a13eeb902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Tue, 13 Jun 2017 12:09:01 +0200 Subject: [PATCH 15/16] Documentation for data_type `sortedset` --- docs/index.asciidoc | 84 +++++++++++++++++++++++------------ lib/logstash/outputs/redis.rb | 19 ++++---- 2 files changed, 64 insertions(+), 39 deletions(-) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index 9d6d5bc..fce0856 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -20,9 +20,12 @@ include::{include_path}/plugin_header.asciidoc[] ==== Description -This output will send events to a Redis queue using RPUSH. -The RPUSH command is supported in Redis v0.0.7+. Using -PUBLISH to a channel requires at least v1.3.8+. +This output will send events to a Redis queue using RPUSH/ZADD/PUBLISH. + +The RPUSH command is supported in Redis v0.0.7+. +Using ZADD is supported in Redis v1.2.0+. +Using PUBLISH to a channel requires at least 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. @@ -43,10 +46,12 @@ This plugin supports the following configuration options plus the <> |<>|No | <> |<>|No | <> |<>|No -| <> |<>, one of `["list", "channel"]`|No +| <> |<>, one of `["list", "sortedset", "channel"]`|No | <> |<>|No | <> |<>|No | <> |<>|No +| <> |<>|No +| <> |<>|No | <> |<>|No | <> |<>|No | <> |<>|No @@ -60,38 +65,38 @@ output plugins.   [id="plugins-{type}s-{plugin}-batch"] -===== `batch` +===== `batch` * Value type is <> * Default value is `false` -Set to true if you want Redis to batch up values and send 1 RPUSH command -instead of one command per value to push on the list. Note that this only -works with `data_type="list"` mode right now. +Set to true if you want Redis to batch up values and send 1 RPUSH or 1 ZADD command +instead of one command per value to push on the list or set. Note that this only +works with `data_type="list"` and `data_type="sortedset"` mode right now. -If true, we send an RPUSH every "batch_events" events or +If true, we send an RPUSH or ZADD every "batch_events" events or "batch_timeout" seconds (whichever comes first). -Only supported for `data_type` is "list". +Only supported for `data_type` "list" or "sortedset". [id="plugins-{type}s-{plugin}-batch_events"] -===== `batch_events` +===== `batch_events` * Value type is <> * Default value is `50` -If batch is set to true, the number of events we queue up for an RPUSH. +If batch is set to true, the number of events we queue up for an RPUSH or ZADD. [id="plugins-{type}s-{plugin}-batch_timeout"] -===== `batch_timeout` +===== `batch_timeout` * Value type is <> * Default value is `5` -If batch is set to true, the maximum amount of time between RPUSH commands +If batch is set to true, the maximum amount of time between RPUSH or ZADD commands when there are pending events to flush. [id="plugins-{type}s-{plugin}-congestion_interval"] -===== `congestion_interval` +===== `congestion_interval` * Value type is <> * Default value is `1` @@ -100,30 +105,32 @@ How often to check for congestion. Default is one second. Zero means to check on every event. [id="plugins-{type}s-{plugin}-congestion_threshold"] -===== `congestion_threshold` +===== `congestion_threshold` * Value type is <> * Default value is `0` -In case Redis `data_type` is `list` and has more than `@congestion_threshold` items, +In case Redis `data_type` is `list` or `sortedset` and has more than `@congestion_threshold` items, block until someone consumes them and reduces congestion, otherwise if there are no consumers Redis will run out of memory, unless it was configured with OOM protection. But even with OOM protection, a single Redis list can block all other users of Redis, until Redis CPU consumption reaches the max allowed RAM size. A default value of 0 means that this limit is disabled. -Only supported for `list` Redis `data_type`. +Only supported for `list` and `sortedset` Redis `data_type`. [id="plugins-{type}s-{plugin}-data_type"] -===== `data_type` +===== `data_type` - * Value can be any of: `list`, `channel` + * Value can be any of: `list`, `sortedset`, `channel` * There is no default value for this setting. Either list or channel. If `redis_type` is list, then we will set RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. +If `redis_type` is sortedset, then we will ZADD to `key` with weight set +to content of `priority_field` [id="plugins-{type}s-{plugin}-db"] -===== `db` +===== `db` * Value type is <> * Default value is `0` @@ -131,7 +138,7 @@ RPUSH to key. If `redis_type` is channel, then we will PUBLISH to `key`. The Redis database number. [id="plugins-{type}s-{plugin}-host"] -===== `host` +===== `host` * Value type is <> * Default value is `["127.0.0.1"]` @@ -148,14 +155,33 @@ For example: ["127.0.0.1:6380", "127.0.0.1"] [id="plugins-{type}s-{plugin}-key"] -===== `key` +===== `key` * Value type is <> * There is no default value for this setting. -The name of a Redis list or channel. Dynamic names are +The name of a Redis list, sortedset or channel. Dynamic names are valid here, for example `logstash-%{type}`. +[id="plugins-{type}s-{plugin}-priority_field"] +===== priority_field` + + * Value type is <> + * Default value is `epoch` + +Priority field to use for data_type `sortedset`, if field doesn't exist, priority will be priority_default +The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) + +[id="plugins-{type}s-{plugin}-priority_default"] +===== priority_field` + + * Value type is <> + * Default value is `-1` + +Default priority for data_type `sortedset` when priority field is not found in the event +The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) + + [id="plugins-{type}s-{plugin}-name"] ===== `name` (DEPRECATED) @@ -166,7 +192,7 @@ valid here, for example `logstash-%{type}`. Name is used for logging in case there are multiple instances. [id="plugins-{type}s-{plugin}-password"] -===== `password` +===== `password` * Value type is <> * There is no default value for this setting. @@ -174,7 +200,7 @@ Name is used for logging in case there are multiple instances. Password to authenticate with. There is no authentication by default. [id="plugins-{type}s-{plugin}-port"] -===== `port` +===== `port` * Value type is <> * Default value is `6379` @@ -192,7 +218,7 @@ The name of the Redis queue (we'll use RPUSH on this). Dynamic names are valid here, for example `logstash-%{type}` [id="plugins-{type}s-{plugin}-reconnect_interval"] -===== `reconnect_interval` +===== `reconnect_interval` * Value type is <> * Default value is `1` @@ -200,7 +226,7 @@ valid here, for example `logstash-%{type}` Interval for reconnecting to failed Redis connections [id="plugins-{type}s-{plugin}-shuffle_hosts"] -===== `shuffle_hosts` +===== `shuffle_hosts` * Value type is <> * Default value is `true` @@ -208,7 +234,7 @@ Interval for reconnecting to failed Redis connections Shuffle the host list during Logstash startup. [id="plugins-{type}s-{plugin}-timeout"] -===== `timeout` +===== `timeout` * Value type is <> * Default value is `5` diff --git a/lib/logstash/outputs/redis.rb b/lib/logstash/outputs/redis.rb index 1c22cce..2514f6f 100644 --- a/lib/logstash/outputs/redis.rb +++ b/lib/logstash/outputs/redis.rb @@ -3,17 +3,16 @@ require "logstash/namespace" require "stud/buffer" -# This output will send events to a Redis queue using RPUSH -# or ZADD or PUBLISH. -# -# The RPUSH command is supported in Redis v0.0.7+. Using -# PUBLISH to a channel requires at least v1.3.8+. +# This output will send events to a Redis queue using RPUSH/ZADD/PUBLISH. + +# The RPUSH command is supported in Redis v0.0.7+. +# Using ZADD is supported in Redis v1.2.0+. +# Using PUBLISH to a channel requires at least 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 ZADD command is supported in Redis v1.2.0+. -# + # For more information, see http://redis.io/[the Redis homepage] # class LogStash::Outputs::Redis < LogStash::Outputs::Base @@ -101,11 +100,11 @@ class LogStash::Outputs::Redis < LogStash::Outputs::Base # Zero means to check on every event. config :congestion_interval, :validate => :number, :default => 1 - # Priority field to use, if field doesn't exist, priority will be priority_default + # Priority field to use for data_type `sortedset`, if field doesn't exist, priority will be priority_default # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) config :priority_field, :validate => :string, :default => "epoch" - # Default priority when priority field is not found in the event + # Default priority for data_type `sortedset` when priority field is not found in the event # The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) config :priority_default, :validate => :number, :default => "-1" From 505b90886d075c4728a3d6dcf4b5933c3fd04ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Decr=C3=AAme?= Date: Tue, 13 Jun 2017 12:10:13 +0200 Subject: [PATCH 16/16] Typo --- docs/index.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index fce0856..063ef74 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -164,7 +164,7 @@ The name of a Redis list, sortedset or channel. Dynamic names are valid here, for example `logstash-%{type}`. [id="plugins-{type}s-{plugin}-priority_field"] -===== priority_field` +===== priority_field * Value type is <> * Default value is `epoch` @@ -173,7 +173,7 @@ Priority field to use for data_type `sortedset`, if field doesn't exist, priorit The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well. (see https://redis.io/commands/zadd) [id="plugins-{type}s-{plugin}-priority_default"] -===== priority_field` +===== priority_default * Value type is <> * Default value is `-1`