-
Notifications
You must be signed in to change notification settings - Fork 414
Support to use classes registered in bootstrap context #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...ud/zookeeper/discovery/configclient/ZookeeperConfigServerBootstrapperCustomizerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.cloud.zookeeper.discovery.configclient; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.curator.RetryPolicy; | ||
import org.apache.curator.drivers.TracerDriver; | ||
import org.apache.curator.ensemble.EnsembleProvider; | ||
import org.apache.curator.ensemble.fixed.FixedEnsembleProvider; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.curator.utils.DefaultTracerDriver; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.BootstrapRegistry; | ||
import org.springframework.boot.BootstrapRegistryInitializer; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.context.properties.bind.BindContext; | ||
import org.springframework.boot.context.properties.bind.BindHandler; | ||
import org.springframework.boot.context.properties.bind.Bindable; | ||
import org.springframework.boot.context.properties.bind.Binder; | ||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName; | ||
import org.springframework.cloud.config.client.ConfigServerInstanceProvider; | ||
import org.springframework.cloud.zookeeper.CuratorFrameworkCustomizer; | ||
import org.springframework.cloud.zookeeper.ZookeeperProperties; | ||
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient; | ||
import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.Ordered; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* @author Ryan Baxter | ||
*/ | ||
public class ZookeeperConfigServerBootstrapperCustomizerTests { | ||
private ConfigurableApplicationContext context; | ||
|
||
@AfterEach | ||
public void after() { | ||
if (context != null) { | ||
context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void enabledAddsInstanceProviderFn() { | ||
AtomicReference<ZookeeperDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>(); | ||
BindHandlerBootstrapper bindHandlerBootstrapper = new BindHandlerBootstrapper(); | ||
context = new SpringApplicationBuilder(ZookeeperConfigServerBootstrapperTests.TestConfig.class) | ||
.listeners(new ZookeeperTestingServer()) | ||
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true", | ||
"spring.cloud.zookeeper.discovery.metadata[mymetadataprop]=mymetadataval", | ||
"spring.cloud.service-registry.auto-registration.enabled=false") | ||
.addBootstrapRegistryInitializer(bindHandlerBootstrapper) | ||
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> { | ||
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext() | ||
.get(ConfigServerInstanceProvider.Function.class); | ||
assertThat(providerFn.apply("id", event.getBootstrapContext() | ||
.get(Binder.class), event.getBootstrapContext() | ||
.get(BindHandler.class), mock(Log.class))).as("Should return empty list.") | ||
.isNotNull(); | ||
bootstrapDiscoveryClient.set(event.getBootstrapContext().get(ZookeeperDiscoveryClient.class)); | ||
CuratorFrameworkCustomizer curatorFrameworkCustomizer = event.getBootstrapContext() | ||
.get(CuratorFrameworkCustomizer.class); | ||
assertThat(curatorFrameworkCustomizer).isInstanceOf(MyCuratorFrameworkCustomizer.class); | ||
RetryPolicy retryPolicy = event.getBootstrapContext().get(RetryPolicy.class); | ||
assertThat(retryPolicy).isInstanceOf(RetryPolicy.class); | ||
EnsembleProvider ensembleProvider = event.getBootstrapContext().get(EnsembleProvider.class); | ||
assertThat(ensembleProvider).isInstanceOf(MyEnsembleProvider.class); | ||
TracerDriver tracerDriver = event.getBootstrapContext().get(TracerDriver.class); | ||
assertThat(tracerDriver).isInstanceOf(MyTracerDriver.class); | ||
})).run(); | ||
|
||
ZookeeperDiscoveryClient discoveryClient = context.getBean(ZookeeperDiscoveryClient.class); | ||
|
||
assertThat(discoveryClient == bootstrapDiscoveryClient.get()).isTrue(); | ||
assertThat(bindHandlerBootstrapper.onSuccessCount).isGreaterThan(0); | ||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
static class TestConfig { | ||
|
||
} | ||
|
||
static class BindHandlerBootstrapper implements BootstrapRegistryInitializer, Ordered { | ||
|
||
private int onSuccessCount = 0; | ||
|
||
private static <T> void registerIfAbsentAndEnabled( | ||
BootstrapRegistry registry, Class<T> type, BootstrapRegistry.InstanceSupplier<T> supplier) { | ||
registry.registerIfAbsent(type, supplier); | ||
} | ||
|
||
@Override | ||
public void initialize(BootstrapRegistry registry) { | ||
registry.register(BindHandler.class, context -> new BindHandler() { | ||
@Override | ||
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, | ||
Object result) { | ||
onSuccessCount++; | ||
return result; | ||
} | ||
}); | ||
|
||
registerIfAbsentAndEnabled(registry, RetryPolicy.class, context -> | ||
new MyRetryPolicy(context.get(ZookeeperProperties.class))); | ||
|
||
registerIfAbsentAndEnabled(registry, CuratorFrameworkCustomizer.class, context -> | ||
new MyCuratorFrameworkCustomizer()); | ||
|
||
registerIfAbsentAndEnabled(registry, EnsembleProvider.class, context -> | ||
new MyEnsembleProvider(context.get(ZookeeperProperties.class))); | ||
|
||
registerIfAbsentAndEnabled(registry, TracerDriver.class, context -> | ||
new MyTracerDriver()); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.HIGHEST_PRECEDENCE; | ||
} | ||
} | ||
|
||
static class MyRetryPolicy extends ExponentialBackoffRetry { | ||
|
||
MyRetryPolicy(ZookeeperProperties properties) { | ||
super(properties.getBaseSleepTimeMs(), properties.getMaxRetries(), properties.getMaxSleepMs()); | ||
} | ||
} | ||
|
||
static class MyCuratorFrameworkCustomizer implements CuratorFrameworkCustomizer { | ||
@Override | ||
public void customize(CuratorFrameworkFactory.Builder builder) { | ||
|
||
} | ||
} | ||
|
||
static class MyEnsembleProvider extends FixedEnsembleProvider { | ||
MyEnsembleProvider(ZookeeperProperties properties) { | ||
super(properties.getConnectString()); | ||
} | ||
|
||
} | ||
|
||
static class MyTracerDriver extends DefaultTracerDriver { | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we ever reach the
orElse
here? I mean even before the lambda gets registered, that will later create thisZookeeperFunction
, the code has already calledCuratorFactory.registerCurator
. (I may be missing some use-case though.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was probably being over cautious here, there is no harm though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from being a bit confusing to readers (who may expect this line to be creating their
CuratorFramework
instance), won't thisgetOrElse
code always end up running theCuratorFactory.curatorFramework(...)
call? If so, that would always build anotherCuratorFramework
instance, start it, and block while it connects (or fails to do so), only to then throw it away (without aclose()
call). When this instance can't connect, it may also write a considerable amount of misleading logs, that could become red herrings when debugging some issue. Not sure if thecuratorFramework(...)
method could also throw an exception or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would only call
CuratorFactory.curatorFramework
if there was not one already present in theBootstrapContext
.I don't see how it would start 2...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be missing something, but my thinking was that:
getOrElse
call have to be evaluated first by the JVM, meaning thatCuratorFactory.curatorFramework(...)
is executed, building & starting the newCuratorFramework
, thenCuratorFramework
intogetOrElse
as theother
parameter, thengetOrElse
itself gets executed, where it will simply ignore theother
parameter when it realizes that there's already aCuratorFramework
in theBootstrapContext
.