spring-cloud-config icon indicating copy to clipboard operation
spring-cloud-config copied to clipboard

Issue when java.util.List is used in @ConfigurationProperties and @RefreshScope

Open deepzGitHub opened this issue 11 years ago • 18 comments

Hi spring could team,

I've encountered a problem when a list of values from yml file is mapped to java.util.List in a bean which is @ConfigurationProperties and @RefreshScope.

This issue is when the list values in yml changes and /refresh is done on the client. New additions and changes to existing entries in list works fine when refreshed but if an entry is removed from yml list - it is never getting updated in the bean property on refresh.

config yml

test:
  configurationProperties:
    myList:
      - value1
      - value2
      - value3

Java Bean

@ConfigurationProperties(prefix="test.configurationProperties")
@RefreshScope
public class ConfigReader {
    private List<String> myList;
........

When value3 is removed from the yml file and /refresh is posted, myList will still have values value1, value2, value3

deepzGitHub avatar Jul 03 '15 15:07 deepzGitHub

Where is the YAML file? In a config repo, or locally in the app?

dsyer avatar Jul 13 '15 15:07 dsyer

I tested this by placing the yaml locally in app and config server. Both cases if we remove an entry from yaml the same is not getting removed from List when refreshed.

Thanks.

deepzGitHub avatar Jul 13 '15 18:07 deepzGitHub

It works for me. Can you share a project with a minimal set of features to reproduce the problem?

dsyer avatar Jul 14 '15 10:07 dsyer

Thanks. Please refer https://github.com/deepzGitHub/RefreshList This is a simple spring boot application were the yaml file is kept locally. Check README.md for the testing scenarios and steps.

deepzGitHub avatar Jul 15 '15 02:07 deepzGitHub

Your RefreshListConfig isn't actually in @RefreshScope. You can put that annotation on a type, but it only has an effect if the type is component scanned. You would have to component scan it, or declare a @Bean (with @RefreshScope).

dsyer avatar Jul 15 '15 13:07 dsyer

Thanks.

Issue #1,

As you suggested I defined RefreshListConfig as @Bean in RefreshListApplication and it worked. But i noticed one issue, I have a logic that needs to be executed whenever this List gets changed. I had written that logic in @PostConstruct method in RefreshListConfig assuming when an http POST on /refresh is done the config change will be initialized before the post construct is executed. When /refresh is done PostConstruct method is executed but at that time the List still has old value but when the reference of RefreshListConfig bean is used in another bean as part of next request it's updating correctly in PostConstruct method. Is that expected behavior?

Issue #2,

If I used a second configuration class to define RefreshListConfig @Bean which is @RefreshScope, and import that configuration using @Import in main configuration class or ComponentScan that configuration class in main configuration class, the list is not getting updated.

This scenario is needed when I have to include a refresh-scoped bean in a dependent library and use with another spring boot application.

I have updated this scenario in https://github.com/deepzGitHub/RefreshList

Please share your thought on both. Thanks

deepzGitHub avatar Jul 16 '15 00:07 deepzGitHub

Regarding #​1 that is the expected behaviour. There is an event you can consume if you want to know which properties changed during a refresh.

Regarding #​2, I don't see any problem with the @Import. Check again, and let us know with more detail if something isn't working (steps to reproduce, or a test case).

dsyer avatar Jul 16 '15 07:07 dsyer

Regarding Issue #2 I still feels there is an issue when an entry is removed from List. I have detailed all the scenarios to replicate the issue in https://github.com/deepzGitHub/RefreshList

List refresh (addition and deletion) works perfect when ComponentScaned ONLY from the java configuration where the Bean is defined

Few Scenarios where the list is getting messed up when some entries are removed

Case 1 -

Component Scan is on a java configuration class present in a package other than the package where RefreshScope bean is defined. (Since the configuration class is in root folder technically it scans all the folders below it)

Case 2 -

The java configuration which defines the refresh scoped bean is scanned twice (One from the Configuration class present in the root of the source package and second scan is from the java configuration where refreshscope bean is defined.)

In both Case 1,2 issue is only when and item is removed from list, additions, updates to String works perfect. This makes me think there is an issue with List

Please advice, I have explained the testing scenarios and code at https://github.com/deepzGitHub/RefreshList

deepzGitHub avatar Jul 16 '15 15:07 deepzGitHub

None of that makes any difference for me. Your app works fine. It does print out the list twice, and the first time is wrong if the list shrank, but that's the way the @PostConstruct is called (once on the old object, and once on the refreshed one).

dsyer avatar Jul 16 '15 17:07 dsyer

Hi Dave,

I'm Sorry, there was an issue with my last commit (there were duplicate packages). The issue is reproducible with the latest code

https://github.com/deepzGitHub/RefreshList

I'm not pointing out the issue in PostConstruct when /refresh is done. But the next request after /refresh access the RefreshListConfig Bean in Controller.java, the list is having the deleted entries (As mentioned in the README.md)

deepzGitHub avatar Jul 16 '15 17:07 deepzGitHub

@deepzGitHub is this still an issue for you?

spencergibb avatar Nov 11 '15 05:11 spencergibb

Closing for lack of feedback.

spencergibb avatar Mar 03 '16 01:03 spencergibb

Hi,

I don't know if you guys appreciate the update, but i have a similar or even the same issue.

We have a spring config server which pull's its configuration from a git repository. In the same project we have a component which we configure by a yaml file. This yaml file contains a few hundred (e.g. 500+ entries) entries with each multiple properties.

All of these entries are added to a database when the app reload or restart. This was firstly done by purging the database content and overwriting it with the entries the app got from the config server.

We're encountering performance issues when we're running the app twice or triple, as for every restart the database (which is really frequently used) is emptied and filled up again.

To fix this, we did use the entries and/or properties, check if there are some changes and update the database.

When we add one or more entries, this works as expected (for a restart as well as a reload of the app). When we delete one or more entries, a restart works fine. But when we reload the application, it's complaining about double entries (which is always the last entry in the properties file).

We think the following happens: 1: We delete one entry (something in the middle, not the lasT) from the config. 2: We reload the app 2b: Spring updates it's array from zero to the max number of entries, note that the number of entries are decreased. 2c: Spring stops updating the array when she reaches the last properties entry in the yaml file. 2d: Spring complains about a double property, what makes sense if the array is never cleaned up, but got only an update. So every value moves one place up (until the 'gap' that was created by removing an entry has filled up), and the last value from the previous config does stil exists. This results in an array which have two double entries at the end. So for example entires[514] and entries[515] are the same.

As i think this is supposed to work, entries[515] should be deleted, as this is not a valid properties entry anymore. It's not declared anymore, so it's not needed and should not persist in the array.

Alwaysc0d3 avatar Oct 09 '17 10:10 Alwaysc0d3

@spencergibb and @dsyer I have updated the issue, could you take a look at this issue please?

Alwaysc0d3 avatar Oct 09 '17 10:10 Alwaysc0d3

I don't think that's anything to do with the original issue here. Please put your @ConfigurationPorperties bean in @RefreshScope and try again. Ask questions in StackOverflow, or gitter.

dsyer avatar Oct 09 '17 13:10 dsyer

Hi, I'm facing the same issue. After some investigation, it seems the problem comes from org.springframework.beans.AbstractNestablePropertyAccessor used to update value.

The method processKeyedProperty supports in-place replacement ou new entry. The remove element is not handled :

