Skip to content

Commit

Permalink
fix(roles): Better logging (and metrics) if a user roles sync fails (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajordens authored Apr 11, 2019
1 parent 2734743 commit 9645925
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.spectator.api.Gauge;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.fiat.config.ResourceProvidersHealthIndicator;
import com.netflix.spinnaker.fiat.config.UnrestrictedResourceConfig;
import com.netflix.spinnaker.fiat.model.UserPermission;
Expand Down Expand Up @@ -71,8 +73,11 @@ public class UserRolesSyncer implements ApplicationListener<RemoteStatusChangedE

private final AtomicBoolean isEnabled;

private final Gauge userRolesSyncCount;

@Autowired
public UserRolesSyncer(Optional<DiscoveryClient> discoveryClient,
Registry registry,
LockManager lockManager,
PermissionsRepository permissionsRepository,
PermissionsResolver permissionsResolver,
Expand All @@ -99,6 +104,8 @@ public UserRolesSyncer(Optional<DiscoveryClient> discoveryClient,
// default to enabled iff discovery is not available
!discoveryClient.isPresent()
);

this.userRolesSyncCount = registry.gauge("fiat.userRoles.syncCount");
}

@Override
Expand All @@ -109,6 +116,7 @@ public void onApplicationEvent(RemoteStatusChangedEvent event) {
@Scheduled(fixedDelay = 30000L)
public void schedule() {
if (syncDelayMs < 0 || !isEnabled.get()) {
log.warn("User roles syncing is disabled (syncDelayMs: {}, isEnabled: {})", syncDelayMs, isEnabled.get());
return;
}

Expand All @@ -118,7 +126,14 @@ public void schedule() {
.withSuccessInterval(Duration.ofMillis(syncDelayMs))
.withFailureInterval(Duration.ofMillis(syncFailureDelayMs));

lockManager.acquireLock(lockOptions, () -> this.syncAndReturn(new ArrayList<>()));
lockManager.acquireLock(lockOptions, () -> {
try {
userRolesSyncCount.set(this.syncAndReturn(new ArrayList<>()));
} catch (Exception e) {
log.error("User roles synchronization failed", e);
userRolesSyncCount.set(-1);
}
});
}

public long syncAndReturn(List<String> roles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.appinfo.InstanceInfo.InstanceStatus
import com.netflix.discovery.DiscoveryClient
import com.netflix.spectator.api.NoopRegistry
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.fiat.config.ResourceProvidersHealthIndicator
import com.netflix.spinnaker.fiat.config.UnrestrictedResourceConfig
import com.netflix.spinnaker.fiat.model.UserPermission
Expand Down Expand Up @@ -47,6 +49,9 @@ class UserRolesSyncerSpec extends Specification {

private static final String UNRESTRICTED = UnrestrictedResourceConfig.UNRESTRICTED_USERNAME;

@Shared
Registry registry = new NoopRegistry()

@Shared
@AutoCleanup("destroy")
EmbeddedRedis embeddedRedis
Expand Down Expand Up @@ -133,6 +138,7 @@ class UserRolesSyncerSpec extends Specification {
@Subject
def syncer = new UserRolesSyncer(
Optional.ofNullable(null),
registry,
lockManager,
repo,
permissionsResolver,
Expand Down Expand Up @@ -205,6 +211,7 @@ class UserRolesSyncerSpec extends Specification {
def lockManager = Mock(LockManager)
def userRolesSyncer = new UserRolesSyncer(
Optional.ofNullable(discoveryClient),
registry,
lockManager,
null,
null,
Expand Down

0 comments on commit 9645925

Please sign in to comment.