Fix NPE in MetadataIndexTemplateService#findV2Template (#54945) (#55001)

This commit fixes potential NPE when there's V2 template with `null` priority.
This is done by using `null`-safe comparator.
This commit is contained in:
Przemko Robakowski 2020-04-09 11:34:20 +02:00 committed by GitHub
parent f6bdd30165
commit adc6e880cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View File

@ -664,7 +664,8 @@ public class MetadataIndexTemplateService {
}
final List<IndexTemplateV2> candidates = new ArrayList<>(matchedTemplates.keySet());
CollectionUtil.timSort(candidates, Comparator.comparingLong(IndexTemplateV2::priority).reversed());
CollectionUtil.timSort(candidates, Comparator.comparing(IndexTemplateV2::priority,
Comparator.nullsLast(Comparator.reverseOrder())));
assert candidates.size() > 0 : "we should have returned early with no candidates";
IndexTemplateV2 winner = candidates.get(0);

View File

@ -478,7 +478,7 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase {
ComponentTemplate ct = ComponentTemplateTests.randomInstance();
state = service.addComponentTemplate(state, true, "ct", ct);
IndexTemplateV2 it = new IndexTemplateV2(Collections.singletonList("i*"), null, Collections.singletonList("ct"), 0L, 1L, null);
IndexTemplateV2 it = new IndexTemplateV2(Collections.singletonList("i*"), null, Collections.singletonList("ct"), null, 1L, null);
state = service.addIndexTemplateV2(state, true, "my-template", it);
IndexTemplateV2 it2 = new IndexTemplateV2(Collections.singletonList("in*"), null, Collections.singletonList("ct"), 10L, 2L, null);
state = service.addIndexTemplateV2(state, true, "my-template2", it2);