diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/RamUsage.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/RamUsage.java new file mode 100644 index 00000000000..a2c618b66e1 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/RamUsage.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you 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 + * + * http://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.elasticsearch.common; + +/** + * @author kimchy (shay.banon) + */ +public class RamUsage { + + private static final String OS_ARCH = System.getProperty("os.arch"); + private static final boolean JRE_IS_64BIT; + + static { + String x = System.getProperty("sun.arch.data.model"); + if (x != null) { + JRE_IS_64BIT = x.indexOf("64") != -1; + } else { + if (OS_ARCH != null && OS_ARCH.indexOf("64") != -1) { + JRE_IS_64BIT = true; + } else { + JRE_IS_64BIT = false; + } + } + } + + public final static int NUM_BYTES_SHORT = 2; + public final static int NUM_BYTES_INT = 4; + public final static int NUM_BYTES_LONG = 8; + public final static int NUM_BYTES_FLOAT = 4; + public final static int NUM_BYTES_DOUBLE = 8; + public final static int NUM_BYTES_CHAR = 2; + public final static int NUM_BYTES_OBJECT_HEADER = 8; + public final static int NUM_BYTES_OBJECT_REF = JRE_IS_64BIT ? 8 : 4; + public final static int NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + NUM_BYTES_INT + NUM_BYTES_OBJECT_REF; + +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/FieldDataCache.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/FieldDataCache.java index e6bc4942cd1..9ac8faf4ec2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/FieldDataCache.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/FieldDataCache.java @@ -41,4 +41,8 @@ public interface FieldDataCache extends IndexComponent, CloseableComponent { void clear(IndexReader reader); void clearUnreferenced(); + + long sizeInBytes(); + + long sizeInBytes(String fieldName); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/none/NoneFieldDataCache.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/none/NoneFieldDataCache.java index 850496b456f..bda7a12fd25 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/none/NoneFieldDataCache.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/none/NoneFieldDataCache.java @@ -61,4 +61,12 @@ public class NoneFieldDataCache extends AbstractIndexComponent implements FieldD @Override public void close() throws ElasticSearchException { } + + @Override public long sizeInBytes() { + return 0; + } + + @Override public long sizeInBytes(String fieldName) { + return 0; + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java index 0f1a1ada1d6..6fa79450329 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java @@ -70,6 +70,28 @@ public abstract class AbstractConcurrentMapFieldDataCache extends AbstractIndexC // nothing to do here... } + @Override public long sizeInBytes() { + // the overhead of the map is not really relevant... + long sizeInBytes = 0; + for (ConcurrentMap map : cache.values()) { + for (FieldData fieldData : map.values()) { + sizeInBytes += fieldData.sizeInBytes(); + } + } + return sizeInBytes; + } + + @Override public long sizeInBytes(String fieldName) { + long sizeInBytes = 0; + for (ConcurrentMap map : cache.values()) { + FieldData fieldData = map.get(fieldName); + if (fieldData != null) { + sizeInBytes += fieldData.sizeInBytes(); + } + } + return sizeInBytes; + } + @Override public FieldData cache(FieldDataType type, IndexReader reader, String fieldName) throws IOException { ConcurrentMap fieldDataCache = cache.get(reader.getFieldCacheKey()); if (fieldDataCache == null) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java index 66631a9cb33..c711902eb95 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java @@ -39,6 +39,8 @@ public abstract class FieldData { private final String fieldName; + private long sizeInBytes = -1; + protected FieldData(String fieldName) { this.fieldName = fieldName; } @@ -56,6 +58,15 @@ public abstract class FieldData { return docFieldData; } + public long sizeInBytes() { + if (sizeInBytes == -1) { + sizeInBytes = computeSizeInBytes(); + } + return sizeInBytes; + } + + protected abstract long computeSizeInBytes(); + protected abstract Doc createFieldData(); /** diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java index 5796ca080d6..3b91193af51 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.field.data.doubles; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.trove.TDoubleArrayList; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; @@ -42,6 +43,10 @@ public abstract class DoubleFieldData extends NumericFieldData this.values = values; } + @Override protected long computeSizeInBytes() { + return RamUsage.NUM_BYTES_FLOAT * values.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + abstract public float value(int docId); abstract public float[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java index bc593f10b48..df53dda4913 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.floats; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -57,6 +58,15 @@ public class MultiValueFloatFieldData extends FloatFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; + } + @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/SingleValueFloatFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/SingleValueFloatFieldData.java index e453c5adc1d..5310d4ef6a9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/SingleValueFloatFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/SingleValueFloatFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.floats; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -47,6 +48,11 @@ public class SingleValueFloatFieldData extends FloatFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + @Override public boolean multiValued() { return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java index 1f6ab721a7f..c82dd45697f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.field.data.ints; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.trove.TIntArrayList; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; @@ -42,6 +43,10 @@ public abstract class IntFieldData extends NumericFieldData { this.values = values; } + @Override protected long computeSizeInBytes() { + return RamUsage.NUM_BYTES_INT * values.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + abstract public int value(int docId); abstract public int[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java index 15a7c15e62b..c804016485f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.ints; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -57,6 +58,15 @@ public class MultiValueIntFieldData extends IntFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; + } + @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/SingleValueIntFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/SingleValueIntFieldData.java index bfe46759310..6caf7287f24 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/SingleValueIntFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/SingleValueIntFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.ints; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -47,6 +48,11 @@ public class SingleValueIntFieldData extends IntFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + @Override public boolean multiValued() { return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java index 643f214515a..d1f8fc2dcca 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.field.data.longs; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.joda.time.DateTimeZone; import org.elasticsearch.common.joda.time.MutableDateTime; import org.elasticsearch.common.thread.ThreadLocals; @@ -52,6 +53,10 @@ public abstract class LongFieldData extends NumericFieldData { this.values = values; } + @Override protected long computeSizeInBytes() { + return RamUsage.NUM_BYTES_LONG * values.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + abstract public long value(int docId); abstract public long[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java index e516542afb2..847c8f54b56 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.longs; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.joda.time.DateTimeZone; import org.elasticsearch.common.joda.time.MutableDateTime; import org.elasticsearch.common.thread.ThreadLocals; @@ -73,6 +74,15 @@ public class MultiValueLongFieldData extends LongFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; + } + @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/SingleValueLongFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/SingleValueLongFieldData.java index 9cd3bf78f51..3732f084c94 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/SingleValueLongFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/SingleValueLongFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.longs; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.joda.time.DateTimeZone; import org.elasticsearch.common.joda.time.MutableDateTime; import org.elasticsearch.common.thread.ThreadLocals; @@ -57,6 +58,11 @@ public class SingleValueLongFieldData extends LongFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + @Override public boolean multiValued() { return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/MultiValueShortFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/MultiValueShortFieldData.java index 54cd624acdd..561dd689130 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/MultiValueShortFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/MultiValueShortFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.shorts; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -57,6 +58,15 @@ public class MultiValueShortFieldData extends ShortFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; + } + @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java index 299909afbb1..b509309d827 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.field.data.shorts; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.trove.TShortArrayList; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; @@ -42,6 +43,10 @@ public abstract class ShortFieldData extends NumericFieldData this.values = values; } + @Override protected long computeSizeInBytes() { + return RamUsage.NUM_BYTES_SHORT * values.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + abstract public short value(int docId); abstract public short[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/SingleValueShortFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/SingleValueShortFieldData.java index b263e54a3c4..0940c689b98 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/SingleValueShortFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/SingleValueShortFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.shorts; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; @@ -47,6 +48,11 @@ public class SingleValueShortFieldData extends ShortFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + @Override public boolean multiValued() { return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java index 6ff02df973e..e33d7f070e8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.strings; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.Strings; import org.elasticsearch.common.thread.ThreadLocals; @@ -47,6 +48,15 @@ public class MultiValueStringFieldData extends StringFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; + } + @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java index 219e9b2fa26..93d2d13c845 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.field.data.strings; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.Strings; import org.elasticsearch.common.thread.ThreadLocals; @@ -41,6 +42,11 @@ public class SingleValueStringFieldData extends StringFieldData { this.ordinals = ordinals; } + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + int[] ordinals() { return ordinals; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/StringFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/StringFieldData.java index f2b5807b23b..e2b98dec722 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/StringFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/StringFieldData.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.field.data.strings; import org.apache.lucene.index.IndexReader; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.index.field.data.FieldData; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -39,6 +40,14 @@ public abstract class StringFieldData extends FieldData { this.values = values; } + @Override protected long computeSizeInBytes() { + long size = RamUsage.NUM_BYTES_ARRAY_HEADER; + for (String value : values) { + size += RamUsage.NUM_BYTES_OBJECT_HEADER + value.length() * RamUsage.NUM_BYTES_CHAR + (3 * RamUsage.NUM_BYTES_INT); + } + return size; + } + abstract public String value(int docId); abstract public String[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java index e3c7bd75ea8..a82faeb0efe 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper.xcontent.geo; import org.apache.lucene.index.IndexReader; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.trove.TDoubleArrayList; import org.elasticsearch.index.field.data.FieldData; @@ -67,6 +68,11 @@ public abstract class GeoPointFieldData extends FieldData return super.docFieldData(docId); } + @Override protected long computeSizeInBytes() { + return (RamUsage.NUM_BYTES_DOUBLE * lat.length + RamUsage.NUM_BYTES_ARRAY_HEADER) + + (RamUsage.NUM_BYTES_DOUBLE * lon.length + RamUsage.NUM_BYTES_ARRAY_HEADER); + } + @Override public String stringValue(int docId) { return value(docId).geohash(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java index 05485a80402..9287f34241e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper.xcontent.geo; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; import org.elasticsearch.index.search.geo.GeoHashUtils; @@ -64,11 +65,20 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { }; // order with value 0 indicates no value - private final int[][] order; + private final int[][] ordinals; - public MultiValueGeoPointFieldData(String fieldName, int[][] order, double[] lat, double[] lon) { + public MultiValueGeoPointFieldData(String fieldName, int[][] ordinals, double[] lat, double[] lon) { super(fieldName, lat, lon); - this.order = order; + this.ordinals = ordinals; + } + + @Override protected long computeSizeInBytes() { + long size = super.computeSizeInBytes(); + size += RamUsage.NUM_BYTES_ARRAY_HEADER; // for the top level array + for (int[] ordinal : ordinals) { + size += RamUsage.NUM_BYTES_INT * ordinal.length + RamUsage.NUM_BYTES_ARRAY_HEADER; + } + return size; } @Override public boolean multiValued() { @@ -76,11 +86,11 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public boolean hasValue(int docId) { - return order[docId] != null; + return ordinals[docId] != null; } @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return; } @@ -90,7 +100,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public GeoPoint value(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return null; } @@ -101,7 +111,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public GeoPoint[] values(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return EMPTY_ARRAY; } @@ -123,7 +133,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public double latValue(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return 0; } @@ -131,7 +141,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public double lonValue(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return 0; } @@ -139,7 +149,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public double[] latValues(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } @@ -156,7 +166,7 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public double[] lonValues(int docId) { - int[] docOrders = order[docId]; + int[] docOrders = ordinals[docId]; if (docOrders == null) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java index 52164a08d7e..ed197973dc4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper.xcontent.geo; +import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.index.field.data.doubles.DoubleFieldData; import org.elasticsearch.index.search.geo.GeoHashUtils; @@ -50,11 +51,16 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { // order with value 0 indicates no value - private final int[] order; + private final int[] ordinals; - public SingleValueGeoPointFieldData(String fieldName, int[] order, double[] lat, double[] lon) { + public SingleValueGeoPointFieldData(String fieldName, int[] ordinals, double[] lat, double[] lon) { super(fieldName, lat, lon); - this.order = order; + this.ordinals = ordinals; + } + + @Override protected long computeSizeInBytes() { + return super.computeSizeInBytes() + + RamUsage.NUM_BYTES_INT * ordinals.length + RamUsage.NUM_BYTES_ARRAY_HEADER; } @Override public boolean multiValued() { @@ -62,11 +68,11 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { } @Override public boolean hasValue(int docId) { - return order[docId] != 0; + return ordinals[docId] != 0; } @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - int loc = order[docId]; + int loc = ordinals[docId]; if (loc == 0) { return; } @@ -74,7 +80,7 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { } @Override public GeoPoint value(int docId) { - int loc = order[docId]; + int loc = ordinals[docId]; if (loc == 0) { return null; } @@ -84,7 +90,7 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { } @Override public GeoPoint[] values(int docId) { - int loc = order[docId]; + int loc = ordinals[docId]; if (loc == 0) { return EMPTY_ARRAY; } @@ -94,15 +100,15 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { } @Override public double latValue(int docId) { - return lat[order[docId]]; + return lat[ordinals[docId]]; } @Override public double lonValue(int docId) { - return lon[order[docId]]; + return lon[ordinals[docId]]; } @Override public double[] latValues(int docId) { - int loc = order[docId]; + int loc = ordinals[docId]; if (loc == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } @@ -112,7 +118,7 @@ public class SingleValueGeoPointFieldData extends GeoPointFieldData { } @Override public double[] lonValues(int docId) { - int loc = order[docId]; + int loc = ordinals[docId]; if (loc == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/IndicesStats.java b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/IndicesStats.java index ef7a10c9e44..86f8314e946 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/IndicesStats.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/IndicesStats.java @@ -38,11 +38,14 @@ public class IndicesStats implements Streamable, Serializable, ToXContent { private ByteSizeValue storeSize; + private ByteSizeValue fieldCacheSize; + IndicesStats() { } - public IndicesStats(ByteSizeValue storeSize) { + public IndicesStats(ByteSizeValue storeSize, ByteSizeValue fieldCacheSize) { this.storeSize = storeSize; + this.fieldCacheSize = fieldCacheSize; } /** @@ -59,6 +62,14 @@ public class IndicesStats implements Streamable, Serializable, ToXContent { return storeSize; } + public ByteSizeValue fieldCacheSize() { + return this.fieldCacheSize; + } + + public ByteSizeValue getFieldCacheSize() { + return this.fieldCacheSize; + } + public static IndicesStats readIndicesStats(StreamInput in) throws IOException { IndicesStats stats = new IndicesStats(); stats.readFrom(in); @@ -67,16 +78,20 @@ public class IndicesStats implements Streamable, Serializable, ToXContent { @Override public void readFrom(StreamInput in) throws IOException { storeSize = ByteSizeValue.readBytesSizeValue(in); + fieldCacheSize = ByteSizeValue.readBytesSizeValue(in); } @Override public void writeTo(StreamOutput out) throws IOException { storeSize.writeTo(out); + fieldCacheSize.writeTo(out); } @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("indices"); builder.field("store_size", storeSize.toString()); builder.field("store_size_in_bytes", storeSize.bytes()); + builder.field("field_cache_size", fieldCacheSize.toString()); + builder.field("field_cache_size_in_bytes", fieldCacheSize.bytes()); builder.endObject(); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/InternalIndicesService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/InternalIndicesService.java index a5d8c9bc3f1..c9ffb7dbf9c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/InternalIndicesService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/InternalIndicesService.java @@ -155,17 +155,19 @@ public class InternalIndicesService extends AbstractLifecycleComponent