Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit 49e751a

Browse files
committed
Have to start some real work. Will complete tests over lunch.
1 parent 9804850 commit 49e751a

File tree

5 files changed

+73
-43
lines changed

5 files changed

+73
-43
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ This plugin does not bundle any JDBC jar files, and does expect them to be in a
1212
particular location. Please ensure you read the 4 installation lines below.
1313

1414
## Headlines
15-
- Support for connection pooling added in 0.2.0 [unreleased until #21 is resolved]
16-
- Support for unsafe statement handling (allowing dynamic queries) in 0.2.0 [unreleased until #21 is resolved]
15+
- Support for connection pooling added in 0.2.0 [unreleased until #10 is resolved]
16+
- Support for unsafe statement handling (allowing dynamic queries) in 0.2.0 [unreleased until #10 is resolved]
17+
- Altered exception handling to now count sequential flushes with exceptions thrown in 0.2.0 [untested and unreleased until #10 is resolved]
1718

1819
## Versions
1920
- See master branch for logstash v2+
@@ -31,20 +32,19 @@ particular location. Please ensure you read the 4 installation lines below.
3132

3233
## Configuration options
3334

34-
| Option | Type | Description | Required? |
35-
| ------ | ---- | ----------- | --------- |
36-
| driver_path | String | File path to jar file containing your JDBC driver. This is optional, and all JDBC jars may be placed in $LOGSTASH_HOME/vendor/jar/jdbc instead. | No |
37-
| connection_string | String | JDBC connection URL | Yes |
38-
| username | String | JDBC username - this is optional as it may be included in the connection string, for many drivers | No |
39-
| password | String | JDBC password - this is optional as it may be included in the connection string, for many drivers | No |
40-
| statement | Array | An array of strings representing the SQL statement to run. Index 0 is the SQL statement that is prepared, all other array entries are passed in as parameters (in order). A parameter may either be a property of the event (i.e. "@timestamp", or "host") or a formatted string (i.e. "%{host} - %{message}" or "%{message}"). If a key is passed then it will be automatically converted as required for insertion into SQL. If it's a formatted string then it will be passed in verbatim. | Yes |
41-
| unsafe_statement | Boolean | If yes, the statement is evaluated for event fields - this allows you to use dynamic table names, etc. **This is highly dangerous** and you should **not** use this unless you are 100% sure that the field(s) you are passing in are 100% safe. Failure to do so will result in possible SQL injections. Please be aware that there is also a potential performance penalty as each event must be evaluated and inserted into SQL one at a time, where as when this is false multiple events are inserted at once. Example statement: [ "insert into %{table_name_field} (column) values(?)", "fieldname" ] | No |
42-
| max_pool_size | Number | Maximum number of connections to open to the SQL server at any 1 time | No |
43-
| connection_timeout | Number | Number of seconds before a SQL connection is closed | No |
44-
| flush_size | Number | Maximum number of entries to buffer before sending to SQL - if this is reached before idle_flush_time | No |
45-
| idle_flush_time | Number | Number of idle seconds before sending data to SQL - even if the flush_size has not yet been reached | No |
46-
| max_repeat_exceptions | Number | Number of times the same exception can repeat before we stop logstash. Set to a value less than 1 if you never want it to stop | No |
47-
| max_repeat_exceptions_time | Number | Maxium number of seconds between exceptions before they're considered "different" exceptions. If you modify idle_flush_time you should consider this value | No |
35+
| Option | Type | Description | Required? | Default |
36+
| ------ | ---- | ----------- | --------- | ------- |
37+
| driver_path | String | File path to jar file containing your JDBC driver. This is optional, and all JDBC jars may be placed in $LOGSTASH_HOME/vendor/jar/jdbc instead. | No | |
38+
| connection_string | String | JDBC connection URL | Yes | |
39+
| username | String | JDBC username - this is optional as it may be included in the connection string, for many drivers | No | |
40+
| password | String | JDBC password - this is optional as it may be included in the connection string, for many drivers | No | |
41+
| statement | Array | An array of strings representing the SQL statement to run. Index 0 is the SQL statement that is prepared, all other array entries are passed in as parameters (in order). A parameter may either be a property of the event (i.e. "@timestamp", or "host") or a formatted string (i.e. "%{host} - %{message}" or "%{message}"). If a key is passed then it will be automatically converted as required for insertion into SQL. If it's a formatted string then it will be passed in verbatim. | Yes | |
42+
| unsafe_statement | Boolean | If yes, the statement is evaluated for event fields - this allows you to use dynamic table names, etc. **This is highly dangerous** and you should **not** use this unless you are 100% sure that the field(s) you are passing in are 100% safe. Failure to do so will result in possible SQL injections. Please be aware that there is also a potential performance penalty as each event must be evaluated and inserted into SQL one at a time, where as when this is false multiple events are inserted at once. Example statement: [ "insert into %{table_name_field} (column) values(?)", "fieldname" ] | No | False |
43+
| max_pool_size | Number | Maximum number of connections to open to the SQL server at any 1 time | No | 5 |
44+
| connection_timeout | Number | Number of seconds before a SQL connection is closed | No | 2800 |
45+
| flush_size | Number | Maximum number of entries to buffer before sending to SQL - if this is reached before idle_flush_time | No | 1000 |
46+
| idle_flush_time | Number | Number of idle seconds before sending data to SQL - even if the flush_size has not yet been reached | No | 1 |
47+
| max_flush_exceptions | Number | Number of sequential flushes which cause an exception, before we stop logstash. Set to a value less than 1 if you never want it to stop. This should be carefully configured with relation to idle_flush_time if your SQL instance is not highly available. | No | 0 |
4848

4949
## Example configurations
5050
If you have a working sample configuration, for a DB thats not listed, pull requests are welcome.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class RingBuffer < Array
2+
attr_reader :max_size
3+
4+
def initialize(max_size, enum = nil)
5+
@max_size = max_size
6+
enum.each { |e| self << e } if enum
7+
end
8+
9+
def <<(el)
10+
if self.size < @max_size || @max_size.nil?
11+
super
12+
else
13+
self.shift
14+
self.push(el)
15+
end
16+
end
17+
18+
alias :push :<<
19+
end

lib/logstash/outputs/jdbc.rb

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "stud/buffer"
55
require "java"
66
require "logstash-output-jdbc_jars"
7+
require "logstash-output-jdbc_ring-buffer"
78

89
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
910
# Adds buffer support
@@ -58,17 +59,20 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
5859
# a timely manner.
5960
#
6061
# If you change this value please ensure that you change
61-
# max_repeat_exceptions_time accordingly.
62+
# max_flush_exceptions accordingly.
6263
config :idle_flush_time, :validate => :number, :default => 1
6364

64-
# Maximum number of repeating (sequential) exceptions, before we stop retrying
65+
# Maximum number of sequential flushes which encounter exceptions, before we stop retrying.
6566
# If set to < 1, then it will infinitely retry.
66-
config :max_repeat_exceptions, :validate => :number, :default => 4
67+
#
68+
# You should carefully tune this in relation to idle_flush_time if your SQL server
69+
# is not highly available.
70+
# i.e. If your idle_flush_time is 1, and your max_flush_exceptions is 200, and your SQL server takes
71+
# longer than 200 seconds to reboot, then logstash will stop.
72+
config :max_flush_exceptions, :validate => :number, :default => 0
6773

68-
# The max number of seconds since the last exception, before we consider it
69-
# a different cause.
70-
# This value should be carefully considered in respect to idle_flush_time.
71-
config :max_repeat_exceptions_time, :validate => :number, :default => 30
74+
config :max_repeat_exceptions, :obsolete => "This has been replaced by max_flush_exceptions - which behaves slightly differently. Please check the documentation."
75+
config :max_repeat_exceptions_time, :obsolete => "This is no longer required"
7276

7377
public
7478
def register
@@ -85,17 +89,12 @@ def register
8589
@pool.setMaximumPoolSize(@max_pool_size)
8690
@pool.setConnectionTimeout(@connection_timeout)
8791

92+
@exceptions_tracker = RingBuffer.new(@max_flush_exceptions)
93+
8894
if (@flush_size > 1000)
8995
@logger.warn("JDBC - Flush size is set to > 1000")
9096
end
9197

92-
@repeat_exception_count = 0
93-
@last_exception_time = Time.now
94-
95-
if (@max_repeat_exceptions > 0) and ((@idle_flush_time * @max_repeat_exceptions) > @max_repeat_exceptions_time)
96-
@logger.warn("JDBC - max_repeat_exceptions_time is set such that it may still permit a looping exception. You probably changed idle_flush_time. Considering increasing max_repeat_exceptions_time.")
97-
end
98-
9998
buffer_initialize(
10099
:max_items => @flush_size,
101100
:max_interval => @idle_flush_time,
@@ -119,21 +118,14 @@ def flush(events, teardown=false)
119118
end
120119

121120
def on_flush_error(e)
122-
return if @max_repeat_exceptions < 1
121+
return if @max_flush_exceptions < 1
123122

124-
if @last_exception == e.to_s
125-
@repeat_exception_count += 1
126-
else
127-
@repeat_exception_count = 0
128-
end
123+
@exceptions_tracker << e.class
129124

130-
if (@repeat_exception_count >= @max_repeat_exceptions) and (Time.now - @last_exception_time) < @max_repeat_exceptions_time
131-
@logger.error("JDBC - Exception repeated more than the maximum configured", :exception => e, :max_repeat_exceptions => @max_repeat_exceptions, :max_repeat_exceptions_time => @max_repeat_exceptions_time)
125+
if @exceptions_tracker.reject { |i| i.nil? }.count >= @max_flush_exceptions
126+
@logger.error("JDBC - max_flush_exceptions has been reached")
132127
raise e
133128
end
134-
135-
@last_exception_time = Time.now
136-
@last_exception = e.to_s
137129
end
138130

139131
def teardown
@@ -239,7 +231,11 @@ def add_statement_event_params(statement, event)
239231
when false
240232
statement.setBoolean(idx + 1, false)
241233
else
242-
statement.setString(idx + 1, event.sprintf(i))
234+
if event[i].nil? and i =~ /%\{/
235+
statement.setString(idx + 1, event.sprintf(i))
236+
else
237+
statement.setString(idx + 1, nil)
238+
end
243239
end
244240
end
245241

logstash-output-jdbc.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-jdbc'
3-
s.version = "0.2.0.rc3"
3+
s.version = "0.2.0.rc4"
44
s.licenses = [ "Apache License (2.0)" ]
55
s.summary = "This plugin allows you to output to SQL, via JDBC"
66
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
@@ -24,4 +24,6 @@ Gem::Specification.new do |s|
2424
s.add_runtime_dependency "logstash-codec-plain"
2525

2626
s.add_development_dependency "logstash-devutils"
27+
28+
s.post_install_message = "logstash-output-jdbc 0.2.0 introduces several new features - please ensure you check the documentation in the README file"
2729
end

spec/outputs/jdbc_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "logstash/devutils/rspec/spec_helper"
2+
require "logstash/outputs/jdbc"
3+
require "stud/temporary"
4+
5+
describe LogStash::Outputs::Jdbc do
6+
7+
it "should register without errors" do
8+
plugin = LogStash::Plugin.lookup("output", "jdbc").new({})
9+
expect { plugin.register }.to_not raise_error
10+
11+
end
12+
13+
end

0 commit comments

Comments
 (0)