-
Notifications
You must be signed in to change notification settings - Fork 34
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 #289: Add alternative execution for counting queries in lock service #292
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
.classpath | ||
.project | ||
.settings/ | ||
/build |
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
|
||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LockServiceCassandra extends StandardLockService { | ||
|
||
|
@@ -62,19 +64,6 @@ public boolean acquireLock() throws LockException { | |
// another node was faster | ||
return false; | ||
} | ||
if ((rowsUpdated == -1) && (database instanceof MSSQLDatabase)) { | ||
|
||
Scope.getCurrentScope().getLog(this.getClass()).info("Database did not return a proper row count (Might have NOCOUNT enabled)"); | ||
database.rollback(); | ||
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql( | ||
new LockDatabaseChangeLogStatement(), database | ||
); | ||
if (sql.length != 1) { | ||
throw new UnexpectedLiquibaseException("Did not expect " + sql.length + " statements"); | ||
} | ||
rowsUpdated = executor.update(new RawSqlStatement("EXEC sp_executesql N'SET NOCOUNT OFF " + | ||
sql[0].toSql().replace("'", "''") + "'")); | ||
} | ||
if (rowsUpdated > 1) { | ||
throw new LockException("Did not update change log lock correctly"); | ||
} | ||
|
@@ -142,11 +131,11 @@ public boolean isDatabaseChangeLogLockTableCreated() { | |
boolean hasChangeLogLockTable; | ||
try { | ||
Statement statement = ((CassandraDatabase) database).getStatement(); | ||
statement.executeQuery("SELECT ID from " + getChangeLogLockTableName()); | ||
statement.executeQuery("SELECT ID FROM " + getChangeLogLockTableName()); | ||
|
||
statement.close(); | ||
hasChangeLogLockTable = true; | ||
} catch (SQLException e) { | ||
Scope.getCurrentScope().getLog(getClass()).info("No " + getChangeLogLockTableName() + " available in cassandra."); | ||
Scope.getCurrentScope().getLog(getClass()).info("No " + getChangeLogLockTableName() + " available in Cassandra."); | ||
hasChangeLogLockTable = false; | ||
} catch (DatabaseException e) { | ||
e.printStackTrace(); | ||
|
@@ -163,9 +152,8 @@ public boolean isDatabaseChangeLogLockTableInitialized(final boolean tableJustCr | |
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database); | ||
|
||
try { | ||
isDatabaseChangeLogLockTableInitialized = executor.queryForInt( | ||
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogLockTableName()) | ||
) > 0; | ||
isDatabaseChangeLogLockTableInitialized = executeCountQueryWithAlternative(executor, | ||
"SELECT COUNT(*) FROM " + getChangeLogLockTableName()) > 0; | ||
} catch (LiquibaseException e) { | ||
if (executor.updatesDatabase()) { | ||
throw new UnexpectedLiquibaseException(e); | ||
|
@@ -185,10 +173,9 @@ private boolean isLocked(Executor executor) throws DatabaseException { | |
|
||
private boolean isLockedByCurrentInstance(Executor executor) throws DatabaseException { | ||
final String lockedBy = NetUtil.getLocalHostName() + " (" + NetUtil.getLocalHostAddress() + ")"; | ||
return executor.queryForInt( | ||
new RawSqlStatement("SELECT COUNT(*) FROM " + getChangeLogLockTableName() + " where " + | ||
"LOCKED = TRUE AND LOCKEDBY = '" + lockedBy + "' ALLOW FILTERING") | ||
) > 0; | ||
return executeCountQueryWithAlternative(executor, | ||
"SELECT COUNT(*) FROM " + getChangeLogLockTableName() + " WHERE " + | ||
"LOCKED = TRUE AND LOCKEDBY = '" + lockedBy + "' ALLOW FILTERING") > 0; | ||
} | ||
|
||
private String getChangeLogLockTableName() { | ||
|
@@ -198,4 +185,21 @@ private String getChangeLogLockTableName() { | |
return database.getDatabaseChangeLogLockTableName(); | ||
} | ||
} | ||
|
||
private int executeCountQueryWithAlternative(final Executor executor, final String query) throws DatabaseException { | ||
if (!query.contains("SELECT COUNT(*)")) { | ||
throw new UnexpectedLiquibaseException("Invalid count query: " + query); | ||
} | ||
try { | ||
return executor.queryForInt(new RawSqlStatement(query)); | ||
} catch (DatabaseException e) { | ||
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. Nice workaround! |
||
// If the count query failed (for example, because counting rows is not implemented - see issue #289 with | ||
// AWS Keyspaces where aggregate functions like COUNT are not supported: | ||
// https://docs.aws.amazon.com/keyspaces/latest/devguide/cassandra-apis.html#cassandra-functions), try to | ||
// execute the same query without the COUNT function then programmatically count returned rows. | ||
final String altQuery = query.replace("SELECT COUNT(*)", "SELECT *"); | ||
final List<Map<String, ?>> rows = executor.queryForList(new RawSqlStatement(altQuery)); | ||
return rows.size(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Thanks for removing the trash here.