Skip to content

Commit

Permalink
fix(build): migrate to HealthContributorRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgogerly committed Oct 18, 2022
1 parent bf3541f commit 3fbabfa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2022 JPMorgan Chase & Co
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.kayenta.config;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.actuate.health.*;

public class OrcaCompositeHealthContributor implements CompositeHealthContributor {

private final StatusAggregator statusAggregator;
private final Map<String, NamedContributor<HealthContributor>> contributors;

public OrcaCompositeHealthContributor(
StatusAggregator statusAggregator, HealthContributorRegistry healthContributorRegistry) {
this.statusAggregator = statusAggregator;

this.contributors = new LinkedHashMap<>();
healthContributorRegistry.forEach(
contributor ->
contributors.put(
contributor.getName(),
NamedContributor.of(contributor.getName(), contributor.getContributor())));
}

@Override
public HealthContributor getContributor(String name) {
return contributors.get(name).getContributor();
}

@Override
public Stream<NamedContributor<HealthContributor>> stream() {
return CompositeHealthContributor.super.stream();
}

@NotNull
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
return contributors.values().iterator();
}

@Override
public void forEach(Consumer<? super NamedContributor<HealthContributor>> action) {
CompositeHealthContributor.super.forEach(action);
}

@Override
public Spliterator<NamedContributor<HealthContributor>> spliterator() {
return CompositeHealthContributor.super.spliterator();
}

public Status status() {
Set<Status> statuses =
this.contributors.values().stream()
.map(contributor -> ((HealthIndicator) contributor.getContributor()).getHealth(false))
.map(Health::getStatus)
.collect(Collectors.toSet());

return this.statusAggregator.getAggregateStatus(statuses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType.PIPELINE;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.kayenta.config.OrcaCompositeHealthContributor;
import com.netflix.spinnaker.kork.discovery.DiscoveryStatusChangeEvent;
import com.netflix.spinnaker.kork.discovery.RemoteStatusChangedEvent;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
Expand All @@ -31,12 +32,9 @@
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.actuate.health.StatusAggregator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -57,7 +55,7 @@ public class PipelineController {
private final ExecutionRepository executionRepository;
private final ObjectMapper kayentaObjectMapper;
private final ConfigurableApplicationContext context;
private final HealthIndicator healthIndicator;
private final OrcaCompositeHealthContributor orcaCompositeHealthContributor;
private final ScheduledAnnotationBeanPostProcessor postProcessor;
private Boolean upAtLeastOnce = false;

Expand All @@ -67,22 +65,24 @@ public PipelineController(
ExecutionRepository executionRepository,
ObjectMapper kayentaObjectMapper,
ConfigurableApplicationContext context,
HealthIndicatorRegistry healthIndicators,
HealthAggregator healthAggregator,
HealthContributorRegistry healthContributorRegistry,
StatusAggregator statusAggregator,
ScheduledAnnotationBeanPostProcessor postProcessor) {
this.executionLauncher = executionLauncher;
this.executionRepository = executionRepository;
this.kayentaObjectMapper = kayentaObjectMapper;
this.context = context;
this.healthIndicator = new CompositeHealthIndicator(healthAggregator, healthIndicators);
this.orcaCompositeHealthContributor =
new OrcaCompositeHealthContributor(statusAggregator, healthContributorRegistry);
this.postProcessor = postProcessor;
}

// TODO(duftler): Expose /inservice and /outofservice endpoints.
@Scheduled(initialDelay = 10000, fixedDelay = 5000)
void startOrcaQueueProcessing() {
if (!upAtLeastOnce) {
Health health = healthIndicator.health();
if (health.getStatus() == Status.UP) {
Status status = orcaCompositeHealthContributor.status();
if (status == Status.UP) {
upAtLeastOnce = true;
context.publishEvent(
new RemoteStatusChangedEvent(new DiscoveryStatusChangeEvent(STARTING, UP)));
Expand All @@ -92,7 +92,7 @@ void startOrcaQueueProcessing() {
} else {
log.warn(
"Health indicators are still reporting DOWN; not starting orca queue processing yet: {}",
health);
status);
}
}
}
Expand Down

0 comments on commit 3fbabfa

Please sign in to comment.