Trying something new.

This commit is contained in:
Karl Southern 2016-05-20 16:30:48 +01:00
parent 5ec985b0df
commit d6869f594c
2 changed files with 18 additions and 45 deletions

View File

@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file, from 0.2.0.
- Change: Removes jar files from repository, in favour of vendoring using jar-dependencies - Change: Removes jar files from repository, in favour of vendoring using jar-dependencies
- Change: Updates to logstash-api v2.0 - Change: Updates to logstash-api v2.0
- Change: Switches from slf4j-nop to log4j for HikariCP logging - Change: Switches from slf4j-nop to log4j for HikariCP logging
- Change: Adds improved support to deal with partially failed batches of inserts - Change: Now selectively retries only a subset of SQL Exceptions
## [0.2.6] - 2016-05-02 ## [0.2.6] - 2016-05-02
- Fix for exception infinite loop - Fix for exception infinite loop

View File

@ -14,41 +14,20 @@ require 'logstash-output-jdbc_jars'
class LogStash::Outputs::Jdbc < LogStash::Outputs::Base class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
STRFTIME_FMT = '%Y-%m-%d %T.%L'.freeze STRFTIME_FMT = '%Y-%m-%d %T.%L'.freeze
# Will never work, but only because it duplicates data (i.e. duplicate keys) RETRYABLE_SQLSTATE_CLASSES = [
# Will log a warning, but not retry. # Classes of retryable SQLSTATE codes
SQL_STATES_IGNORE = [ # Not all in the class will be retryable. However, this is the best that
### Class: Unqualified Successful Completion # we've got right now.
# Success. This shouldn't get thrown, but JDBC driver quality varies, so who knows. # If something is missing, or database specific they'll have to do.
0000, '08', # Connection Exception
'24', # Invalid Cursor State (Maybe retry-able in some circumstances)
### Class: Constraint Violation '25', # Invalid Transaction State
# Integrity constraint violation. '40', # Transaction Rollback
23000, '53', # Insufficient Resources
# A violation of the constraint imposed by a unique index or a unique constraint occurred. '54', # Program Limit Exceeded (MAYBE)
23505 '55', # Object Not In Prerequisite State
].freeze '57', # Operator Intervention
'58', # System Error
# Will log an error, but not retry.
SQL_STATES_FATAL = [
### Class: Data Exception
# Character data, right truncation occurred. Field too small.
22001,
# Numeric value out of range.
22003,
# A null value is not allowed.
22004,
# Invalid datetime format.
22007,
# A parameter or host variable value is invalid.
22023,
# Character conversion resulted in truncation.
22524,
### Constraint Violation
# The insert or update value of a foreign key is invalid.
23503,
# The range of values for the identity column or sequence is exhausted.
23522
].freeze ].freeze
config_name 'jdbc' config_name 'jdbc'
@ -216,19 +195,13 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
) )
statement = add_statement_event_params(statement, event) if @statement.length > 1 statement = add_statement_event_params(statement, event) if @statement.length > 1
statement.execute statement.execute
rescue java.sql.SQLException => e rescue => e
if SQL_STATES_IGNORE.include? e.getSQLState if e.class == java.sql.SQLException and !RETRYABLE_SQLSTATE_CLASSES.include?(e.getSQLState.to_s[0,2])
@logger.warn('JDBC - Dropping event. Ignore-able exception (duplicate key most likely)', exception: e, event: event) @logger.error('JDBC - Non-retryable SQL exception. Dropping event. If you think this is in error please log an issue with the details from this exception.', exception: e, state_code: e.getSQLState, event: event)
elsif SQL_STATES_FATAL.include? e.getSQLState
@logger.error('JDBC - Fatal SQL exception. Can never succeed. Dropping event.', exception: e, event: event)
else else
log_jdbc_exception(e) log_jdbc_exception(e)
events_to_retry.push(event) events_to_retry.push(event)
end end
rescue => e
# Something else happened.
log_jdbc_exception(e)
events_to_retry.push(event)
ensure ensure
statement.close unless statement.nil? statement.close unless statement.nil?
end end