Skip to content

Fix bug: mysql statement leak - #1627

Merged
javeme merged 20 commits into
apache:masterfrom
z7658329:bugfix-branch
Nov 3, 2021
Merged

Fix bug: mysql statement leak#1627
javeme merged 20 commits into
apache:masterfrom
z7658329:bugfix-branch

Conversation

@z7658329

@z7658329 z7658329 commented Nov 2, 2021

Copy link
Copy Markdown
Member

Fix bug: mysql statement leak

fix #1626

Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
@javeme javeme changed the title Bugfix branch Fix bug: mysql statement leak Nov 2, 2021

@javeme javeme left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thank you for your contribution.

try {
while (!this.results.isClosed() && this.results.next()) {
MysqlBackendEntry entry = this.row2Entry(this.results);
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

prefer to add hasNext method to ResultSetWrapper to wrap isClosed() and next()

while (!this.results.isClosed() && this.results.next()) {
MysqlBackendEntry entry = this.row2Entry(this.results);
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) {
MysqlBackendEntry entry = this.row2Entry(this.resultSetWrapper.getResultSet());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename to next()

protected Iterator<BackendEntry> results2Entries(Query query,
ResultSet results) {
return new MysqlEntryIterator(results, query, this::mergeEntries);
ResultSetWrapper resultSetWrapper) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

keep results name

try {
ResultSet resultSet = session.select(select);
resultSetWrapper = session.select(select);
ResultSet resultSet = resultSetWrapper.getResultSet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename to ResultSet rs?

private PaloLoadInfo getLoadInfoByLabel(String label) {
String sql = String.format("SHOW LOAD WHERE LABEL = '%s'", label);
ResultSet result;
ResultSetWrapper resultSetWrapper = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

if (result.next()) {
return new PaloLoadInfo(result);
resultSetWrapper = this.select(sql);
ResultSet resultSet = resultSetWrapper.getResultSet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

for (StringBuilder selection : selections) {
ResultSet results = session.select(selection.toString());
rs.extend(parser.apply(query, results));
ResultSetWrapper resultSetWrapper = session.select(selection.toString());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

keep results name

}
return this.conn.createStatement().execute(sql);
Statement statement = null;
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can use try (Statement statement = this.conn.createStatement())

} finally {
if (resultSetWrapper != null) {
resultSetWrapper.close();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems lack of ResultSetWrapper file

Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
@codecov

codecov Bot commented Nov 2, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1627 (f2b0c8d) into master (0b80d77) will increase coverage by 0.36%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1627      +/-   ##
============================================
+ Coverage     62.81%   63.18%   +0.36%     
- Complexity     6691     6715      +24     
============================================
  Files           418      419       +1     
  Lines         34504    34527      +23     
  Branches       4778     4779       +1     
============================================
+ Hits          21674    21816     +142     
+ Misses        10581    10456     -125     
- Partials       2249     2255       +6     
Impacted Files Coverage Δ
...egraph/backend/store/mysql/MysqlEntryIterator.java 0.00% <0.00%> (ø)
...u/hugegraph/backend/store/mysql/MysqlSessions.java 0.00% <0.00%> (ø)
...aidu/hugegraph/backend/store/mysql/MysqlTable.java 0.00% <0.00%> (ø)
...idu/hugegraph/backend/store/mysql/MysqlTables.java 0.00% <0.00%> (ø)
...ugegraph/backend/store/mysql/ResultSetWrapper.java 0.00% <0.00%> (ø)
.../baidu/hugegraph/backend/query/ConditionQuery.java 86.38% <0.00%> (+0.27%) ⬆️
...egraph/backend/store/cassandra/CassandraStore.java 72.64% <0.00%> (+0.85%) ⬆️
.../backend/store/cassandra/CassandraSessionPool.java 57.14% <0.00%> (+1.02%) ⬆️
...egraph/backend/store/cassandra/CassandraShard.java 52.77% <0.00%> (+1.85%) ⬆️
.../backend/store/scylladb/ScyllaDBStoreProvider.java 94.59% <0.00%> (+94.59%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0b80d77...f2b0c8d. Read the comment docs.

try {
while (!this.resultSetWrapper.getResultSet().isClosed() && this.resultSetWrapper.getResultSet().next()) {
MysqlBackendEntry entry = this.row2Entry(this.resultSetWrapper.getResultSet());
while (!this.results.isClosed() && this.results.next()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move !this.results.isClosed() into next() method

return resultSet.next();
}

public boolean isClosed() throws SQLException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

merge into next()

}
}

public ResultSet getResultSet() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

keep resultSet() style

return resultSet;
}

public void setResultSet(ResultSet resultSet) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove it

Comment on lines +50 to +56
public Statement getStatement() {
return statement;
}

public void setStatement(Statement statement) {
this.statement = statement;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove them

Comment on lines +11 to +12
private ResultSet resultSet;
private Statement statement;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

set final

Statement statement = this.conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
return new ResultSetWrapper(rs, statement);
} catch (SQLException sqlException) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SQLException e

assert this.conn.getAutoCommit();
return this.conn.createStatement().executeQuery(sql);
try {
Statement statement = this.conn.createStatement();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move out of try-catch

ResultSet rs = statement.executeQuery(sql);
return new ResultSetWrapper(rs, statement);
} catch (SQLException sqlException) {
throw sqlException;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

close statement if exception occurred when executeQuery()

try {
ResultSet resultSet = session.select(select);
results = session.select(select);
ResultSet resultSet = results.getResultSet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ResultSet rs

Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
public ResultSet resultSet() {
return resultSet;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

delete empty line

}
} catch (SQLException e) {
throw new BackendException(
"Failed to close resultSet and statement", e);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Failed to close resultSet or Statement

also prefer align " with BackendException in prior line

@@ -66,7 +66,7 @@ protected final boolean fetch() {

try {
while (!this.results.isClosed() && this.results.next()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can remove !this.results.isClosed() && since done in results.next()

}
} catch (SQLException e) {
throw new BackendException(
"Failed to close resultSet and statement", e);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

align with "new" and keep "ResultSet and Statement"

Comment on lines +29 to +31
if (this.statement != null) {
this.statement.close();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move to finally-block

if (result.next()) {
return new PaloLoadInfo(result);
try (ResultSetWrapper results = this.select(sql)){
ResultSet resultSet = results.resultSet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also keep ResultSet rs

Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
Fix bug: mysql statement leak

fix apache#1626
@z7658329
z7658329 requested review from javeme and zhoney November 2, 2021 11:34
@javeme
javeme merged commit ec81e76 into apache:master Nov 3, 2021
@dosubot dosubot Bot mentioned this pull request Jul 21, 2026
1 task
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.

[Bug] mysql 后端运行一段时间可能内存泄漏

4 participants