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

[ISSUE #11231]Optimize the handleSpringBinder method in PropertiesUtil. #11240

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

stone-98
Copy link
Contributor

@stone-98 stone-98 commented Oct 9, 2023

What is the purpose of the change

The Nacos source code startup results in an error as follows:

2023-10-09 21:26:50.729  WARN 4688 --- [           main] c.a.n.c.s.c.ConfigChangeConfigs          : [ConfigChangeConfigs]Refresh config plugin properties failed 

java.lang.reflect.InvocationTargetException: null
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.alibaba.nacos.sys.utils.PropertiesUtil.handleSpringBinder(PropertiesUtil.java:62)
	at com.alibaba.nacos.sys.utils.PropertiesUtil.getPropertiesWithPrefix(PropertiesUtil.java:35)
	at com.alibaba.nacos.config.server.configuration.ConfigChangeConfigs.refreshPluginProperties(ConfigChangeConfigs.java:56)
	at com.alibaba.nacos.config.server.configuration.ConfigChangeConfigs.<init>(ConfigChangeConfigs.java:50)
	at com.alibaba.nacos.config.server.configuration.ConfigChangeConfigs$$EnhancerBySpringCGLIB$$fb3b6b82.<init>(<generated>)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1326)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:921)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
	at com.alibaba.nacos.Nacos.main(Nacos.java:48)
Caused by: java.util.NoSuchElementException: No value bound
	at org.springframework.boot.context.properties.bind.BindResult.get(BindResult.java:55)
	... 47 common frames omitted

Reason: The Nacos source code by default does not have configuration related to change plugins (nacos.core.config.plugin.*).

Below is the source code related to BindResult:

public final class BindResult<T> {
    ...
    public T get() throws NoSuchElementException {
        if (this.value == null) {
            throw new NoSuchElementException("No value bound");
        } else {
            return this.value;
        }
    }

    public T orElse(T other) {
        return this.value != null ? this.value : other;
    }
   ...
}

Brief changelog

  • Change the previous approach of using reflection calls to using object-based invocations.

  • Replace the previous usage of the BindResult#get() method with BindResult#orElse method, which defaults to returning null if not present. Then, perform null-check handling at the outer layer.

Verifying this change

Enable authentication plugin

Before the modification

Authentication plugin reads the content

image-20231009214817170

After the modification

Fix the error in the configuration change plugin.

Authentication-related property content

image-20231009214628958

Consistent with the content before the change.

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@codecov-commenter
Copy link

Codecov Report

Merging #11240 (11220ca) into develop (7911eb0) will increase coverage by 0.08%.
The diff coverage is 85.71%.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             develop   #11240      +/-   ##
=============================================
+ Coverage      53.97%   54.05%   +0.08%     
+ Complexity      5580     5579       -1     
=============================================
  Files            907      907              
  Lines          28844    28838       -6     
  Branches        3191     3192       +1     
=============================================
+ Hits           15568    15589      +21     
+ Misses         11933    11902      -31     
- Partials        1343     1347       +4     
Files Coverage Δ
...va/com/alibaba/nacos/sys/utils/PropertiesUtil.java 60.00% <100.00%> (-21.82%) ⬇️
...ava/com/alibaba/nacos/auth/config/AuthConfigs.java 60.65% <90.00%> (-1.02%) ⬇️
...baba/nacos/core/remote/tls/RpcServerTlsConfig.java 68.75% <66.66%> (+4.04%) ⬆️

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7911eb0...11220ca. Read the comment docs.

@KomachiSion KomachiSion merged commit d85e3f7 into alibaba:develop Oct 11, 2023
7 checks passed
@KomachiSion KomachiSion added the kind/enhancement Category issues or prs related to enhancement. label Oct 11, 2023
@KomachiSion KomachiSion added this to the 2.3.0 milestone Oct 11, 2023
@KomachiSion KomachiSion linked an issue Oct 11, 2023 that may be closed by this pull request
lowezheng added a commit to lowezheng/nacos that referenced this pull request Nov 12, 2023
* develop: (137 commits)
  Develop refactor topn (alibaba#11352)
  Develop fill ut common (alibaba#11335)
  Optimize MapperManager (alibaba#11195)
  Simplify the validate method for serviceinfo (alibaba#11312)
  add startup conditions (alibaba#11305)
  Fix some typos (alibaba#11269)
  fix: rename tar name (alibaba#11281)
  优化节点显示,添加mode (alibaba#11275)
  [ISSUE alibaba#11253]To fix the triggering of the listener upon failover con… (alibaba#11254)
  Add description for new plugin. (alibaba#11268)
  Upgrade to 2.3.0-BETA. (alibaba#11262)
  [ISSUE alibaba#11255]Update PathEncoderManagerTest#testEncodeWithNonExistOs. (alibaba#11256)
  Fill ut for common module (alibaba#11247)
  Optimize ThreadPoolManager (alibaba#11206)
  Fix npe when setup ack response in GrpcClient (alibaba#11210)
  [ISSUE#11192] batchRegisterInstance add recalculateRevision (alibaba#11232)
  feat(alibaba#11236): Remove invalid assertion in com.alibaba.nacos.config.server.service.ConfigCacheService#dumpChange. (alibaba#11237)
  feat(11115): http support cas publish. (alibaba#11120)
  [ISSUE alibaba#11231]Optimize the handleSpringBinder method in PropertiesUtil. (alibaba#11240)
  ISSUE alibaba#11212 (alibaba#11213)
  ...

# Conflicts:
#	console-ui/package-lock.json
#	console-ui/package.json
#	console-ui/src/pages/Login/Login.jsx
#	core/src/main/java/com/alibaba/nacos/core/listener/StartingApplicationListener.java
#	pom.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why is reflection used in the PropertiesUtil method?
3 participants