Skip to content

Commit

Permalink
Removed Trigger check on push, added jitter to push refresh, reworked…
Browse files Browse the repository at this point in the history
… cache check
  • Loading branch information
mrm9084 committed Jun 29, 2020
1 parent 0f910ce commit 37e968f
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,32 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.microsoft.azure.spring.cloud.config.properties.AppConfigurationStoreMonitoring.PushNotification;
import com.microsoft.azure.spring.cloud.config.properties.AppConfigurationStoreTrigger;
import com.microsoft.azure.spring.cloud.config.properties.ConfigStore;

public class AppConfigurationEndpoint {

private static final String CONFIG_STORE_TOPIC = "configurationstores";

private static final String KEY = "key";

private static final String LABEL = "label";

private final JsonNode request;

private final String endpoint;

private final String store;

private AppConfigurationStoreTrigger trigger;

private List<ConfigStore> configStores;

private Map<String, String> allRequestParams;

public AppConfigurationEndpoint(JsonNode request, List<ConfigStore> configStores,
Map<String, String> allRequestParams) {
this.request = request;
this.configStores = configStores;
this.allRequestParams = allRequestParams;

JsonNode validationTopic = request.findValue(VALIDATION_TOPIC);
if (validationTopic != null) {
String topic = validationTopic.asText();
JsonNode requestTopic = request.findValue(VALIDATION_TOPIC);
if (requestTopic != null) {
String topic = requestTopic.asText();
store = topic.substring(topic.indexOf(CONFIG_STORE_TOPIC) + CONFIG_STORE_TOPIC.length() + 1);
endpoint = "https://" + store + ".azconfig.io";
} else {
store = "";
endpoint = "";
throw new IllegalArgumentException("Refresh request missing topic field.");
}

}
Expand All @@ -68,12 +57,12 @@ public boolean authenticate() {
return false;
}

if (!allRequestParams.containsKey(primaryTokenName)
|| !allRequestParams.get(primaryTokenName).equals(primaryTokenSecret)) {
if (allRequestParams.containsKey(primaryTokenName)
&& allRequestParams.get(primaryTokenName).equals(primaryTokenSecret)) {
return true;
}
if (!allRequestParams.containsKey(secondaryTokenName)
|| !allRequestParams.get(secondaryTokenName).equals(secondaryTokenSecret)) {
if (allRequestParams.containsKey(secondaryTokenName)
&& allRequestParams.get(secondaryTokenName).equals(secondaryTokenSecret)) {
return true;
}

Expand All @@ -83,24 +72,9 @@ public boolean authenticate() {
}

public boolean triggerRefresh() {
JsonNode key = request.findValue(KEY);
JsonNode label = request.findValue(LABEL);
for (ConfigStore configStore : configStores) {
if (configStore.getEndpoint().equals(endpoint) && configStore.getMonitoring().isEnabled()) {
for (AppConfigurationStoreTrigger trigger : configStore.getMonitoring().getTriggers()) {
if (trigger.getLabel() == null && label == null) {
if (key != null && key.asText().equals(trigger.getKey())) {
this.trigger = trigger;
return true;
}
} else if (label != null) {
if (key != null && key.asText().equals(trigger.getKey())
&& label.asText().equals(trigger.getLabel())) {
this.trigger = trigger;
return true;
}
}
}
return true;
}
}
return false;
Expand All @@ -120,11 +94,4 @@ public String getStore() {
return store;
}

/**
* @return the trigger
*/
public AppConfigurationStoreTrigger getTrigger() {
return trigger;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,19 @@ public AppConfigurationBusRefreshEndpoint(ApplicationEventPublisher context, Str
@ResponseBody
public String refresh(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map<String, String> allRequestParams) throws IOException {

String reference = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

JsonNode kvReference = objectmapper.readTree(reference);
AppConfigurationEndpoint validation = new AppConfigurationEndpoint(kvReference, appConfiguration.getStores(),
allRequestParams);

AppConfigurationEndpoint validation;
try {
validation = new AppConfigurationEndpoint(kvReference, appConfiguration.getStores(),
allRequestParams);
} catch (IllegalArgumentException e) {
LOGGER.error(e.getMessage());
return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
}

if (!validation.authenticate()) {
return HttpStatus.UNAUTHORIZED.getReasonPhrase();
}
Expand All @@ -68,8 +74,7 @@ public String refresh(HttpServletRequest request, HttpServletResponse response,
} else {
if (validation.triggerRefresh()) {
// Spring Bus is in use, will publish a RefreshRemoteApplicationEvent
publish(new AppConfigurationBusRefreshEvent(validation.getEndpoint(), this, getInstanceId(),
validation.getTrigger()));
publish(new AppConfigurationBusRefreshEvent(validation.getEndpoint(), this, getInstanceId()));
return HttpStatus.OK.getReasonPhrase();
} else {
LOGGER.debug("Non Refreshable notification");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@

import org.springframework.cloud.bus.event.RemoteApplicationEvent;

import com.microsoft.azure.spring.cloud.config.properties.AppConfigurationStoreTrigger;

public class AppConfigurationBusRefreshEvent extends RemoteApplicationEvent {

private static final long serialVersionUID = 1L;

private final String endpoint;

private final AppConfigurationStoreTrigger trigger;

AppConfigurationBusRefreshEvent(String endpoint, AppConfigurationBusRefreshEndpoint source, String origin,
AppConfigurationStoreTrigger trigger) {
super("Refresh Event", origin, null);
AppConfigurationBusRefreshEvent(String endpoint, AppConfigurationBusRefreshEndpoint source, String origin) {
super("App Configuration Refresh Event", origin, null);
this.endpoint = endpoint;
this.trigger = trigger;
}

/**
Expand All @@ -31,11 +25,4 @@ public String getEndpoint() {
return endpoint;
}

/**
* @return the trigger
*/
public AppConfigurationStoreTrigger getTrigger() {
return trigger;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AppConfigurationBusRefreshEventListener(AppConfigurationRefresh appConfig
@Override
public void onApplicationEvent(AppConfigurationBusRefreshEvent event) {
try {
appConfigurationRefresh.resetCache(event.getEndpoint(), event.getTrigger());
appConfigurationRefresh.resetCache(event.getEndpoint());
} catch (Exception e) {
LOGGER.error("Refresh failed with unexpected exception.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String refresh(HttpServletRequest request, HttpServletResponse response,
// Will just refresh the local configurations
// contextRefresher.refresh();
publisher.publishEvent(
new AppConfigurationRefreshEvent(validation.getEndpoint(), validation.getTrigger()));
new AppConfigurationRefreshEvent(validation.getEndpoint()));
return HttpStatus.OK.getReasonPhrase();
} else {
LOGGER.debug("Non Refreshable notification");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@

import org.springframework.context.ApplicationEvent;

import com.microsoft.azure.spring.cloud.config.properties.AppConfigurationStoreTrigger;

public class AppConfigurationRefreshEvent extends ApplicationEvent {

private static final long serialVersionUID = 1L;

private final String endpoint;

private final AppConfigurationStoreTrigger trigger;

public AppConfigurationRefreshEvent(String endpoint, AppConfigurationStoreTrigger trigger) {
super(endpoint + "/" + trigger.toString());
public AppConfigurationRefreshEvent(String endpoint) {
super(endpoint);
this.endpoint = endpoint;
this.trigger = trigger;
}

/**
Expand All @@ -29,12 +24,4 @@ public AppConfigurationRefreshEvent(String endpoint, AppConfigurationStoreTrigge
public String getEndpoint() {
return endpoint;
}

/**
* @return the trigger
*/
public AppConfigurationStoreTrigger getTrigger() {
return trigger;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AppConfigurationRefreshEventListener(AppConfigurationRefresh appConfigura
@Override
public void onApplicationEvent(AppConfigurationRefreshEvent event) {
try {
appConfigurationRefresh.resetCache(event.getEndpoint(), event.getTrigger());
appConfigurationRefresh.resetCache(event.getEndpoint());
} catch (Exception e) {
LOGGER.error("Refresh failed with unexpected exception.", e);
}
Expand Down
Loading

0 comments on commit 37e968f

Please sign in to comment.