Skip to content

Commit

Permalink
Merge pull request #26 from cfieber/master
Browse files Browse the repository at this point in the history
Updates kork-core with more complete configuration for Eureka
  • Loading branch information
cfieber committed Dec 1, 2015
2 parents 3222d4a + fa6725a commit 486b426
Show file tree
Hide file tree
Showing 24 changed files with 557 additions and 403 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

buildscript {

repositories {
jcenter()
maven { url 'http://dl.bintray.com/spinnaker/gradle/' }
Expand All @@ -33,7 +33,7 @@ allprojects {
group = 'com.netflix.spinnaker.kork'

spinnaker {
dependenciesVersion = "0.19.0"
dependenciesVersion = "0.22.0"
}

jacoco {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public AstyanaxConfiguration astyanaxConfiguration() {
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)
.setCqlVersion("3.0.0")
.setTargetCassandraVersion("1.2");
.setTargetCassandraVersion("2.0");
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class AstyanaxComponentsSpec extends Specification {

where:
description | properties
"by default" | [:]
"if explicitly enabled" | ["cassandra.embedded": "true"]
}

Expand Down
3 changes: 3 additions & 0 deletions kork-core/kork-core.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
dependencies {
compile spinnaker.dependency('bootAutoConfigure')
compile spinnaker.dependency('bootActuator')
compile spinnaker.dependency('archaiusCore')
compile spinnaker.dependency('eurekaClient')
compile spinnaker.dependency('awsCore')
compile spinnaker.dependency('spectatorApi')
compile spinnaker.dependency('spectatorAws')
compile spinnaker.dependency('spectatorGc')
compile spinnaker.dependency('spectatorJvm')
testCompile spinnaker.dependency('junit')
testRuntime spinnaker.dependency('slf4jSimple')
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package com.netflix.spinnaker.kork;

import com.netflix.spinnaker.kork.archaius.ArchaiusConfiguration;
import com.netflix.spinnaker.kork.aws.AwsComponents;
import com.netflix.spinnaker.kork.eureka.EurekaComponents;
import com.netflix.spinnaker.kork.metrics.SpectatorConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ConditionalOnMissingClass(name = {"com.netflix.config.NetflixConfiguration"})
@Import({EurekaComponents.class, AwsComponents.class})
@Import({ArchaiusConfiguration.class, EurekaComponents.class, SpectatorConfiguration.class, AwsComponents.class})
public class PlatformComponents {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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.spinnaker.kork.archaius;

import com.netflix.config.AbstractPollingScheduler;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.spinnaker.kork.internal.Precondition;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.CompositeConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePropertySource;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Configuration
public class ArchaiusConfiguration {

@Autowired
ConfigurableApplicationContext applicationContext;

@Autowired(required = false)
List<ClasspathPropertySource> propertyBindings;

/**
* This is a BeanPostProcessor to ensure early initialization only.
*/
static class ArchaiusInitializingBeanPostProcessor implements BeanPostProcessor, Ordered {
private final ConfigurableApplicationContext applicationContext;
private final AbstractPollingScheduler pollingScheduler;
private final SpringEnvironmentPolledConfigurationSource polledConfigurationSource;
private final List<ClasspathPropertySource> propertyBindings;
private final DynamicConfiguration configurationInstance;

public ArchaiusInitializingBeanPostProcessor(ConfigurableApplicationContext applicationContext, AbstractPollingScheduler pollingScheduler, SpringEnvironmentPolledConfigurationSource polledConfigurationSource, List<ClasspathPropertySource> propertyBindings) {
this.applicationContext = Precondition.notNull(applicationContext, "applicationContext");
this.pollingScheduler = Precondition.notNull(pollingScheduler, "pollingScheduler");
this.polledConfigurationSource = Precondition.notNull(polledConfigurationSource, "polledConfigurationSource");
this.propertyBindings = propertyBindings != null ? propertyBindings : Collections.emptyList();
initPropertyBindings();

configurationInstance = new DynamicConfiguration(polledConfigurationSource, pollingScheduler);
if (!ConfigurationManager.isConfigurationInstalled()) {
ConfigurationManager.install(new CompositeConfiguration());
}
CompositeConfiguration config = (CompositeConfiguration) ConfigurationManager.getConfigInstance();
config.addConfiguration(configurationInstance);

applicationContext.getBeanFactory().registerSingleton("environmentBackedConfig", ConfigurationManager.getConfigInstance());
applicationContext.getBeanFactory().registerAlias("environmentBackedConfig", "abstractConfiguration");
}

@PreDestroy
public void shutdown() {
pollingScheduler.stop();
((CompositeConfiguration) ConfigurationManager.getConfigInstance()).removeConfiguration(configurationInstance);
}

private void initPropertyBindings() {
MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources();
Set<String> activeProfiles = new HashSet<>(Arrays.asList(applicationContext.getEnvironment().getActiveProfiles()));
for (ClasspathPropertySource binding : propertyBindings) {
for (String profile : activeProfiles) {
if (binding.supportsProfile(profile)) {
res(binding.getBaseName(), profile).ifPresent(sources::addLast);
}
}
res(binding.getBaseName(), null).ifPresent(sources::addLast);
}
}

private Optional<ResourcePropertySource> res(String base, String profile) {
String name = base;
String res = "/" + base;
if (profile != null && !profile.isEmpty()) {
name += ": " + profile;
res += "-" + profile;
}
res += ".properties";
Resource r = applicationContext.getResource(res);
if (r.exists()) {
try {
return Optional.of(new ResourcePropertySource(name, r));
} catch (IOException ioe) {
throw new RuntimeException("Error loading property source [" + name + "]: " + res, ioe);
}
}
return Optional.empty();
}


@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 10;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}

@Bean
ArchaiusInitializingBeanPostProcessor archaiusInitializingBeanPostProcessor() {
return new ArchaiusInitializingBeanPostProcessor(applicationContext, pollingScheduler(), polledConfigurationSource(), propertyBindings);
}

@Bean
AbstractPollingScheduler pollingScheduler() {
int initialDelayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.INITIAL_DELAY_PROPERTY, Integer.class, 0);
int delayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.DELAY_PROPERTY, Integer.class, (int) TimeUnit.SECONDS.toMillis(15));
return new FixedDelayPollingScheduler(initialDelayMillis, delayMillis, false);
}

@Bean
SpringEnvironmentPolledConfigurationSource polledConfigurationSource() {
return new SpringEnvironmentPolledConfigurationSource(applicationContext.getEnvironment());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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.spinnaker.kork.archaius;

public interface ClasspathPropertySource {
String getBaseName();

default boolean supportsProfile(String profile) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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.spinnaker.kork.archaius;

import com.netflix.spinnaker.kork.internal.Precondition;

public class DefaultClasspathPropertySource implements ClasspathPropertySource {
private final String baseName;

public DefaultClasspathPropertySource(String baseName) {
this.baseName = Precondition.notNull(baseName, "baseName");
}

@Override
public String getBaseName() {
return baseName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015 Netflix, Inc.
*
* 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.spinnaker.kork.archaius;

import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
import com.netflix.spinnaker.kork.internal.Precondition;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class SpringEnvironmentPolledConfigurationSource implements PolledConfigurationSource {

private final ConfigurableEnvironment environment;

public SpringEnvironmentPolledConfigurationSource(ConfigurableEnvironment environment) {
this.environment = Precondition.notNull(environment, "environment");
}

@Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
Map<String, Object> result = new HashMap<>();
environment.getPropertySources().iterator().forEachRemaining(source -> {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String key : enumerable.getPropertyNames()) {
result.putIfAbsent(key, enumerable.getProperty(key));
}
}
});
return PollResult.createFull(result);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.internal.Precondition;

public class InstrumentedBackoffStrategy implements RetryPolicy.BackoffStrategy {
private final Registry registry;
Expand All @@ -31,16 +32,8 @@ public InstrumentedBackoffStrategy(Registry registry) {
}

public InstrumentedBackoffStrategy(Registry registry, RetryPolicy.BackoffStrategy delegate) {
if (registry == null) {
throw new NullPointerException("registry");
}

if (delegate == null) {
throw new NullPointerException("delegate");
}

this.registry = registry;
this.delegate = delegate;
this.registry = Precondition.notNull(registry, "registry");
this.delegate = Precondition.notNull(delegate, "delegate");
}

public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest, AmazonClientException exception, int retriesAttempted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.internal.Precondition;

public class InstrumentedRetryCondition implements RetryPolicy.RetryCondition {
private final Registry registry;
Expand All @@ -31,16 +32,8 @@ public InstrumentedRetryCondition(Registry registry) {
}

public InstrumentedRetryCondition(Registry registry, RetryPolicy.RetryCondition delegate) {
if (registry == null) {
throw new NullPointerException("registry");
}

if (delegate == null) {
throw new NullPointerException("delegate");
}

this.registry = registry;
this.delegate = delegate;
this.registry = Precondition.notNull(registry, "registry");
this.delegate = Precondition.notNull(delegate, "delegate");
}

@Override
Expand Down
Loading

0 comments on commit 486b426

Please sign in to comment.