Skip to content
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

Support to use classes registered in bootstrap context #325

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ public List<ServiceInstance> apply(String serviceId, Binder binder, BindHandler
return Collections.emptyList();
}

ZookeeperProperties properties = binder.bind(ZookeeperProperties.PREFIX, Bindable.of(ZookeeperProperties.class))
.orElse(new ZookeeperProperties());
RetryPolicy retryPolicy = new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), properties.getMaxRetries(),
properties.getMaxSleepMs());
ZookeeperProperties properties = context.getOrElse(ZookeeperProperties.class, binder.bind(ZookeeperProperties.PREFIX, Bindable.of(ZookeeperProperties.class))
.orElse(new ZookeeperProperties()));
RetryPolicy retryPolicy = context.getOrElse(RetryPolicy.class, new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), properties.getMaxRetries(),
properties.getMaxSleepMs()));
try {
CuratorFramework curatorFramework = CuratorFactory.curatorFramework(properties, retryPolicy, Stream::of,
() -> null, () -> null);
InstanceSerializer<ZookeeperInstance> serializer = new JsonInstanceSerializer<>(ZookeeperInstance.class);
ZookeeperDiscoveryProperties discoveryProperties = binder.bind(ZookeeperDiscoveryProperties.PREFIX, Bindable
CuratorFramework curatorFramework = context.getOrElse(CuratorFramework.class, CuratorFactory.curatorFramework(properties, retryPolicy, Stream::of,
Copy link

@pantherdd pantherdd Dec 20, 2023

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 this ZookeeperFunction, the code has already called CuratorFactory.registerCurator. (I may be missing some use-case though.)

Copy link
Contributor Author

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.

Copy link

@pantherdd pantherdd Dec 20, 2023

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 this getOrElse code always end up running the CuratorFactory.curatorFramework(...) call? If so, that would always build another CuratorFramework instance, start it, and block while it connects (or fails to do so), only to then throw it away (without a close() 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 the curatorFramework(...) method could also throw an exception or not.

Copy link
Contributor Author

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 the BootstrapContext.

I don't see how it would start 2...

Copy link

@pantherdd pantherdd Dec 21, 2023

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:

  1. the parameters of this getOrElse call have to be evaluated first by the JVM, meaning that CuratorFactory.curatorFramework(...) is executed, building & starting the new CuratorFramework, then
  2. the JVM passes this new CuratorFramework into getOrElse as the other parameter, then
  3. getOrElse itself gets executed, where it will simply ignore the other parameter when it realizes that there's already a CuratorFramework in the BootstrapContext.

() -> null, () -> null));
InstanceSerializer<ZookeeperInstance> serializer = context.getOrElse(InstanceSerializer.class, new JsonInstanceSerializer<>(ZookeeperInstance.class));
ZookeeperDiscoveryProperties discoveryProperties = context.getOrElse(ZookeeperDiscoveryProperties.class, binder.bind(ZookeeperDiscoveryProperties.PREFIX, Bindable
.of(ZookeeperDiscoveryProperties.class), bindHandler)
.orElseGet(() -> new ZookeeperDiscoveryProperties(new InetUtils(new InetUtilsProperties())));
DefaultServiceDiscoveryCustomizer customizer = new DefaultServiceDiscoveryCustomizer(curatorFramework, discoveryProperties, serializer);
.orElseGet(() -> new ZookeeperDiscoveryProperties(new InetUtils(new InetUtilsProperties()))));
ServiceDiscoveryCustomizer customizer = context.getOrElse(ServiceDiscoveryCustomizer.class, new DefaultServiceDiscoveryCustomizer(curatorFramework, discoveryProperties, serializer));
ServiceDiscovery<ZookeeperInstance> serviceDiscovery = customizer.customize(ServiceDiscoveryBuilder.builder(ZookeeperInstance.class));
ZookeeperDependencies dependencies = binder.bind(ZookeeperDependencies.PREFIX, Bindable
ZookeeperDependencies dependencies = context.getOrElse(ZookeeperDependencies.class, binder.bind(ZookeeperDependencies.PREFIX, Bindable
.of(ZookeeperDependencies.class), bindHandler)
.orElseGet(ZookeeperDependencies::new);
.orElseGet(ZookeeperDependencies::new));
return new ZookeeperDiscoveryClient(serviceDiscovery, dependencies, discoveryProperties).getInstances(serviceId);
}
catch (Exception e) {
Expand Down
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 {

}

}