-
Notifications
You must be signed in to change notification settings - Fork 994
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
PHOENIX-6185 : Propagate info available in SQLExceptionInfo to SQLTimeoutException #919
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b82989
PHOENIX-6185 : Propagate info available in SQLExceptionInfo to OPERAT…
virajjasani 1856548
fixing checkstyle
virajjasani ce86a90
incorporating review
virajjasani 2e7e226
adding unit test
virajjasani 650912b
Merge branch 'master' of github.com:apache/phoenix into PHOENIX-6185-…
virajjasani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
phoenix-core/src/it/java/org/apache/phoenix/end2end/OperationTimeoutWithReasonIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.phoenix.end2end; | ||
|
||
import org.apache.phoenix.util.EnvironmentEdge; | ||
import org.apache.phoenix.util.EnvironmentEdgeManager; | ||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import static org.apache.phoenix.exception.SQLExceptionCode.OPERATION_TIMED_OUT; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
public class OperationTimeoutWithReasonIT extends ParallelStatsDisabledIT { | ||
|
||
private static final class MyClock extends EnvironmentEdge { | ||
private long time; | ||
private final long delay; | ||
|
||
public MyClock (long time, long delay) { | ||
this.time = time; | ||
this.delay = delay; | ||
} | ||
|
||
@Override | ||
public long currentTime() { | ||
long currentTime = this.time; | ||
this.time += this.delay; | ||
return currentTime; | ||
} | ||
} | ||
|
||
@Test | ||
public void testOperationTimeout() throws SQLException { | ||
final String tableName = generateUniqueName(); | ||
final String ddl = "CREATE TABLE " + tableName | ||
+ " (COL1 VARCHAR NOT NULL PRIMARY KEY, COL2 VARCHAR)"; | ||
try (Connection conn = DriverManager.getConnection(getUrl()); | ||
Statement stmt = conn.createStatement()) { | ||
stmt.execute(ddl); | ||
final String dml = String.format("UPSERT INTO %s VALUES (?, ?)", | ||
tableName); | ||
try(PreparedStatement prepStmt = conn.prepareStatement(dml)) { | ||
for (int i = 1; i <= 100; i++) { | ||
prepStmt.setString(1, "key" + i); | ||
prepStmt.setString(2, "value" + i); | ||
prepStmt.executeUpdate(); | ||
} | ||
} | ||
conn.commit(); | ||
} | ||
|
||
try (Connection conn = DriverManager.getConnection(getUrl()); | ||
Statement stmt = conn.createStatement()) { | ||
stmt.setQueryTimeout(5); // 5 sec | ||
ResultSet rs = stmt.executeQuery(String.format("SELECT * FROM %s", | ||
tableName)); | ||
// Use custom EnvironmentEdge to timeout query with a longer delay in ms | ||
MyClock clock = new MyClock(10, 10000); | ||
EnvironmentEdgeManager.injectEdge(clock); | ||
try { | ||
rs.next(); | ||
fail(); | ||
} catch (SQLException e) { | ||
assertEquals(OPERATION_TIMED_OUT.getErrorCode(), | ||
e.getErrorCode()); | ||
assertTrue(e.getMessage().contains("Query couldn't be " + | ||
"completed in the allotted time: 5000 ms")); | ||
} | ||
} finally { | ||
EnvironmentEdgeManager.reset(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,8 +453,12 @@ public SQLException newException(SQLExceptionInfo info) { | |
OPERATION_TIMED_OUT(6000, "TIM01", "Operation timed out.", new Factory() { | ||
@Override | ||
public SQLException newException(SQLExceptionInfo info) { | ||
return new SQLTimeoutException(OPERATION_TIMED_OUT.getMessage(), | ||
OPERATION_TIMED_OUT.getSQLState(), OPERATION_TIMED_OUT.getErrorCode()); | ||
final String reason = info.getMessage() != null | ||
? info.getMessage() : OPERATION_TIMED_OUT.getMessage(); | ||
return new SQLTimeoutException(reason, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry missed this in my previous review.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
OPERATION_TIMED_OUT.getSQLState(), | ||
OPERATION_TIMED_OUT.getErrorCode(), | ||
info.getRootCause()); | ||
} | ||
}), | ||
FUNCTION_UNDEFINED(6001, "42F01", "Function undefined.", new Factory() { | ||
|
58 changes: 58 additions & 0 deletions
58
phoenix-core/src/test/java/org/apache/phoenix/util/SQLExceptionCodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.phoenix.util; | ||
|
||
import org.apache.phoenix.exception.SQLExceptionCode; | ||
import org.apache.phoenix.exception.SQLExceptionInfo; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.sql.SQLException; | ||
|
||
public class SQLExceptionCodeTest { | ||
|
||
@Test | ||
public void testOperationTimedOutTest() { | ||
SQLException sqlException = new SQLExceptionInfo | ||
.Builder(SQLExceptionCode.OPERATION_TIMED_OUT) | ||
.setMessage("Test Operation Timedout") | ||
.setRootCause(new IllegalArgumentException("TestOpsTimeout1")) | ||
.build().buildException(); | ||
Assert.assertEquals("Test Operation Timedout", | ||
sqlException.getMessage()); | ||
Assert.assertEquals("TestOpsTimeout1", | ||
sqlException.getCause().getMessage()); | ||
Assert.assertTrue(sqlException.getCause() instanceof | ||
IllegalArgumentException); | ||
Assert.assertEquals(sqlException.getErrorCode(), | ||
SQLExceptionCode.OPERATION_TIMED_OUT.getErrorCode()); | ||
Assert.assertEquals(sqlException.getSQLState(), | ||
SQLExceptionCode.OPERATION_TIMED_OUT.getSQLState()); | ||
sqlException = new SQLExceptionInfo | ||
.Builder(SQLExceptionCode.OPERATION_TIMED_OUT) | ||
.build().buildException(); | ||
Assert.assertEquals(SQLExceptionCode.OPERATION_TIMED_OUT.getMessage(), | ||
sqlException.getMessage()); | ||
Assert.assertNull(sqlException.getCause()); | ||
Assert.assertEquals(sqlException.getErrorCode(), | ||
SQLExceptionCode.OPERATION_TIMED_OUT.getErrorCode()); | ||
Assert.assertEquals(sqlException.getSQLState(), | ||
SQLExceptionCode.OPERATION_TIMED_OUT.getSQLState()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check that e.getCause() != null ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it is null in this case because for the test, rootCause is not provided here (BaseResultIterators):
However, I tested by manually adding rootCause and receiving on test side. Hence, it is working fine but since relevant source code does not add root cause,
e.getCause() != null
would not be true here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@virajjasani Can we just write a unit test creating OPERATION_TIMED_OUT exception with custom message and custom fake exception. Ideally we wouldn't even need integration test but the test that you wrote is very nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Let's keep both unit and IT.
Thanks