Skip to content

DATAES-817 - StreamQueries does only delete the last scrollid. #449

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

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -93,7 +90,6 @@
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
Expand All @@ -105,6 +101,7 @@
import org.springframework.data.elasticsearch.client.reactive.HostProvider.Verification;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.Indices;
import org.springframework.data.elasticsearch.client.util.NamedXContents;
import org.springframework.data.elasticsearch.client.util.ScrollState;
import org.springframework.data.util.Lazy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -115,7 +112,6 @@
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.reactive.function.BodyExtractors;
Expand Down Expand Up @@ -835,8 +831,7 @@ private <T> Publisher<? extends T> handleClientError(String logId, Request reque
.error(new ElasticsearchStatusException(content, RestStatus.fromCode(response.statusCode().value())));
}
return Mono.just(content);
})
.doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
}).doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
.flatMap(content -> doDecode(response, responseType, content));
}

Expand Down Expand Up @@ -893,42 +888,5 @@ public Collection<ElasticsearchHost> hosts() {
}
}

/**
* Mutable state object holding scrollId to be used for {@link SearchScrollRequest#scroll(Scroll)}
*
* @author Christoph Strobl
* @since 3.2
*/
private static class ScrollState {

private final Object lock = new Object();

private final List<String> pastIds = new ArrayList<>(1);
@Nullable private String scrollId;

@Nullable
String getScrollId() {
return scrollId;
}

List<String> getScrollIds() {

synchronized (lock) {
return Collections.unmodifiableList(new ArrayList<>(pastIds));
}
}

void updateScrollId(String scrollId) {

if (StringUtils.hasText(scrollId)) {

synchronized (lock) {

this.scrollId = scrollId;
pastIds.add(scrollId);
}
}
}
}
// endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.client.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.search.Scroll;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
* Mutable state object holding scrollId to be used for {@link SearchScrollRequest#scroll(Scroll)}
*
* @author Christoph Strobl
* @author Peter-Josef Meisch
* @since 3.2
*/
public class ScrollState {

private final Object lock = new Object();

private final Set<String> pastIds = new LinkedHashSet<>();
@Nullable private String scrollId;

public ScrollState() {}

public ScrollState(String scrollId) {
updateScrollId(scrollId);
}

@Nullable
public String getScrollId() {
return scrollId;
}

public List<String> getScrollIds() {

synchronized (lock) {
return Collections.unmodifiableList(new ArrayList<>(pastIds));
}
}

public void updateScrollId(String scrollId) {

if (StringUtils.hasText(scrollId)) {

synchronized (lock) {

this.scrollId = scrollId;
pastIds.add(scrollId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -341,7 +342,14 @@ abstract protected <T> SearchScrollHits<T> searchScrollContinue(@Nullable String
/*
* internal use only, not for public API
*/
abstract protected void searchScrollClear(String scrollId);
protected void searchScrollClear(String scrollId) {
searchScrollClear(Collections.singletonList(scrollId));
}

/*
* internal use only, not for public API
*/
abstract protected void searchScrollClear(List<String> scrollIds);

abstract protected MultiSearchResponse.Item[] getMultiSearchResult(MultiSearchRequest request);
// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ public <T> SearchScrollHits<T> searchScrollContinue(@Nullable String scrollId, l
}

@Override
public void searchScrollClear(String scrollId) {
public void searchScrollClear(List<String> scrollIds) {
ClearScrollRequest request = new ClearScrollRequest();
request.addScrollId(scrollId);
request.scrollIds(scrollIds);
execute(client -> client.clearScroll(request, RequestOptions.DEFAULT));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ public <T> SearchScrollHits<T> searchScrollContinue(@Nullable String scrollId, l
}

@Override
public void searchScrollClear(String scrollId) {
client.prepareClearScroll().addScrollId(scrollId).execute().actionGet();
public void searchScrollClear(List<String> scrollIds) {
client.prepareClearScroll().setScrollIds(scrollIds).execute().actionGet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package org.springframework.data.elasticsearch.core;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.function.Function;

import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.data.elasticsearch.client.util.ScrollState;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand All @@ -38,12 +40,12 @@ abstract class StreamQueries {
*
* @param searchHits the initial hits
* @param continueScrollFunction function to continue scrolling applies to the current scrollId.
* @param clearScrollConsumer consumer to clear the scroll context by accepting the current scrollId.
* @param clearScrollConsumer consumer to clear the scroll context by accepting the scrollIds to clear.
* @param <T>
* @return the {@link SearchHitsIterator}.
*/
static <T> SearchHitsIterator<T> streamResults(SearchScrollHits<T> searchHits,
Function<String, SearchScrollHits<T>> continueScrollFunction, Consumer<String> clearScrollConsumer) {
Function<String, SearchScrollHits<T>> continueScrollFunction, Consumer<List<String>> clearScrollConsumer) {

Assert.notNull(searchHits, "searchHits must not be null.");
Assert.notNull(searchHits.getScrollId(), "scrollId of searchHits must not be null.");
Expand All @@ -59,17 +61,17 @@ static <T> SearchHitsIterator<T> streamResults(SearchScrollHits<T> searchHits,

// As we couldn't retrieve single result with scroll, store current hits.
private volatile Iterator<SearchHit<T>> scrollHits = searchHits.iterator();
private volatile String scrollId = searchHits.getScrollId();
private volatile boolean continueScroll = scrollHits.hasNext();
private volatile ScrollState scrollState = new ScrollState(searchHits.getScrollId());

@Override
public void close() {

try {
clearScrollConsumer.accept(scrollId);
clearScrollConsumer.accept(scrollState.getScrollIds());
} finally {
scrollHits = null;
scrollId = null;
scrollState = null;
}
}

Expand Down Expand Up @@ -102,9 +104,9 @@ public boolean hasNext() {
}

if (!scrollHits.hasNext()) {
SearchScrollHits<T> nextPage = continueScrollFunction.apply(scrollId);
SearchScrollHits<T> nextPage = continueScrollFunction.apply(scrollState.getScrollId());
scrollHits = nextPage.iterator();
scrollId = nextPage.getScrollId();
scrollState.updateScrollId(nextPage.getScrollId());
continueScroll = scrollHits.hasNext();
}

Expand All @@ -127,6 +129,5 @@ public void remove() {
}

// utility constructor
private StreamQueries() {
}
private StreamQueries() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.client.util;

import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

/**
* @author Peter-Josef Meisch
*/
class ScrollStateTest {

@Test // DATAES-817
void shouldReturnLastSetScrollId() {
ScrollState scrollState = new ScrollState();

scrollState.updateScrollId("id-1");
scrollState.updateScrollId("id-2");

assertThat(scrollState.getScrollId()).isEqualTo("id-2");
}

@Test
void shouldReturnUniqueListOfUsedScrollIdsInCorrectOrder() {

ScrollState scrollState = new ScrollState();

scrollState.updateScrollId("id-1");
scrollState.updateScrollId("id-2");
scrollState.updateScrollId("id-1");
scrollState.updateScrollId("id-3");
scrollState.updateScrollId("id-2");

assertThat(scrollState.getScrollIds()).isEqualTo(Arrays.asList("id-1", "id-2", "id-3"));
}
}
Loading