Skip to content

Commit

Permalink
fix(provider/cf): Ensure up-to-date data in server group caching agen…
Browse files Browse the repository at this point in the history
…t and force cache (#4560)
  • Loading branch information
ncknt committed May 1, 2020
1 parent 82bc466 commit 359411f
Showing 1 changed file with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,46 +145,50 @@ public List<CloudFoundryApplication> all() {
// if the update time doesn't match then we need to update the cache
// if the app is not found in the cache we need to process with `map` and update the cache
try {
forkJoinPool.submit(
() ->
newCloudFoundryAppList
.parallelStream()
.filter(
app -> {
CloudFoundryServerGroup cachedApp = findById(app.getGuid());
if (cachedApp != null) {
if (!cachedApp
.getUpdatedTime()
.equals(app.getUpdatedAt().toInstant().toEpochMilli())) {
log.trace(
"App '{}' cached version is out of date on foundation '{}'",
app.getName(),
this.account);
return true;
} else {
return false;
}
} else {
log.trace(
"App '{}' not found in cache for foundation '{}'",
app.getName(),
this.account);
return true;
}
})
.map(this::map)
.forEach(sg -> serverGroupCache.put(sg.getId(), sg)));

forkJoinPool.submit(
() ->
// execute health check on instances, set number of available instances and health
// status
newCloudFoundryAppList
.parallelStream()
.forEach(
a ->
serverGroupCache.put(
a.getGuid(), checkHealthStatus(findById(a.getGuid()), a))));
forkJoinPool
.submit(
() ->
newCloudFoundryAppList
.parallelStream()
.filter(
app -> {
CloudFoundryServerGroup cachedApp = findById(app.getGuid());
if (cachedApp != null) {
if (!cachedApp
.getUpdatedTime()
.equals(app.getUpdatedAt().toInstant().toEpochMilli())) {
log.trace(
"App '{}' cached version is out of date on foundation '{}'",
app.getName(),
this.account);
return true;
} else {
return false;
}
} else {
log.trace(
"App '{}' not found in cache for foundation '{}'",
app.getName(),
this.account);
return true;
}
})
.map(this::map)
.forEach(sg -> serverGroupCache.put(sg.getId(), sg)))
.get();

forkJoinPool
.submit(
() ->
// execute health check on instances, set number of available instances and health
// status
newCloudFoundryAppList
.parallelStream()
.forEach(
a ->
serverGroupCache.put(
a.getGuid(), checkHealthStatus(findById(a.getGuid()), a))))
.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -230,9 +234,11 @@ public List<CloudFoundryApplication> all() {

@Nullable
public CloudFoundryServerGroup findServerGroupByNameAndSpaceId(String name, String spaceId) {
return Optional.ofNullable(findServerGroupId(name, spaceId))
.flatMap(serverGroupId -> Optional.ofNullable(findById(serverGroupId)))
.orElse(null);
Optional<CloudFoundryServerGroup> result =
safelyCall(() -> api.all(null, 1, singletonList(name), singletonList(spaceId)))
.flatMap(page -> page.getResources().stream().findFirst().map(this::map));
result.ifPresent(sg -> serverGroupCache.put(sg.getId(), sg));
return result.orElse(null);
}

@Nullable
Expand Down

0 comments on commit 359411f

Please sign in to comment.