logstash-integration-jdbc 5.0.0 → 5.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e61cdfeea13d5b4fe6183e2d3877e0de3b21d5542d7d4b6c53bee0d26735c82
4
- data.tar.gz: 4f9e93b0d787e0e0fe69bebdaa1676c0b4fe0681a5e65e58661a49807e4e8e33
3
+ metadata.gz: 39722eec0f50423361c7a332054ffeaab4dde768e0a84d45535d10dc1661fc55
4
+ data.tar.gz: cd1f3ed6d68671687d921d4d8e88a8821d9536777f2f5d543954d2848f21340a
5
5
  SHA512:
6
- metadata.gz: 47c7befa425f46f45c2ba621742acd8d2cc4aaed39f8f63a8d457333f33cebf3b443f6dfaeeb7baddf3a5d23ab87c067175c4c12a6667f1d2a88baafa97c6488
7
- data.tar.gz: f2273443e0326dacf3809c79d01ced56c383c2632821160ac6c203d0b3479ed63df7a5ce6fe1711f2c7630aa2510e53b492248d3474ef651873d77589252fd1b
6
+ metadata.gz: 78b8c924c351b1e80b1456b019c2e7e9fb7305410c748c15f05bc6088f1d9a9d4e0c09cfecaa71804bcad2e33b81a5610ef859023d1bdf26cce3f92db50e6c80
7
+ data.tar.gz: 593d2160fc67da503c7f5db0ff42f1c33ed65b9316d398a86c402504c149b266f6e2b1e8fbe2ff965e193341753e23d7d16585597906bb6dadb8b1356e41cb8a
@@ -1,3 +1,20 @@
1
+ ## 5.0.5
2
+ - Fixed user sequel_opts not being passed down properly [#37](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/37)
3
+ - Refactored jdbc_streaming to share driver loading, so the fixes from the jdbc plugin also effect jdbc_streaming
4
+
5
+ ## 5.0.4
6
+ - Fixed issue where JDBC Drivers that don't correctly register with Java's DriverManager fail to load (such as Sybase) [#34](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/34)
7
+
8
+ ## 5.0.3
9
+ - Fixed issue where a lost connection to the database can cause errors when using prepared statements with the scheduler [#25](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/25)
10
+
11
+ ## 5.0.2
12
+ - Fixed potential resource leak by ensuring scheduler is shutdown when a pipeline encounter an error during the running [#28](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/28)
13
+
14
+ ## 5.0.1
15
+ - Fixed tracking_column regression with Postgresql Numeric types [#17](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/17)
16
+ - Fixed driver loading when file not accessible [#15](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/15)
17
+
1
18
  ## 5.0.0
2
19
  - Initial Release of JDBC Integration Plugin, incorporating [logstash-input-jdbc](https://github.com/logstash-plugins/logstash-input-jdbc), [logstash-filter-jdbc_streaming](https://github.com/logstash-plugins/logstash-filter-jdbc_streaming) and
3
20
  [logstash-filter-jdbc_static](https://github.com/logstash-plugins/logstash-filter-jdbc_static)
data/LICENSE CHANGED
@@ -1,13 +1,202 @@
1
- Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
2
1
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
6
5
 
7
- http://www.apache.org/licenses/LICENSE-2.0
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
7
 
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2020 Elastic and contributors
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -339,6 +339,7 @@ required you can pass them separated by a comma.
339
339
  NOTE: If not provided, Plugin will look for the driver class in the Logstash Java classpath. Additionally, if the library
340
340
  does not appear to be being loaded correctly via this setting, placing the relevant jar(s) in the Logstash Java
341
341
  classpath rather than via this setting may help.
342
+ Please also make sure the path is readable by the Logstash process (e.g. `logstash` user when running as a service).
342
343
 
343
344
  [id="plugins-{type}s-{plugin}-jdbc_fetch_size"]
344
345
  ===== `jdbc_fetch_size`
@@ -95,7 +95,7 @@ module LogStash module Filters module Jdbc
95
95
  end
96
96
  begin
97
97
  db = nil
98
- ::Sequel::JDBC.load_driver(driver_class)
98
+ @options_hash[:driver] = ::Sequel::JDBC.load_driver(driver_class)
99
99
  @connection_string = connection_string
100
100
  if user
101
101
  @options_hash[:user] = user
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require "logstash/filters/base"
3
3
  require "logstash/namespace"
4
+ require "logstash/plugin_mixins/jdbc/common"
4
5
  require "logstash/plugin_mixins/jdbc_streaming"
5
6
  require "logstash/plugin_mixins/jdbc_streaming/cache_payload"
6
7
  require "logstash/plugin_mixins/jdbc_streaming/statement_handler"
@@ -46,6 +47,7 @@ require "lru_redux"
46
47
  # }
47
48
  #
48
49
  module LogStash module Filters class JdbcStreaming < LogStash::Filters::Base
50
+ include LogStash::PluginMixins::Jdbc::Common
49
51
  include LogStash::PluginMixins::JdbcStreaming
50
52
 
51
53
  config_name "jdbc_streaming"
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require "logstash/inputs/base"
3
3
  require "logstash/namespace"
4
+ require "logstash/plugin_mixins/jdbc/common"
4
5
  require "logstash/plugin_mixins/jdbc/jdbc"
5
6
 
6
7
  # this require_relative returns early unless the JRuby version is between 9.2.0.0 and 9.2.8.0
@@ -126,6 +127,7 @@ require_relative "tzinfo_jruby_patch"
126
127
  # ---------------------------------------------------------------------------------------------------
127
128
  #
128
129
  module LogStash module Inputs class Jdbc < LogStash::Inputs::Base
130
+ include LogStash::PluginMixins::Jdbc::Common
129
131
  include LogStash::PluginMixins::Jdbc::Jdbc
130
132
  config_name "jdbc"
131
133
 
@@ -270,6 +272,7 @@ module LogStash module Inputs class Jdbc < LogStash::Inputs::Base
270
272
  end
271
273
 
272
274
  def run(queue)
275
+ load_driver
273
276
  if @schedule
274
277
  @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
275
278
  @scheduler.cron @schedule do
@@ -282,6 +285,10 @@ module LogStash module Inputs class Jdbc < LogStash::Inputs::Base
282
285
  end
283
286
  end # def run
284
287
 
288
+ def close
289
+ @scheduler.shutdown if @scheduler
290
+ end
291
+
285
292
  def stop
286
293
  close_jdbc_connection
287
294
  @scheduler.shutdown(:wait) if @scheduler
@@ -0,0 +1,66 @@
1
+
2
+ module LogStash module PluginMixins module Jdbc
3
+ module Common
4
+
5
+ private
6
+
7
+ def complete_sequel_opts(defaults = {})
8
+ sequel_opts = @sequel_opts.
9
+ map { |key,val| [key.is_a?(String) ? key.to_sym : key, val] }.
10
+ map { |key,val| [key, val.eql?('true') ? true : (val.eql?('false') ? false : val)] }
11
+ sequel_opts = defaults.merge Hash[sequel_opts]
12
+ sequel_opts[:user] = @jdbc_user unless @jdbc_user.nil? || @jdbc_user.empty?
13
+ sequel_opts[:password] = @jdbc_password.value unless @jdbc_password.nil?
14
+ sequel_opts[:driver] = @driver_impl # Sequel uses this as a fallback, if given URI doesn't auto-load the driver correctly
15
+ sequel_opts
16
+ end
17
+
18
+ def load_driver
19
+ return @driver_impl if @driver_impl ||= nil
20
+
21
+ require "java"
22
+ require "sequel"
23
+ require "sequel/adapters/jdbc"
24
+
25
+ load_driver_jars
26
+ begin
27
+ @driver_impl = Sequel::JDBC.load_driver(@jdbc_driver_class)
28
+ rescue Sequel::AdapterNotFound => e # Sequel::AdapterNotFound, "#{@jdbc_driver_class} not loaded"
29
+ # fix this !!!
30
+ message = if jdbc_driver_library_set?
31
+ "Are you sure you've included the correct jdbc driver in :jdbc_driver_library?"
32
+ else
33
+ ":jdbc_driver_library is not set, are you sure you included " +
34
+ "the proper driver client libraries in your classpath?"
35
+ end
36
+ raise LogStash::PluginLoadingError, "#{e}. #{message}"
37
+ end
38
+ end
39
+
40
+ def load_driver_jars
41
+ if jdbc_driver_library_set?
42
+ @jdbc_driver_library.split(",").each do |driver_jar|
43
+ @logger.debug("loading #{driver_jar}")
44
+ # load 'driver.jar' is different than load 'some.rb' as it only causes the file to be added to
45
+ # JRuby's class-loader lookup (class) path - won't raise a LoadError when file is not readable
46
+ unless FileTest.readable?(driver_jar)
47
+ raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, " +
48
+ "file not readable (please check user and group permissions for the path)"
49
+ end
50
+ begin
51
+ require driver_jar
52
+ rescue LoadError => e
53
+ raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e.message}"
54
+ rescue StandardError => e
55
+ raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e}"
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def jdbc_driver_library_set?
62
+ !@jdbc_driver_library.nil? && !@jdbc_driver_library.empty?
63
+ end
64
+
65
+ end
66
+ end end end
@@ -106,17 +106,12 @@ module LogStash module PluginMixins module Jdbc
106
106
 
107
107
  private
108
108
  def jdbc_connect
109
- opts = {
110
- :user => @jdbc_user,
111
- :password => @jdbc_password.nil? ? nil : @jdbc_password.value,
112
- :pool_timeout => @jdbc_pool_timeout,
113
- :keep_reference => false
114
- }.merge(@sequel_opts)
109
+ sequel_opts = complete_sequel_opts(:pool_timeout => @jdbc_pool_timeout, :keep_reference => false)
115
110
  retry_attempts = @connection_retry_attempts
116
111
  loop do
117
112
  retry_attempts -= 1
118
113
  begin
119
- return Sequel.connect(@jdbc_connection_string, opts=opts)
114
+ return Sequel.connect(@jdbc_connection_string, sequel_opts)
120
115
  rescue Sequel::PoolTimeout => e
121
116
  if retry_attempts <= 0
122
117
  @logger.error("Failed to connect to database. #{@jdbc_pool_timeout} second timeout exceeded. Tried #{@connection_retry_attempts} times.")
@@ -127,7 +122,7 @@ module LogStash module PluginMixins module Jdbc
127
122
  # rescue Java::JavaSql::SQLException, ::Sequel::Error => e
128
123
  rescue ::Sequel::Error => e
129
124
  if retry_attempts <= 0
130
- @logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", :error_message => e.message, )
125
+ @logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", :error_message => e.message)
131
126
  raise e
132
127
  else
133
128
  @logger.error("Unable to connect to database. Trying again", :error_message => e.message)
@@ -137,49 +132,10 @@ module LogStash module PluginMixins module Jdbc
137
132
  end
138
133
  end
139
134
 
140
- private
141
-
142
- def load_driver_jars
143
- unless @jdbc_driver_library.nil? || @jdbc_driver_library.empty?
144
- @jdbc_driver_library.split(",").each do |driver_jar|
145
- begin
146
- @logger.debug("loading #{driver_jar}")
147
- # Use https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-jar-files to make classes from jar
148
- # available
149
- require driver_jar
150
- rescue LoadError => e
151
- raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e.message}"
152
- end
153
- end
154
- end
155
- end
156
-
157
- private
158
135
  def open_jdbc_connection
159
- require "java"
160
- require "sequel"
161
- require "sequel/adapters/jdbc"
162
-
136
+ # at this point driver is already loaded
163
137
  Sequel.application_timezone = @plugin_timezone.to_sym
164
- if @drivers_loaded.false?
165
- begin
166
- load_driver_jars
167
- Sequel::JDBC.load_driver(@jdbc_driver_class)
168
- rescue LogStash::Error => e
169
- # raised in load_drivers, e.cause should be the caught Java exceptions
170
- raise LogStash::PluginLoadingError, "#{e.message} and #{e.cause.message}"
171
- rescue Sequel::AdapterNotFound => e
172
- # fix this !!!
173
- message = if @jdbc_driver_library.nil?
174
- ":jdbc_driver_library is not set, are you sure you included
175
- the proper driver client libraries in your classpath?"
176
- else
177
- "Are you sure you've included the correct jdbc driver in :jdbc_driver_library?"
178
- end
179
- raise LogStash::PluginLoadingError, "#{e}. #{message}"
180
- end
181
- @drivers_loaded.make_true
182
- end
138
+
183
139
  @database = jdbc_connect()
184
140
  @database.extension(:pagination)
185
141
  if @jdbc_default_timezone
@@ -218,7 +174,6 @@ module LogStash module PluginMixins module Jdbc
218
174
  public
219
175
  def prepare_jdbc_connection
220
176
  @connection_lock = ReentrantLock.new
221
- @drivers_loaded = Concurrent::AtomicBoolean.new
222
177
  end
223
178
 
224
179
  public
@@ -249,7 +204,9 @@ module LogStash module PluginMixins module Jdbc
249
204
  end
250
205
  success = true
251
206
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError, Java::JavaSql::SQLException => e
252
- @logger.warn("Exception when executing JDBC query", :exception => e)
207
+ details = { :exception => e.message }
208
+ details[:backtrace] = e.backtrace if @logger.debug?
209
+ @logger.warn("Exception when executing JDBC query", details)
253
210
  else
254
211
  @value_tracker.set_value(sql_last_value)
255
212
  ensure
@@ -97,7 +97,14 @@ module LogStash module PluginMixins module Jdbc
97
97
  end
98
98
  bind_value_sql_last_value(sql_last_value)
99
99
  statement_logger.log_statement_parameters(statement, parameters, nil)
100
- db.call(name, parameters)
100
+ begin
101
+ db.call(name, parameters)
102
+ rescue => e
103
+ # clear the statement prepared flag - the statement may be closed by this
104
+ # time.
105
+ statement_prepared.make_false
106
+ raise e
107
+ end
101
108
  end
102
109
 
103
110
  def post_init(plugin)
@@ -62,7 +62,7 @@ module LogStash module PluginMixins module Jdbc
62
62
 
63
63
  class NumericValueTracker < ValueTracking
64
64
  def set_initial
65
- common_set_initial(:gcd, 0)
65
+ common_set_initial(:abs, 0)
66
66
  end
67
67
 
68
68
  def set_value(value)
@@ -55,37 +55,11 @@ module LogStash module PluginMixins module JdbcStreaming
55
55
  config :jdbc_validation_timeout, :validate => :number, :default => 3600
56
56
  end
57
57
 
58
- private
59
-
60
- def load_driver_jars
61
- unless @jdbc_driver_library.nil? || @jdbc_driver_library.empty?
62
- @jdbc_driver_library.split(",").each do |driver_jar|
63
- begin
64
- @logger.debug("loading #{driver_jar}")
65
- # Use https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-jar-files to make classes from jar
66
- # available
67
- require driver_jar
68
- rescue LoadError => e
69
- raise LogStash::PluginLoadingError, "unable to load #{driver_jar} from :jdbc_driver_library, #{e.message}"
70
- end
71
- end
72
- end
73
- end
74
-
75
58
  public
76
59
  def prepare_jdbc_connection
77
- require "sequel"
78
- require "sequel/adapters/jdbc"
79
- require "java"
80
-
81
- load_driver_jars
82
-
83
- @sequel_opts_symbols = @sequel_opts.inject({}) {|hash, (k,v)| hash[k.to_sym] = v; hash}
84
- @sequel_opts_symbols[:user] = @jdbc_user unless @jdbc_user.nil? || @jdbc_user.empty?
85
- @sequel_opts_symbols[:password] = @jdbc_password.value unless @jdbc_password.nil?
60
+ load_driver
86
61
 
87
- Sequel::JDBC.load_driver(@jdbc_driver_class)
88
- @database = Sequel.connect(@jdbc_connection_string, @sequel_opts_symbols)
62
+ @database = Sequel.connect(@jdbc_connection_string, complete_sequel_opts)
89
63
  if @jdbc_validate_connection
90
64
  @database.extension(:connection_validator)
91
65
  @database.pool.connection_validation_timeout = @jdbc_validation_timeout
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-integration-jdbc'
3
- s.version = '5.0.0'
3
+ s.version = '5.0.5'
4
4
  s.licenses = ['Apache License (2.0)']
5
5
  s.summary = "Integration with JDBC - input and filter plugins"
6
6
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -4,7 +4,6 @@ require "logstash/filters/jdbc_static"
4
4
  require "sequel"
5
5
  require "sequel/adapters/jdbc"
6
6
  require "stud/temporary"
7
- require "timecop"
8
7
 
9
8
  module LogStash module Filters
10
9
  describe JdbcStatic, :integration => true do
@@ -35,6 +34,7 @@ module LogStash module Filters
35
34
  let(:settings) do
36
35
  {
37
36
  "jdbc_user" => ENV['USER'],
37
+ "jdbc_password" => ENV["POSTGRES_PASSWORD"],
38
38
  "jdbc_driver_class" => "org.postgresql.Driver",
39
39
  "jdbc_driver_library" => "/usr/share/logstash/postgresql.jar",
40
40
  "staging_directory" => temp_import_path_plugin,
@@ -88,6 +88,7 @@ module LogStash module Filters
88
88
  let(:settings) do
89
89
  {
90
90
  "jdbc_user" => ENV['USER'],
91
+ "jdbc_password" => ENV["POSTGRES_PASSWORD"],
91
92
  "jdbc_driver_class" => "org.postgresql.Driver",
92
93
  "jdbc_driver_library" => "/usr/share/logstash/postgresql.jar",
93
94
  "staging_directory" => temp_import_path_plugin,
@@ -134,18 +135,16 @@ module LogStash module Filters
134
135
  describe "scheduled operation" do
135
136
  context "given a loader_schedule" do
136
137
  it "should properly schedule" do
137
- settings["loader_schedule"] = "*/10 * * * * * UTC"
138
- Timecop.travel(Time.now.utc - 3600)
139
- Timecop.scale(60)
138
+ settings["loader_schedule"] = "*/3 * * * * * UTC"
140
139
  static_filter = JdbcStatic.new(settings)
141
140
  runner = Thread.new(static_filter) do |filter|
142
141
  filter.register
143
142
  end
144
- sleep 3
143
+ runner.join(4)
144
+ sleep 4
145
145
  static_filter.filter(event)
146
146
  expect(static_filter.loader_runner.reload_count).to be > 1
147
147
  static_filter.close
148
- Timecop.return
149
148
  expect(event.get("server")).to eq([{"ip"=>"10.3.1.1", "name"=>"mv-server-1", "location"=>"MV-9-6-4"}])
150
149
  end
151
150
  end
@@ -16,7 +16,10 @@ module LogStash module Filters
16
16
  "jdbc:postgresql://postgresql:5432") + "/jdbc_streaming_db?user=postgres"
17
17
 
18
18
  let(:mixin_settings) do
19
- { "jdbc_driver_class" => "org.postgresql.Driver",
19
+ {
20
+ "jdbc_user" => ENV['USER'],
21
+ "jdbc_password" => ENV["POSTGRES_PASSWORD"],
22
+ "jdbc_driver_class" => "org.postgresql.Driver",
20
23
  "jdbc_driver_library" => "/usr/share/logstash/postgresql.jar",
21
24
  "jdbc_connection_string" => jdbc_connection_string
22
25
  }
@@ -10,28 +10,37 @@ module LogStash module Filters module Jdbc
10
10
  let(:driver_class) { "org.apache.derby.jdbc.EmbeddedDriver" }
11
11
  let(:driver_library) { nil }
12
12
  subject(:read_only_db) { described_class.create(connection_string, driver_class, driver_library) }
13
+ let(:stub_driver_class) { double(driver_class).as_null_object }
13
14
 
14
15
  describe "basic operations" do
15
16
  describe "initializing" do
17
+ before(:each) do
18
+ allow(Sequel::JDBC).to receive(:load_driver).and_return(stub_driver_class)
19
+ end
20
+
16
21
  it "tests the connection with defaults" do
17
22
  expect(Sequel::JDBC).to receive(:load_driver).once.with(driver_class)
18
- expect(Sequel).to receive(:connect).once.with(connection_string, {:test => true})
23
+ expect(Sequel).to receive(:connect).once.with(connection_string, {:test => true, :driver => stub_driver_class})
19
24
  expect(read_only_db.empty_record_set).to eq([])
20
25
  end
21
26
 
22
- it "tests the connection with fully specified arguments" do
23
- connection_str = "a connection string"
24
- user = "a user"
25
- password = Util::Password.new("secret")
26
- expect(Sequel::JDBC).to receive(:load_driver).once.with("a driver class")
27
- expect(Sequel).to receive(:connect).once.with(connection_str, {:user => user, :password => password.value, :test => true}).and_return(db)
28
- described_class.create(connection_str, "a driver class", nil, user, password)
27
+ context 'with fully-specified arguments' do
28
+ let(:connection_string) { "a connection string" }
29
+ let(:user) { "a user" }
30
+ let(:password) { Util::Password.new("secret") }
31
+ let(:driver_class) { "a driver class" }
32
+
33
+ it "tests the connection" do
34
+ expect(Sequel::JDBC).to receive(:load_driver).once.with(driver_class)
35
+ expect(Sequel).to receive(:connect).once.with(connection_string, {:user => user, :password => password.value, :test => true, :driver => stub_driver_class}).and_return(db)
36
+ described_class.create(connection_string, driver_class, nil, user, password)
37
+ end
29
38
  end
30
39
 
31
40
  it "connects with defaults" do
32
41
  expect(Sequel::JDBC).to receive(:load_driver).once.with(driver_class)
33
- expect(Sequel).to receive(:connect).once.with(connection_string, {:test => true}).and_return(db)
34
- expect(Sequel).to receive(:connect).once.with(connection_string, {}).and_return(db)
42
+ expect(Sequel).to receive(:connect).once.with(connection_string, {:test => true, :driver => stub_driver_class}).and_return(db)
43
+ expect(Sequel).to receive(:connect).once.with(connection_string, {:driver => stub_driver_class}).and_return(db)
35
44
  expect(read_only_db.connected?).to be_falsey
36
45
  read_only_db.connect("a caller specific error message")
37
46
  expect(read_only_db.connected?).to be_truthy
@@ -16,10 +16,11 @@ module LogStash module Filters module Jdbc
16
16
  describe "basic operations" do
17
17
  context "connecting to a db" do
18
18
  it "connects with defaults" do
19
- expect(::Sequel::JDBC).to receive(:load_driver).once.with("org.apache.derby.jdbc.EmbeddedDriver")
19
+ stub_driver_class = double('org.apache.derby.jdbc.EmbeddedDriver').as_null_object
20
+ expect(::Sequel::JDBC).to receive(:load_driver).once.with("org.apache.derby.jdbc.EmbeddedDriver").and_return(stub_driver_class)
20
21
  # two calls to connect because ReadWriteDatabase does verify_connection and connect
21
- expect(::Sequel).to receive(:connect).once.with(connection_string_regex, {:test => true}).and_return(db)
22
- expect(::Sequel).to receive(:connect).once.with(connection_string_regex, {}).and_return(db)
22
+ expect(::Sequel).to receive(:connect).once.with(connection_string_regex, {:driver => stub_driver_class, :test => true}).and_return(db)
23
+ expect(::Sequel).to receive(:connect).once.with(connection_string_regex, {:driver => stub_driver_class}).and_return(db)
23
24
  expect(read_write_db.empty_record_set).to eq([])
24
25
  end
25
26
 
@@ -27,9 +28,10 @@ module LogStash module Filters module Jdbc
27
28
  connection_str = "a connection string"
28
29
  user = "a user"
29
30
  password = Util::Password.new("secret")
30
- expect(::Sequel::JDBC).to receive(:load_driver).once.with("a driver class")
31
- expect(::Sequel).to receive(:connect).once.with(connection_str, {:user => user, :password => password.value, :test => true}).and_return(db)
32
- expect(::Sequel).to receive(:connect).once.with(connection_str, {:user => user, :password => password.value}).and_return(db)
31
+ stub_driver_class = double('com.example.Driver')
32
+ expect(::Sequel::JDBC).to receive(:load_driver).once.with("a driver class").and_return(stub_driver_class)
33
+ expect(::Sequel).to receive(:connect).once.with(connection_str, {:driver => stub_driver_class, :user => user, :password => password.value, :test => true}).and_return(db)
34
+ expect(::Sequel).to receive(:connect).once.with(connection_str, {:driver => stub_driver_class, :user => user, :password => password.value}).and_return(db)
33
35
  described_class.create(connection_str, "a driver class", nil, user, password)
34
36
  end
35
37
  end
@@ -1,4 +1,5 @@
1
1
  require "logstash/devutils/rspec/spec_helper"
2
+ require "logstash/devutils/rspec/shared_examples"
2
3
  require "logstash/filters/jdbc_streaming"
3
4
  require 'jdbc/derby'
4
5
  require "sequel"
@@ -3,7 +3,6 @@ require "logstash/inputs/jdbc"
3
3
  require "sequel"
4
4
  require "sequel/adapters/jdbc"
5
5
 
6
- # This test requires: Firebird installed to Mac OSX, it uses the built-in example database `employee`
7
6
 
8
7
  describe LogStash::Inputs::Jdbc, :integration => true do
9
8
  # This is a necessary change test-wide to guarantee that no local timezone
@@ -19,6 +18,7 @@ describe LogStash::Inputs::Jdbc, :integration => true do
19
18
  "jdbc_connection_string" => jdbc_connection_string,
20
19
  "jdbc_driver_library" => "/usr/share/logstash/postgresql.jar",
21
20
  "jdbc_user" => "postgres",
21
+ "jdbc_password" => ENV["POSTGRES_PASSWORD"],
22
22
  "statement" => 'SELECT FIRST_NAME, LAST_NAME FROM "employee" WHERE EMP_NO = 2'
23
23
  }
24
24
  end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/devutils/rspec/shared_examples"
3
4
  require "logstash/inputs/jdbc"
4
5
  require "jdbc/derby"
5
6
  require "sequel"
@@ -12,9 +13,13 @@ require "date"
12
13
  # We do not need to set TZ env var anymore because we can have 'Sequel.application_timezone' set to utc by default now.
13
14
 
14
15
  describe LogStash::Inputs::Jdbc do
16
+ let(:connection_string) { "jdbc:derby:memory:testdb;create=true" }
15
17
  let(:mixin_settings) do
16
- { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
17
- "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"}
18
+ {
19
+ "jdbc_user" => ENV['USER'],
20
+ "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
21
+ "jdbc_connection_string" => connection_string
22
+ }
18
23
  end
19
24
  let(:settings) { {} }
20
25
  let(:plugin) { LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings)) }
@@ -28,10 +33,11 @@ describe LogStash::Inputs::Jdbc do
28
33
  # before body
29
34
  Jdbc::Derby.load_driver
30
35
  db.create_table :test_table do
31
- DateTime :created_at
32
- Integer :num
33
- String :string
34
- DateTime :custom_time
36
+ DateTime :created_at
37
+ BigDecimal :big_num
38
+ Integer :num
39
+ String :string
40
+ DateTime :custom_time
35
41
  end
36
42
  db << "CREATE TABLE types_table (num INTEGER, string VARCHAR(255), started_at DATE, custom_time TIMESTAMP, ranking DECIMAL(16,6))"
37
43
  db << "CREATE TABLE test1_table (num INTEGER, string VARCHAR(255), custom_time TIMESTAMP, created_at TIMESTAMP)"
@@ -110,6 +116,57 @@ describe LogStash::Inputs::Jdbc do
110
116
  end
111
117
  end
112
118
 
119
+ context "when sequel opts has user option" do
120
+ let(:settings) do
121
+ {
122
+ "jdbc_user" => 'system',
123
+ "statement" => "SELECT * from test_table",
124
+ "sequel_opts" => { "user" => 'from-opts' }
125
+ }
126
+ end
127
+
128
+ before do
129
+ plugin.register
130
+ end
131
+
132
+ after do
133
+ plugin.stop
134
+ end
135
+
136
+ it "should honor set jdbc-user when connecting" do
137
+ expect( Sequel ).to receive(:connect).with connection_string, hash_including(:user=>"system")
138
+ plugin.send(:jdbc_connect)
139
+ end
140
+ end
141
+
142
+ context "with sequel opts" do
143
+ let(:settings) do
144
+ {
145
+ "jdbc_user" => 'system',
146
+ "statement" => "SELECT * from test_table",
147
+ "sequel_opts" => {
148
+ "truthy" => 'true',
149
+ "falsey" => 'false',
150
+ "foo" => 'bar',
151
+ "jdbc_properties" => { "some" => 'true' }
152
+ }
153
+ }
154
+ end
155
+
156
+ before do
157
+ plugin.register
158
+ end
159
+
160
+ after do
161
+ plugin.stop
162
+ end
163
+
164
+ it "should symbolize keys" do
165
+ expect( Sequel ).to receive(:connect).with connection_string,
166
+ hash_including(:truthy => true, :falsey => false, :foo => 'bar', :jdbc_properties => { 'some' => 'true' })
167
+ plugin.send(:jdbc_connect)
168
+ end
169
+ end
113
170
 
114
171
  context "when neither statement and statement_filepath arguments are passed" do
115
172
  it "should fail to register" do
@@ -189,6 +246,18 @@ describe LogStash::Inputs::Jdbc do
189
246
  Timecop.return
190
247
  end
191
248
 
249
+ it "cleans up scheduler resources on close" do
250
+ runner = Thread.new do
251
+ plugin.run(queue)
252
+ end
253
+ sleep 1
254
+ plugin.do_close
255
+
256
+ scheduler = plugin.instance_variable_get(:@scheduler)
257
+ expect(scheduler).to_not be_nil
258
+ expect(scheduler.down?).to be_truthy
259
+ end
260
+
192
261
  end
193
262
 
194
263
  context "when scheduling and previous runs are to be preserved" do
@@ -545,6 +614,54 @@ describe LogStash::Inputs::Jdbc do
545
614
  end
546
615
  end
547
616
 
617
+ context "Test Sql Last Value with a BigDecimal `sqlLastValue`" do
618
+ let(:mixin_settings) do
619
+ { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
620
+ "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"
621
+ }
622
+ end
623
+
624
+ let(:settings) do
625
+ { "statement" => "SELECT big_num, created_at FROM test_table WHERE big_num > :sql_last_value",
626
+ "use_column_value" => true,
627
+ "tracking_column" => "big_num",
628
+ "last_run_metadata_path" => Stud::Temporary.pathname }
629
+ end
630
+
631
+ let(:nums) { [BigDecimal.new(10), BigDecimal.new(20), BigDecimal.new(30), BigDecimal.new(40), BigDecimal.new(50)] }
632
+
633
+ before do
634
+ plugin.register
635
+ end
636
+
637
+ after do
638
+ plugin.stop
639
+ end
640
+
641
+ it "should only pick up new values" do
642
+ test_table = db[:test_table]
643
+
644
+ plugin.run(queue)
645
+ expect(queue.size).to eq(0)
646
+ test_table.insert(:big_num => nums[0], :created_at => Time.now.utc)
647
+ test_table.insert(:big_num => nums[1], :created_at => Time.now.utc)
648
+ queue = []
649
+ # Stop and restart the plugin to have it read saved value of `sql_last_value`
650
+ plugin.stop
651
+ plugin.register
652
+ plugin.run(queue)
653
+ expect(queue.size).to eq(2)
654
+ test_table.insert(:big_num => nums[2], :created_at => Time.now.utc)
655
+ test_table.insert(:big_num => nums[3], :created_at => Time.now.utc)
656
+ test_table.insert(:big_num => nums[4], :created_at => Time.now.utc)
657
+ queue = []
658
+ plugin.stop
659
+ plugin.register
660
+ plugin.run(queue)
661
+ expect(queue.size).to eq(3)
662
+ end
663
+ end
664
+
548
665
  context "when iteratively running plugin#run with timestamp tracking column with column value" do
549
666
  let(:mixin_settings) do
550
667
  { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver",
@@ -1291,6 +1408,38 @@ describe LogStash::Inputs::Jdbc do
1291
1408
  end
1292
1409
  end
1293
1410
 
1411
+ context "when an unreadable jdbc_driver_path entry is present" do
1412
+ let(:driver_jar_path) do
1413
+ jar_file = $CLASSPATH.find { |name| name.index(Jdbc::Derby.driver_jar) }
1414
+ raise "derby jar not found on class-path" unless jar_file
1415
+ jar_file.sub('file:', '')
1416
+ end
1417
+
1418
+ let(:invalid_driver_jar_path) do
1419
+ path = File.join(Dir.mktmpdir, File.basename(driver_jar_path))
1420
+ FileUtils.cp driver_jar_path, path
1421
+ FileUtils.chmod "u=x,go=", path
1422
+ path
1423
+ end
1424
+
1425
+ let(:settings) do
1426
+ { "statement" => "SELECT * from types_table", "jdbc_driver_library" => invalid_driver_jar_path }
1427
+ end
1428
+
1429
+ before do
1430
+ plugin.register
1431
+ end
1432
+
1433
+ after do
1434
+ plugin.stop
1435
+ end
1436
+
1437
+ it "raise a loading error" do
1438
+ expect { plugin.run(queue) }.
1439
+ to raise_error(LogStash::PluginLoadingError, /unable to load .*? from :jdbc_driver_library, file not readable/)
1440
+ end
1441
+ end
1442
+
1294
1443
  context "when using prepared statements" do
1295
1444
  let(:last_run_value) { 250 }
1296
1445
  let(:expected_queue_size) { 100 }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-integration-jdbc
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-09 00:00:00.000000000 Z
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -210,7 +210,6 @@ files:
210
210
  - CONTRIBUTORS
211
211
  - Gemfile
212
212
  - LICENSE
213
- - NOTICE.TXT
214
213
  - README.md
215
214
  - docs/filter-jdbc_static.asciidoc
216
215
  - docs/filter-jdbc_streaming.asciidoc
@@ -235,6 +234,7 @@ files:
235
234
  - lib/logstash/inputs/jdbc.rb
236
235
  - lib/logstash/inputs/tzinfo_jruby_patch.rb
237
236
  - lib/logstash/plugin_mixins/jdbc/checked_count_logger.rb
237
+ - lib/logstash/plugin_mixins/jdbc/common.rb
238
238
  - lib/logstash/plugin_mixins/jdbc/jdbc.rb
239
239
  - lib/logstash/plugin_mixins/jdbc/statement_handler.rb
240
240
  - lib/logstash/plugin_mixins/jdbc/value_tracking.rb
@@ -242,7 +242,6 @@ files:
242
242
  - lib/logstash/plugin_mixins/jdbc_streaming/cache_payload.rb
243
243
  - lib/logstash/plugin_mixins/jdbc_streaming/parameter_handler.rb
244
244
  - lib/logstash/plugin_mixins/jdbc_streaming/statement_handler.rb
245
- - lib/logstash/plugin_mixins/statement_handler.rb
246
245
  - logstash-integration-jdbc.gemspec
247
246
  - spec/filters/env_helper.rb
248
247
  - spec/filters/integration/jdbc_static_spec.rb
@@ -291,7 +290,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
290
  version: '0'
292
291
  requirements: []
293
292
  rubyforge_project:
294
- rubygems_version: 2.7.9
293
+ rubygems_version: 2.6.13
295
294
  signing_key:
296
295
  specification_version: 4
297
296
  summary: Integration with JDBC - input and filter plugins
data/NOTICE.TXT DELETED
@@ -1,5 +0,0 @@
1
- Elasticsearch
2
- Copyright 2012-2018 Elasticsearch
3
-
4
- This product includes software developed by The Apache Software
5
- Foundation (http://www.apache.org/).