Skip to content

Commit

Permalink
Add segment attributes to the _segments API.
Browse files Browse the repository at this point in the history
This contains information about whether high compression was enabled for instance.

Closes elastic#26130
  • Loading branch information
jpountz committed Aug 10, 2017
1 parent 3fc27b0 commit 118d24e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
Expand Up @@ -155,6 +155,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
builder.endArray();
}
if (segment.attributes != null && segment.attributes.isEmpty() == false) {
builder.field("attributes", segment.attributes);
}
builder.endObject();
}
builder.endObject();
Expand Down
Expand Up @@ -703,6 +703,7 @@ protected Segment[] getSegmentInfo(SegmentInfos lastCommittedSegmentInfos, boole
if (verbose) {
segment.ramTree = Accountables.namedAccountable("root", segmentReader);
}
segment.attributes = info.info.getAttributes();
// TODO: add more fine grained mem stats values to per segment info here
segments.put(info.info.name, segment);
}
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/org/elasticsearch/index/engine/Segment.java
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Segment implements Streamable {

Expand All @@ -55,6 +56,7 @@ public class Segment implements Streamable {
public long memoryInBytes;
public Sort segmentSort;
public Accountable ramTree = null;
public Map<String, String> attributes;

Segment() {
}
Expand Down Expand Up @@ -128,6 +130,14 @@ public Sort getSegmentSort() {
return segmentSort;
}

/**
* Return segment attributes.
* @see org.apache.lucene.index.SegmentInfo#getAttributes()
*/
public Map<String, String> getAttributes() {
return attributes;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -173,6 +183,11 @@ public void readFrom(StreamInput in) throws IOException {
} else {
segmentSort = null;
}
if (in.getVersion().onOrAfter(Version.V_6_1_0) && in.readBoolean()) {
attributes = in.readMap(StreamInput::readString, StreamInput::readString);
} else {
attributes = null;
}
}

@Override
Expand All @@ -196,6 +211,13 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
writeSegmentSort(out, segmentSort);
}
if (out.getVersion().onOrAfter(Version.V_6_1_0)) {
boolean hasAttributes = attributes != null;
out.writeBoolean(hasAttributes);
if (hasAttributes) {
out.writeMap(attributes, StreamOutput::writeString, StreamOutput::writeString);
}
}
}

Sort readSegmentSort(StreamInput in) throws IOException {
Expand Down Expand Up @@ -329,6 +351,7 @@ public String toString() {
", mergeId='" + mergeId + '\'' +
", memoryInBytes=" + memoryInBytes +
(segmentSort != null ? ", sort=" + segmentSort : "") +
", attributes=" + attributes +
'}';
}
}
Expand Up @@ -29,6 +29,7 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.lucene50.Lucene50StoredFieldsFormat;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.NumericDocValuesField;
Expand Down Expand Up @@ -138,6 +139,7 @@
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;

Expand Down Expand Up @@ -495,6 +497,7 @@ public void testSegments() throws Exception {
assertThat(segments.get(0).getDeletedDocs(), equalTo(0));
assertThat(segments.get(0).isCompound(), equalTo(true));
assertThat(segments.get(0).ramTree, nullValue());
assertThat(segments.get(0).getAttributes().keySet(), Matchers.contains(Lucene50StoredFieldsFormat.MODE_KEY));

engine.flush();

Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

public class SegmentTests extends ESTestCase {
Expand Down Expand Up @@ -81,6 +82,9 @@ static Segment randomSegment() {
segment.mergeId = randomAlphaOfLengthBetween(1, 10);
segment.memoryInBytes = randomNonNegativeLong();
segment.segmentSort = randomIndexSort();
if (randomBoolean()) {
segment.attributes = Collections.singletonMap("foo", "bar");
}
return segment;
}

Expand Down

0 comments on commit 118d24e

Please sign in to comment.