gestalt-config / gestalt

A Java configuration library
https://gestalt-config.github.io/gestalt/
Apache License 2.0
81 stars 2 forks source link

Reload MapConfigSource fails #150

Closed brainbytes42 closed 8 months ago

brainbytes42 commented 8 months ago

Hi,

sorry to have the next issue, but I just ran into it...

I thought about ways to change the config programmataically, i.e. if some setting is found to be not working, it shall be overwritten to have a useful value at other parts of the program.

The only way, that I came up with, was to add a MapConfigSource as last Source, add changes if necessary, and then trigger a ManualConfigReloadStrategy.

Unfortunately, this fails here: https://github.com/gestalt-config/gestalt/blob/42f9d53dbcf8c80fcf5a65decacf9a2b8cf55891/gestalt-core/src/main/java/org/github/gestalt/config/GestaltCore.java#L161-L163

In my Debugger, I have reloadSource = MapConfigSource@3408. But from my three sourcePackages, only one is a MapConfigSource, but with different Address (MapConfigSource@3491). At least the underlying HashMap is the same (HashMap@3410) for both MapConfigSources...

credmond-git commented 8 months ago

Can you take a look at https://github.com/gestalt-config/gestalt/blob/1590ba80cca5d3ae33c653c0f46e198d5659ffad/gestalt-core/src/test/java/org/github/gestalt/config/decoder/ProxyDecoderTest.java#L610

See if this example helps how to do this.

I will leave this open till I add some more documentation around dynamic reloading.

credmond-git commented 8 months ago

Here is the code snippet that might help

 Map<String, String> configs = new HashMap<>();
configs.put("db.channel", "100");
configs.put("db.uri", "mysql.com");
configs.put("db.password", "pass");

Gestalt gestalt = builder
    .addSource(MapConfigSourceBuilder.builder()
        .setCustomConfig(configs)
        .addConfigReloadStrategy(new ManualConfigReloadStrategy())
        .build())
    .build();

If you add the ManualConfigReloadStrategy to the specific MapConfigSourceBuilder it should work.

credmond-git commented 8 months ago

I have added some unit tests around this and there seems to be a bug with the ManualConfigReloadStrategy. I am investigating.

credmond-git commented 8 months ago

Can you please try v0.24.6, it has a fix for a bug with the reloads not getting called. Preventing the cache from getting cleared.

I also added some documentation around reloads https://github.com/gestalt-config/gestalt?tab=readme-ov-file#dynamic-configuration-with-reload-strategies

credmond-git commented 8 months ago

Furthermore, i created a unit tests with a basic implementation to cover the usecase and prevent regressions. https://github.com/gestalt-config/gestalt/blob/b80bf2dabfc811b36e7ccfdb0fb81101c42bcd28/gestalt-core/src/test/java/org/github/gestalt/config/integration/GestaltIntegrationTests.java#L264

brainbytes42 commented 8 months ago

Your Investigation was successful - updating the version solved the issue for me. Thank you!