Skip to content

Commit

Permalink
Merge pull request #76 from ajordens/ItemDAO-health-checks
Browse files Browse the repository at this point in the history
Include each ItemDAO in the health check
  • Loading branch information
ajordens committed Mar 19, 2016
2 parents a871bd1 + b7ebdef commit 32951a1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.netflix.spinnaker.front50.model.application
import com.netflix.spinnaker.front50.exception.NotFoundException
import com.netflix.spinnaker.front50.model.ItemDAO

public interface ApplicationDAO extends ItemDAO<Application> {
public interface ApplicationDAO extends com.netflix.spinnaker.front50.model.ItemDAO<Application> {
Application findByName(String name) throws NotFoundException

Collection<Application> search(Map<String, String> attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class StrategyRepository implements PipelineStrategyDAO {

@Override
boolean isHealthy() {
return false
return true
}

List<Pipeline> getPipelinesByApplication(String application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public abstract class S3Support<T extends Timestamped> {
private final int refreshIntervalMs;
private final String bucket;

private long lastRefreshedTime;

protected final String rootFolder;

protected final AtomicReference<Set<T>> allItemsCache = new AtomicReference<>();
Expand Down Expand Up @@ -81,8 +83,11 @@ public Collection<T> all() {
return allItemsCache.get().stream().collect(Collectors.toList());
}

/**
* @return Healthy if refreshed in the past 45s
*/
public boolean isHealthy() {
return allItemsCache.get() != null;
return (System.currentTimeMillis() - lastRefreshedTime) < 45000 && allItemsCache.get() != null;
}

public T findById(String id) throws NotFoundException {
Expand All @@ -95,6 +100,7 @@ public T findById(String id) throws NotFoundException {
throw new IllegalStateException(e);
} catch (AmazonS3Exception e) {
if (e.getStatusCode() == 404) {
log.warn(String.format("No item found with id of %s", id.toLowerCase()));
throw new NotFoundException(String.format("No item found with id of %s", id.toLowerCase()));
}

Expand Down Expand Up @@ -214,6 +220,7 @@ protected Set<T> fetchAllItems(Set<T> existingItems) {
existingItemsByName.put(item.getId().toLowerCase(), item);
});

lastRefreshedTime = System.currentTimeMillis();
return existingItemsByName.values().stream().collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package com.netflix.spinnaker.front50.config

import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.front50.model.application.ApplicationDAO
import com.netflix.spinnaker.front50.model.pipeline.PipelineDAO
import com.netflix.spinnaker.front50.model.pipeline.PipelineStrategyDAO
import com.netflix.spinnaker.front50.model.project.ProjectDAO
import com.netflix.spinnaker.kork.web.interceptors.MetricsInterceptor
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
Expand All @@ -38,4 +43,24 @@ public class Front50WebConfig extends WebMvcConfigurerAdapter {
)
)
}

@Bean
ItemDAOHealthIndicator applicationDAOHealthIndicator(ApplicationDAO applicationDAO) {
return new ItemDAOHealthIndicator(itemDAO: applicationDAO)
}

@Bean
ItemDAOHealthIndicator projectDAOHealthIndicator(ProjectDAO projectDAO) {
return new ItemDAOHealthIndicator(itemDAO: projectDAO)
}

@Bean
ItemDAOHealthIndicator pipelineDAOHealthIndicator(PipelineDAO pipelineDAO) {
return new ItemDAOHealthIndicator(itemDAO: pipelineDAO)
}

@Bean
ItemDAOHealthIndicator pipelineStrategyDAOHealthIndicator(PipelineStrategyDAO pipelineStrategyDAO) {
return new ItemDAOHealthIndicator(itemDAO: pipelineStrategyDAO)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@

package com.netflix.spinnaker.front50.config

import com.netflix.spinnaker.front50.model.application.ApplicationDAO
import org.springframework.beans.factory.annotation.Autowired
import com.netflix.spinnaker.front50.model.ItemDAO
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.HealthIndicator
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

import java.util.concurrent.atomic.AtomicReference

@Component
public class ApplicationDAOProviderHealthIndicator implements HealthIndicator {
public class ItemDAOHealthIndicator implements HealthIndicator {

@Autowired
ApplicationDAO applicationDAO
ItemDAO itemDAO

private final AtomicReference<Health> lastHealth = new AtomicReference<>(null)

Expand All @@ -47,13 +43,13 @@ public class ApplicationDAOProviderHealthIndicator implements HealthIndicator {
def healthBuilder = new Health.Builder().up()

try {
if (applicationDAO.healthy) {
healthBuilder.withDetail(applicationDAO.class.simpleName, "Healthy")
if (itemDAO.healthy) {
healthBuilder.withDetail(itemDAO.class.simpleName, "Healthy")
} else {
healthBuilder.down().withDetail(applicationDAO.class.simpleName, "Unhealthy")
healthBuilder.down().withDetail(itemDAO.class.simpleName, "Unhealthy")
}
} catch (RuntimeException e) {
healthBuilder.down().withDetail(applicationDAO.class.simpleName, "Unhealthy: `${e.message}`" as String)
healthBuilder.down().withDetail(itemDAO.class.simpleName, "Unhealthy: `${e.message}`" as String)
}

lastHealth.set(healthBuilder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import org.springframework.boot.actuate.health.Status
import spock.lang.Shared
import spock.lang.Specification

class ApplicationDAOProviderHealthIndicatorSpec extends Specification {
class ItemDAOHealthIndicatorSpec extends Specification {
@Shared
ApplicationDAOProviderHealthIndicator healthCheck
ItemDAOHealthIndicator healthCheck

@Shared
ApplicationDAO dao

void setup() {
dao = Mock(ApplicationDAO)
healthCheck = new ApplicationDAOProviderHealthIndicator(applicationDAO: dao)
healthCheck = new ItemDAOHealthIndicator(itemDAO: dao)
}

void 'health check should return 5xx error if dao is not working'() {
Expand Down

0 comments on commit 32951a1

Please sign in to comment.