Skip to content

Commit fd68d22

Browse files
committed
Enable force refresh by forceRefresh query parameter
1 parent c372fcf commit fd68d22

26 files changed

+561
-264
lines changed

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractScmEnvironmentRepository.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@ public synchronized Environment findOne(String application, String profile, Stri
5757

5858
@Override
5959
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
60+
return findOne(application, profile, label, includeOrigin, false);
61+
}
62+
63+
@Override
64+
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin,
65+
boolean forceRefresh) {
6066
NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(),
6167
new NativeEnvironmentProperties(), this.observationRegistry);
62-
Locations locations = getLocations(application, profile, label);
68+
Locations locations = getLocations(application, profile, label, forceRefresh);
6369
delegate.setSearchLocations(locations.getLocations());
64-
Environment result = delegate.findOne(application, profile, "", includeOrigin);
70+
Environment result = delegate.findOne(application, profile, "", includeOrigin, forceRefresh);
6571
result.setVersion(locations.getVersion());
6672
result.setLabel(label);
6773
return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CompositeEnvironmentRepository.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,25 @@ public Environment findOne(String application, String profile, String label) {
7777

7878
@Override
7979
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
80+
return findOne(application, profile, label, includeOrigin, false);
81+
}
82+
83+
@Override
84+
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
85+
boolean forceRefresh) {
8086
Environment env = new Environment(application, new String[] { profile }, label, null, null);
8187
if (this.environmentRepositories.size() == 1) {
8288
Environment envRepo = this.environmentRepositories.get(0).findOne(application, profile, label,
83-
includeOrigin);
89+
includeOrigin, forceRefresh);
8490
env.addAll(envRepo.getPropertySources());
8591
env.setVersion(envRepo.getVersion());
8692
env.setState(envRepo.getState());
8793
}
8894
else {
8995
for (EnvironmentRepository repo : environmentRepositories) {
9096
try {
91-
env.addAll(repo.findOne(application, profile, label, includeOrigin).getPropertySources());
97+
env.addAll(repo.findOne(application, profile, label, includeOrigin, forceRefresh)
98+
.getPropertySources());
9299
}
93100
catch (Exception e) {
94101
if (failOnError) {

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,36 @@ public void setAcceptEmpty(boolean acceptEmpty) {
106106

107107
@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
108108
produces = MediaType.APPLICATION_JSON_VALUE)
109-
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
110-
return getEnvironment(name, profiles, null, false);
109+
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles,
110+
@RequestParam(defaultValue = "false") boolean forceRefresh) {
111+
return getEnvironment(name, profiles, null, false, forceRefresh);
111112
}
112113

113114
@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
114115
produces = EnvironmentMediaType.V2_JSON)
115-
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles) {
116-
return getEnvironment(name, profiles, null, true);
116+
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
117+
@RequestParam(defaultValue = "false") boolean forceRefresh) {
118+
return getEnvironment(name, profiles, null, true, forceRefresh);
117119
}
118120

119121
@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
120-
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) {
121-
return getEnvironment(name, profiles, label, false);
122+
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label,
123+
@RequestParam(defaultValue = "false") boolean forceRefresh) {
124+
return getEnvironment(name, profiles, label, false, forceRefresh);
122125
}
123126

124127
@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = EnvironmentMediaType.V2_JSON)
125128
public Environment labelledIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
126-
@PathVariable String label) {
127-
return getEnvironment(name, profiles, label, true);
129+
@PathVariable String label, @RequestParam(defaultValue = "false") boolean forceRefresh) {
130+
return getEnvironment(name, profiles, label, true, forceRefresh);
128131
}
129132

