Skip to content

Commit

Permalink
Fix NPE with lenient aggregators merging in segmentMetadata. (#15078)
Browse files Browse the repository at this point in the history
When merging analyses, lenient merging sets unmergeable aggregators
to null. Merging such a null aggregator record into a nonnull record
would potentially lead to NPE in getMergingFactory.

The new code only calls getMergingFactory if both the old and new
aggregators are nonnull; else, if either is null, then the merged
aggregator is also set to null.
  • Loading branch information
gianm authored Oct 4, 2023
1 parent 632811b commit a9021e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,26 @@ public static SegmentAnalysis mergeAnalyses(
for (Map.Entry<String, AggregatorFactory> entry : analysis.getAggregators().entrySet()) {
final String aggregatorName = entry.getKey();
final AggregatorFactory aggregator = entry.getValue();
AggregatorFactory merged = aggregators.get(aggregatorName);
if (merged != null) {
try {
merged = merged.getMergingFactory(aggregator);
}
catch (AggregatorFactoryNotMergeableException e) {
final boolean isMergedYet = aggregators.containsKey(aggregatorName);
AggregatorFactory merged;

if (!isMergedYet) {
merged = aggregator;
} else {
merged = aggregators.get(aggregatorName);

if (merged != null && aggregator != null) {
try {
merged = merged.getMergingFactory(aggregator);
}
catch (AggregatorFactoryNotMergeableException e) {
merged = null;
}
} else {
merged = null;
}
} else {
merged = aggregator;
}

aggregators.put(aggregatorName, merged);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,44 @@ public void testMergeAggregatorsConflict()
)
);

// Simulate multi-level lenient merge (unmerged first)
Assert.assertEquals(
new SegmentAnalysis(
"dummy_2021-01-01T00:00:00.000Z_2021-01-02T00:00:00.000Z_merged",
null,
new LinkedHashMap<>(),
0,
0,
expectedLenient,
null,
null,
null
),
mergeLenient(
analysis1,
mergeLenient(analysis1, analysis2)
)
);

// Simulate multi-level lenient merge (unmerged second)
Assert.assertEquals(
new SegmentAnalysis(
"dummy_2021-01-01T00:00:00.000Z_2021-01-02T00:00:00.000Z_merged",
null,
new LinkedHashMap<>(),
0,
0,
expectedLenient,
null,
null,
null
),
mergeLenient(
mergeLenient(analysis1, analysis2),
analysis1
)
);

Assert.assertEquals(
new SegmentAnalysis(
"dummy_2021-01-01T00:00:00.000Z_2021-01-02T00:00:00.000Z_merged",
Expand Down

0 comments on commit a9021e4

Please sign in to comment.