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

optimize getIndices in IndicesSegmentResponse #80064

Merged
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 @@ -40,6 +40,50 @@
- match: { indices.index1.shards.0.0.routing.primary: true}
- match: { indices.index1.shards.0.0.segments._0.num_docs: 1}

---
"segments from multiple indices":

- do:
indices.create:
index: index1
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"

- do:
indices.create:
index: index2
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"

- do:
index:
index: index1
body: { foo: bar }
refresh: true

- do:
index:
index: index2
body: { foo: bar }
refresh: true

- do:
cluster.health:
wait_for_status: green

- do:
indices.segments: {}

- gte: { _shards.total: 2 }
- match: { indices.index1.shards.0.0.routing.primary: true}
- match: { indices.index1.shards.0.0.segments._0.num_docs: 1}
- match: { indices.index2.shards.0.0.routing.primary: true}
- match: { indices.index2.shards.0.0.segments._0.num_docs: 1}

---
"closed segments test":
- skip:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ public class IndexSegments implements Iterable<IndexShardSegments> {

private final Map<Integer, IndexShardSegments> indexShards;

IndexSegments(String index, ShardSegments[] shards) {
IndexSegments(String index, List<ShardSegments> shards) {
DaveCTurner marked this conversation as resolved.
Show resolved Hide resolved
this.index = index;

Map<Integer, List<ShardSegments>> tmpIndexShards = new HashMap<>();
final Map<Integer, List<ShardSegments>> segmentsByShardId = new HashMap<>();
for (ShardSegments shard : shards) {
List<ShardSegments> lst = tmpIndexShards.get(shard.getShardRouting().id());
if (lst == null) {
lst = new ArrayList<>();
tmpIndexShards.put(shard.getShardRouting().id(), lst);
}
lst.add(shard);
segmentsByShardId.computeIfAbsent(shard.getShardRouting().id(), k -> new ArrayList<>()).add(shard);
}
indexShards = new HashMap<>();
for (Map.Entry<Integer, List<ShardSegments>> entry : tmpIndexShards.entrySet()) {
for (Map.Entry<Integer, List<ShardSegments>> entry : segmentsByShardId.entrySet()) {
indexShards.put(
entry.getKey(),
new IndexShardSegments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class IndicesSegmentResponse extends BroadcastResponse {

private final ShardSegments[] shards;

private Map<String, IndexSegments> indicesSegments;
private volatile Map<String, IndexSegments> indicesSegments;

IndicesSegmentResponse(StreamInput in) throws IOException {
super(in);
Expand All @@ -61,19 +59,12 @@ public Map<String, IndexSegments> getIndices() {
}
Map<String, IndexSegments> indicesSegments = new HashMap<>();

Set<String> indices = new HashSet<>();
final Map<String, List<ShardSegments>> segmentsByIndex = new HashMap<>();
for (ShardSegments shard : shards) {
indices.add(shard.getShardRouting().getIndexName());
segmentsByIndex.computeIfAbsent(shard.getShardRouting().getIndexName(), k -> new ArrayList<>()).add(shard);
}

for (String indexName : indices) {
List<ShardSegments> shards = new ArrayList<>();
for (ShardSegments shard : this.shards) {
if (shard.getShardRouting().getIndexName().equals(indexName)) {
shards.add(shard);
}
}
indicesSegments.put(indexName, new IndexSegments(indexName, shards.toArray(new ShardSegments[shards.size()])));
for (Map.Entry<String, List<ShardSegments>> entry : segmentsByIndex.entrySet()) {
indicesSegments.put(entry.getKey(), new IndexSegments(entry.getKey(), entry.getValue()));
}
this.indicesSegments = indicesSegments;
return indicesSegments;
Expand Down