Skip to content

Add try,catch statement for query - #65

Open
JaeGeunBang wants to merge 1 commit into
tagomoris:masterfrom
JaeGeunBang:add-try-catch-statement
Open

Add try,catch statement for query#65
JaeGeunBang wants to merge 1 commit into
tagomoris:masterfrom
JaeGeunBang:add-try-catch-statement

Conversation

@JaeGeunBang

Copy link
Copy Markdown

Add try, catch statements.

I am using fluent-plugin-mysql plygun to save log to MySQL periodically every minute.

However, after Mysql2 error, the data received from source can't save to MySQL. Keep trying retry.

Even though Mysql2 error occurs after adding try, catch statements, data received from source can save to MySQL.

Before

2020-02-14 10:15:53 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 1
...
2020-02-14 10:07:55 +0900 [warn]: #0 failed to flush the buffer. retry_time=1 next_retry_seconds=2020-02-14 10:07:56 +0900 chunk="59e7ed53eb5cc4525fdaeeb7686061cd" error_class=Mysql2::Error error="Column 'date_time' cannot be null"
  2020-02-14 10:07:55 +0900 [warn]: #0 suppressed same stacktrace
2020-02-14 10:07:56 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 1
2020-02-14 10:07:56 +0900 [warn]: #0 failed to flush the buffer. retry_time=2 next_retry_seconds=2020-02-14 10:07:58 +0900 chunk="59e7ed53eb5cc4525fdaeeb7686061cd" error_class=Mysql2::Error error="Column 'date_time' cannot be null"
  2020-02-14 10:07:56 +0900 [warn]: #0 suppressed same stacktrace
2020-02-14 10:07:58 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 1

After

2020-02-14 10:13:02 +0900 [info]: #0 fluentd worker is now running worker=0
2020-02-14 10:15:24 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 3645
2020-02-14 10:15:53 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 1
2020-02-14 10:15:53 +0900 [warn]: #0 an unexpected query error occurred
2020-02-14 10:16:21 +0900 [info]: #0 bulk insert values size (table: searchzum_component_click_test) => 3662

y-ken added a commit that referenced this pull request Jun 17, 2026
out_mysql_bulk's write/1 opened a fresh MySQL connection on every flush and
closed it only on the success path. That had two problems:

* A failing INSERT (e.g. a NOT NULL violation on bad input) skipped the close
  and leaked the connection on every Fluentd retry.
* Even on success, reconnecting for each flush is wasteful.

Now the connection is kept open and reused across successful flushes, and
reconnected only when there is no usable handler or when the target database
changed (the INSERT uses an unqualified table name, so it must run on a
connection bound to the right database). On error the connection is dropped so
the next retry reconnects (this also recovers from a server-side
"gone away"), and the held connection is released on plugin shutdown via #close.

The error is deliberately NOT rescued-and-swallowed. PR #65 proposed
`rescue Exception` + log.warn to stop endless retries, but swallowing the error
makes Fluentd treat the flush as successful and purge the chunk, silently
dropping every buffered record in it (a whole bulk batch, not just the bad row).
Letting it propagate keeps Fluentd's retry / secondary handling intact; operators
who want to cap retries on permanently-bad data should use retry_max_times /
<secondary>, which this change leaves working.

Note: as before, write/1 uses a single @handler instance variable and is not
safe for flush_thread_count > 1; that pre-existing limitation is unchanged.

Add regression tests:
- unit (no live MySQL): a stubbed handler proves write/1 reuses one connection
  across successful flushes and closes it on shutdown, and that a failing INSERT
  re-raises while closing and dropping the connection.
- integration: a NOT NULL violation (the exact error from PR #65) must raise out
  of write/1 and insert nothing; a second successful flush reuses the same
  connection.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant