Add segment attributes to the `_segments` API. (#26157)

This contains information about whether high compression was enabled for instance.

Closes #26130
This commit is contained in:
Adrien Grand 2017-08-16 19:01:29 +02:00 committed by GitHub
parent a975f4e5d6
commit 22292e8d96
5 changed files with 34 additions and 0 deletions

View File

@ -155,6 +155,9 @@ public class IndicesSegmentResponse extends BroadcastResponse implements ToXCont
}
builder.endArray();
}
if (segment.attributes != null && segment.attributes.isEmpty() == false) {
builder.field("attributes", segment.attributes);
}
builder.endObject();
}
builder.endObject();

View File

@ -703,6 +703,7 @@ public abstract class Engine implements Closeable {
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);
}

View File

@ -39,6 +39,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Segment implements Streamable {
@ -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() {
}
@ -128,6 +130,14 @@ public class Segment implements Streamable {
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;
@ -173,6 +183,11 @@ public class Segment implements Streamable {
} 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
@ -196,6 +211,13 @@ public class Segment implements Streamable {
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 {
@ -329,6 +351,7 @@ public class Segment implements Streamable {
", mergeId='" + mergeId + '\'' +
", memoryInBytes=" + memoryInBytes +
(segmentSort != null ? ", sort=" + segmentSort : "") +
", attributes=" + attributes +
'}';
}
}

View File

@ -29,6 +29,7 @@ import org.apache.logging.log4j.core.filter.RegexFilter;
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;
@ -138,6 +139,7 @@ import org.elasticsearch.test.IndexSettingsModule;
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;
@ -495,6 +497,7 @@ public class InternalEngineTests extends ESTestCase {
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();

View File

@ -31,6 +31,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;
public class SegmentTests extends ESTestCase {
@ -81,6 +82,9 @@ public class SegmentTests extends ESTestCase {
segment.mergeId = randomAlphaOfLengthBetween(1, 10);
segment.memoryInBytes = randomNonNegativeLong();
segment.segmentSort = randomIndexSort();
if (randomBoolean()) {
segment.attributes = Collections.singletonMap("foo", "bar");
}
return segment;
}