-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add distributed locking to jobs in alerting #1403
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,13 @@ import org.opensearch.alerting.alerts.AlertIndices | |
import org.opensearch.alerting.alerts.AlertMover.Companion.moveAlerts | ||
import org.opensearch.alerting.core.JobRunner | ||
import org.opensearch.alerting.core.ScheduledJobIndices | ||
import org.opensearch.alerting.core.lock.LockModel | ||
import org.opensearch.alerting.core.lock.LockService | ||
import org.opensearch.alerting.model.MonitorRunResult | ||
import org.opensearch.alerting.model.WorkflowRunResult | ||
import org.opensearch.alerting.model.destination.DestinationContextFactory | ||
import org.opensearch.alerting.opensearchapi.retry | ||
import org.opensearch.alerting.opensearchapi.suspendUntil | ||
import org.opensearch.alerting.script.TriggerExecutionContext | ||
import org.opensearch.alerting.settings.AlertingSettings | ||
import org.opensearch.alerting.settings.AlertingSettings.Companion.ALERT_BACKOFF_COUNT | ||
|
@@ -221,6 +224,11 @@ object MonitorRunnerService : JobRunner, CoroutineScope, AbstractLifecycleCompon | |
return this | ||
} | ||
|
||
fun registerLockService(lockService: LockService): MonitorRunnerService { | ||
monitorCtx.lockService = lockService | ||
return this | ||
} | ||
|
||
// Updates destination settings when the reload API is called so that new keystore values are visible | ||
fun reloadDestinationSettings(settings: Settings) { | ||
monitorCtx.destinationSettings = loadDestinationSettings(settings) | ||
|
@@ -292,20 +300,40 @@ object MonitorRunnerService : JobRunner, CoroutineScope, AbstractLifecycleCompon | |
when (job) { | ||
is Workflow -> { | ||
launch { | ||
logger.debug( | ||
"PERF_DEBUG: executing workflow ${job.id} on node " + | ||
monitorCtx.clusterService!!.state().nodes().localNode.id | ||
) | ||
runJob(job, periodStart, periodEnd, false) | ||
var lock: LockModel? = null | ||
try { | ||
lock = monitorCtx.client!!.suspendUntil<Client, LockModel?> { | ||
monitorCtx.lockService!!.acquireLock(job, it) | ||
} ?: return@launch | ||
logger.debug("lock ${lock!!.lockId} acquired") | ||
logger.debug( | ||
"PERF_DEBUG: executing workflow ${job.id} on node " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: I think we should get rid of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
monitorCtx.clusterService!!.state().nodes().localNode.id | ||
) | ||
runJob(job, periodStart, periodEnd, false) | ||
} finally { | ||
monitorCtx.client!!.suspendUntil<Client, Boolean> { monitorCtx.lockService!!.release(lock, it) } | ||
logger.debug("lock ${lock!!.lockId} released") | ||
} | ||
} | ||
} | ||
is Monitor -> { | ||
launch { | ||
logger.debug( | ||
"PERF_DEBUG: executing ${job.monitorType} ${job.id} on node " + | ||
monitorCtx.clusterService!!.state().nodes().localNode.id | ||
) | ||
runJob(job, periodStart, periodEnd, false) | ||
var lock: LockModel? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: this code looks identical to 303-317. We should create a private method to avoid the duplication |
||
try { | ||
lock = monitorCtx.client!!.suspendUntil<Client, LockModel?> { | ||
monitorCtx.lockService!!.acquireLock(job, it) | ||
} ?: return@launch | ||
logger.debug("lock ${lock!!.lockId} acquired") | ||
logger.debug( | ||
"PERF_DEBUG: executing ${job.monitorType} ${job.id} on node " + | ||
monitorCtx.clusterService!!.state().nodes().localNode.id | ||
) | ||
runJob(job, periodStart, periodEnd, false) | ||
} finally { | ||
monitorCtx.client!!.suspendUntil<Client, Boolean> { monitorCtx.lockService!!.release(lock, it) } | ||
logger.debug("lock ${lock!!.lockId} released") | ||
} | ||
} | ||
} | ||
else -> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ import org.opensearch.action.support.IndicesOptions | |
import org.opensearch.action.support.WriteRequest.RefreshPolicy | ||
import org.opensearch.action.support.master.AcknowledgedResponse | ||
import org.opensearch.alerting.MonitorMetadataService | ||
import org.opensearch.alerting.core.lock.LockModel | ||
import org.opensearch.alerting.core.lock.LockService | ||
import org.opensearch.alerting.opensearchapi.suspendUntil | ||
import org.opensearch.alerting.util.AlertingException | ||
import org.opensearch.alerting.util.ScheduledJobUtils.Companion.WORKFLOW_DELEGATE_PATH | ||
|
@@ -49,11 +51,14 @@ object DeleteMonitorService : | |
private val log = LogManager.getLogger(this.javaClass) | ||
|
||
private lateinit var client: Client | ||
private lateinit var lockService: LockService | ||
|
||
fun initialize( | ||
client: Client, | ||
lockService: LockService | ||
) { | ||
DeleteMonitorService.client = client | ||
DeleteMonitorService.lockService = lockService | ||
} | ||
|
||
/** | ||
|
@@ -65,6 +70,7 @@ object DeleteMonitorService : | |
val deleteResponse = deleteMonitor(monitor.id, refreshPolicy) | ||
deleteDocLevelMonitorQueriesAndIndices(monitor) | ||
deleteMetadata(monitor) | ||
deleteLock(monitor) | ||
return DeleteMonitorResponse(deleteResponse.id, deleteResponse.version) | ||
} | ||
|
||
|
@@ -148,6 +154,10 @@ object DeleteMonitorService : | |
} | ||
} | ||
|
||
private suspend fun deleteLock(monitor: Monitor) { | ||
client.suspendUntil<Client, Boolean> { lockService.deleteLock(LockModel.generateLockId(monitor.id), it) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try catch? |
||
} | ||
|
||
/** | ||
* Checks if the monitor is part of the workflow | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could consider adding adding some delay between retries on acquiring the lock