Bläddra i källkod

Allow opting out of known Lucene file extensions check (#74316)

Allows plugin developers of custom codecs to opt out of the assertion in LuceneFilesExtensionsTests that checks that all
encountered Lucene file extensions are known to this class. In the future, we would like to add a proper plugin extension
point for this.

Relates #74150
Yannick Welsch 4 år sedan
förälder
incheckning
632901f4a3

+ 43 - 0
qa/evil-tests/src/test/java/org/elasticsearch/index/store/LuceneFilesExtensionsTests.java

@@ -0,0 +1,43 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.index.store;
+
+import org.elasticsearch.Assertions;
+import org.elasticsearch.core.SuppressForbidden;
+import org.elasticsearch.test.ESTestCase;
+
+import static org.hamcrest.Matchers.containsString;
+
+public class LuceneFilesExtensionsTests extends ESTestCase {
+
+    public void testUnknownFileExtension() {
+        if (Assertions.ENABLED) {
+            AssertionError e = expectThrows(AssertionError.class, () -> LuceneFilesExtensions.fromExtension("abc"));
+            assertThat(e.getMessage(), containsString("unknown Lucene file extension [abc]"));
+
+            setEsAllowUnknownLuceneFileExtensions("true");
+            try {
+                assertNull(LuceneFilesExtensions.fromExtension("abc"));
+            } finally {
+                setEsAllowUnknownLuceneFileExtensions(null);
+            }
+        } else {
+            assertNull(LuceneFilesExtensions.fromExtension("abc"));
+        }
+    }
+
+    @SuppressForbidden(reason = "set or clear system property es.allow_unknown_lucene_file_extensions")
+    public void setEsAllowUnknownLuceneFileExtensions(final String value) {
+        if (value == null) {
+            System.clearProperty("es.allow_unknown_lucene_file_extensions");
+        } else {
+            System.setProperty("es.allow_unknown_lucene_file_extensions", value);
+        }
+    }
+}

+ 11 - 1
server/src/main/java/org/elasticsearch/index/store/LuceneFilesExtensions.java

@@ -71,6 +71,16 @@ public enum LuceneFilesExtensions {
     // Lucene 9.0 indexed vectors metadata
     // Lucene 9.0 indexed vectors metadata
     VEM("vem","Vector Metadata", true, false);
     VEM("vem","Vector Metadata", true, false);
 
 
+    /**
+     * Allow plugin developers of custom codecs to opt out of the assertion in {@link #fromExtension}
+     * that checks that all encountered file extensions are known to this class.
+     * In the future, we would like to add a proper plugin extension point for this.
+     */
+    private static boolean allowUnknownLuceneFileExtensions() {
+        return Boolean.parseBoolean(
+            System.getProperty("es.allow_unknown_lucene_file_extensions", "false"));
+    }
+
     /**
     /**
      * Lucene file's extension.
      * Lucene file's extension.
      */
      */
@@ -128,7 +138,7 @@ public enum LuceneFilesExtensions {
     public static LuceneFilesExtensions fromExtension(String ext) {
     public static LuceneFilesExtensions fromExtension(String ext) {
         if (ext != null && ext.isEmpty() == false) {
         if (ext != null && ext.isEmpty() == false) {
             final LuceneFilesExtensions extension = extensions.get(ext);
             final LuceneFilesExtensions extension = extensions.get(ext);
-            assert extension != null: "unknown Lucene file extension [" + ext + ']';
+            assert allowUnknownLuceneFileExtensions() || extension != null: "unknown Lucene file extension [" + ext + ']';
             return extension;
             return extension;
         }
         }
         return null;
         return null;

+ 0 - 1
x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/blob/BlobStoreCacheService.java

@@ -260,7 +260,6 @@ public class BlobStoreCacheService extends AbstractLifecycleComponent {
      */
      */
     public ByteRange computeBlobCacheByteRange(String fileName, long fileLength, ByteSizeValue maxMetadataLength) {
     public ByteRange computeBlobCacheByteRange(String fileName, long fileLength, ByteSizeValue maxMetadataLength) {
         final LuceneFilesExtensions fileExtension = LuceneFilesExtensions.fromExtension(IndexFileNames.getExtension(fileName));
         final LuceneFilesExtensions fileExtension = LuceneFilesExtensions.fromExtension(IndexFileNames.getExtension(fileName));
-        assert fileExtension != null : "unknown Lucene file extension [" + fileName + "] - should it be considered a metadata file?";
 
 
         if (useLegacyCachedBlobSizes()) {
         if (useLegacyCachedBlobSizes()) {
             if (fileLength <= ByteSizeUnit.KB.toBytes(8L)) {
             if (fileLength <= ByteSizeUnit.KB.toBytes(8L)) {