Skip to content

Commit

Permalink
Fixed service supply. Now we try to use all matchin service descripto…
Browse files Browse the repository at this point in the history
…rs even for a single instance, as any may return an empty optional (if appropriate supplier type), and we would not yield an instance, even when we should.

As a result the method "explodeFilterAndSort" was going through all service manager and collected all instances - which resulted in errors if a provider with lower weight was not capable of creating an intance because of unsatisfied dependency.
  • Loading branch information
tomas-langer committed Dec 17, 2024
1 parent 990ba2c commit ef78142
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 0 additions & 1 deletion integrations/oci/tls-certificates/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@
<CA_OCID>SETME</CA_OCID>
<SERVER_CERT_OCID>SETME</SERVER_CERT_OCID>
<SERVER_KEY_OCID>SETME</SERVER_KEY_OCID>

<oci.real.usage>false</oci.real.usage>
</systemPropertyVariables>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void configIsMonitoredForChange() throws Exception {
assertThat("sanity",
caCertDownloadCountBaseline0,
equalTo(0));
assertThat("santiy",
assertThat("sanity",
pkDownloadCountBaseLine0,
equalTo(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ final class ServiceSupplies {
private ServiceSupplies() {
}

private static <T> Optional<ServiceInstance<T>> oneInstance(Lookup lookup,
List<ServiceManager<T>> serviceManagers) {
// we may have more than one service manager, as the first one(s) may not provide a value
traceLookup(lookup, "explode, filter, and sort");

for (ServiceManager<T> serviceManager : serviceManagers) {
Optional<ServiceInstance<T>> thisManager = serviceManager.activator()
.instances(lookup)
.stream()
.flatMap(List::stream)
.map(it -> serviceManager.registryInstance(lookup, it))
.findFirst();

traceLookupInstance(lookup, serviceManager, thisManager.map(List::of).orElseGet(List::of));

if (thisManager.isPresent()) {
return thisManager;
}
}

return Optional.empty();
}

private static <T> List<ServiceInstance<T>> explodeFilterAndSort(Lookup lookup,
List<ServiceManager<T>> serviceManagers) {
// this method is called when we resolve instances, so we can safely assume any scope is active
Expand Down Expand Up @@ -133,9 +156,7 @@ static class ServiceSupply<T> extends ServiceSupplyBase<T> implements Supplier<T

Supplier<T> supplier;

supplier = () -> explodeFilterAndSort(lookup, managers)
.stream()
.findFirst()
supplier = () -> oneInstance(lookup, managers)
.map(ServiceInstance::get)
.orElseThrow(() -> new ServiceRegistryException(
"Neither of matching services could provide a value. Descriptors: " + managers + ", "
Expand All @@ -158,9 +179,7 @@ static class ServiceSupplyOptional<T> extends ServiceSupplyBase<T> implements Su

@Override
public Optional<T> get() {
Optional<ServiceInstance<T>> first = explodeFilterAndSort(super.lookup, super.managers)
.stream()
.findFirst();
Optional<ServiceInstance<T>> first = oneInstance(super.lookup, super.managers);
return first.map(Supplier::get);
}
}
Expand All @@ -173,9 +192,7 @@ static class ServiceInstanceSupplyOptional<T> extends ServiceSupplyBase<T> imple

@Override
public Optional<ServiceInstance<T>> get() {
return explodeFilterAndSort(super.lookup, super.managers)
.stream()
.findFirst();
return oneInstance(super.lookup, super.managers);
}
}

Expand Down

0 comments on commit ef78142

Please sign in to comment.