use of com.hubspot.singularity.SingularityRequestHistory in project Singularity by HubSpot.
the class BlendedHistoryTest method testBlendedRequestHistory.
// DESCENDING
@Test
public void testBlendedRequestHistory() {
HistoryManager hm = mock(HistoryManager.class);
String rid = "rid";
request = new SingularityRequestBuilder(rid, RequestType.WORKER).build();
RequestHistoryHelper rhh = new RequestHistoryHelper(requestManager, hm);
mockRequestHistory(hm, Collections.<SingularityRequestHistory>emptyList());
Assert.assertTrue(rhh.getBlendedHistory(rid, 0, 100).isEmpty());
Assert.assertTrue(!rhh.getFirstHistory(rid).isPresent());
Assert.assertTrue(!rhh.getLastHistory(rid).isPresent());
mockRequestHistory(hm, Arrays.asList(makeHistory(52, RequestHistoryType.EXITED_COOLDOWN), makeHistory(51, RequestHistoryType.ENTERED_COOLDOWN), makeHistory(50, RequestHistoryType.CREATED)));
List<SingularityRequestHistory> history = rhh.getBlendedHistory(rid, 0, 5);
Assert.assertTrue(history.size() == 3);
saveHistory(100, RequestHistoryType.DELETED);
saveHistory(120, RequestHistoryType.CREATED);
history = rhh.getBlendedHistory(rid, 0, 5);
Assert.assertTrue(history.size() == 5);
Assert.assertTrue(history.get(0).getCreatedAt() == 120);
Assert.assertTrue(history.get(4).getCreatedAt() == 50);
history = rhh.getBlendedHistory(rid, 1, 5);
Assert.assertTrue(history.size() == 4);
Assert.assertTrue(history.get(0).getCreatedAt() == 100);
Assert.assertTrue(history.get(3).getCreatedAt() == 50);
history = rhh.getBlendedHistory(rid, 2, 5);
Assert.assertTrue(history.size() == 3);
Assert.assertTrue(history.get(0).getCreatedAt() == 52);
Assert.assertTrue(history.get(2).getCreatedAt() == 50);
mockRequestHistory(hm, Collections.<SingularityRequestHistory>emptyList());
history = rhh.getBlendedHistory(rid, 3, 5);
Assert.assertTrue(history.isEmpty());
history = rhh.getBlendedHistory(rid, 1, 5);
Assert.assertTrue(history.size() == 1);
Assert.assertTrue(history.get(0).getCreatedAt() == 100);
Assert.assertTrue(rhh.getFirstHistory(rid).get().getCreatedAt() == 100);
Assert.assertTrue(rhh.getLastHistory(rid).get().getCreatedAt() == 120);
mockRequestHistory(hm, Arrays.asList(makeHistory(1, RequestHistoryType.EXITED_COOLDOWN)));
Assert.assertTrue(rhh.getFirstHistory(rid).get().getCreatedAt() == 1);
Assert.assertTrue(rhh.getLastHistory(rid).get().getCreatedAt() == 120);
}
use of com.hubspot.singularity.SingularityRequestHistory in project Singularity by HubSpot.
the class SingularitySchedulerTest method itCorrectlyUpdatesRequestDeletingStateHistory.
@Test
public void itCorrectlyUpdatesRequestDeletingStateHistory() {
initRequest();
Assert.assertEquals(RequestState.ACTIVE, requestManager.getRequest(requestId).get().getState());
Assert.assertEquals(1, requestManager.getRequestHistory(requestId).size());
requestManager.startDeletingRequest(request, Optional.absent(), Optional.<String>absent(), Optional.<String>absent(), Optional.of("the cake is a lie"));
Assert.assertEquals(RequestState.DELETING, requestManager.getRequest(requestId).get().getState());
Assert.assertEquals(2, requestManager.getRequestHistory(requestId).size());
cleaner.drainCleanupQueue();
Assert.assertEquals(3, requestManager.getRequestHistory(requestId).size());
List<RequestHistoryType> historyTypes = new ArrayList<>();
for (SingularityRequestHistory request : requestManager.getRequestHistory(requestId)) {
historyTypes.add(request.getEventType());
}
Assert.assertTrue(historyTypes.contains(RequestHistoryType.CREATED));
Assert.assertTrue(historyTypes.contains(RequestHistoryType.DELETING));
Assert.assertTrue(historyTypes.contains(RequestHistoryType.DELETED));
}
use of com.hubspot.singularity.SingularityRequestHistory in project Singularity by HubSpot.
the class RequestManager method save.
public SingularityCreateResult save(SingularityRequest request, RequestState state, RequestHistoryType eventType, long timestamp, Optional<String> user, Optional<String> message) {
saveHistory(new SingularityRequestHistory(timestamp, user, eventType, request, message));
leaderCache.putRequest(new SingularityRequestWithState(request, state, timestamp));
return save(getRequestPath(request.getId()), new SingularityRequestWithState(request, state, timestamp), requestTranscoder);
}
use of com.hubspot.singularity.SingularityRequestHistory in project Singularity by HubSpot.
the class RequestHelper method fillDataForRequestsAndFilter.
public List<SingularityRequestParent> fillDataForRequestsAndFilter(List<SingularityRequestWithState> requests, SingularityUser user, boolean filterRelevantForUser, boolean includeFullRequestData, Optional<Integer> limit, List<RequestType> requestTypeFilters) {
final Map<String, Optional<SingularityTaskIdHistory>> mostRecentTasks = new ConcurrentHashMap<>();
final Map<String, SingularityRequestDeployState> deployStates = deployManager.getRequestDeployStatesByRequestIds(requests.stream().map((r) -> r.getRequest().getId()).collect(Collectors.toList()));
final Map<String, Optional<SingularityRequestHistory>> requestIdToLastHistory;
if (includeFullRequestData) {
requestIdToLastHistory = requests.parallelStream().collect(Collectors.toMap((r) -> r.getRequest().getId(), (r) -> getMostRecentHistoryFromZk(r.getRequest().getId())));
} else {
requestIdToLastHistory = Collections.emptyMap();
}
Optional<SingularityUserSettings> maybeUserSettings = userManager.getUserSettings(user.getId());
return requests.parallelStream().filter((request) -> {
if (!requestTypeFilters.isEmpty() && !requestTypeFilters.contains(request.getRequest().getRequestType())) {
return false;
}
if (!filterRelevantForUser || user.equals(SingularityUser.DEFAULT_USER)) {
return true;
}
String requestId = request.getRequest().getId();
if (maybeUserSettings.isPresent() && maybeUserSettings.get().getStarredRequestIds().contains(requestId)) {
// This is a starred request for the user
return true;
}
if (request.getRequest().getGroup().isPresent() && user.getGroups().contains(request.getRequest().getGroup().get())) {
// The user is in the group for this request
return true;
}
if (includeFullRequestData) {
if (userModifiedRequestLast(requestIdToLastHistory.getOrDefault(requestId, Optional.absent()), user)) {
return true;
}
}
return userAssociatedWithDeploy(Optional.fromNullable(deployStates.get(requestId)), user);
}).map((request) -> {
Long lastActionTime = null;
if (includeFullRequestData) {
lastActionTime = getLastActionTimeForRequest(request.getRequest(), requestIdToLastHistory.getOrDefault(request.getRequest().getId(), Optional.absent()), Optional.fromNullable(deployStates.get(request.getRequest().getId())), mostRecentTasks.computeIfAbsent(request.getRequest().getId(), (id) -> getMostRecentTask(request.getRequest())));
} else {
// To save on zk calls, if not returning all data, use the most recent deploy timestamps
Optional<SingularityRequestDeployState> deployState = Optional.fromNullable(deployStates.get(request.getRequest().getId()));
if (deployState.isPresent()) {
if (deployState.get().getPendingDeploy().isPresent()) {
lastActionTime = deployState.get().getPendingDeploy().get().getTimestamp();
}
if (deployState.get().getActiveDeploy().isPresent()) {
lastActionTime = deployState.get().getActiveDeploy().get().getTimestamp();
}
}
if (lastActionTime == null) {
lastActionTime = 0L;
}
}
return new RequestParentWithLastActionTime(request, lastActionTime, maybeUserSettings.isPresent() && maybeUserSettings.get().getStarredRequestIds().contains(request.getRequest().getId()));
}).sorted().limit(limit.or(requests.size())).map((parentWithActionTime) -> {
SingularityRequestWithState requestWithState = parentWithActionTime.getRequestWithState();
if (includeFullRequestData) {
CompletableFuture<Optional<SingularityTaskIdsByStatus>> maybeTaskIdsByStatus = CompletableFuture.supplyAsync(() -> getTaskIdsByStatusForRequest(requestWithState)).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringBounce>> maybeExpiringBounce = CompletableFuture.supplyAsync(() -> requestManager.getExpiringBounce(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringPause>> maybeExpiringPause = CompletableFuture.supplyAsync(() -> requestManager.getExpiringPause(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringScale>> maybeExpiringScale = CompletableFuture.supplyAsync(() -> requestManager.getExpiringScale(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringSkipHealthchecks>> maybeExpiringSkipHealthchecks = CompletableFuture.supplyAsync(() -> requestManager.getExpiringSkipHealthchecks(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
return new SingularityRequestParent(requestWithState.getRequest(), requestWithState.getState(), Optional.fromNullable(deployStates.get(requestWithState.getRequest().getId())), // full deploy data not provided
Optional.absent(), // full deploy data not provided
Optional.absent(), // full deploy data not provided
Optional.absent(), maybeExpiringBounce.join(), maybeExpiringPause.join(), maybeExpiringScale.join(), maybeExpiringSkipHealthchecks.join(), maybeTaskIdsByStatus.join(), requestIdToLastHistory.getOrDefault(requestWithState.getRequest().getId(), Optional.absent()), mostRecentTasks.computeIfAbsent(requestWithState.getRequest().getId(), (id) -> getMostRecentTask(requestWithState.getRequest())));
} else {
return new SingularityRequestParent(requestWithState.getRequest(), requestWithState.getState(), Optional.fromNullable(deployStates.get(requestWithState.getRequest().getId())), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent());
}
}).collect(Collectors.toList());
}
use of com.hubspot.singularity.SingularityRequestHistory in project Singularity by HubSpot.
the class SingularityWebhookSender method checkRequestUpdates.
private int checkRequestUpdates(SingularityWebhook webhook, List<CompletableFuture<Response>> webhookFutures) {
final List<SingularityRequestHistory> requestUpdates = webhookManager.getQueuedRequestHistoryForHook(webhook.getId());
int numRequestUpdates = 0;
for (SingularityRequestHistory requestUpdate : requestUpdates) {
webhookFutures.add(webhookSemaphore.call(() -> executeWebhookAsync(webhook, requestUpdate, new SingularityRequestWebhookAsyncHandler(webhookManager, webhook, requestUpdate, shouldDeleteUpdateOnFailure(numRequestUpdates, requestUpdate.getCreatedAt())))));
}
return requestUpdates.size();
}
Aggregations