Skip to content

Commit

Permalink
fix(roles): Introduce cache / short refresh around clouddriver calls (#…
Browse files Browse the repository at this point in the history
…243)

Cuts down on the number of calls to clouddriver for metadata that does
not change all that frequently.

Noticed a fairly high volume of calls falling through from x509
authentication attempts, more than seemed reasonable.
  • Loading branch information
ajordens authored Jul 2, 2018
1 parent 452bc21 commit 9f364c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ProviderHealthTracker {
private final long maximumStalenessTimeMs;
private AtomicLong lastSuccessfulUpdateTimeMs = new AtomicLong(-1);

@Autowired
public ProviderHealthTracker(long maximumStalenessTimeMs) {
this.maximumStalenessTimeMs = maximumStalenessTimeMs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -36,9 +37,6 @@
*/
@Slf4j
public class ClouddriverService implements HealthTrackable, InitializingBean {

private static final String GROUP_KEY = "clouddriverService";

private final ClouddriverApi clouddriverApi;

@Autowired
Expand All @@ -53,52 +51,37 @@ public ClouddriverService(ClouddriverApi clouddriverApi) {
}

@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
try {
getAccounts();
getApplications();
refreshAccounts();
refreshApplications();
} catch (Exception e) {
log.warn("Cache initialization failed: ", e);
}
}

public List<Account> getAccounts() {
return new SimpleJava8HystrixCommand<>(
GROUP_KEY,
"getAccounts",
() -> {
accountCache.set(clouddriverApi.getAccounts());
healthTracker.success();
return accountCache.get();
},
(Throwable cause) -> {
logFallback("account", cause);
List<Account> accounts = accountCache.get();
if (accounts == null) {
throw new HystrixBadRequestException("Clouddriver is unavailable", cause);
}
return accounts;
}).execute();
return accountCache.get();
}

public List<Application> getApplications() {
return new SimpleJava8HystrixCommand<>(
GROUP_KEY,
"getApplications",
() -> {
applicationCache.set(clouddriverApi.getApplications());
healthTracker.success();
return applicationCache.get();
},
(Throwable cause) -> {
logFallback("application", cause);
List<Application> applications = applicationCache.get();
if (applications == null) {
throw new HystrixBadRequestException("Clouddriver is unavailable", cause);
}
return applications;
})
.execute();
return applicationCache.get();
}

@Scheduled(fixedDelayString = "${fiat.clouddriverRefreshMs:30000}")
public void refreshAccounts() {
accountCache.set(
clouddriverApi.getAccounts()
);
healthTracker.success();
}

@Scheduled(fixedDelayString = "${fiat.clouddriverRefreshMs:30000}")
public void refreshApplications() {
applicationCache.set(
clouddriverApi.getApplications()
);
healthTracker.success();
}

private static void logFallback(String resource, Throwable cause) {
Expand Down

0 comments on commit 9f364c4

Please sign in to comment.