                    int size = list.size();
		if (index >= size && index < this.autoGrowCollectionLimit) {
			for (int i = size; i < index; i++) {
				try {
					list.add(null);
				}
				catch (NullPointerException ex) {
					...."': List does not support filling up gaps with null elements");
				}
			}
			list.add(convertedValue);
		}
		else {
			try {
				list.set(index, convertedValue);
			}
			catch (IndexOutOfBoundsException ex) {
				.... ex);
			}
		}

Where list is the current value of property.

Here is a use case when we remove an item

If I have a property toto:

  • A
  • B
  • C

then I change it to

toto:

  • B
  • C

After checking if modification occured, the ContextRefresh publishes new EnvironmentChangeEvent(context, keys) where keys are in format toto[0] = B .... toto[1]= C

When the rebinder consumes the event it triggers (it's a shortcut) bindPropertiesToTarget ... so processKeyedProperty processes the key toto[0]. In the previous piece of code list variable has the current size 3 and index correspond is 0 so runtime executes a list.set(...) <-- in-place replacement.

Same thing is done with the key toto[1].

Finally, at the end of the rebind, list instance is made of 3 items 1rst with value B (new value of the 1rst entry) 2nd with value C (new value of the 2nd entry) 3trd with value C (unchanged property)

RaphC avatar Feb 20 '19 18:02 RaphC

@RaphC while List and refresh are common to this issue, I think it is unrelated, please open a new issue with steps to recreate the problem (a project would be even better).

spencergibb avatar Feb 20 '19 18:02 spencergibb

Hi @spencergibb , ok. I will create a new issue with gist and step to recreate.

RaphC avatar Feb 20 '19 20:02 RaphC