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

Support searching for request logs with a specified date range via SingularityClient. #1962

Merged
merged 4 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
import com.hubspot.singularity.api.SingularityPauseRequest;
import com.hubspot.singularity.api.SingularityPriorityFreeze;
import com.hubspot.singularity.api.SingularityRunNowRequest;
import com.hubspot.singularity.api.SingularityS3SearchRequest;
import com.hubspot.singularity.api.SingularityS3SearchResult;
import com.hubspot.singularity.api.SingularityScaleRequest;
import com.hubspot.singularity.api.SingularityUnpauseRequest;
import com.hubspot.singularity.api.SingularityUpdateGroupsRequest;
Expand Down Expand Up @@ -199,6 +201,7 @@ public class SingularityClient {
private static final String SANDBOX_READ_FILE_FORMAT = SANDBOX_FORMAT + "/%s/read";

private static final String S3_LOG_FORMAT = "%s/logs";
private static final String S3_LOG_SEARCH_LOGS = S3_LOG_FORMAT + "/search";
private static final String S3_LOG_GET_TASK_LOGS = S3_LOG_FORMAT + "/task/%s";
private static final String S3_LOG_GET_REQUEST_LOGS = S3_LOG_FORMAT + "/request/%s";
private static final String S3_LOG_GET_DEPLOY_LOGS = S3_LOG_FORMAT + "/request/%s/deploy/%s";
Expand Down Expand Up @@ -1354,6 +1357,26 @@ public Optional<MesosFileChunkObject> readSandBoxFile(String taskId, String path
// S3 LOGS
//

/**
* Retrieve the list of logs stored in S3 based on a specified search request
*
* @param searchRequest
* The parameters upon which to base the search
* @return
* A result in the form of a {@link SingularityS3SearchResult}
*/
public SingularityS3SearchResult getLogs(SingularityS3SearchRequest searchRequest) {
final Function<String, String> requestUri = (host) -> String.format(S3_LOG_SEARCH_LOGS, getApiBase(host));

final Optional<SingularityS3SearchResult> maybeResult = post(requestUri, "S3 log search", Optional.of(searchRequest), Optional.of(SingularityS3SearchResult.class));

if (!maybeResult.isPresent()) {
throw new SingularityClientException("Singularity url not found", 404);
} else {
return maybeResult.get();
}
}

/**
* Retrieve the list of logs stored in S3 for a specific task
*
Expand Down Expand Up @@ -1381,11 +1404,24 @@ public Collection<SingularityS3Log> getTaskLogs(String taskId) {
* A collection of {@link SingularityS3Log}
*/
public Collection<SingularityS3Log> getRequestLogs(String requestId) {
return getRequestLogs(requestId, Optional.absent(), Optional.absent());
}

public Collection<SingularityS3Log> getRequestLogs(String requestId, Optional<Long> start, Optional<Long> end) {
final Function<String, String> requestUri = (host) -> String.format(S3_LOG_GET_REQUEST_LOGS, getApiBase(host), requestId);

final String type = String.format("S3 logs for request %s", requestId);

return getCollection(requestUri, type, S3_LOG_COLLECTION);
Map<String, Object> dateRange = new HashMap<>();
if (start.isPresent()) {
dateRange.put("start", start.get());
}

if (end.isPresent()) {
dateRange.put("end", end.get());
}

return getCollectionWithParams(requestUri, type, Optional.of(dateRange), S3_LOG_COLLECTION);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,12 @@ private boolean isCurrentDeploy(String requestId, String deployId) {
private Collection<String> getS3PrefixesForRequest(S3Configuration s3Configuration, String requestId, Optional<Long> startArg, Optional<Long> endArg, String group) {
Optional<SingularityRequestHistory> firstHistory = requestHistoryHelper.getFirstHistory(requestId);

checkNotFound(firstHistory.isPresent(), "No request history found for %s", requestId);

long start = firstHistory.get().getCreatedAt();
if (startArg.isPresent()) {
start = Math.max(startArg.get(), start);
if (!startArg.isPresent()) {
checkBadRequest(firstHistory.isPresent(), "No request history found for %s. A start time must be specified.", requestId);
}

long start = Math.max(startArg.or(0L), firstHistory.get().getCreatedAt());

Optional<SingularityRequestHistory> lastHistory = requestHistoryHelper.getLastHistory(requestId);

long end = System.currentTimeMillis();
Expand Down