Skip to content

Commit

Permalink
Fixes for race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
JornC committed Feb 6, 2022
1 parent 21527e5 commit 4db453f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public class SearchContext {
public Map<String, SearchSuggestion> getResults() {
final Map<String, SearchSuggestion> ret = new HashMap<>();

ret.putAll(cache);
if (searching) {
ret.putAll(cache);
}
ret.putAll(results);

return ret;
Expand All @@ -50,6 +52,10 @@ public void addResults(final Collection<SearchSuggestion> results) {
}

public void setCacheResults(final Collection<SearchSuggestion> results) {
if (!isSearching()) {
return;
}

this.cache.putAll(results.stream()
.collect(Collectors.toMap(v -> v.id, v -> v)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface SearchDaemonAsynchronousEventBinder extends EventBinder<SearchDaemonAs

@Inject SearchServiceAsync service;

private final GWTAtomicInteger cacheCounter = new GWTAtomicInteger();
private final GWTAtomicInteger counter = new GWTAtomicInteger();

private String currentSearchId;
Expand All @@ -91,11 +92,11 @@ private void processFailure(final Integer count, final Throwable e) {

private void processResult(final Integer count, final SearchResult result) {
if (!countMatches(count)) {
processOldResults(result);
processOldResults(count, result);

// And try once more if there are no results
if (result.results.length == 0) {
fetchOldResults(result.uuid);
fetchOldResults(count, result.uuid);
}
return;
}
Expand All @@ -121,12 +122,21 @@ public void onSearchTextCommand(final SearchTextCommand c) {
/**
* Process old results, by actualizing the cache result set (but not the
* 'actual' result set).
*
* @param count
*/
private void processOldResults(final SearchResult result) {
private void processOldResults(final Integer count, final SearchResult result) {
if (result.complete) {
context.clearCache();
}

// Increment cache counter so we only process results that are newer than the
// oldest we know
if (cacheCounter.get() > count) {
return;
}
cacheCounter.set(count);

context.setCacheResults(Stream.of(result.results)
.collect(Collectors.toList()));
}
Expand All @@ -143,19 +153,19 @@ private void processResults(final SearchResult result, final Integer count) {
}
}

private void fetchSearchResults(final String uuid, final Integer count) {
private void fetchSearchResults(final String uuid, final int count) {
SchedulerUtil.delay(() -> {
if (!countMatches(count)) {
fetchOldResults(uuid);
fetchOldResults(count, uuid);
return;
}

service.retrieveSearchResults(currentSearchId, callbackFactory.apply(count));
}, DELAY);
}

private void fetchOldResults(final String uuid) {
service.retrieveSearchResults(uuid, AppAsyncCallback.create(this::processOldResults));
private void fetchOldResults(final int count, final String uuid) {
service.retrieveSearchResults(uuid, AppAsyncCallback.create(v -> processOldResults(count, v)));
}

private void clear() {
Expand Down

0 comments on commit 4db453f

Please sign in to comment.