Skip to content

Commit 60e3eae

Browse files
committed
Removed unnecessary logic
1 parent 0890ecf commit 60e3eae

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileFileSupplier.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.file.Path;
1919
import java.util.Objects;
2020
import java.util.Optional;
21+
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.atomic.AtomicReference;
2223
import java.util.function.Supplier;
2324
import software.amazon.awssdk.annotations.SdkPublicApi;
@@ -105,8 +106,8 @@ static ProfileFileSupplier fixedProfileFile(ProfileFile profileFile) {
105106
}
106107

107108
/**
108-
* Creates a {@link ProfileFileSupplier} by combining the {@link ProfileFile} objects from two {@code ProfileFileSupplier}s.
109-
* Objects are passed into {@link ProfileFile.Aggregator}.
109+
* Creates a {@link ProfileFileSupplier} by combining the {@link ProfileFile} objects from multiple {@code
110+
* ProfileFileSupplier}s. Objects are passed into {@link ProfileFile.Aggregator}.
110111
*
111112
* @param suppliers Array of {@code ProfileFileSupplier} objects. {@code ProfileFile} objects are passed to
112113
* {@link ProfileFile.Aggregator#addFile(ProfileFile)} in the same argument order as the supplier that
@@ -118,25 +119,41 @@ static ProfileFileSupplier aggregate(ProfileFileSupplier... suppliers) {
118119
return new ProfileFileSupplier() {
119120

120121
final AtomicReference<ProfileFile> currentAggregateProfileFile = new AtomicReference<>();
122+
final ConcurrentHashMap<Supplier<ProfileFile>, ProfileFile> currentValuesBySupplier = new ConcurrentHashMap<>();
121123

122124
@Override
123125
public ProfileFile get() {
124-
ProfileFile.Aggregator aggregator = ProfileFile.aggregator();
126+
boolean refreshAggregate = false;
125127
for (ProfileFileSupplier supplier : suppliers) {
126-
aggregator.addFile(supplier.get());
128+
if (didSuppliedValueChange(supplier)) {
129+
refreshAggregate = true;
130+
}
131+
}
132+
133+
if (refreshAggregate) {
134+
refreshCurrentAggregate();
127135
}
128136

129-
return refreshAndGetCurrentAggregate(aggregator);
137+
return currentAggregateProfileFile.get();
138+
}
139+
140+
private boolean didSuppliedValueChange(Supplier<ProfileFile> supplier) {
141+
ProfileFile next = supplier.get();
142+
ProfileFile current = currentValuesBySupplier.put(supplier, next);
143+
144+
return !Objects.equals(next, current);
130145
}
131146

132-
private ProfileFile refreshAndGetCurrentAggregate(ProfileFile.Aggregator aggregator) {
147+
private void refreshCurrentAggregate() {
148+
ProfileFile.Aggregator aggregator = ProfileFile.aggregator();
149+
currentValuesBySupplier.values()
150+
.stream()
151+
.forEach(aggregator::addFile);
133152
ProfileFile current = currentAggregateProfileFile.get();
134153
ProfileFile next = aggregator.build();
135154
if (!Objects.equals(current, next)) {
136155
currentAggregateProfileFile.compareAndSet(current, next);
137156
}
138-
139-
return currentAggregateProfileFile.get();
140157
}
141158

142159
};

core/profiles/src/main/java/software/amazon/awssdk/profiles/internal/ProfileFileRefresher.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.time.Instant;
2424
import java.util.Objects;
2525
import java.util.function.Consumer;
26-
import java.util.function.Function;
2726
import java.util.function.Supplier;
2827
import software.amazon.awssdk.annotations.SdkInternalApi;
2928
import software.amazon.awssdk.annotations.SdkTestInternalApi;
@@ -44,12 +43,10 @@ public final class ProfileFileRefresher {
4443
private volatile ProfileFileRefreshRecord currentRefreshRecord;
4544
private final Supplier<ProfileFile> profileFile;
4645
private final Path profileFilePath;
47-
private final Function<RuntimeException, ProfileFile> exceptionHandler;
4846
private final Consumer<ProfileFile> onProfileFileReload;
4947
private final Clock clock;
5048

5149
private ProfileFileRefresher(Builder builder) {
52-
this.exceptionHandler = builder.exceptionHandler;
5350
this.clock = builder.clock;
5451
this.profileFile = builder.profileFile;
5552
this.profileFilePath = builder.profileFilePath;
@@ -81,18 +78,7 @@ public ProfileFile refreshIfStale() {
8178
}
8279

8380
private RefreshResult<ProfileFileRefreshRecord> refreshResult() {
84-
try {
85-
return reloadAsRefreshResultIfStale();
86-
} catch (RuntimeException exception) {
87-
Instant now = Instant.now();
88-
ProfileFile exceptionProfileFile = exceptionHandler.apply(exception);
89-
ProfileFileRefreshRecord refreshRecord = ProfileFileRefreshRecord.builder()
90-
.profileFile(exceptionProfileFile)
91-
.refreshTime(now)
92-
.build();
93-
94-
return wrapIntoRefreshResult(refreshRecord, now);
95-
}
81+
return reloadAsRefreshResultIfStale();
9682
}
9783

9884
private RefreshResult<ProfileFileRefreshRecord> reloadAsRefreshResultIfStale() {
@@ -156,7 +142,6 @@ public static final class Builder {
156142
private Supplier<ProfileFile> profileFile;
157143
private Path profileFilePath;
158144
private Consumer<ProfileFile> onProfileFileReload = p -> { };
159-
private Function<RuntimeException, ProfileFile> exceptionHandler;
160145
private Clock clock = Clock.systemUTC();
161146

162147
private Builder() {
@@ -181,15 +166,6 @@ public Builder clock(Clock clock) {
181166
return this;
182167
}
183168

184-
/**
185-
* @param exceptionHandler Handler which takes action when a Runtime exception occurs while loading a profile file.
186-
* Handler can return a previously stored profile file or throw back the exception.
187-
*/
188-
public Builder exceptionHandler(Function<RuntimeException, ProfileFile> exceptionHandler) {
189-
this.exceptionHandler = exceptionHandler;
190-
return this;
191-
}
192-
193169
/**
194170
* Sets a custom action to perform when a profile file is reloaded. This action is executed when both the cache is stale
195171
* and the disk file associated with the profile file has been modified since the last load.

0 commit comments

Comments
 (0)