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

Fix bug: mysql statement leak #1627

Merged
merged 20 commits into from
Nov 3, 2021
Merged

Fix bug: mysql statement leak #1627

merged 20 commits into from
Nov 3, 2021

Conversation

z7658329
Copy link
Member

@z7658329 z7658329 commented Nov 2, 2021

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
Copy link
Contributor

@javeme javeme left a comment

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.

@@ -65,8 +65,8 @@ protected final boolean fetch() {
}

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
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
Contributor

Choose a reason for hiding this comment

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

rename to next()

@@ -672,8 +672,8 @@ protected void wrapOffset(StringBuilder select, Query query) {
}

protected Iterator<BackendEntry> results2Entries(Query query,
ResultSet results) {
return new MysqlEntryIterator(results, query, this::mergeEntries);
ResultSetWrapper resultSetWrapper) {
Copy link
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
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?

@@ -196,21 +197,22 @@ private PaloFile getOrCreate(String table) {
@SuppressWarnings("unused")
private PaloLoadInfo getLoadInfoByLabel(String label) {
String sql = String.format("SHOW LOAD WHERE LABEL = '%s'", label);
ResultSet result;
ResultSetWrapper resultSetWrapper = null;
Copy link
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
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -364,8 +364,8 @@ public Number queryNumber(Session session, Query query) {
List<StringBuilder> selections = this.query2Select(this.table(), query);
try {
for (StringBuilder selection : selections) {
ResultSet results = session.select(selection.toString());
rs.extend(parser.apply(query, results));
ResultSetWrapper resultSetWrapper = session.select(selection.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

keep results name

@@ -486,7 +493,17 @@ public boolean execute(String sql) throws SQLException {
if (!this.conn.getAutoCommit()) {
this.end();
}
return this.conn.createStatement().execute(sql);
Statement statement = null;
try {
Copy link
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
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
Copy link

codecov bot commented Nov 2, 2021

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.

@@ -65,8 +65,8 @@ protected final boolean fetch() {
}

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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
@javeme javeme merged commit ec81e76 into apache:master Nov 3, 2021
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