-
-
Couldn't load subscription status.
- Fork 193
BE: Implement app restart on dynamic config change #1151
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
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ff3b48
Implement app restart on dynamic config change. Resolves #616
Haarolean 04b4b19
Allow enabling auto reload without using dynamic config itself
Haarolean b008d1b
WIP
Haarolean b99a1d6
wip
Haarolean 59824d1
wip
Haarolean 146937c
wip
Haarolean 21ad391
Fixes
germanosin 588b91e
cleanup
Haarolean 125a32c
Add volatile for roles
Haarolean d866833
Merge remote-tracking branch 'origin/main' into issues/reload
Haarolean b33620b
Revert unnecessary changes
Haarolean 6474fb6
Checkstyle
Haarolean b1ac366
Add auto reload config property
Haarolean 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
125 changes: 125 additions & 0 deletions
125
api/src/main/java/io/kafbat/ui/service/app/ConfigReloadService.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,125 @@ | ||
| package io.kafbat.ui.service.app; | ||
|
|
||
| import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties; | ||
| import io.kafbat.ui.util.MultiFileWatcher; | ||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.annotation.PreDestroy; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import java.util.stream.StreamSupport; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.boot.context.properties.bind.Binder; | ||
| import org.springframework.boot.env.OriginTrackedMapPropertySource; | ||
| import org.springframework.boot.env.YamlPropertySourceLoader; | ||
| import org.springframework.boot.origin.Origin; | ||
| import org.springframework.boot.origin.OriginTrackedValue; | ||
| import org.springframework.boot.origin.TextResourceOrigin; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
| import org.springframework.core.env.PropertySource; | ||
| import org.springframework.core.io.FileSystemResource; | ||
| import org.springframework.core.io.Resource; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| @ConditionalOnProperty(value = "config.autoreload", havingValue = "true") | ||
| public class ConfigReloadService { | ||
|
|
||
| private static final String THREAD_NAME = "config-watcher-thread"; | ||
|
|
||
| private final ConfigurableEnvironment environment; | ||
| private final RoleBasedAccessControlProperties rbacProperties; | ||
| private final YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader(); | ||
|
|
||
| private Thread watcherThread; | ||
| private MultiFileWatcher multiFileWatcher; | ||
|
|
||
| @PostConstruct | ||
| public void init() { | ||
| var propertySourcePaths = StreamSupport.stream(environment.getPropertySources().spliterator(), false) | ||
| .filter(OriginTrackedMapPropertySource.class::isInstance) | ||
| .map(OriginTrackedMapPropertySource.class::cast) | ||
| .flatMap(ps -> ps.getSource().values().stream()) | ||
| .map(v -> (v instanceof OriginTrackedValue otv) ? otv.getOrigin() : null) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(o -> Stream.iterate(o, Objects::nonNull, Origin::getParent)) | ||
| .filter(TextResourceOrigin.class::isInstance) | ||
| .map(TextResourceOrigin.class::cast) | ||
| .map(TextResourceOrigin::getResource) | ||
| .filter(Objects::nonNull) | ||
| .filter(Resource::exists) | ||
| .filter(Resource::isReadable) | ||
| .filter(Resource::isFile) | ||
| .map(r -> { | ||
| try { | ||
| return r.getURI(); | ||
| } catch (IOException e) { | ||
| log.error("can't retrieve resource URL", e); | ||
| return null; | ||
| } | ||
| }) | ||
| .filter(Objects::nonNull) | ||
| .map(Paths::get) | ||
| .collect(Collectors.toCollection(LinkedHashSet::new)); | ||
|
|
||
| if (propertySourcePaths.isEmpty()) { | ||
| log.debug("No config files found, auto reload is disabled"); | ||
| return; | ||
| } | ||
|
|
||
| log.debug("Auto reload is enabled, will watch for config changes"); | ||
|
|
||
| try { | ||
| this.multiFileWatcher = new MultiFileWatcher(propertySourcePaths, this::reloadFile); | ||
| this.watcherThread = new Thread(multiFileWatcher::watchLoop, THREAD_NAME); | ||
| this.watcherThread.start(); | ||
| } catch (IOException e) { | ||
| log.error("Error while registering watch service", e); | ||
| } | ||
| } | ||
|
|
||
| private void reloadFile(Path path) { | ||
| log.info("Reloading file {}", path); | ||
| try { | ||
| if (!path.toString().endsWith(".yml") && !path.toString().endsWith(".yaml")) { | ||
| log.trace("Skipping non-YML file {}", path); | ||
| } | ||
|
|
||
| String name = String.format("Config resource 'file [%s] via location '%s'", | ||
| path.toAbsolutePath(), | ||
| path.toAbsolutePath()); // TODO extract an obj reference from env | ||
|
|
||
| List<PropertySource<?>> load = yamlLoader.load(path.toString(), new FileSystemResource(path)); | ||
| environment.getPropertySources().remove(name); | ||
| environment.getPropertySources().addFirst(load.getFirst()); | ||
| Binder binder = Binder.get(environment); | ||
|
|
||
| binder.bind("rbac", RoleBasedAccessControlProperties.class) | ||
| .ifBound(bound -> rbacProperties.setRoles(bound.getRoles())); | ||
| } catch (Throwable e) { | ||
| log.error("Error while reloading file {}", path, e); | ||
| } | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void shutdown() { | ||
| try { | ||
| if (multiFileWatcher != null) { | ||
| multiFileWatcher.close(); | ||
| } | ||
| } catch (IOException ignored) { | ||
| } | ||
| if (watcherThread != null) { | ||
| this.watcherThread.interrupt(); | ||
| } | ||
| } | ||
| } | ||
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
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
110 changes: 110 additions & 0 deletions
110
api/src/main/java/io/kafbat/ui/util/MultiFileWatcher.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,110 @@ | ||
| package io.kafbat.ui.util; | ||
|
|
||
| import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; | ||
| import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; | ||
| import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.net.URI; | ||
| import java.nio.file.ClosedWatchServiceException; | ||
| import java.nio.file.FileSystems; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.WatchEvent; | ||
| import java.nio.file.WatchKey; | ||
| import java.nio.file.WatchService; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Consumer; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| @Slf4j | ||
| public final class MultiFileWatcher implements AutoCloseable { | ||
|
|
||
| private final WatchService watchService = FileSystems.getDefault().newWatchService(); | ||
| private final Set<URI> watchedFiles = ConcurrentHashMap.newKeySet(); | ||
| private final Map<WatchKey, Path> watchDirsByKey = new HashMap<>(); | ||
| private final Consumer<Path> reloader; | ||
|
|
||
| public MultiFileWatcher(Collection<Path> filesToWatch, Consumer<Path> reloader) throws IOException { | ||
| Assert.notNull(reloader, "reloader must not be null"); | ||
| this.reloader = reloader; | ||
|
|
||
| if (filesToWatch.isEmpty()) { | ||
| log.warn("No files to watch, aborting"); | ||
| } | ||
|
|
||
|
|
||
| watchedFiles.addAll(filesToWatch.stream() | ||
| .map(p -> p.toAbsolutePath().normalize()) | ||
| .map(Path::toUri) | ||
| .toList() | ||
| ); | ||
|
|
||
| if (watchedFiles.isEmpty()) { | ||
| log.warn("No files to watch resolved, aborting"); | ||
| return; | ||
| } | ||
|
|
||
| log.debug("Going to watch {} files", watchedFiles.size()); | ||
| log.trace("Watching files: {}", watchedFiles.stream().map(URI::toString).toList()); | ||
|
|
||
| var directories = filesToWatch | ||
| .stream() | ||
| .map(Path::getParent) | ||
| .distinct() | ||
| .toList(); | ||
|
|
||
| directories | ||
| .forEach(dir -> { | ||
| try { | ||
| var key = dir.register(watchService, ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE); | ||
| watchDirsByKey.put(key, dir); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| }); | ||
|
|
||
| log.trace("Watching directories: {}", directories.stream().map(Path::toString).toList()); | ||
| } | ||
|
|
||
| public void watchLoop() { | ||
| while (true) { | ||
| try { | ||
| var key = watchService.take(); | ||
| Path dir = watchDirsByKey.get(key); | ||
| if (dir == null) { | ||
| continue; | ||
| } | ||
|
|
||
| for (WatchEvent<?> event : key.pollEvents()) { | ||
| Path relativePath = (Path) event.context(); | ||
| Path path = dir.resolve(relativePath); | ||
| if (watchedFiles.contains(path.toAbsolutePath().normalize().toUri())) { | ||
| reloader.accept(path); | ||
| } | ||
| } | ||
| key.reset(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } catch (ClosedWatchServiceException e) { | ||
| log.trace("Watch service closed, exiting watcher thread"); | ||
| break; | ||
| } catch (Exception e) { | ||
| log.error("Error while calling the reloader", e); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| watchService.close(); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.