Jelajahi Sumber

Speed up ILM Logging (#85284)

We serialize the step key a lot in logging and use the very expensive `Strings.toString` for it.
With changes to ILM incoming that massively improve ILM performance across the board this is one of the
few remaining slow spots in profiling.
Just using string concatenation instead while leaving the format unchange almost fully removes
the `toString` method and thus the logging from profiling.

Co-authored-by: Joe Gallo <joe.gallo@elastic.co>
Armin Braun 3 tahun lalu
induk
melakukan
fa1053e972

+ 1 - 1
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Step.java

@@ -166,7 +166,7 @@ public abstract class Step {
 
         @Override
         public String toString() {
-            return Strings.toString(this);
+            return "{\"phase\":\"" + phase + "\",\"action\":\"" + action + "\",\"name\":\"" + name + "\"}";
         }
 
         @Override

+ 14 - 0
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/StepKeyTests.java

@@ -6,11 +6,15 @@
  */
 package org.elasticsearch.xpack.core.ilm;
 
+import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.Writeable;
 import org.elasticsearch.test.AbstractSerializingTestCase;
 import org.elasticsearch.xcontent.XContentParser;
+import org.elasticsearch.xcontent.json.JsonXContent;
 import org.elasticsearch.xpack.core.ilm.Step.StepKey;
 
+import java.io.IOException;
+
 public class StepKeyTests extends AbstractSerializingTestCase<StepKey> {
 
     @Override
@@ -47,4 +51,14 @@ public class StepKeyTests extends AbstractSerializingTestCase<StepKey> {
 
         return new StepKey(phase, action, step);
     }
+
+    public void testToString() throws IOException {
+        // toString yields parseable json
+        StepKey s = randomStepKey();
+        XContentParser parser = createParser(JsonXContent.jsonXContent, s.toString());
+        assertEquals(s, StepKey.parse(parser));
+
+        // although we're not actually using Strings.toString for performance reasons, we expect the same result as if we had
+        assertEquals(Strings.toString(s), s.toString());
+    }
 }