Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory overhead of searches #6471

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
type: perf
issue: 6469
title: "Searching for a large number of resources can use a lot of
memory, due to the nature of deduplication of results in memory.
We will instead push this responsibility to the db to save
reduce this overhead.
"
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.springframework.beans.factory.BeanFactory;
Expand Down Expand Up @@ -376,6 +377,7 @@ public IBundleProvider registerSearch(
myContext.getResourceDefinition(theResourceType).getImplementingClass();
final ISearchBuilder<JpaPid> sb = mySearchBuilderFactory.newSearchBuilder(theResourceType, resourceTypeClass);
sb.setFetchSize(mySyncSize);
sb.setRequireTotal(theParams.getCount() != null);

final Integer loadSynchronousUpTo = getLoadSynchronousUpToOrNull(theCacheControlDirective);
boolean isOffsetQuery = theParams.isOffsetQuery();
Expand All @@ -393,14 +395,23 @@ public IBundleProvider registerSearch(

try {
return direct.get();

} catch (ResourceNotFoundInIndexException theE) {
// some resources were not found in index, so we will inform this and resort to JPA search
ourLog.warn(
"Some resources were not found in index. Make sure all resources were indexed. Resorting to database search.");
}
}

// we set a max to fetch from the db for synchronous searches;
// otherwise, we would have to load everything into memory (or force the db to do so);
// So let's set a max value here
Integer maxToLoad = ObjectUtils.firstNonNull(
loadSynchronousUpTo,
theParams.getCount() != null ? theParams.getCount() + 1 : null,
myStorageSettings.getInternalSynchronousSearchSize());
ourLog.debug("Setting a max fetch value of {} for synchronous search", maxToLoad);
sb.setMaxResultsToFetch(maxToLoad);

ourLog.debug("Search {} is loading in synchronous mode", searchUuid);
return mySynchronousSearchSvc.executeQuery(
theParams, theRequestDetails, searchUuid, sb, loadSynchronousUpTo, theRequestPartitionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public IBundleProvider executeQuery(
resources, theRequestDetails, myInterceptorBroadcaster);

SimpleBundleProvider bundleProvider = new SimpleBundleProvider(resources);
if (hasACount) {
if (hasACount && theSb.requiresTotal()) {
bundleProvider.setTotalResourcesRequestedReturned(receivedResourceCount);
}
if (theParams.isOffsetQuery()) {
Expand Down
Loading
Loading