Skip to content

#601 Acquire lock at the start of running a job instead of just on write. #602

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

Merged
merged 1 commit into from
May 29, 2025

Conversation

yruslan
Copy link
Collaborator

@yruslan yruslan commented May 29, 2025

Closes #601

Summary by CodeRabbit

  • Refactor

    • Improved task execution flow with centralized and explicit lock management, including special handling for transient tasks.
    • Unified logic for skipping tasks that are already running or locked.
  • Bug Fixes

    • Enhanced reliability in handling concurrent task execution, ensuring tasks are skipped gracefully when appropriate.

Copy link

coderabbitai bot commented May 29, 2025

Walkthrough

The changes remove the AlreadyRunningSkipException class and refactor the task execution flow in TaskRunnerBase. Lock acquisition is now performed for the entire job before validation and execution, with explicit handling for transient tasks and unified skip logic when locks cannot be obtained.

Changes

File(s) Change Summary
pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/AlreadyRunningSkipException.scala Deleted the AlreadyRunningSkipException class.
pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala Refactored task execution to acquire locks for the full job, centralized lock logic, and updated method signatures.

Sequence Diagram(s)

sequenceDiagram
    participant Scheduler
    participant TaskRunnerBase
    participant LockFactory
    participant Task

    Scheduler->>TaskRunnerBase: runTask(task)
    TaskRunnerBase->>TaskRunnerBase: doValidateOrSkipTask(task)
    alt Task should be skipped
        TaskRunnerBase-->>Scheduler: Return skipped status
    else
        TaskRunnerBase->>LockFactory: acquireLock(task)
        alt Lock acquired
            TaskRunnerBase->>Task: validateAndRun(task)
            TaskRunnerBase->>LockFactory: releaseLock(task)
            TaskRunnerBase-->>Scheduler: Return run status
        else Lock not acquired
            alt Skipping locked tasks allowed
                TaskRunnerBase-->>Scheduler: Return skipped status
            else
                TaskRunnerBase-->>Scheduler: Throw exception
            end
        end
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Acquire locks for the full job, not just for the write operation (#601)

Poem

Oh, what a hop in the task runner’s den,
Locks now acquired before jobs begin!
No more exceptions for rabbits to skip,
The flow is much smoother—no trip or slip.
With every job run, the warren’s secure,
Pramen’s new logic is clever and pure!
🐇🔒✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b9d276 and 475f81e.

📒 Files selected for processing (2)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/AlreadyRunningSkipException.scala (0 hunks)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala (3 hunks)
💤 Files with no reviewable changes (1)
  • pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/AlreadyRunningSkipException.scala
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Test Coverage on Scala 2.12.18
  • GitHub Check: Test Spark 3.5.5 on Scala 2.13.16
  • GitHub Check: Test Spark 2.4.8 on Scala 2.11.12
  • GitHub Check: Test Spark 3.4.4 on Scala 2.13.16
  • GitHub Check: Test Spark 3.5.5 on Scala 2.12.20
  • GitHub Check: Test Spark 3.4.4 on Scala 2.12.20
  • GitHub Check: Test Spark 3.3.4 on Scala 2.12.20
  • GitHub Check: Test Spark 3.3.4 on Scala 2.13.16
🔇 Additional comments (4)
pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala (4)

31-31: LGTM!

The import for TokenLockFactoryAllow is correctly added and necessary for the new lock handling logic.


139-139: Method call updates are consistent.

The replacement of doValidateAndRunTask with doValidateOrSkipTask properly reflects the new logic that handles skip conditions before validation.

Also applies to: 148-150


154-163: Good separation of skip logic.

The doValidateOrSkipTask method cleanly handles the skip reason check before proceeding to validation and execution, improving code clarity and maintainability.


165-187: Excellent lock management implementation.

The centralized lock acquisition logic properly:

  • Differentiates between transient and non-transient tasks with appropriate lock factories
  • Acquires locks before validation/execution as intended
  • Handles both skip and exception scenarios based on runtime configuration
  • Ensures lock release in the finally block

This addresses the PR objective effectively.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or {PR Summary} to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Unit Test Coverage

Overall Project 84.92% -0.05% 🍏
Files changed 63.64%

Module Coverage
pramen:core Jacoco Report 85.8% -0.06%
Files
Module File Coverage
pramen:core Jacoco Report TaskRunnerBase.scala 84.5% -1.77%

@yruslan yruslan requested a review from Copilot May 29, 2025 09:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR centralizes lock management by acquiring the lock at the start of a task run instead of during the write phase, ensuring more reliable concurrent execution handling.

  • Consolidates task validation and lock acquisition logic into doValidateOrSkipTask.
  • Removes redundant lock acquisition in the run method and eliminates the custom AlreadyRunningSkipException.
  • Introduces the use of TokenLockFactoryAllow for transient tasks.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala Refactored task execution flow to centralize lock management and streamline task validation.
pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/AlreadyRunningSkipException.scala Removed obsolete exception in favor of unified error handling with standard exceptions.
Comments suppressed due to low confidence (2)

pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala:154

  • [nitpick] The public method is renamed to doValidateOrSkipTask while its private helper remains as doValidateAndRunTask, which may be confusing. Consider renaming the private method (e.g., to doRunTaskWithLock) for clarity.
protected def doValidateOrSkipTask(task: Task): RunStatus = {

pramen/core/src/main/scala/za/co/absa/pramen/core/runner/task/TaskRunnerBase.scala:175

  • With the removal of AlreadyRunningSkipException, verify that downstream exception handling is updated to properly handle IllegalStateException for concurrently running tasks.
throw new IllegalStateException(s"Another instance is already running for ${task.job.outputTable.name} for ${task.infoDate}")


private def doValidateAndRunTask(task: Task, started: Instant): RunStatus = {
val isTransient = task.job.outputTable.format.isTransient
val taskLockFactory = if (isTransient) new TokenLockFactoryAllow else lockFactory
Copy link
Preview

Copilot AI May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] If TokenLockFactoryAllow is stateless, consider reusing an instance instead of creating a new one for each transient task to optimize resource usage.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Will do it in one of next PRs

@yruslan yruslan merged commit 0c758fe into main May 29, 2025
9 checks passed
@yruslan yruslan deleted the feature/601-early-lock-acquire branch May 29, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Acquire locks for the full job, not just for the write operation
1 participant