Skip to content

Commit

Permalink
Adding '_all' as option to get all segments
Browse files Browse the repository at this point in the history
Signed-off-by: Bharathwaj G <[email protected]>
  • Loading branch information
bharath-techie committed Aug 22, 2022
1 parent 611ed25 commit 78c645b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,40 @@

package org.opensearch.action.admin.indices.segments;

import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.broadcast.BroadcastRequest;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.opensearch.action.ValidateActions.addValidationError;

/**
* Transport request for retrieving PITs segment information
*/
public class PitSegmentsRequest extends BroadcastRequest<PitSegmentsRequest> {
private boolean verbose = false;
private List<String> pitIds;
private final List<String> pitIds = new ArrayList<>();

public PitSegmentsRequest() {
this(Strings.EMPTY_ARRAY);
}

public PitSegmentsRequest(StreamInput in) throws IOException {
super(in);
pitIds = Arrays.asList(in.readStringArray());
pitIds.addAll(Arrays.asList(in.readStringArray()));
verbose = in.readBoolean();
}

public PitSegmentsRequest(String... indices) {
super(indices);
pitIds = Collections.emptyList();
public PitSegmentsRequest(String... pitIds) {
super(pitIds);
this.pitIds.addAll(Arrays.asList(pitIds));
}

/**
Expand All @@ -64,11 +67,21 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(verbose);
}

public Collection<String> getPitIds() {
public List<String> getPitIds() {
return Collections.unmodifiableList(pitIds);
}

public void setPitIds(List<String> pitIds) {
this.pitIds = pitIds;
public void clearAndSetPitIds(List<String> pitIds) {
this.pitIds.clear();
this.pitIds.addAll(pitIds);
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (pitIds == null || pitIds.isEmpty()) {
validationException = addValidationError("no pit ids specified", validationException);
}
return validationException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ public TransportPitSegmentsAction(
*/
@Override
protected void doExecute(Task task, PitSegmentsRequest request, ActionListener<IndicesSegmentResponse> listener) {
if (request.getPitIds().isEmpty()) {
List<String> pitIds = request.getPitIds();
if (pitIds.size() == 1 && "_all".equals(pitIds.get(0))) {
pitService.getAllPits(ActionListener.wrap(response -> {
request.setPitIds(response.getPitInfos().stream().map(ListPitInfo::getPitId).collect(Collectors.toList()));
request.clearAndSetPitIds(response.getPitInfos().stream().map(ListPitInfo::getPitId).collect(Collectors.toList()));
super.doExecute(task, request, listener);
}, listener::onFailure));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public static void assertGetAllPitsEmpty(Client client) throws ExecutionExceptio
}

public static void assertSegments(boolean isEmpty, String index, long expectedShardSize, Client client) {
IndicesSegmentResponse indicesSegmentResponse = client.execute(PitSegmentsAction.INSTANCE, new PitSegmentsRequest()).actionGet();
PitSegmentsRequest pitSegmentsRequest = new PitSegmentsRequest("_all");
IndicesSegmentResponse indicesSegmentResponse = client.execute(PitSegmentsAction.INSTANCE, pitSegmentsRequest).actionGet();
assertTrue(indicesSegmentResponse.getShardFailures() == null || indicesSegmentResponse.getShardFailures().length == 0);
assertEquals(indicesSegmentResponse.getIndices().isEmpty(), isEmpty);
if (!isEmpty) {
Expand Down

0 comments on commit 78c645b

Please sign in to comment.