Skip to content

Commit 676925a

Browse files
committed
Change
1 parent 9fb7d09 commit 676925a

File tree

11 files changed

+114
-58
lines changed

11 files changed

+114
-58
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void writeVInt(int i) throws IOException {
141141
}
142142
}
143143

144-
protected static int vIntLength(int value) {
144+
public static int vIntLength(int value) {
145145
int leadingZeros = Integer.numberOfLeadingZeros(value);
146146
if (leadingZeros >= 25) {
147147
return 1;

server/src/main/java/org/elasticsearch/index/engine/TranslogDirectoryReader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ private static LeafReader createInMemoryReader(
182182
boolean rootDocOnly,
183183
Translog.Index operation
184184
) {
185+
final String id = Uid.decodeId(operation.uid());
185186
final ParsedDocument parsedDocs = documentParser.parseDocument(
186-
new SourceToParse(operation.id(), operation.source(), XContentHelper.xContentType(operation.source()), operation.routing()),
187+
new SourceToParse(id, operation.source(), XContentHelper.xContentType(operation.source()), operation.routing()),
187188
mappingLookup
188189
);
189190

@@ -244,7 +245,7 @@ protected StoredFieldsReader doGetSequentialStoredFieldsReader(StoredFieldsReade
244245
}
245246
};
246247
} catch (IOException e) {
247-
throw new EngineException(shardId, "failed to create an in-memory segment for get [" + operation.id() + "]", e);
248+
throw new EngineException(shardId, "failed to create an in-memory segment for get [" + id + "]", e);
248249
}
249250
}
250251

@@ -339,7 +340,7 @@ private static class TranslogLeafReader extends LeafReader {
339340
this.engineConfig = engineConfig;
340341
this.onSegmentCreated = onSegmentCreated;
341342
this.directory = directory;
342-
this.uid = Uid.encodeId(operation.id());
343+
this.uid = operation.uid();
343344
}
344345

345346
private LeafReader getDelegate() {

server/src/main/java/org/elasticsearch/index/mapper/Uid.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ private static String decodeBase64Id(byte[] idBytes, int offset, int length) {
173173
return Strings.BASE_64_NO_PADDING_URL_ENCODER.encodeToString(idBytes);
174174
}
175175

176+
/** Decode an indexed id back to its original form.
177+
* @see #encodeId */
178+
public static String decodeId(BytesRef idBytes) {
179+
return decodeId(idBytes.bytes, idBytes.offset, idBytes.length);
180+
}
181+
176182
/** Decode an indexed id back to its original form.
177183
* @see #encodeId */
178184
public static String decodeId(byte[] idBytes) {

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,12 @@ private Engine.Result applyTranslogOperation(Engine engine, Translog.Operation o
21302130
index.getAutoGeneratedIdTimestamp(),
21312131
true,
21322132
origin,
2133-
new SourceToParse(index.id(), index.source(), XContentHelper.xContentType(index.source()), index.routing())
2133+
new SourceToParse(
2134+
Uid.decodeId(index.uid()),
2135+
index.source(),
2136+
XContentHelper.xContentType(index.source()),
2137+
index.routing()
2138+
)
21342139
);
21352140
}
21362141
case DELETE -> {
@@ -2140,7 +2145,7 @@ private Engine.Result applyTranslogOperation(Engine engine, Translog.Operation o
21402145
delete.seqNo(),
21412146
delete.primaryTerm(),
21422147
delete.version(),
2143-
delete.id(),
2148+
Uid.decodeId(delete.uid()),
21442149
versionType,
21452150
UNASSIGNED_SEQ_NO,
21462151
0,

server/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ public static final class Index extends Operation {
12431243
public static final int FORMAT_REORDERED = FORMAT_NO_DOC_TYPE + 1;
12441244
public static final int SERIALIZATION_FORMAT = FORMAT_REORDERED;
12451245

1246-
private final String id;
1246+
private final BytesRef uid;
12471247
private final long autoGeneratedIdTimestamp;
12481248
private final long version;
12491249
private final BytesReference source;
@@ -1252,15 +1252,15 @@ public static final class Index extends Operation {
12521252
private static Index readFrom(StreamInput in) throws IOException {
12531253
final int format = in.readVInt(); // SERIALIZATION_FORMAT
12541254
assert format >= FORMAT_NO_PARENT : "format was: " + format;
1255-
String id;
1255+
BytesRef uid;
12561256
BytesReference source;
12571257
String routing;
12581258
long version;
12591259
long autoGeneratedIdTimestamp;
12601260
long seqNo;
12611261
long primaryTerm;
12621262
if (format < FORMAT_REORDERED) {
1263-
id = in.readString();
1263+
uid = Uid.encodeId(in.readString());
12641264
if (format < FORMAT_NO_DOC_TYPE) {
12651265
in.readString();
12661266
// can't assert that this is _doc because pre-8.0 indexes can have any name for a type
@@ -1279,16 +1279,16 @@ private static Index readFrom(StreamInput in) throws IOException {
12791279
autoGeneratedIdTimestamp = in.readLong();
12801280
seqNo = in.readLong();
12811281
primaryTerm = in.readLong();
1282+
uid = in.readBytesRef();
12821283
routing = in.readOptionalString();
1283-
id = in.readString();
12841284
source = in.readBytesReference();
12851285
}
1286-
return new Index(id, seqNo, primaryTerm, version, source, routing, autoGeneratedIdTimestamp);
1286+
return new Index(uid, seqNo, primaryTerm, version, source, routing, autoGeneratedIdTimestamp);
12871287
}
12881288

12891289
public Index(Engine.Index index, Engine.IndexResult indexResult) {
12901290
this(
1291-
index.id(),
1291+
index.uid(),
12921292
indexResult.getSeqNo(),
12931293
index.primaryTerm(),
12941294
indexResult.getVersion(),
@@ -1306,9 +1306,21 @@ public Index(
13061306
BytesReference source,
13071307
String routing,
13081308
long autoGeneratedIdTimestamp
1309+
) {
1310+
this(Uid.encodeId(id), seqNo, primaryTerm, version, source, routing, autoGeneratedIdTimestamp);
1311+
}
1312+
1313+
public Index(
1314+
BytesRef uid,
1315+
long seqNo,
1316+
long primaryTerm,
1317+
long version,
1318+
BytesReference source,
1319+
String routing,
1320+
long autoGeneratedIdTimestamp
13091321
) {
13101322
super(seqNo, primaryTerm);
1311-
this.id = id;
1323+
this.uid = uid;
13121324
this.source = source;
13131325
this.version = version;
13141326
this.routing = routing;
@@ -1322,14 +1334,14 @@ public Type opType() {
13221334

13231335
@Override
13241336
public long estimateSize() {
1325-
return (2 * id.length()) + source.length() + (routing != null ? 2 * routing.length() : 0) + (4 * Long.BYTES); // timestamp,
1337+
return uid.length + source.length() + (routing != null ? 2 * routing.length() : 0) + (4 * Long.BYTES); // timestamp,
13261338
// seq_no,
13271339
// primary_term,
13281340
// and version
13291341
}
13301342

1331-
public String id() {
1332-
return this.id;
1343+
public BytesRef uid() {
1344+
return uid;
13331345
}
13341346

13351347
public String routing() {
@@ -1351,8 +1363,8 @@ protected void writeHeader(int format, StreamOutput out) throws IOException {
13511363
out.writeLong(autoGeneratedIdTimestamp);
13521364
out.writeLong(seqNo);
13531365
out.writeLong(primaryTerm);
1366+
out.writeBytesRef(uid);
13541367
out.writeOptionalString(routing);
1355-
out.writeString(id);
13561368
out.writeVInt(source == null ? 0 : source.length());
13571369
}
13581370

@@ -1363,7 +1375,7 @@ public void writeBody(final StreamOutput out) throws IOException {
13631375
: FORMAT_NO_VERSION_TYPE;
13641376
if (format < FORMAT_REORDERED) {
13651377
out.writeVInt(format);
1366-
out.writeString(id);
1378+
out.writeString(Uid.decodeId(uid.bytes, uid.offset, uid.length));
13671379
if (format < FORMAT_NO_DOC_TYPE) {
13681380
out.writeString(MapperService.SINGLE_MAPPING_NAME);
13691381
}
@@ -1396,7 +1408,7 @@ public boolean equals(Object o) {
13961408

13971409
@Override
13981410
public int hashCode() {
1399-
int result = id.hashCode();
1411+
int result = uid.hashCode();
14001412
result = 31 * result + Long.hashCode(seqNo);
14011413
result = 31 * result + Long.hashCode(primaryTerm);
14021414
result = 31 * result + Long.hashCode(version);
@@ -1410,7 +1422,7 @@ public int hashCode() {
14101422
public String toString() {
14111423
return "Index{"
14121424
+ "id='"
1413-
+ id
1425+
+ Uid.decodeId(uid.bytes, uid.offset, uid.length)
14141426
+ '\''
14151427
+ ", seqNo="
14161428
+ seqNo
@@ -1431,7 +1443,7 @@ public static boolean equalsWithoutAutoGeneratedTimestamp(Translog.Index o1, Tra
14311443
if (o1.version != o2.version
14321444
|| o1.seqNo != o2.seqNo
14331445
|| o1.primaryTerm != o2.primaryTerm
1434-
|| o1.id.equals(o2.id) == false
1446+
|| o1.uid.equals(o2.uid) == false
14351447
|| Objects.equals(o1.routing, o2.routing) == false) {
14361448
return false;
14371449
}
@@ -1475,13 +1487,13 @@ public static final class Delete extends Operation {
14751487
public static final int FORMAT_REORDERED = FORMAT_NO_DOC_TYPE + 1;
14761488
public static final int SERIALIZATION_FORMAT = FORMAT_REORDERED;
14771489

1478-
private final String id;
1490+
private final BytesRef uid;
14791491
private final long version;
14801492

14811493
private static Delete readFrom(StreamInput in) throws IOException {
14821494
final int format = in.readVInt();// SERIALIZATION_FORMAT
14831495
assert format >= FORMAT_6_0 : "format was: " + format;
1484-
final String id;
1496+
final BytesRef uid;
14851497
final long version;
14861498
final long seqNo;
14871499
final long primaryTerm;
@@ -1490,7 +1502,7 @@ private static Delete readFrom(StreamInput in) throws IOException {
14901502
in.readString();
14911503
// Can't assert that this is _doc because pre-8.0 indexes can have any name for a type
14921504
}
1493-
id = in.readString();
1505+
uid = Uid.encodeId(in.readString());
14941506
if (format < FORMAT_NO_DOC_TYPE) {
14951507
final String docType = in.readString();
14961508
assert docType.equals(IdFieldMapper.NAME) : docType + " != " + IdFieldMapper.NAME;
@@ -1506,23 +1518,28 @@ private static Delete readFrom(StreamInput in) throws IOException {
15061518
version = in.readLong();
15071519
seqNo = in.readLong();
15081520
primaryTerm = in.readLong();
1509-
id = in.readString();
1521+
uid = in.readBytesRef();
15101522
}
1511-
return new Delete(id, seqNo, primaryTerm, version);
1523+
return new Delete(uid, seqNo, primaryTerm, version);
15121524
}
15131525

15141526
public Delete(Engine.Delete delete, Engine.DeleteResult deleteResult) {
1515-
this(delete.id(), deleteResult.getSeqNo(), delete.primaryTerm(), deleteResult.getVersion());
1527+
this(delete.uid(), deleteResult.getSeqNo(), delete.primaryTerm(), deleteResult.getVersion());
15161528
}
15171529

15181530
/** utility for testing */
15191531
public Delete(String id, long seqNo, long primaryTerm) {
15201532
this(id, seqNo, primaryTerm, Versions.MATCH_ANY);
15211533
}
15221534

1535+
/** utility for testing */
15231536
public Delete(String id, long seqNo, long primaryTerm, long version) {
1537+
this(Uid.encodeId(id), seqNo, primaryTerm, version);
1538+
}
1539+
1540+
public Delete(BytesRef uid, long seqNo, long primaryTerm, long version) {
15241541
super(seqNo, primaryTerm);
1525-
this.id = Objects.requireNonNull(id);
1542+
this.uid = Objects.requireNonNull(uid);
15261543
this.version = version;
15271544
}
15281545

@@ -1533,7 +1550,7 @@ public Type opType() {
15331550

15341551
@Override
15351552
public long estimateSize() {
1536-
return (2 * id.length()) + (3 * Long.BYTES); // seq_no, primary_term, and version;
1553+
return uid.length + (3 * Long.BYTES); // seq_no, primary_term, and version;
15371554
}
15381555

15391556
@Override
@@ -1542,11 +1559,11 @@ protected void writeHeader(int format, StreamOutput out) throws IOException {
15421559
out.writeLong(version);
15431560
out.writeLong(seqNo);
15441561
out.writeLong(primaryTerm);
1545-
out.writeString(id);
1562+
out.writeBytesRef(uid);
15461563
}
15471564

1548-
public String id() {
1549-
return id;
1565+
public BytesRef uid() {
1566+
return uid;
15501567
}
15511568

15521569
public long version() {
@@ -1563,10 +1580,10 @@ public void writeBody(final StreamOutput out) throws IOException {
15631580
if (format < FORMAT_NO_DOC_TYPE) {
15641581
out.writeString(MapperService.SINGLE_MAPPING_NAME);
15651582
}
1566-
out.writeString(id);
1583+
out.writeString(Uid.decodeId(uid));
15671584
if (format < FORMAT_NO_DOC_TYPE) {
15681585
out.writeString(IdFieldMapper.NAME);
1569-
out.writeBytesRef(Uid.encodeId(id));
1586+
out.writeBytesRef(uid);
15701587
}
15711588
out.writeLong(version);
15721589
out.writeLong(seqNo);
@@ -1587,12 +1604,12 @@ public boolean equals(Object o) {
15871604

15881605
Delete delete = (Delete) o;
15891606

1590-
return id.equals(delete.id) && seqNo == delete.seqNo && primaryTerm == delete.primaryTerm && version == delete.version;
1607+
return uid.equals(delete.uid) && seqNo == delete.seqNo && primaryTerm == delete.primaryTerm && version == delete.version;
15911608
}
15921609

15931610
@Override
15941611
public int hashCode() {
1595-
int result = id.hashCode();
1612+
int result = uid.hashCode();
15961613
result += 31 * Long.hashCode(seqNo);
15971614
result = 31 * result + Long.hashCode(primaryTerm);
15981615
result = 31 * result + Long.hashCode(version);
@@ -1601,7 +1618,16 @@ public int hashCode() {
16011618

16021619
@Override
16031620
public String toString() {
1604-
return "Delete{" + "id='" + id + "', seqNo=" + seqNo + ", primaryTerm=" + primaryTerm + ", version=" + version + '}';
1621+
return "Delete{"
1622+
+ "id='"
1623+
+ Uid.decodeId(uid)
1624+
+ "', seqNo="
1625+
+ seqNo
1626+
+ ", primaryTerm="
1627+
+ primaryTerm
1628+
+ ", version="
1629+
+ version
1630+
+ '}';
16051631
}
16061632
}
16071633

server/src/main/java/org/elasticsearch/index/translog/TranslogHeaderWriter.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.util.BytesRef;
1313
import org.elasticsearch.common.bytes.BytesReference;
1414
import org.elasticsearch.common.io.stream.RecyclerBytesStreamOutput;
15+
import org.elasticsearch.common.io.stream.StreamOutput;
1516
import org.elasticsearch.common.util.ByteUtils;
1617

1718
import java.io.IOException;
@@ -35,15 +36,24 @@ private TranslogHeaderWriter() {
3536
}
3637

3738
public static void writeIndexHeader(RecyclerBytesStreamOutput buffer, Translog.Index index) throws IOException {
38-
BytesRef page = buffer.tryGetPageForWrite(FIXED_INDEX_HEADER_SIZE);
39+
int uidLen = index.uid().length;
40+
int uidVIntLen = RecyclerBytesStreamOutput.vIntLength(uidLen);
41+
BytesRef page = buffer.tryGetPageForWrite(FIXED_INDEX_HEADER_SIZE + uidLen + uidVIntLen);
3942
if (page != null) {
40-
writeFastIndexHeader(buffer, index, page);
43+
writeFastIndexHeader(buffer, index, page, uidLen, uidVIntLen);
4144
} else {
4245
writeSlowIndexHeader(buffer, index);
4346
}
4447
}
4548

46-
private static void writeFastIndexHeader(RecyclerBytesStreamOutput buffer, Translog.Index index, BytesRef page) throws IOException {
49+
private static void writeFastIndexHeader(
50+
RecyclerBytesStreamOutput buffer,
51+
Translog.Index index,
52+
BytesRef page,
53+
int uidLen,
54+
int uidVIntLen
55+
) throws IOException {
56+
BytesRef uid = index.uid();
4757
String routing = index.routing();
4858

4959
int off = page.offset;
@@ -55,14 +65,15 @@ private static void writeFastIndexHeader(RecyclerBytesStreamOutput buffer, Trans
5565
ByteUtils.writeLongBE(index.getAutoGeneratedIdTimestamp(), bytes, off + 14);
5666
ByteUtils.writeLongBE(index.seqNo(), bytes, off + 22);
5767
ByteUtils.writeLongBE(index.primaryTerm(), bytes, off + 30);
58-
bytes[off + 38] = index.routing() == null ? (byte) 0 : (byte) 1;
68+
StreamOutput.putVInt(bytes, uid.length, off + 38);
69+
System.arraycopy(uid.bytes, uid.offset, bytes, off + 38 + uidVIntLen, uid.length);
70+
bytes[off + 38 + uidVIntLen + uidLen] = index.routing() == null ? (byte) 0 : (byte) 1;
5971

6072
long variableLengthStart = buffer.position();
6173
// Write variable length items in header
6274
if (routing != null) {
6375
buffer.writeString(routing);
6476
}
65-
buffer.writeString(index.id());
6677

6778
BytesReference source = index.source();
6879
int sourceLength = source == null ? 0 : source.length();
@@ -109,7 +120,7 @@ private static void writeFastDeleteHeader(RecyclerBytesStreamOutput buffer, Tran
109120

110121
long variableLengthStart = buffer.position();
111122
// Write variable length items in header
112-
buffer.writeString(delete.id());
123+
buffer.writeBytesRef(delete.uid());
113124
int variableLengthSize = (int) (buffer.position() - variableLengthStart);
114125
// The total operation size is the header size + 4 bytes for checksum
115126
int sizeOfOperation = FIXED_DELETE_HEADER_SIZE - Integer.BYTES + variableLengthSize + Integer.BYTES;

0 commit comments

Comments
 (0)