-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sequence Numbers and enforce Primary Terms
Adds a counter to each write operation on a shard. This sequence numbers is indexed into lucene using doc values, for now (we will probably require indexing to support range searchers in the future). On top of this, primary term semantics are enforced and shards will refuse write operation coming from an older primary. Other notes: - The add SequenceServiceNumber is just a skeleton and will be replaced with much heavier one, once we have all the building blocks (i.e., checkpoints). - I completely ignored recovery - for this we will need checkpoints as well. - A new based class is introduced for all single doc write operations. This is handy to unify common logic (like toXContent). - For now, we don't use seq# as versioning. We could in the future. Relates to #10708 Closes #14651
- Loading branch information
Showing
52 changed files
with
1,095 additions
and
707 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
core/src/main/java/org/elasticsearch/action/DocWriteResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.action; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentBuilderString; | ||
import org.elasticsearch.index.seqno.SequenceNumbersService; | ||
import org.elasticsearch.index.shard.ShardId; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A base class for the response of a write operation that involves a single doc | ||
*/ | ||
public abstract class DocWriteResponse extends ReplicationResponse implements ToXContent { | ||
|
||
private ShardId shardId; | ||
private String id; | ||
private String type; | ||
private long version; | ||
private long seqNo; | ||
|
||
public DocWriteResponse(ShardId shardId, String type, String id, long seqNo, long version) { | ||
this.shardId = shardId; | ||
this.type = type; | ||
this.id = id; | ||
this.seqNo = seqNo; | ||
this.version = version; | ||
} | ||
|
||
// needed for deserialization | ||
protected DocWriteResponse() { | ||
} | ||
|
||
/** | ||
* The index the document was changed in. | ||
*/ | ||
public String getIndex() { | ||
return this.shardId.getIndex(); | ||
} | ||
|
||
|
||
/** | ||
* The exact shard the document was changed in. | ||
*/ | ||
public ShardId getShardId() { | ||
return this.shardId; | ||
} | ||
|
||
/** | ||
* The type of the document changed. | ||
*/ | ||
public String getType() { | ||
return this.type; | ||
} | ||
|
||
/** | ||
* The id of the document changed. | ||
*/ | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
/** | ||
* Returns the current version of the doc. | ||
*/ | ||
public long getVersion() { | ||
return this.version; | ||
} | ||
|
||
/** | ||
* Returns the sequence number assigned for this change. Returns {@link SequenceNumbersService#UNASSIGNED_SEQ_NO} if the operation wasn't | ||
* performed (i.e., an update operation that resulted in a NOOP). | ||
*/ | ||
public long getSeqNo() { | ||
return seqNo; | ||
} | ||
|
||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
shardId = ShardId.readShardId(in); | ||
type = in.readString(); | ||
id = in.readString(); | ||
version = in.readZLong(); | ||
seqNo = in.readZLong(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
shardId.writeTo(out); | ||
out.writeString(type); | ||
out.writeString(id); | ||
out.writeZLong(version); | ||
out.writeZLong(seqNo); | ||
} | ||
|
||
static final class Fields { | ||
static final XContentBuilderString _INDEX = new XContentBuilderString("_index"); | ||
static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); | ||
static final XContentBuilderString _ID = new XContentBuilderString("_id"); | ||
static final XContentBuilderString _VERSION = new XContentBuilderString("_version"); | ||
static final XContentBuilderString _SHARD_ID = new XContentBuilderString("_shard_id"); | ||
static final XContentBuilderString _SEQ_NO = new XContentBuilderString("_seq_no"); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
ReplicationResponse.ShardInfo shardInfo = getShardInfo(); | ||
builder.field(Fields._INDEX, getIndex()) | ||
.field(Fields._TYPE, getType()) | ||
.field(Fields._ID, getId()) | ||
.field(Fields._VERSION, getVersion()); | ||
//nocommit: i'm not sure we want to expose it in the api but it will be handy for debugging while we work... | ||
builder.field(Fields._SHARD_ID, shardId.id()); | ||
if (getSeqNo() >= 0) { | ||
builder.field(Fields._SEQ_NO, getSeqNo()); | ||
} | ||
shardInfo.toXContent(builder, params); | ||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.