Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: for deadlock tests
Browse files Browse the repository at this point in the history
sattvikc committed Sep 15, 2023
1 parent bb7064a commit 8c13f45
Showing 2 changed files with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1414,39 +1414,75 @@ public static Map<String, List<String>> getTenantIdsForUserIds_transaction(Start
String[] userIds)
throws SQLException, StorageQueryException {
if (userIds != null && userIds.length > 0) {
StringBuilder QUERY = new StringBuilder("SELECT user_id, tenant_id "
+ "FROM " + Config.getConfig(start).getUsersTable());
QUERY.append(" WHERE user_id IN (");
for (int i = 0; i < userIds.length; i++) {
if (Start.isEnabledForDeadlockTesting()) {
assert (Start.isTesting);
StringBuilder QUERY = new StringBuilder("SELECT user_id, tenant_id "
+ "FROM " + Config.getConfig(start).getUsersTable());
QUERY.append(" WHERE user_id IN (");
for (int i = 0; i < userIds.length; i++) {

QUERY.append("?");
if (i != userIds.length - 1) {
// not the last element
QUERY.append(",");
QUERY.append("?");
if (i != userIds.length - 1) {
// not the last element
QUERY.append(",");
}
}
}
QUERY.append(") AND app_id = ?");
QUERY.append(")");

return execute(sqlCon, QUERY.toString(), pst -> {
for (int i = 0; i < userIds.length; i++) {
// i+1 cause this starts with 1 and not 0, and 1 is appId
pst.setString(i + 1, userIds[i]);
}
}, result -> {
Map<String, List<String>> finalResult = new HashMap<>();
for (String userId : userIds) {
finalResult.put(userId, new ArrayList<>());
}

while (result.next()) {
String userId = result.getString("user_id").trim();
String tenantId = result.getString("tenant_id");

return execute(sqlCon, QUERY.toString(), pst -> {
finalResult.get(userId).add(tenantId);
}
return finalResult;
});
} else {
StringBuilder QUERY = new StringBuilder("SELECT user_id, tenant_id "
+ "FROM " + Config.getConfig(start).getUsersTable());
QUERY.append(" WHERE user_id IN (");
for (int i = 0; i < userIds.length; i++) {
// i+1 cause this starts with 1 and not 0, and 1 is appId
pst.setString(i + 1, userIds[i]);
}
pst.setString(userIds.length + 1, appIdentifier.getAppId());
}, result -> {
Map<String, List<String>> finalResult = new HashMap<>();
for (String userId : userIds) {
finalResult.put(userId, new ArrayList<>());

QUERY.append("?");
if (i != userIds.length - 1) {
// not the last element
QUERY.append(",");
}
}
QUERY.append(") AND app_id = ?");

while (result.next()) {
String userId = result.getString("user_id").trim();
String tenantId = result.getString("tenant_id");
return execute(sqlCon, QUERY.toString(), pst -> {
for (int i = 0; i < userIds.length; i++) {
// i+1 cause this starts with 1 and not 0, and 1 is appId
pst.setString(i + 1, userIds[i]);
}
pst.setString(userIds.length + 1, appIdentifier.getAppId());
}, result -> {
Map<String, List<String>> finalResult = new HashMap<>();
for (String userId : userIds) {
finalResult.put(userId, new ArrayList<>());
}

finalResult.get(userId).add(tenantId);
}
return finalResult;
});
while (result.next()) {
String userId = result.getString("user_id").trim();
String tenantId = result.getString("tenant_id");

finalResult.get(userId).add(tenantId);
}
return finalResult;
});
}
}

return new HashMap<>();
Original file line number Diff line number Diff line change
@@ -684,18 +684,16 @@ public static List<LoginMethod> getUsersInfoUsingIdList(Start start, Set<String>
// we don't want the following query to be optimized while doing a deadlock testing
// so that we can ensure that the deadlock is happening and test that the deadlock recovery is working
// as expected
// TODO
String QUERY = "SELECT user_id, email, phone_number, time_joined "
+ "FROM " + getConfig(start).getPasswordlessUsersTable() + " WHERE user_id IN (" +
Utils.generateCommaSeperatedQuestionMarks(ids.size()) + ") AND app_id = ?";
Utils.generateCommaSeperatedQuestionMarks(ids.size()) + ")";

List<UserInfoPartial> userInfos = execute(start, QUERY, pst -> {
int index = 1;
for (String id : ids) {
pst.setString(index, id);
index++;
}
pst.setString(index, appIdentifier.getAppId());
}, result -> {
List<UserInfoPartial> finalResult = new ArrayList<>();
while (result.next()) {

0 comments on commit 8c13f45

Please sign in to comment.