130-
public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin) {
133+
public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin,
134+
boolean forceRefresh) {
131135
try {
132136
name = normalize(name);
133137
label = normalize(label);
134-
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin);
138+
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, forceRefresh);
135139
if (!this.acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())) {
136140
throw new EnvironmentNotFoundException("Profile Not found");
137141
}
@@ -153,16 +157,17 @@ private String normalize(String part) {
153157

154158
@GetMapping("/{name}-{profiles}.properties")
155159
public ResponseEntity<String> properties(@PathVariable String name, @PathVariable String profiles,
156-
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws IOException {
157-
return labelledProperties(name, profiles, null, resolvePlaceholders);
160+
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
161+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws IOException {
162+
return labelledProperties(name, profiles, null, resolvePlaceholders, forceRefresh);
158163
}
159164

160165
@GetMapping("/{label}/{name}-{profiles}.properties")
161166
public ResponseEntity<String> labelledProperties(@PathVariable String name, @PathVariable String profiles,
162-
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
163-
throws IOException {
167+
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
168+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws IOException {
164169
validateProfiles(profiles);
165-
Environment environment = labelled(name, profiles, label);
170+
Environment environment = labelled(name, profiles, label, forceRefresh);
166171
Map<String, Object> properties = convertToProperties(environment);
167172
String propertiesString = getPropertiesString(properties);
168173
if (resolvePlaceholders) {
@@ -173,16 +178,17 @@ public ResponseEntity<String> labelledProperties(@PathVariable String name, @Pat
173178

174179
@GetMapping("{name}-{profiles}.json")
175180
public ResponseEntity<String> jsonProperties(@PathVariable String name, @PathVariable String profiles,
176-
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
177-
return labelledJsonProperties(name, profiles, null, resolvePlaceholders);
181+
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
182+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
183+
return labelledJsonProperties(name, profiles, null, resolvePlaceholders, forceRefresh);
178184
}
179185

180186
@GetMapping("/{label}/{name}-{profiles}.json")
181187
public ResponseEntity<String> labelledJsonProperties(@PathVariable String name, @PathVariable String profiles,
182-
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
183-
throws Exception {
188+
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
189+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
184190
validateProfiles(profiles);
185-
Environment environment = labelled(name, profiles, label);
191+
Environment environment = labelled(name, profiles, label, forceRefresh);
186192
Map<String, Object> properties = convertToMap(environment);
187193
String json = this.objectMapper.writeValueAsString(properties);
188194
if (resolvePlaceholders) {
@@ -204,16 +210,17 @@ private String getPropertiesString(Map<String, Object> properties) {
204210

205211
@GetMapping({ "/{name}-{profiles}.yml", "/{name}-{profiles}.yaml" })
206212
public ResponseEntity<String> yaml(@PathVariable String name, @PathVariable String profiles,
207-
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
208-
return labelledYaml(name, profiles, null, resolvePlaceholders);
213+
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
214+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
215+
return labelledYaml(name, profiles, null, resolvePlaceholders, forceRefresh);
209216
}
210217

211218
@GetMapping({ "/{label}/{name}-{profiles}.yml", "/{label}/{name}-{profiles}.yaml" })
212219
public ResponseEntity<String> labelledYaml(@PathVariable String name, @PathVariable String profiles,
213-
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
214-
throws Exception {
220+
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
221+
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
215222
validateProfiles(profiles);
216-
Environment environment = labelled(name, profiles, label);
223+
Environment environment = labelled(name, profiles, label, forceRefresh);
217224
Map<String, Object> result = convertToMap(environment);
218225
if (this.stripDocument && result.size() == 1 && result.keySet().iterator().next().equals("document")) {
219226
Object value = result.get("document");

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentEncryptorEnvironmentRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ public Environment findOne(String name, String profiles, String label) {
6161

6262
@Override
6363
public Environment findOne(String name, String profiles, String label, boolean includeOrigin) {
64-
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin);
64+
return findOne(name, profiles, label, includeOrigin, false);
65+
}
66+
67+
@Override
68+
public Environment findOne(String name, String profiles, String label, boolean includeOrigin,
69+
boolean forceRefresh) {
70+
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, forceRefresh);
6571
if (this.environmentEncryptors != null) {
6672
for (EnvironmentEncryptor environmentEncryptor : environmentEncryptors) {
6773
environment = environmentEncryptor.decrypt(environment);

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ default Environment findOne(String application, String profile, String label, bo
3030
return findOne(application, profile, label);
3131
}
3232

33+
default Environment findOne(String application, String profile, String label, boolean includeOrigin,
34+
boolean forceRefresh) {
35+
return findOne(application, profile, label, includeOrigin);
36+
}
37+
3338
}

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
9090
*/
9191
private int refreshRate = 0;
9292

93+
/**
94+
* Allow forceRefresh query parameter to force refresh the git repository regardless
95+
* of refreshRate if true.
96+
*/
97+
private boolean allowForceRefresh = false;
98+
9399
/**
94100
* Valid SSH private key. Must be set if ignoreLocalSshSettings is true and Git URI is
95101
* SSH format.
@@ -189,6 +195,14 @@ public void setRefreshRate(int refreshRate) {
189195
this.refreshRate = refreshRate;
190196
}
191197

198+
public boolean getAllowForceRefresh() {
199+
return this.allowForceRefresh;
200+
}
201+
202+
public void setAllowForceRefresh(boolean allowForceRefresh) {
203+
this.allowForceRefresh = allowForceRefresh;
204+
}
205+
192206
public String getPrivateKey() {
193207
return this.privateKey;
194208
}

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
9696
*/
9797
private int timeout;
9898

99+
/**
100+
* Allow forceRefresh query parameter to force refresh the git repository regardless
101+
* of refreshRate if true.
102+
*/
103+
public boolean allowForceRefresh = false;
104+
99105
/**
100106
* Time (in seconds) between refresh of the git repository.
101107
*/
@@ -158,6 +164,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
158164
this.timeout = properties.getTimeout();
159165
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
160166
this.refreshRate = properties.getRefreshRate();
167+
this.allowForceRefresh = properties.getAllowForceRefresh();
161168
this.skipSslValidation = properties.isSkipSslValidation();
162169
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
163170
this.tryMasterBranch = properties.isTryMasterBranch();
@@ -196,6 +203,14 @@ public void setRefreshRate(int refreshRate) {
196203
this.refreshRate = refreshRate;
197204
}
198205

206+
public boolean getAllowForceRefresh() {
207+
return this.allowForceRefresh;
208+
}
209+
210+
public void setAllowForceRefresh(boolean allowForceRefresh) {
211+
this.allowForceRefresh = allowForceRefresh;
212+
}
213+
199214
public TransportConfigCallback getTransportConfigCallback() {
200215
return this.transportConfigCallback;
201216
}
@@ -254,19 +269,24 @@ public void setSkipSslValidation(boolean skipSslValidation) {
254269

255270
@Override
256271
public synchronized Locations getLocations(String application, String profile, String label) {
272+
return getLocations(application, profile, label, false);
273+
}
274+
275+
@Override
276+
public synchronized Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
257277
if (label == null) {
258278
label = this.defaultLabel;
259279
}
260280
String version;
261281
try {
262-
version = refresh(label);
282+
version = refresh(label, forceRefresh);
263283
}
264284
catch (Exception e) {
265285
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
266286
&& tryMasterBranch) {
267287
logger.info("Could not refresh default label " + label, e);
268288
logger.info("Will try to refresh master label instead.");
269-
version = refresh(JGitEnvironmentProperties.MASTER_LABEL);
289+
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, forceRefresh);
270290
}
271291
else {
272292
throw e;
@@ -289,11 +309,11 @@ public synchronized void afterPropertiesSet() throws Exception {
289309
* @param label label to refresh
290310
* @return head id
291311
*/
292-
public String refresh(String label) {
312+
public String refresh(String label, boolean forceRefresh) {
293313
Git git = null;
294314
try {
295315
git = createGitClient();
296-
if (shouldPull(git)) {
316+
if (shouldPull(git, forceRefresh)) {
297317
FetchResult fetchStatus = fetch(git, label);
298318
if (this.deleteUntrackedBranches && fetchStatus != null) {
299319
deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);
@@ -467,11 +487,10 @@ private Ref checkout(Git git, String label) throws GitAPIException {
467487
return checkout.call();
468488
}
469489

470-
protected boolean shouldPull(Git git) throws GitAPIException {
490+
protected boolean shouldPull(Git git, boolean forceRefresh) throws GitAPIException {
471491
boolean shouldPull;
472-
473-
if (this.refreshRate < 0 || (this.refreshRate > 0
474-
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000))) {
492+
if (!(this.allowForceRefresh && forceRefresh) && (this.refreshRate < 0 || (this.refreshRate > 0
493+
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000)))) {
475494
return false;
476495
}
477496

0 commit comments

Comments
 (0)