Skip to content

Commit c14a50a

Browse files
committed
Attempt to calculate size for circuit breaker
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent eff861a commit c14a50a

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

server/src/main/java/org/opensearch/cluster/metadata/Metadata.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.logging.log4j.LogManager;
3636
import org.apache.logging.log4j.Logger;
3737
import org.apache.lucene.util.CollectionUtil;
38+
import org.apache.lucene.util.RamUsageEstimator;
3839
import org.opensearch.action.AliasesRequest;
3940
import org.opensearch.cluster.ClusterState;
4041
import org.opensearch.cluster.ClusterState.FeatureAware;
@@ -820,18 +821,18 @@ public Map<String, IndexMetadata> indices() {
820821
}
821822

822823
/**
823-
* Estimates the serialized size of the indices metadata in bytes.
824+
* Estimates the serialized size of the indices mappings in bytes.
824825
* This is useful for circuit breaker calculations.
825826
*
826827
* @param indexPatterns array of index names/patterns to estimate, empty array means all indices
827828
* @return estimated size in bytes
828829
*/
829-
public long estimateIndicesSize(String[] indexPatterns) {
830+
public long estimateIndicesMappingsSize(String[] indexPatterns) {
830831
long size = 0;
831832
if (indexPatterns.length == 0) {
832833
// Estimate all indices
833834
for (IndexMetadata indexMetadata : indices.values()) {
834-
size += estimateIndexSize(indexMetadata);
835+
size += estimateIndexMappingsSize(indexMetadata);
835836
}
836837
} else {
837838
// Estimate matching indices
@@ -841,20 +842,20 @@ public long estimateIndicesSize(String[] indexPatterns) {
841842
// _all pattern matches everything
842843
for (IndexMetadata indexMetadata : indices.values()) {
843844
if (matchedIndices.add(indexMetadata.getIndex().getName())) {
844-
size += estimateIndexSize(indexMetadata);
845+
size += estimateIndexMappingsSize(indexMetadata);
845846
}
846847
}
847848
} else {
848849
// Check for exact match first, then wildcard
849850
IndexMetadata exactMatch = indices.get(pattern);
850851
if (exactMatch != null && matchedIndices.add(pattern)) {
851-
size += estimateIndexSize(exactMatch);
852+
size += estimateIndexMappingsSize(exactMatch);
852853
} else {
853854
// Wildcard matching
854855
for (IndexMetadata indexMetadata : indices.values()) {
855856
String indexName = indexMetadata.getIndex().getName();
856857
if (Regex.simpleMatch(pattern, indexName) && matchedIndices.add(indexName)) {
857-
size += estimateIndexSize(indexMetadata);
858+
size += estimateIndexMappingsSize(indexMetadata);
858859
}
859860
}
860861
}
@@ -864,21 +865,16 @@ public long estimateIndicesSize(String[] indexPatterns) {
864865
return size;
865866
}
866867

867-
private long estimateIndexSize(IndexMetadata indexMetadata) {
868-
long size = 2048; // ~2KB for basic index metadata
868+
private long estimateIndexMappingsSize(IndexMetadata indexMetadata) {
869+
long size = 0L;
869870

870-
// Mapping size estimate
871+
// 1) Mapping: count the actual compressed bytes (what GET _mapping typically serializes from)
871872
if (indexMetadata.mapping() != null) {
872-
size += indexMetadata.mapping().source().compressed().length;
873+
final byte[] compressed = indexMetadata.mapping().source().compressed();
874+
size += RamUsageEstimator.sizeOf(compressed);
873875
}
874876

875-
// Settings size estimate
876-
size += indexMetadata.getSettings().size() * 64; // ~64 bytes per setting
877-
878-
// Aliases size estimate
879-
size += indexMetadata.getAliases().size() * 256; // ~256 bytes per alias
880-
881-
return size;
877+
return RamUsageEstimator.alignObjectSize(size);
882878
}
883879

884880
public Map<String, IndexMetadata> getIndices() {

server/src/main/java/org/opensearch/rest/action/admin/indices/RestGetMappingAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String getName() {
101101
@Override
102102
public long estimateHeapUsage(RestRequest request) {
103103
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
104-
return clusterService.state().metadata().estimateIndicesSize(indices);
104+
return clusterService.state().metadata().estimateIndicesMappingsSize(indices);
105105
}
106106

107107
@Override

0 commit comments

Comments
 (0)