Skip to content

Commit 087b711

Browse files
committed
Fix CPU stall in geotile_grid aggregation by limiting max tiles per doc (#20413)
Signed-off-by: CodeItAlone <subrato213432@gmail.com>
1 parent b981203 commit 087b711

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

modules/geo/src/main/java/org/opensearch/geo/search/aggregations/bucket/geogrid/GeoGridAggregator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@
5151
import java.util.Collections;
5252
import java.util.List;
5353
import java.util.Map;
54+
import org.apache.logging.log4j.LogManager;
55+
import org.apache.logging.log4j.Logger;
56+
5457

5558
/**
5659
* Aggregates data expressed as longs (for efficiency's sake) but formats results as aggregation-specific strings.
5760
*
5861
* @opensearch.api
5962
*/
63+
6064
public abstract class GeoGridAggregator<T extends BaseGeoGrid> extends BucketsAggregator {
65+
private static final Logger logger = LogManager.getLogger(GeoGridAggregator.class);
6166

6267
protected final int requiredSize;
6368
protected final int shardSize;
@@ -94,13 +99,21 @@ public ScoreMode scoreMode() {
9499
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
95100
SortedNumericDocValues values = valuesSource.longValues(ctx);
96101
return new LeafBucketCollectorBase(sub, null) {
102+
final int MAX_TILES_PER_DOCUMENT = 10000;
103+
97104
@Override
98105
public void collect(int doc, long owningBucketOrd) throws IOException {
99106
if (values.advanceExact(doc)) {
100107
final int valuesCount = values.docValueCount();
101108

109+
if (valuesCount > MAX_TILES_PER_DOCUMENT) {
110+
// Log a warning so the user knows why data is missing
111+
logger.warn("Skipping doc [{}] in aggregation [{}] due to excessive tiles: [{}]", doc, name, valuesCount);
112+
return;
113+
}
102114
long previous = Long.MAX_VALUE;
103115
for (int i = 0; i < valuesCount; ++i) {
116+
104117
final long val = values.nextValue();
105118
if (previous != val || i == 0) {
106119
long bucketOrdinal = bucketOrds.add(owningBucketOrd, val);

0 commit comments

Comments
 (0)