Skip to content

Commit 486b426

Browse files
committed
Merge pull request #26 from cfieber/master
Updates kork-core with more complete configuration for Eureka
2 parents 3222d4a + fa6725a commit 486b426

File tree

24 files changed

+557
-403
lines changed

24 files changed

+557
-403
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
buildscript {
18-
18+
1919
repositories {
2020
jcenter()
2121
maven { url 'http://dl.bintray.com/spinnaker/gradle/' }
@@ -33,7 +33,7 @@ allprojects {
3333
group = 'com.netflix.spinnaker.kork'
3434

3535
spinnaker {
36-
dependenciesVersion = "0.19.0"
36+
dependenciesVersion = "0.22.0"
3737
}
3838

3939
jacoco {

kork-cassandra/src/main/java/com/netflix/spinnaker/kork/astyanax/AstyanaxComponents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public AstyanaxConfiguration astyanaxConfiguration() {
6060
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
6161
.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)
6262
.setCqlVersion("3.0.0")
63-
.setTargetCassandraVersion("1.2");
63+
.setTargetCassandraVersion("2.0");
6464
}
6565

6666
@Bean

kork-cassandra/src/test/groovy/com/netflix/spinnaker/kork/astyanax/AstyanaxComponentsSpec.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class AstyanaxComponentsSpec extends Specification {
5050

5151
where:
5252
description | properties
53-
"by default" | [:]
5453
"if explicitly enabled" | ["cassandra.embedded": "true"]
5554
}
5655

kork-core/kork-core.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
dependencies {
22
compile spinnaker.dependency('bootAutoConfigure')
33
compile spinnaker.dependency('bootActuator')
4+
compile spinnaker.dependency('archaiusCore')
45
compile spinnaker.dependency('eurekaClient')
56
compile spinnaker.dependency('awsCore')
67
compile spinnaker.dependency('spectatorApi')
78
compile spinnaker.dependency('spectatorAws')
9+
compile spinnaker.dependency('spectatorGc')
10+
compile spinnaker.dependency('spectatorJvm')
811
testCompile spinnaker.dependency('junit')
912
testRuntime spinnaker.dependency('slf4jSimple')
1013
}

kork-core/src/main/java/com/netflix/spinnaker/kork/PlatformComponents.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
package com.netflix.spinnaker.kork;
1818

19+
import com.netflix.spinnaker.kork.archaius.ArchaiusConfiguration;
1920
import com.netflix.spinnaker.kork.aws.AwsComponents;
2021
import com.netflix.spinnaker.kork.eureka.EurekaComponents;
22+
import com.netflix.spinnaker.kork.metrics.SpectatorConfiguration;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
2224
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.context.annotation.Import;
2426

2527
@Configuration
26-
@ConditionalOnMissingClass(name = {"com.netflix.config.NetflixConfiguration"})
27-
@Import({EurekaComponents.class, AwsComponents.class})
28+
@Import({ArchaiusConfiguration.class, EurekaComponents.class, SpectatorConfiguration.class, AwsComponents.class})
2829
public class PlatformComponents {
29-
3030
}
31-
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License")
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.kork.archaius;
18+
19+
import com.netflix.config.AbstractPollingScheduler;
20+
import com.netflix.config.ConfigurationManager;
21+
import com.netflix.config.DynamicConfiguration;
22+
import com.netflix.config.FixedDelayPollingScheduler;
23+
import com.netflix.spinnaker.kork.internal.Precondition;
24+
import org.apache.commons.configuration.AbstractConfiguration;
25+
import org.apache.commons.configuration.CompositeConfiguration;
26+
import org.springframework.beans.BeansException;
27+
import org.springframework.beans.factory.FactoryBean;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.beans.factory.config.AbstractFactoryBean;
30+
import org.springframework.beans.factory.config.BeanPostProcessor;
31+
import org.springframework.context.ApplicationContext;
32+
import org.springframework.context.ConfigurableApplicationContext;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.context.annotation.DependsOn;
36+
import org.springframework.core.Ordered;
37+
import org.springframework.core.env.ConfigurableEnvironment;
38+
import org.springframework.core.env.MutablePropertySources;
39+
import org.springframework.core.io.ClassPathResource;
40+
import org.springframework.core.io.Resource;
41+
import org.springframework.core.io.ResourceLoader;
42+
import org.springframework.core.io.support.ResourcePropertySource;
43+
44+
import javax.annotation.PostConstruct;
45+
import javax.annotation.PreDestroy;
46+
import java.io.IOException;
47+
import java.util.*;
48+
import java.util.concurrent.TimeUnit;
49+
50+
@Configuration
51+
public class ArchaiusConfiguration {
52+
53+
@Autowired
54+
ConfigurableApplicationContext applicationContext;
55+
56+
@Autowired(required = false)
57+
List<ClasspathPropertySource> propertyBindings;
58+
59+
/**
60+
* This is a BeanPostProcessor to ensure early initialization only.
61+
*/
62+
static class ArchaiusInitializingBeanPostProcessor implements BeanPostProcessor, Ordered {
63+
private final ConfigurableApplicationContext applicationContext;
64+
private final AbstractPollingScheduler pollingScheduler;
65+
private final SpringEnvironmentPolledConfigurationSource polledConfigurationSource;
66+
private final List<ClasspathPropertySource> propertyBindings;
67+
private final DynamicConfiguration configurationInstance;
68+
69+
public ArchaiusInitializingBeanPostProcessor(ConfigurableApplicationContext applicationContext, AbstractPollingScheduler pollingScheduler, SpringEnvironmentPolledConfigurationSource polledConfigurationSource, List<ClasspathPropertySource> propertyBindings) {
70+
this.applicationContext = Precondition.notNull(applicationContext, "applicationContext");
71+
this.pollingScheduler = Precondition.notNull(pollingScheduler, "pollingScheduler");
72+
this.polledConfigurationSource = Precondition.notNull(polledConfigurationSource, "polledConfigurationSource");
73+
this.propertyBindings = propertyBindings != null ? propertyBindings : Collections.emptyList();
74+
initPropertyBindings();
75+
76+
configurationInstance = new DynamicConfiguration(polledConfigurationSource, pollingScheduler);
77+
if (!ConfigurationManager.isConfigurationInstalled()) {
78+
ConfigurationManager.install(new CompositeConfiguration());
79+
}
80+
CompositeConfiguration config = (CompositeConfiguration) ConfigurationManager.getConfigInstance();
81+
config.addConfiguration(configurationInstance);
82+
83+
applicationContext.getBeanFactory().registerSingleton("environmentBackedConfig", ConfigurationManager.getConfigInstance());
84+
applicationContext.getBeanFactory().registerAlias("environmentBackedConfig", "abstractConfiguration");
85+
}
86+
87+
@PreDestroy
88+
public void shutdown() {
89+
pollingScheduler.stop();
90+
((CompositeConfiguration) ConfigurationManager.getConfigInstance()).removeConfiguration(configurationInstance);
91+
}
92+
93+
private void initPropertyBindings() {
94+
MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources();
95+
Set<String> activeProfiles = new HashSet<>(Arrays.asList(applicationContext.getEnvironment().getActiveProfiles()));
96+
for (ClasspathPropertySource binding : propertyBindings) {
97+
for (String profile : activeProfiles) {
98+
if (binding.supportsProfile(profile)) {
99+
res(binding.getBaseName(), profile).ifPresent(sources::addLast);
100+
}
101+
}
102+
res(binding.getBaseName(), null).ifPresent(sources::addLast);
103+
}
104+
}
105+
106+
private Optional<ResourcePropertySource> res(String base, String profile) {
107+
String name = base;
108+
String res = "/" + base;
109+
if (profile != null && !profile.isEmpty()) {
110+
name += ": " + profile;
111+
res += "-" + profile;
112+
}
113+
res += ".properties";
114+
Resource r = applicationContext.getResource(res);
115+
if (r.exists()) {
116+
try {
117+
return Optional.of(new ResourcePropertySource(name, r));
118+
} catch (IOException ioe) {
119+
throw new RuntimeException("Error loading property source [" + name + "]: " + res, ioe);
120+
}
121+
}
122+
return Optional.empty();
123+
}
124+
125+
126+
@Override
127+
public int getOrder() {
128+
return Ordered.HIGHEST_PRECEDENCE + 10;
129+
}
130+
131+
@Override
132+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
133+
return bean;
134+
}
135+
136+
@Override
137+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
138+
return bean;
139+
}
140+
}
141+
142+
@Bean
143+
ArchaiusInitializingBeanPostProcessor archaiusInitializingBeanPostProcessor() {
144+
return new ArchaiusInitializingBeanPostProcessor(applicationContext, pollingScheduler(), polledConfigurationSource(), propertyBindings);
145+
}
146+
147+
@Bean
148+
AbstractPollingScheduler pollingScheduler() {
149+
int initialDelayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.INITIAL_DELAY_PROPERTY, Integer.class, 0);
150+
int delayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.DELAY_PROPERTY, Integer.class, (int) TimeUnit.SECONDS.toMillis(15));
151+
return new FixedDelayPollingScheduler(initialDelayMillis, delayMillis, false);
152+
}
153+
154+
@Bean
155+
SpringEnvironmentPolledConfigurationSource polledConfigurationSource() {
156+
return new SpringEnvironmentPolledConfigurationSource(applicationContext.getEnvironment());
157+
}
158+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License")
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.kork.archaius;
18+
19+
public interface ClasspathPropertySource {
20+
String getBaseName();
21+
22+
default boolean supportsProfile(String profile) {
23+
return true;
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License")
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.kork.archaius;
18+
19+
import com.netflix.spinnaker.kork.internal.Precondition;
20+
21+
public class DefaultClasspathPropertySource implements ClasspathPropertySource {
22+
private final String baseName;
23+
24+
public DefaultClasspathPropertySource(String baseName) {
25+
this.baseName = Precondition.notNull(baseName, "baseName");
26+
}
27+
28+
@Override
29+
public String getBaseName() {
30+
return baseName;
31+
}
32+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License")
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.kork.archaius;
18+
19+
import com.netflix.config.PollResult;
20+
import com.netflix.config.PolledConfigurationSource;
21+
import com.netflix.spinnaker.kork.internal.Precondition;
22+
import org.springframework.core.env.ConfigurableEnvironment;
23+
import org.springframework.core.env.EnumerablePropertySource;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import java.util.Optional;
28+
29+
public class SpringEnvironmentPolledConfigurationSource implements PolledConfigurationSource {
30+
31+
private final ConfigurableEnvironment environment;
32+
33+
public SpringEnvironmentPolledConfigurationSource(ConfigurableEnvironment environment) {
34+
this.environment = Precondition.notNull(environment, "environment");
35+
}
36+
37+
@Override
38+
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
39+
Map<String, Object> result = new HashMap<>();
40+
environment.getPropertySources().iterator().forEachRemaining(source -> {
41+
if (source instanceof EnumerablePropertySource) {
42+
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
43+
for (String key : enumerable.getPropertyNames()) {
44+
result.putIfAbsent(key, enumerable.getProperty(key));
45+
}
46+
}
47+
});
48+
return PollResult.createFull(result);
49+
}
50+
}
51+

kork-core/src/main/java/com/netflix/spinnaker/kork/aws/InstrumentedBackoffStrategy.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.amazonaws.retry.PredefinedRetryPolicies;
2222
import com.amazonaws.retry.RetryPolicy;
2323
import com.netflix.spectator.api.Registry;
24+
import com.netflix.spinnaker.kork.internal.Precondition;
2425

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

3334
public InstrumentedBackoffStrategy(Registry registry, RetryPolicy.BackoffStrategy delegate) {
34-
if (registry == null) {
35-
throw new NullPointerException("registry");
36-
}
37-
38-
if (delegate == null) {
39-
throw new NullPointerException("delegate");
40-
}
41-
42-
this.registry = registry;
43-
this.delegate = delegate;
35+
this.registry = Precondition.notNull(registry, "registry");
36+
this.delegate = Precondition.notNull(delegate, "delegate");
4437
}
4538

4639
public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest, AmazonClientException exception, int retriesAttempted) {

0 commit comments

Comments
 (0)