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

[Metricbeat] Fix Socket leak in postgresql module #11419

Closed
wants to merge 3 commits into from

Conversation

adriansr
Copy link
Contributor

This patch refactors the postgresql module to fix a socket leak caused by creating too many instances of sql.DB.

Fixes #11393

@adriansr adriansr requested a review from a team as a code owner March 24, 2019 22:30
@@ -102,8 +102,15 @@ func ParseURL(mod mb.Module, rawURL string) (mb.HostData, error) {
return h, nil
}

func QueryStats(db *sql.DB, query string) ([]map[string]interface{}, error) {
rows, err := db.Query(query)
func QueryStats(driver, uri, query string) ([]map[string]interface{}, error) {

Choose a reason for hiding this comment

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

exported function QueryStats should have comment or be unexported

@@ -102,8 +102,15 @@ func ParseURL(mod mb.Module, rawURL string) (mb.HostData, error) {
return h, nil
}

func QueryStats(db *sql.DB, query string) ([]map[string]interface{}, error) {
rows, err := db.Query(query)
func QueryStats(driver, uri, query string) ([]map[string]interface{}, error) {

Choose a reason for hiding this comment

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

exported function QueryStats should have comment or be unexported

This patch refactors the postgresql module to fix a socket leak caused
by creating too many instances of sql.DB.

Fixes elastic#11393
@adriansr adriansr added bug Metricbeat Metricbeat needs_backport PR is waiting to be backported to other branches. review labels Mar 24, 2019
Copy link
Member

@ruflin ruflin left a comment

Choose a reason for hiding this comment

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

Overall LGTM, some minor tweaks. Could you also make hound happy?


// DBCache keeps a cache of databases for different drivers and URIs.
var DBCache = dbCacheType{
dbs: make(map[cacheKey]*sql.DB, 4),
Copy link
Member

Choose a reason for hiding this comment

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

Could you share some background on the size here?


type dbCacheType struct {
dbs map[cacheKey]*sql.DB
lock sync.Mutex
Copy link
Member

Choose a reason for hiding this comment

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

Nit: What about making this just sync.Mutex to compose dbCacheType with it? Like this below it would be just cache.Lock().


db, err = sql.Open(driver, uri)
if db != nil {
cache.dbs[key] = db
Copy link
Member

Choose a reason for hiding this comment

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

In case of err != nil we should not assign the cache value as otherwise during the next run, it will not return an error.

@jsoriano
Copy link
Member

@adriansr thanks for investigating this.

Did you try to upgrade the postgresql driver we use? It seems that at least a connection leak was fixed recently.

I'd prefer if we can follow an approach as the one used in the MySQL module and avoid maintaining our own connections pool if possible.

@adriansr
Copy link
Contributor Author

adriansr commented Apr 2, 2019

It's true that this was a bit of a shotgun approach to workaround the problem. I've submitted a fix to the lib/pq library (lib/pq#843) so will close this PR.

@adriansr adriansr closed this Apr 2, 2019
@adriansr adriansr removed the needs_backport PR is waiting to be backported to other branches. label Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metricbeat postgresql connection leak under Windows
4 participants