-
Notifications
You must be signed in to change notification settings - Fork 188
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
Add postgres support for JDBIHistory #1844
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
941d687
* adds migrations (single changeset) for PG
mikebell90 56db901
Fix unit tests by allowing h2 to be used as a database driver class. …
mikebell90 6d1a8e1
* Fix documentation references to databases (made more generic with s…
mikebell90 1866365
swap limit/offset
ssalinas 633ba9e
Merge pull request #3 from HubSpot/fix_limit_offset
mikebell90 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
153 changes: 153 additions & 0 deletions
153
...larityService/src/main/java/com/hubspot/singularity/data/history/AbstractHistoryJDBI.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,153 @@ | ||
package com.hubspot.singularity.data.history; | ||
|
||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.google.common.base.Optional; | ||
import com.hubspot.singularity.ExtendedTaskState; | ||
import com.hubspot.singularity.OrderDirection; | ||
import com.hubspot.singularity.SingularityTaskIdHistory; | ||
|
||
import org.skife.jdbi.v2.Query; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
// Common code for DB queries | ||
public abstract class AbstractHistoryJDBI implements HistoryJDBI { | ||
protected static final Logger LOG = LoggerFactory.getLogger(HistoryJDBI.class); | ||
|
||
private static final String GET_TASK_ID_HISTORY_QUERY = "SELECT taskId, requestId, updatedAt, lastTaskStatus, runId FROM taskHistory"; | ||
private static final String GET_TASK_ID_HISTORY_COUNT_QUERY = "SELECT COUNT(*) FROM taskHistory"; | ||
|
||
protected void addWhereOrAnd(StringBuilder sqlBuilder, boolean shouldUseWhere) { | ||
if (shouldUseWhere) { | ||
sqlBuilder.append(" WHERE "); | ||
} else { | ||
sqlBuilder.append(" AND "); | ||
} | ||
} | ||
|
||
protected void applyTaskIdHistoryBaseQuery(StringBuilder sqlBuilder, Map<String, Object> binds, Optional<String> requestId, Optional<String> deployId, Optional<String> runId, Optional<String> host, | ||
Optional<ExtendedTaskState> lastTaskStatus, Optional<Long> startedBefore, Optional<Long> startedAfter, Optional<Long> updatedBefore, | ||
Optional<Long> updatedAfter) { | ||
if (requestId.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("requestId = :requestId"); | ||
binds.put("requestId", requestId.get()); | ||
} | ||
|
||
if (deployId.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("deployId = :deployId"); | ||
binds.put("deployId", deployId.get()); | ||
} | ||
|
||
if (runId.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("runId = :runId"); | ||
binds.put("runId", runId.get()); | ||
} | ||
|
||
if (host.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("host = :host"); | ||
binds.put("host", host.get()); | ||
} | ||
|
||
if (lastTaskStatus.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("lastTaskStatus = :lastTaskStatus"); | ||
binds.put("lastTaskStatus", lastTaskStatus.get().name()); | ||
} | ||
|
||
if (startedBefore.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("startedAt < :startedBefore"); | ||
binds.put("startedBefore", new Date(startedBefore.get())); | ||
} | ||
|
||
if (startedAfter.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("startedAt > :startedAfter"); | ||
binds.put("startedAfter", new Date(startedAfter.get())); | ||
} | ||
|
||
if (updatedBefore.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("updatedAt < :updatedBefore"); | ||
binds.put("updatedBefore", new Date(updatedBefore.get())); | ||
} | ||
|
||
if (updatedAfter.isPresent()) { | ||
addWhereOrAnd(sqlBuilder, binds.isEmpty()); | ||
sqlBuilder.append("updatedAt > :updatedAfter"); | ||
binds.put("updatedAfter", new Date(updatedAfter.get())); | ||
} | ||
} | ||
|
||
@Override | ||
public List<SingularityTaskIdHistory> getTaskIdHistory(Optional<String> requestId, Optional<String> deployId, Optional<String> runId, Optional<String> host, | ||
Optional<ExtendedTaskState> lastTaskStatus, Optional<Long> startedBefore, Optional<Long> startedAfter, Optional<Long> updatedBefore, | ||
Optional<Long> updatedAfter, Optional<OrderDirection> orderDirection, Optional<Integer> limitStart, Integer limitCount) { | ||
|
||
final Map<String, Object> binds = new HashMap<>(); | ||
final StringBuilder sqlBuilder = new StringBuilder(GET_TASK_ID_HISTORY_QUERY); | ||
|
||
applyTaskIdHistoryBaseQuery(sqlBuilder, binds, requestId, deployId, runId, host, lastTaskStatus, startedBefore, startedAfter, updatedBefore, updatedAfter); | ||
|
||
sqlBuilder.append(" ORDER BY startedAt "); | ||
sqlBuilder.append(orderDirection.or(OrderDirection.DESC).name()); | ||
|
||
if (!requestId.isPresent()) { | ||
sqlBuilder.append(", requestId "); | ||
sqlBuilder.append(orderDirection.or(OrderDirection.DESC).name()); | ||
} | ||
|
||
// NOTE: PG, MySQL are both compatible with OFFSET LIMIT syntax, while only MySQL understands LIMIT offset, limit. | ||
if (limitStart.isPresent()) { | ||
sqlBuilder.append(" OFFSET :limitStart "); | ||
binds.put("limitStart", limitStart.get()); | ||
} | ||
|
||
if (limitCount != null ){ | ||
sqlBuilder.append(" LIMIT :limitCount"); | ||
binds.put("limitCount", limitCount); | ||
} | ||
|
||
final String sql = sqlBuilder.toString(); | ||
|
||
LOG.trace("Generated sql for task search: {}, binds: {}", sql, binds); | ||
|
||
final Query<SingularityTaskIdHistory> query = getHandle().createQuery(sql).mapTo(SingularityTaskIdHistory.class); | ||
for (Map.Entry<String, Object> entry : binds.entrySet()) { | ||
query.bind(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return query.list(); | ||
} | ||
|
||
@Override | ||
public int getTaskIdHistoryCount(Optional<String> requestId, Optional<String> deployId, Optional<String> runId, Optional<String> host, | ||
Optional<ExtendedTaskState> lastTaskStatus, Optional<Long> startedBefore, Optional<Long> startedAfter, Optional<Long> updatedBefore, | ||
Optional<Long> updatedAfter) { | ||
|
||
final Map<String, Object> binds = new HashMap<>(); | ||
final StringBuilder sqlBuilder = new StringBuilder(GET_TASK_ID_HISTORY_COUNT_QUERY); | ||
|
||
applyTaskIdHistoryBaseQuery(sqlBuilder, binds, requestId, deployId, runId, host, lastTaskStatus, startedBefore, startedAfter, updatedBefore, updatedAfter); | ||
|
||
final String sql = sqlBuilder.toString(); | ||
|
||
LOG.trace("Generated sql for task search count: {}, binds: {}", sql, binds); | ||
|
||
final Query<Integer> query = getHandle().createQuery(sql).mapTo(Integer.class); | ||
for (Map.Entry<String, Object> entry : binds.entrySet()) { | ||
query.bind(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return query.first(); | ||
} | ||
|
||
} |
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.
Only change in common code - flagging so you notice