-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distributed event notification of GWC config changes
Send a distributed event upon GWC config change and react accordingly on the receiving end by forcing a reload of the `GWCConfig` and applying the change to the `ConfigurableBlobStore`, as done in `GWCSettingsPage.save()`.
- Loading branch information
Showing
6 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
src/gwc/core/src/main/java/org/geoserver/cloud/gwc/event/ConfigChangeEvent.java
This file contains 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,16 @@ | ||
package org.geoserver.cloud.gwc.event; | ||
|
||
@SuppressWarnings("serial") | ||
public class ConfigChangeEvent extends GeoWebCacheEvent { | ||
|
||
public static final String OBJECT_ID = "gwcConfig"; | ||
|
||
public ConfigChangeEvent(Object source) { | ||
super(source, Type.MODIFIED); | ||
} | ||
|
||
@Override | ||
protected String getObjectId() { | ||
return OBJECT_ID; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/gwc/core/src/main/java/org/geoserver/gwc/config/CloudGwcConfigPersister.java
This file contains 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,120 @@ | ||
/* | ||
* (c) 2024 Open Source Geospatial Foundation - all rights reserved This code is licensed under the | ||
* GPL 2.0 license, available at the root application directory. | ||
*/ | ||
package org.geoserver.gwc.config; | ||
|
||
import com.thoughtworks.xstream.XStream; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.geoserver.cloud.gwc.event.ConfigChangeEvent; | ||
import org.geoserver.config.util.XStreamPersister; | ||
import org.geoserver.config.util.XStreamPersisterFactory; | ||
import org.geoserver.gwc.ConfigurableBlobStore; | ||
import org.geoserver.platform.GeoServerExtensions; | ||
import org.geoserver.platform.GeoServerResourceLoader; | ||
import org.geoserver.platform.resource.Resource; | ||
import org.geoserver.platform.resource.Resource.Type; | ||
import org.geoserver.util.DimensionWarning; | ||
import org.geowebcache.storage.blobstore.memory.CacheConfiguration; | ||
import org.springframework.context.event.EventListener; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* extends {@link GWCConfigPersister} to send {@link ConfigChangeEvent}s upon {@link | ||
* #save(org.geoserver.gwc.config.GWCConfig)} | ||
*/ | ||
@Slf4j | ||
public class CloudGwcConfigPersister extends GWCConfigPersister { | ||
|
||
private Consumer<ConfigChangeEvent> eventPublisher; | ||
private XStreamPersisterFactory xspf; | ||
private GWCConfig configuration; | ||
|
||
public CloudGwcConfigPersister( | ||
XStreamPersisterFactory xspf, | ||
GeoServerResourceLoader resourceLoader, | ||
Consumer<ConfigChangeEvent> eventPublisher) { | ||
super(xspf, resourceLoader); | ||
this.xspf = xspf; | ||
this.eventPublisher = eventPublisher; | ||
} | ||
|
||
/** | ||
* Override to get a hold to the config and be able of re-loading it upon {@link | ||
* ConfigChangeEvent}, since super.config is private | ||
*/ | ||
@Override | ||
public GWCConfig getConfig() { | ||
if (null == configuration) { | ||
configuration = super.getConfig(); | ||
} | ||
return configuration; | ||
} | ||
|
||
/** | ||
* Override to publish a {@link ConfigChangeEvent} | ||
* | ||
* @see #reloadOnRemoteConfigChangeEvent(ConfigChangeEvent) | ||
*/ | ||
@Override | ||
public void save(final GWCConfig config) throws IOException { | ||
super.save(config); | ||
this.configuration = config; | ||
eventPublisher.accept(new ConfigChangeEvent(this)); | ||
} | ||
|
||
@EventListener(ConfigChangeEvent.class) | ||
public void reloadOnRemoteConfigChangeEvent(ConfigChangeEvent event) throws IOException { | ||
final boolean isRemote = event.getSource() != this; | ||
if (isRemote) { | ||
log.info("Reloading gwc configuration upon remote config change event"); | ||
GWCConfig config = reload(); | ||
this.configuration = config; | ||
|
||
// Update ConfigurableBlobStore | ||
ConfigurableBlobStore blobstore = GeoServerExtensions.bean(ConfigurableBlobStore.class); | ||
if (blobstore != null) { | ||
blobstore.setChanged(config, false); | ||
} | ||
} | ||
} | ||
|
||
// super.loadConfig() is private | ||
private synchronized GWCConfig reload() throws IOException { | ||
Resource configFile = findConfigFile(); | ||
if (configFile == null || configFile.getType() == Type.UNDEFINED) { | ||
throw new IllegalStateException( | ||
"gwc config resource does not exist: %s".formatted(GWC_CONFIG_FILE)); | ||
} | ||
|
||
XStreamPersister xmlPersister = this.xspf.createXMLPersister(); | ||
configure(xmlPersister.getXStream()); | ||
try (InputStream in = configFile.in()) { | ||
return xmlPersister.load(in, GWCConfig.class); | ||
} | ||
} | ||
|
||
// super.configureXstream() is private | ||
private void configure(XStream xs) { | ||
xs.alias("GeoServerGWCConfig", GWCConfig.class); | ||
xs.alias("defaultCachingGridSetIds", HashSet.class); | ||
xs.alias("defaultCoverageCacheFormats", HashSet.class); | ||
xs.alias("defaultVectorCacheFormats", HashSet.class); | ||
xs.alias("defaultOtherCacheFormats", HashSet.class); | ||
xs.alias("InnerCacheConfiguration", CacheConfiguration.class); | ||
xs.alias("warning", DimensionWarning.WarningType.class); | ||
xs.allowTypes( | ||
new Class[] { | ||
GWCConfig.class, CacheConfiguration.class, DimensionWarning.WarningType.class | ||
}); | ||
xs.addDefaultImplementation(LinkedHashSet.class, Set.class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...wc/integration-bus/src/main/java/org/geoserver/cloud/gwc/bus/RemoteConfigChangeEvent.java
This file contains 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,28 @@ | ||
/* | ||
* (c) 2024 Open Source Geospatial Foundation - all rights reserved This code is licensed under the | ||
* GPL 2.0 license, available at the root application directory. | ||
*/ | ||
package org.geoserver.cloud.gwc.bus; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import lombok.NonNull; | ||
|
||
import org.geoserver.cloud.gwc.event.ConfigChangeEvent; | ||
|
||
/** | ||
* @since 1.9 | ||
*/ | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
@SuppressWarnings("serial") | ||
public class RemoteConfigChangeEvent extends RemoteGeoWebCacheEvent { | ||
|
||
public RemoteConfigChangeEvent(Object source, @NonNull String originService) { | ||
super(source, originService); | ||
} | ||
|
||
protected @Override String getObjectId() { | ||
return ConfigChangeEvent.OBJECT_ID; | ||
} | ||
} |
This file contains 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