Skip to content

Commit

Permalink
Introduce RequestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
opeco17 committed Apr 13, 2024
1 parent fd68d22 commit ff91175
Show file tree
Hide file tree
Showing 25 changed files with 459 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.AbstractScmAccessor;
import org.springframework.cloud.config.server.support.AbstractScmAccessorProperties;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

Expand Down Expand Up @@ -57,17 +58,18 @@ public synchronized Environment findOne(String application, String profile, Stri

@Override
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label, includeOrigin, false);
return findOne(application, profile, label, includeOrigin,
new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(),
new NativeEnvironmentProperties(), this.observationRegistry);
Locations locations = getLocations(application, profile, label, forceRefresh);
Locations locations = getLocations(application, profile, label, ctx);
delegate.setSearchLocations(locations.getLocations());
Environment result = delegate.findOne(application, profile, "", includeOrigin, forceRefresh);
Environment result = delegate.findOne(application, profile, "", includeOrigin, ctx);
result.setVersion(locations.getVersion());
result.setLabel(label);
return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.OrderComparator;

/**
Expand Down Expand Up @@ -77,25 +78,25 @@ public Environment findOne(String application, String profile, String label) {

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label, includeOrigin, false);
return findOne(application, profile, label, includeOrigin,
new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
Environment env = new Environment(application, new String[] { profile }, label, null, null);
if (this.environmentRepositories.size() == 1) {
Environment envRepo = this.environmentRepositories.get(0).findOne(application, profile, label,
includeOrigin, forceRefresh);
includeOrigin, ctx);
env.addAll(envRepo.getPropertySources());
env.setVersion(envRepo.getVersion());
env.setState(envRepo.getState());
}
else {
for (EnvironmentRepository repo : environmentRepositories) {
try {
env.addAll(repo.findOne(application, profile, label, includeOrigin, forceRefresh)
.getPropertySources());
env.addAll(repo.findOne(application, profile, label, includeOrigin, ctx).getPropertySources());
}
catch (Exception e) {
if (failOnError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.cloud.config.environment.EnvironmentMediaType;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.PathUtils;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -108,34 +109,38 @@ public void setAcceptEmpty(boolean acceptEmpty) {
produces = MediaType.APPLICATION_JSON_VALUE)
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, null, false, forceRefresh);
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
return getEnvironment(name, profiles, null, false, ctx);
}

@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
produces = EnvironmentMediaType.V2_JSON)
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, null, true, forceRefresh);
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
return getEnvironment(name, profiles, null, true, ctx);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, label, false, forceRefresh);
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
return getEnvironment(name, profiles, label, false, ctx);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = EnvironmentMediaType.V2_JSON)
public Environment labelledIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label, @RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, label, true, forceRefresh);
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
return getEnvironment(name, profiles, label, true, ctx);
}

public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
try {
name = normalize(name);
label = normalize(label);
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, forceRefresh);
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, ctx);
if (!this.acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())) {
throw new EnvironmentNotFoundException("Profile Not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.environment.PropertyValueDescriptor;
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
import org.springframework.cloud.config.server.support.RequestContext;

/**
* A delegating {@link EnvironmentRepository} that can decrypt the properties if an
Expand Down Expand Up @@ -61,13 +62,12 @@ public Environment findOne(String name, String profiles, String label) {

@Override
public Environment findOne(String name, String profiles, String label, boolean includeOrigin) {
return findOne(name, profiles, label, includeOrigin, false);
return findOne(name, profiles, label, includeOrigin, new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public Environment findOne(String name, String profiles, String label, boolean includeOrigin,
boolean forceRefresh) {
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, forceRefresh);
public Environment findOne(String name, String profiles, String label, boolean includeOrigin, RequestContext ctx) {
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, ctx);
if (this.environmentEncryptors != null) {
for (EnvironmentEncryptor environmentEncryptor : environmentEncryptors) {
environment = environmentEncryptor.decrypt(environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.config.server.environment;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;

/**
* @author Dave Syer
Expand All @@ -31,7 +32,7 @@ default Environment findOne(String application, String profile, String label, bo
}

default Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
return findOne(application, profile, label, includeOrigin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.UrlResource;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -269,24 +270,24 @@ public void setSkipSslValidation(boolean skipSslValidation) {

@Override
public synchronized Locations getLocations(String application, String profile, String label) {
return getLocations(application, profile, label, false);
return getLocations(application, profile, label, new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public synchronized Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
public synchronized Locations getLocations(String application, String profile, String label, RequestContext ctx) {
if (label == null) {
label = this.defaultLabel;
}
String version;
try {
version = refresh(label, forceRefresh);
version = refresh(label, ctx.getForceRefresh());
}
catch (Exception e) {
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
&& tryMasterBranch) {
logger.info("Could not refresh default label " + label, e);
logger.info("Will try to refresh master label instead.");
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, forceRefresh);
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, ctx.getForceRefresh());
}
else {
throw e;
Expand Down Expand Up @@ -488,6 +489,7 @@ private Ref checkout(Git git, String label) throws GitAPIException {
}

protected boolean shouldPull(Git git, boolean forceRefresh) throws GitAPIException {
System.out.println(forceRefresh);
boolean shouldPull;
if (!(this.allowForceRefresh && forceRefresh) && (this.refreshRate < 0 || (this.refreshRate > 0
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000)))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.beans.BeanUtils;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -119,14 +120,14 @@ public void setRepos(Map<String, PatternMatchingJGitEnvironmentRepository> repos
}

@Override
public Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
public Locations getLocations(String application, String profile, String label, RequestContext ctx) {
for (PatternMatchingJGitEnvironmentRepository repository : this.repos.values()) {
if (repository.matches(application, profile, label)) {
for (JGitEnvironmentRepository candidate : getRepositories(repository, application, profile, label)) {
try {
Environment source = candidate.findOne(application, profile, label, false, forceRefresh);
Environment source = candidate.findOne(application, profile, label, false, ctx);
if (source != null) {
return candidate.getLocations(application, profile, label, forceRefresh);
return candidate.getLocations(application, profile, label, ctx);
}
}
catch (Exception e) {
Expand All @@ -141,23 +142,22 @@ public Locations getLocations(String application, String profile, String label,
}
JGitEnvironmentRepository candidate = getRepository(this, application, profile, label);
if (candidate == this) {
return super.getLocations(application, profile, label, forceRefresh);
return super.getLocations(application, profile, label, ctx);
}
return candidate.getLocations(application, profile, label, forceRefresh);
return candidate.getLocations(application, profile, label, ctx);
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
for (PatternMatchingJGitEnvironmentRepository repository : this.repos.values()) {
if (repository.matches(application, profile, label)) {
for (JGitEnvironmentRepository candidate : getRepositories(repository, application, profile, label)) {
try {
if (label == null) {
label = candidate.getDefaultLabel();
}
Environment source = candidate.findOne(application, profile, label, includeOrigin,
forceRefresh);
Environment source = candidate.findOne(application, profile, label, includeOrigin, ctx);
if (source != null) {
return source;
}
Expand All @@ -177,26 +177,26 @@ public Environment findOne(String application, String profile, String label, boo
label = candidate.getDefaultLabel();
}
try {
return findOneFromCandidate(candidate, application, profile, label, includeOrigin, forceRefresh);
return findOneFromCandidate(candidate, application, profile, label, includeOrigin, ctx);
}
catch (Exception e) {
if (MultipleJGitEnvironmentProperties.MAIN_LABEL.equals(label) && isTryMasterBranch()) {
logger.info("Cannot find Environment with default label " + getDefaultLabel(), e);
logger.info("Will try to find Environment master label instead.");
candidate = getRepository(this, application, profile, MultipleJGitEnvironmentProperties.MASTER_LABEL);
return findOneFromCandidate(candidate, application, profile,
MultipleJGitEnvironmentProperties.MASTER_LABEL, includeOrigin, forceRefresh);
MultipleJGitEnvironmentProperties.MASTER_LABEL, includeOrigin, ctx);
}
throw e;
}
}

private Environment findOneFromCandidate(JGitEnvironmentRepository candidate, String application, String profile,
String label, boolean includeOrigin, boolean forceRefresh) {
String label, boolean includeOrigin, RequestContext ctx) {
if (candidate == this) {
return super.findOne(application, profile, label, includeOrigin, forceRefresh);
return super.findOne(application, profile, label, includeOrigin, ctx);
}
return candidate.findOne(application, profile, label, includeOrigin, forceRefresh);
return candidate.findOne(application, profile, label, includeOrigin, ctx);
}

private List<JGitEnvironmentRepository> getRepositories(JGitEnvironmentRepository repository, String application,
Expand Down Expand Up @@ -298,14 +298,14 @@ public boolean matches(String application, String profile, String label) {

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {

if (this.pattern == null || this.pattern.length == 0) {
return null;
}

if (PatternMatchUtils.simpleMatch(this.pattern, application + "/" + profile)) {
return super.findOne(application, profile, label, includeOrigin, forceRefresh);
return super.findOne(application, profile, label, includeOrigin, ctx);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.boot.context.config.StandardConfigDataResource;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
Expand Down Expand Up @@ -137,12 +138,11 @@ public Environment findOne(String config, String profile, String label) {

@Override
public Environment findOne(String config, String profile, String label, boolean includeOrigin) {
return findOne(config, profile, label, includeOrigin, false);
return findOne(config, profile, label, includeOrigin, new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public Environment findOne(String config, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
public Environment findOne(String config, String profile, String label, boolean includeOrigin, RequestContext ctx) {

try {
ConfigurableEnvironment environment = getEnvironment(config, profile, label);
Expand All @@ -161,7 +161,7 @@ public void onPropertySourceAdded(org.springframework.core.env.PropertySource<?>
environment.getPropertySources().remove("config-data-setup");
return clean(ObservationEnvironmentRepositoryWrapper
.wrap(this.observationRegistry, new PassthruEnvironmentRepository(environment))
.findOne(config, profile, label, includeOrigin, forceRefresh), propertySourceToConfigData);
.findOne(config, profile, label, includeOrigin, ctx), propertySourceToConfigData);
}
catch (Exception e) {
String msg = String.format("Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.observation.ObservationRegistry;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;

/**
* Wraps a {@link EnvironmentRepository} execution with observation.
Expand Down Expand Up @@ -68,17 +69,18 @@ public Environment findOne(String application, String profile, String label) {

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label, includeOrigin, false);
return findOne(application, profile, label, includeOrigin,
new RequestContext.Builder().forceRefresh(false).build());
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
RequestContext ctx) {
ObservationEnvironmentRepositoryContext context = new ObservationEnvironmentRepositoryContext(
this.delegate.getClass(), application, profile, label);
return DocumentedConfigObservation.ENVIRONMENT_REPOSITORY
.observation(null, CONVENTION, () -> context, this.registry)
.observe(() -> this.delegate.findOne(application, profile, label, includeOrigin, forceRefresh));
.observe(() -> this.delegate.findOne(application, profile, label, includeOrigin, ctx));
}

/**
Expand Down
Loading

0 comments on commit ff91175

Please sign in to comment.