瀏覽代碼

Fix NPE in IndexService#getNodeMappingStats (#91334)

The field mapperService in IndexService can be null, which needs to be
handled in getNodeMappingStats. It returns a null in that case, which is
already handled by the caller CommonStats.
Stéphane Campinas 2 年之前
父節點
當前提交
bb3698cfee

+ 6 - 0
docs/changelog/91334.yaml

@@ -0,0 +1,6 @@
+pr: 91334
+summary: "Fix NPE in IndexService getNodeMappingStats"
+area: "Stats"
+type: bug
+issues:
+  - 91259

+ 3 - 1
server/src/main/java/org/elasticsearch/index/IndexService.java

@@ -300,8 +300,10 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust
     }
 
     public NodeMappingStats getNodeMappingStats() {
+        if (mapperService == null) {
+            return null;
+        }
         long totalCount = mapperService().mappingLookup().getTotalFieldsCount();
-        Index index = index();
         long totalEstimatedOverhead = totalCount * 1024L; // 1KiB estimated per mapping
         NodeMappingStats indexNodeMappingStats = new NodeMappingStats(totalCount, totalEstimatedOverhead);
         return indexNodeMappingStats;

+ 13 - 0
server/src/test/java/org/elasticsearch/index/IndexServiceTests.java

@@ -57,6 +57,19 @@ public class IndexServiceTests extends ESSingleNodeTestCase {
         return new CompressedXContent(Strings.toString(builder));
     }
 
+    public void testClosedIndexHasNullNodeMappingStats() throws Exception {
+        final IndexService indexService = createIndex("test", Settings.EMPTY);
+        ensureGreen("test");
+
+        final Index index = indexService.index();
+        assertAcked(client().admin().indices().prepareClose(index.getName()));
+        assertBusy(() -> assertTrue("Index not found: " + index.getName(), getInstanceFromNode(IndicesService.class).hasIndex(index)));
+
+        final IndexService closedIndexService = getInstanceFromNode(IndicesService.class).indexServiceSafe(index);
+        assertNotSame(indexService, closedIndexService);
+        assertNull(closedIndexService.getNodeMappingStats());
+    }
+
     public void testBaseAsyncTask() throws Exception {
         IndexService indexService = createIndex("test", Settings.EMPTY);
         AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));