Browse Source

Deduplicate RetentionLease.source (#92347)

We only ever see the strings "ccr" or "peer recovery" here.
A recent experiment with ccr and many follower clusters following
one leader, revealed 100s of MB being wasted on duplicate "ccr"
strings when fetching index stats -> lets fix this by simple
deduplication.
Armin Braun 2 years ago
parent
commit
d800d26e5d

+ 7 - 5
server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java

@@ -99,7 +99,12 @@ public final class RetentionLease implements ToXContentObject, Writeable {
         this.id = id;
         this.retainingSequenceNumber = retainingSequenceNumber;
         this.timestamp = timestamp;
-        this.source = source;
+        // deduplicate the string instances to save memory for the known possible source values
+        this.source = switch (source) {
+            case "ccr" -> "ccr";
+            case ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE -> ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE;
+            default -> source;
+        };
     }
 
     /**
@@ -109,10 +114,7 @@ public final class RetentionLease implements ToXContentObject, Writeable {
      * @throws IOException if an I/O exception occurs reading from the stream
      */
     public RetentionLease(final StreamInput in) throws IOException {
-        id = in.readString();
-        retainingSequenceNumber = in.readZLong();
-        timestamp = in.readVLong();
-        source = in.readString();
+        this(in.readString(), in.readZLong(), in.readVLong(), in.readString());
     }
 
     /**