Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functions to close a mysql connection. #10355

Merged
merged 5 commits into from
Jan 28, 2019
Merged

New functions to close a mysql connection. #10355

merged 5 commits into from
Jan 28, 2019

Conversation

jarrocha
Copy link
Contributor

@jarrocha jarrocha commented Jan 27, 2019

This is a possible fix for issue #9415. Added a function in the mysql file and
a method for the metricset to close the db too.

I wanted to give it a shot since the last update was done more than 1 month ago. After researching the issue and feedback I thought about this possible solution. Is my approach good or do you prefer the metricset method doing all the work without calling to a function in the mysql file?

Let me know if there's a specific place where these functions should be called.

Also,
https://github.com/elastic/beats/blob/824d443bc182a12f30859667da4739beef9c8606/metricbeat/mb/module/wrapper_test.go#L66
is empty. Should it call these new functions for assertion? Just thinking of a possible new commit.

This is a possible fix for issue #9415. Added a function in the mysql file and
a method for the metricset to close the db too.
@jarrocha jarrocha requested a review from a team as a code owner January 27, 2019 19:42
@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?


// CloseDB uses the database handle to close the connection and prevents future queries.
func CloseDB(db *sql.DB) error {
err := db.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems to be unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted


// Close closes the database connection and prevents future queries.
func (m *MetricSet) Close() error {
err := m.db.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a nil check for the db. And errors.Wrap() can safely be called when err is nil and it just returns nil.

if m.db == nil {
  return nil
}
return errors.Wrap(m.db.Close(), "failed to close mysql database client")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same fix should be added to metricbeat/module/mysql/galera_status/status.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Thanks for the feedback.

Question, the loadStatus function in the status.go files does not check the db pointer. Should this check be done too?

func (m *MetricSet) loadStatus(db *sql.DB) (map[string]string, error) {
	// Returns the global status, also for versions previous 5.0.2
	rows, err := db.Query("SHOW /*!50002 GLOBAL */ STATUS;")
	if err != nil {
		return nil, err
	}
	defer rows.Close()
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this path isn't reachable with a nil db.

// Close closes the database connection and prevents future queries.
func (m *MetricSet) Close() error {
if m.db == nil {
return errors.New("invalid database handle")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be an error to have a nil db. This means that it never fetched or it never successfully initialized the DB client. In any case there's nothing for Close() to do, but it's not an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I understand, I was missing the scope of work of this function. Now, I understand your initial code feedback. Thank you.

@andrewkroh
Copy link
Member

Can you please add an entry to the CHANGELOG.next.asciidoc file under the bugs / metricbeat section.

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Will wait for CI to test everything before merging. Thanks

@andrewkroh
Copy link
Member

jenkins, test this

@jarrocha
Copy link
Contributor Author

Thank you for your patience.

@andrewkroh andrewkroh merged commit f06cf46 into elastic:master Jan 28, 2019
@ruflin ruflin added the Team:Integrations Label for the Integrations team label Jan 28, 2019
@ruflin
Copy link
Contributor

ruflin commented Jan 28, 2019

@jarrocha Thanks for this fix.

@jsoriano Should we also backport this?

@jsoriano
Copy link
Member

Sure, doing it, thanks @jarrocha for fixing this!

jsoriano pushed a commit to jsoriano/beats that referenced this pull request Jan 29, 2019
Fixes elastic#9415 by closing the db handle.

(cherry picked from commit f06cf46)
jsoriano added a commit that referenced this pull request Jan 30, 2019
Fixes #9415 by closing the db handle.

(cherry picked from commit f06cf46)

Co-authored-by: Jaime A <[email protected]>
@jarrocha
Copy link
Contributor Author

Not a problem. I would like to keep going and grow. Do you have any suggestions? I have taken a look at this one which seems more challenging but you might know better?
https://github.com/elastic/beats/issues/6659
Your feedback is appreciated. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Metricbeat Metricbeat Team:Integrations Label for the Integrations team v6.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants