Ver código fonte

Fix estimate size of translog operations (#59206)

Make sure that the estimateSize method includes all fields of translog operations.
Nhat Nguyen 5 anos atrás
pai
commit
4c1b081e27

+ 12 - 6
server/src/main/java/org/elasticsearch/index/translog/Translog.java

@@ -1161,7 +1161,10 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
 
         @Override
         public long estimateSize() {
-            return (id.length() * 2) + source.length() + 12;
+            return (2 * id.length())
+                + source.length()
+                + (routing != null ? 2 * routing.length() : 0)
+                + (4 * Long.BYTES); // timestamp, seq_no, primary_term, and version
         }
 
         public String id() {
@@ -1328,7 +1331,8 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
 
         @Override
         public long estimateSize() {
-            return 4 + (id.length() * 2) + 24;
+            return (2 * id.length())
+                + (3 * Long.BYTES); // seq_no, primary_term, and version;
         }
 
         public String id() {
@@ -1384,14 +1388,16 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
 
             Delete delete = (Delete) o;
 
-            return version == delete.version &&
+            return id.equals(delete.id) &&
                 seqNo == delete.seqNo &&
-                primaryTerm == delete.primaryTerm;
+                primaryTerm == delete.primaryTerm &&
+                version == delete.version;
         }
 
         @Override
         public int hashCode() {
-            int result = Long.hashCode(seqNo);
+            int result = id.hashCode();
+            result += 31 * Long.hashCode(seqNo);
             result = 31 * result + Long.hashCode(primaryTerm);
             result = 31 * result + Long.hashCode(version);
             return result;
@@ -1477,7 +1483,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC
 
         @Override
         public int hashCode() {
-            return 31 * 31 * 31 + 31 * 31 * Long.hashCode(seqNo) + 31 * Long.hashCode(primaryTerm) + reason().hashCode();
+            return 31 * 31 * Long.hashCode(seqNo) + 31 * Long.hashCode(primaryTerm) + reason().hashCode();
         }
 
         @Override

+ 2 - 6
x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java

@@ -143,8 +143,8 @@ public class ShardChangesActionTests extends ESSingleNodeTestCase {
 
         final IndexShard indexShard = indexService.getShard(0);
         final Translog.Operation[] operations = ShardChangesAction.getOperations(indexShard, indexShard.getLastKnownGlobalCheckpoint(),
-            0, 12, indexShard.getHistoryUUID(), new ByteSizeValue(256, ByteSizeUnit.BYTES));
-        assertThat(operations.length, equalTo(12));
+            0, randomIntBetween(100, 500), indexShard.getHistoryUUID(), new ByteSizeValue(256, ByteSizeUnit.BYTES));
+        assertThat(operations.length, equalTo(8));
         assertThat(operations[0].seqNo(), equalTo(0L));
         assertThat(operations[1].seqNo(), equalTo(1L));
         assertThat(operations[2].seqNo(), equalTo(2L));
@@ -153,10 +153,6 @@ public class ShardChangesActionTests extends ESSingleNodeTestCase {
         assertThat(operations[5].seqNo(), equalTo(5L));
         assertThat(operations[6].seqNo(), equalTo(6L));
         assertThat(operations[7].seqNo(), equalTo(7L));
-        assertThat(operations[8].seqNo(), equalTo(8L));
-        assertThat(operations[9].seqNo(), equalTo(9L));
-        assertThat(operations[10].seqNo(), equalTo(10L));
-        assertThat(operations[11].seqNo(), equalTo(11L));
     }
 
     public void testGetOperationsAlwaysReturnAtLeastOneOp() throws Exception {