forked from opensearch-project/alerting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add distributed locking to jobs in alerting
Signed-off-by: Subhobrata Dey <[email protected]>
- Loading branch information
Showing
10 changed files
with
592 additions
and
1 deletion.
There are no files selected for viewing
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
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
132 changes: 132 additions & 0 deletions
132
core/src/main/kotlin/org/opensearch/alerting/core/lock/LockModel.kt
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,132 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.core.lock | ||
|
||
import org.opensearch.core.xcontent.ToXContent | ||
import org.opensearch.core.xcontent.ToXContentObject | ||
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 | ||
|
||
class LockModel( | ||
val lockId: String, | ||
val scheduledJobId: String, | ||
val lockTime: Instant, | ||
val lockDurationSeconds: Long, | ||
val released: Boolean, | ||
val seqNo: Long, | ||
val primaryTerm: Long | ||
) : ToXContentObject { | ||
|
||
constructor( | ||
copyLock: LockModel, | ||
seqNo: Long, | ||
primaryTerm: Long | ||
) : this ( | ||
copyLock.lockId, | ||
copyLock.scheduledJobId, | ||
copyLock.lockTime, | ||
copyLock.lockDurationSeconds, | ||
copyLock.released, | ||
seqNo, | ||
primaryTerm | ||
) | ||
|
||
constructor( | ||
copyLock: LockModel, | ||
released: Boolean | ||
) : this ( | ||
copyLock.lockId, | ||
copyLock.scheduledJobId, | ||
copyLock.lockTime, | ||
copyLock.lockDurationSeconds, | ||
released, | ||
copyLock.seqNo, | ||
copyLock.primaryTerm | ||
) | ||
|
||
constructor( | ||
copyLock: LockModel, | ||
updateLockTime: Instant, | ||
lockDurationSeconds: Long, | ||
released: Boolean | ||
) : this ( | ||
copyLock.lockId, | ||
copyLock.scheduledJobId, | ||
updateLockTime, | ||
lockDurationSeconds, | ||
released, | ||
copyLock.seqNo, | ||
copyLock.primaryTerm | ||
) | ||
|
||
constructor( | ||
scheduledJobId: String, | ||
lockTime: Instant, | ||
lockDurationSeconds: Long, | ||
released: Boolean | ||
) : this ( | ||
generateLockId(scheduledJobId), | ||
scheduledJobId, | ||
lockTime, | ||
lockDurationSeconds, | ||
released, | ||
SequenceNumbers.UNASSIGNED_SEQ_NO, | ||
SequenceNumbers.UNASSIGNED_PRIMARY_TERM | ||
) | ||
|
||
fun expired(): Boolean { | ||
return lockTime.epochSecond + lockDurationSeconds < Instant.now().epochSecond | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field(SCHEDULED_JOB_ID, scheduledJobId) | ||
.field(LOCK_TIME, lockTime.epochSecond) | ||
.field(LOCK_DURATION, lockDurationSeconds) | ||
.field(RELEASED, released) | ||
.endObject() | ||
return builder | ||
} | ||
|
||
companion object { | ||
const val SCHEDULED_JOB_ID = "scheduled_job_id" | ||
const val LOCK_TIME = "lock_time" | ||
const val LOCK_DURATION = "lock_duration_seconds" | ||
const val RELEASED = "released" | ||
|
||
fun generateLockId(scheduledJobId: String): String { | ||
return "$scheduledJobId-lock" | ||
} | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser, seqNo: Long, primaryTerm: Long): LockModel { | ||
lateinit var scheduledJobId: String | ||
lateinit var lockTime: Instant | ||
var lockDurationSeconds: Long = 0L | ||
var released: Boolean = false | ||
|
||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
SCHEDULED_JOB_ID -> scheduledJobId = xcp.text() | ||
LOCK_TIME -> lockTime = Instant.ofEpochSecond(xcp.longValue()) | ||
LOCK_DURATION -> lockDurationSeconds = xcp.longValue() | ||
RELEASED -> released = xcp.booleanValue() | ||
} | ||
} | ||
return LockModel(generateLockId(scheduledJobId), scheduledJobId, lockTime, lockDurationSeconds, released, seqNo, primaryTerm) | ||
} | ||
} | ||
} |
Oops, something went wrong.