Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.index.seqno.SequenceNumbers;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -37,8 +36,6 @@ public final class LockModel implements ToXContentObject, Writeable {

// Rest Fields
public static final String GET_LOCK_ACTION = "get_lock_action";
public static final String SEQUENCE_NUMBER = "seq_no";
public static final String PRIMARY_TERM = "primary_term";
public static final String LOCK_ID = "lock_id";
public static final String LOCK_MODEL = "lock_model";

Expand All @@ -48,18 +45,14 @@ public final class LockModel implements ToXContentObject, Writeable {
private final Instant lockTime;
private final long lockDurationSeconds;
private final boolean released;
private final long seqNo;
private final long primaryTerm;

/**
* Use this constructor to copy existing lock and update the seqNo and primaryTerm.
*
* @param copyLock JobSchedulerLockModel to copy from.
* @param seqNo sequence number from OpenSearch document.
* @param primaryTerm primary term from OpenSearch document.
*/
public LockModel(final LockModel copyLock, long seqNo, long primaryTerm) {
this(copyLock.jobIndexName, copyLock.jobId, copyLock.lockTime, copyLock.lockDurationSeconds, copyLock.released, seqNo, primaryTerm);
public LockModel(final LockModel copyLock) {
this(copyLock.jobIndexName, copyLock.jobId, copyLock.lockTime, copyLock.lockDurationSeconds, copyLock.released);
}

/**
Expand All @@ -69,15 +62,7 @@ public LockModel(final LockModel copyLock, long seqNo, long primaryTerm) {
* @param released boolean flag to indicate if the lock is released
*/
public LockModel(final LockModel copyLock, final boolean released) {
this(
copyLock.jobIndexName,
copyLock.jobId,
copyLock.lockTime,
copyLock.lockDurationSeconds,
released,
copyLock.seqNo,
copyLock.primaryTerm
);
this(copyLock.jobIndexName, copyLock.jobId, copyLock.lockTime, copyLock.lockDurationSeconds, released);
}

/**
Expand All @@ -89,30 +74,10 @@ public LockModel(final LockModel copyLock, final boolean released) {
* @param released boolean flag to indicate if the lock is released
*/
public LockModel(final LockModel copyLock, final Instant updateLockTime, final long lockDurationSeconds, final boolean released) {
this(copyLock.jobIndexName, copyLock.jobId, updateLockTime, lockDurationSeconds, released, copyLock.seqNo, copyLock.primaryTerm);
this(copyLock.jobIndexName, copyLock.jobId, updateLockTime, lockDurationSeconds, released);
}

public LockModel(String jobIndexName, String jobId, Instant lockTime, long lockDurationSeconds, boolean released) {
this(
jobIndexName,
jobId,
lockTime,
lockDurationSeconds,
released,
SequenceNumbers.UNASSIGNED_SEQ_NO,
SequenceNumbers.UNASSIGNED_PRIMARY_TERM
);
}

public LockModel(
String jobIndexName,
String jobId,
Instant lockTime,
long lockDurationSeconds,
boolean released,
long seqNo,
long primaryTerm
) {
this.lockId = jobIndexName + LOCK_ID_DELIMITER + jobId;
this.jobIndexName = jobIndexName;
// The jobId parameter does not necessarily need to represent the id of a job scheduler job, as it is being used
Expand All @@ -121,27 +86,17 @@ public LockModel(
this.lockTime = lockTime;
this.lockDurationSeconds = lockDurationSeconds;
this.released = released;
this.seqNo = seqNo;
this.primaryTerm = primaryTerm;
}

public LockModel(StreamInput in) throws IOException {
this(
in.readString(),
in.readString(),
in.readInstant(),
in.readLong(),
in.readBoolean(),
SequenceNumbers.UNASSIGNED_SEQ_NO,
SequenceNumbers.UNASSIGNED_PRIMARY_TERM
);
this(in.readString(), in.readString(), in.readInstant(), in.readLong(), in.readBoolean());
}

public static String generateLockId(String jobIndexName, String jobId) {
return jobIndexName + LOCK_ID_DELIMITER + jobId;
}

public static LockModel parse(final XContentParser parser, long seqNo, long primaryTerm) throws IOException {
public static LockModel parse(final XContentParser parser) throws IOException {
String jobIndexName = null;
String jobId = null;
Instant lockTime = null;
Expand Down Expand Up @@ -178,9 +133,7 @@ public static LockModel parse(final XContentParser parser, long seqNo, long prim
requireNonNull(jobId, "JobId cannot be null"),
requireNonNull(lockTime, "lockTime cannot be null"),
requireNonNull(lockDurationSecond, "lockDurationSeconds cannot be null"),
requireNonNull(released, "released cannot be null"),
seqNo,
primaryTerm
requireNonNull(released, "released cannot be null")
);
}

Expand Down Expand Up @@ -241,14 +194,6 @@ public boolean isReleased() {
return released;
}

public long getSeqNo() {
return seqNo;
}

public long getPrimaryTerm() {
return primaryTerm;
}

public boolean isExpired() {
return lockTime.getEpochSecond() + lockDurationSeconds < Instant.now().getEpochSecond();
}
Expand All @@ -260,8 +205,6 @@ public boolean equals(Object o) {
LockModel lockModel = (LockModel) o;
return lockDurationSeconds == lockModel.lockDurationSeconds
&& released == lockModel.released
&& seqNo == lockModel.seqNo
&& primaryTerm == lockModel.primaryTerm
&& lockId.equals(lockModel.lockId)
&& jobIndexName.equals(lockModel.jobIndexName)
&& jobId.equals(lockModel.jobId)
Expand All @@ -270,7 +213,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(lockId, jobIndexName, jobId, lockTime, lockDurationSeconds, released, seqNo, primaryTerm);
return Objects.hash(lockId, jobIndexName, jobId, lockTime, lockDurationSeconds, released);
}

@Override
Expand All @@ -281,7 +224,5 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeInstant(this.lockTime);
out.writeLong(this.lockDurationSeconds);
out.writeBoolean(this.released);
out.writeLong(this.seqNo);
out.writeLong(this.primaryTerm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.index.seqno.SequenceNumbers;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -35,36 +34,20 @@ public final class StatusHistoryModel implements ToXContentObject, Writeable {
private final Instant startTime;
private final Instant endTime;
private final int status;
private final long seqNo;
private final long primaryTerm;

public StatusHistoryModel(String jobIndexName, String jobId, Instant startTime, Instant endTime, int status) {
this(jobIndexName, jobId, startTime, endTime, status, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
}

public StatusHistoryModel(
String jobIndexName,
String jobId,
Instant startTime,
Instant endTime,
int status,
long seqNo,
long primaryTerm
) {
this.jobIndexName = jobIndexName;
this.jobId = jobId;
this.startTime = startTime;
this.endTime = endTime;
this.status = status;
this.seqNo = seqNo;
this.primaryTerm = primaryTerm;
}

public StatusHistoryModel(StreamInput in) throws IOException {
this(in.readString(), in.readString(), in.readInstant(), in.readOptionalInstant(), in.readInt(), in.readLong(), in.readLong());
this(in.readString(), in.readString(), in.readInstant(), in.readOptionalInstant(), in.readInt());
}

public static StatusHistoryModel parse(final XContentParser parser, long seqNo, long primaryTerm) throws IOException {
public static StatusHistoryModel parse(final XContentParser parser) throws IOException {
String jobIndexName = null;
String jobId = null;
Instant startTime = null;
Expand Down Expand Up @@ -103,9 +86,7 @@ public static StatusHistoryModel parse(final XContentParser parser, long seqNo,
requireNonNull(jobId, "JobId cannot be null"),
requireNonNull(startTime, "startTime cannot be null"),
endTime,
requireNonNull(status, "status cannot be null"),
seqNo,
primaryTerm
requireNonNull(status, "status cannot be null")
);
}

Expand Down Expand Up @@ -146,22 +127,12 @@ public int getStatus() {
return status;
}

public long getSeqNo() {
return seqNo;
}

public long getPrimaryTerm() {
return primaryTerm;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StatusHistoryModel that = (StatusHistoryModel) o;
return seqNo == that.seqNo
&& primaryTerm == that.primaryTerm
&& jobIndexName.equals(that.jobIndexName)
return jobIndexName.equals(that.jobIndexName)
&& jobId.equals(that.jobId)
&& startTime.equals(that.startTime)
&& Objects.equals(endTime, that.endTime)
Expand All @@ -170,7 +141,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(jobIndexName, jobId, startTime, endTime, status, seqNo, primaryTerm);
return Objects.hash(jobIndexName, jobId, startTime, endTime, status);
}

@Override
Expand All @@ -180,7 +151,5 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeInstant(this.startTime);
out.writeOptionalInstant(this.endTime);
out.writeInt(this.status);
out.writeLong(this.seqNo);
out.writeLong(this.primaryTerm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,7 @@ public void recordJobHistory(
findHistoryRecord(jobIndexName, jobId, startTime, ActionListener.wrap(existingRecord -> {
if (existingRecord != null) {
// Update existing record
StatusHistoryModel updatedModel = new StatusHistoryModel(
jobIndexName,
jobId,
startTime,
endTime,
status,
existingRecord.getSeqNo(),
existingRecord.getPrimaryTerm()
);
StatusHistoryModel updatedModel = new StatusHistoryModel(jobIndexName, jobId, startTime, endTime, status);
updateHistoryRecord(
updatedModel,
ActionListener.wrap(updated -> listener.onResponse(updated != null), listener::onFailure)
Expand Down Expand Up @@ -186,8 +178,6 @@ public void updateHistoryRecord(final StatusHistoryModel historyModelupdate, Act

UpdateRequest updateRequest = new UpdateRequest().index(JOB_HISTORY_INDEX_NAME)
.id(documentId)
.setIfSeqNo(historyModelupdate.getSeqNo())
.setIfPrimaryTerm(historyModelupdate.getPrimaryTerm())
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.doc(historyModelupdate.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.fetchSource(true);
Expand All @@ -201,9 +191,7 @@ public void updateHistoryRecord(final StatusHistoryModel historyModelupdate, Act
historyModelupdate.getJobId(),
historyModelupdate.getStartTime(),
historyModelupdate.getEndTime(),
historyModelupdate.getStatus(),
response.getSeqNo(),
response.getPrimaryTerm()
historyModelupdate.getStatus()
)
),
exception -> {
Expand Down Expand Up @@ -237,7 +225,7 @@ public void findHistoryRecord(
XContentParser parser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.getSourceAsString());
parser.nextToken();
listener.onResponse(StatusHistoryModel.parse(parser, response.getSeqNo(), response.getPrimaryTerm()));
listener.onResponse(StatusHistoryModel.parse(parser));
} catch (IOException e) {
logger.error("IOException occurred parsing history record", e);
listener.onResponse(null);
Expand Down
Loading
Loading