-
Notifications
You must be signed in to change notification settings - Fork 0
Create arbitrary locks with datacontext in pipeline steps #95
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
ARMmaster17
merged 5 commits into
main
from
80-create-arbitrary-locks-with-datacontext-in-pipeline-steps
May 29, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c63e93
#80 Local pipeline lock implementation
ARMmaster17 417225a
#80 Switched to middleware architecture
ARMmaster17 16e5909
#80 Arbitrary locks in DataContext
ARMmaster17 a1b8ade
#80 Linter fixes
ARMmaster17 026e94f
#80 Even more linter fixes
ARMmaster17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,40 @@ | ||
| import unittest | ||
|
|
||
| from watergrid.context import DataContext | ||
| from watergrid.pipelines.pipeline import Pipeline | ||
| from watergrid.steps import Step | ||
|
|
||
|
|
||
| class MockSetLockStep(Step): | ||
| def __init__(self): | ||
| super().__init__(self.__class__.__name__) | ||
|
|
||
| def run(self, context: DataContext): | ||
| if not context.lock.acquire(): | ||
| raise Exception("Unable to obtain lock") | ||
|
|
||
|
|
||
| class MockVerifyStep(Step): | ||
| def __init__(self): | ||
| super().__init__(self.__class__.__name__) | ||
| self.mock_flag = False | ||
|
|
||
| def run(self, context: DataContext): | ||
| self.mock_flag = context.lock.has_lock() | ||
|
|
||
| def get_flag(self) -> bool: | ||
| return self.mock_flag | ||
|
|
||
|
|
||
| class CustomStepLockTestCase(unittest.TestCase): | ||
| def test_can_use_lock(self): | ||
| mock_step = MockVerifyStep() | ||
| pipeline = Pipeline("test_pipeline") | ||
| pipeline.add_step(MockSetLockStep()) | ||
| pipeline.add_step(mock_step) | ||
| pipeline.run() | ||
| self.assertTrue(mock_step.get_flag()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or 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 hidden or 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,30 @@ | ||
| from watergrid.locks.PipelineLock import PipelineLock | ||
|
|
||
|
|
||
| class LocalPipelineLock(PipelineLock): | ||
| def acquire(self) -> bool: | ||
| if not self._lock_obj: | ||
| self._lock_obj = True | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
| def has_lock(self) -> bool: | ||
| return self._lock_obj | ||
|
|
||
| def extend_lease(self): | ||
| pass | ||
|
|
||
| def release(self): | ||
| self._lock_obj = False | ||
|
|
||
| def read_key(self, key: str): | ||
| return self.__key_value[key] | ||
|
|
||
| def write_key(self, key: str, value): | ||
| self.__key_value[key] = value | ||
|
|
||
| def __init__(self, lock_timeout: int = 60): | ||
| super().__init__(lock_timeout) | ||
| self._lock_obj = False | ||
| self.__key_value = {} |
This file contains hidden or 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,22 @@ | ||
| from abc import abstractmethod, ABC | ||
|
|
||
| from watergrid.context import DataContext | ||
| from watergrid.steps import Step | ||
|
|
||
|
|
||
| class ContextMiddleware(ABC): | ||
| """ | ||
| Abstract class representing an action that is run before and after | ||
| every context run within each step in a pipeline. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def pre_context(self, step: Step, context: DataContext): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def post_context(self, step: Step, context: DataContext): | ||
| pass |
This file contains hidden or 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,19 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class PipelineMiddleware(ABC): | ||
| """ | ||
| Abstract class representing an action that is run before and after | ||
| every run of the pipeline. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def pre_pipeline(self, pipeline): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def post_pipeline(self, pipeline): | ||
| pass |
This file contains hidden or 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,21 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from watergrid.steps import Step | ||
|
|
||
|
|
||
| class StepMiddleware(ABC): | ||
| """ | ||
| Abstract class representing an action that runs before and after each | ||
| action. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def pre_step(self, step: Step, contexts: list) -> None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def post_step(self, step: Step, contexts: list) -> None: | ||
| pass |
Empty file.
This file contains hidden or 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,16 @@ | ||
| from watergrid.context import DataContext | ||
| from watergrid.metrics.MetricsStore import MetricsStore | ||
| from watergrid.middleware.ContextMiddleware import ContextMiddleware | ||
| from watergrid.steps import Step | ||
|
|
||
|
|
||
| class ContextMetricsMiddleware(ContextMiddleware): | ||
| def pre_context(self, step: Step, context: DataContext): | ||
| self._store.start_step_monitoring(step.get_step_name()) | ||
|
|
||
| def post_context(self, step: Step, context: DataContext): | ||
| self._store.stop_step_monitoring() | ||
|
|
||
| def __init__(self, store: MetricsStore): | ||
| super().__init__() | ||
| self._store = store |
This file contains hidden or 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,22 @@ | ||
| from watergrid.context import DataContext | ||
| from watergrid.locks.LocalPipelineLock import LocalPipelineLock | ||
| from watergrid.middleware.ContextMiddleware import ContextMiddleware | ||
| from watergrid.steps import Step | ||
|
|
||
|
|
||
| class LockInjectorMiddleware(ContextMiddleware): | ||
| """ | ||
| Modifies a context before an after a step to ensure that the lock context | ||
| is not deep copied as when using local locks this will break the | ||
| functionality of the lock. | ||
| """ | ||
|
|
||
| def pre_context(self, step: Step, context: DataContext): | ||
| context.lock = self._pipeline.lock | ||
|
|
||
| def post_context(self, step: Step, context: DataContext): | ||
| context.lock = None | ||
|
|
||
| def __init__(self, pipeline): | ||
| super().__init__() | ||
| self._pipeline = pipeline |
Empty file.
This file contains hidden or 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,19 @@ | ||
| import time | ||
|
|
||
| from watergrid.middleware.PipelineMiddlware import PipelineMiddleware | ||
|
|
||
|
|
||
| class LastRunMiddleware(PipelineMiddleware): | ||
| @property | ||
| def last_run(self): | ||
| return self._last_run | ||
|
|
||
| def pre_pipeline(self, pipeline): | ||
| pass | ||
|
|
||
| def post_pipeline(self, pipeline): | ||
| self._last_run = time.time() | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self._last_run = None |
14 changes: 14 additions & 0 deletions
14
watergrid/middleware/pipeline/PipelineMetricsMiddleware.py
This file contains hidden or 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,14 @@ | ||
| from watergrid.metrics.MetricsStore import MetricsStore | ||
| from watergrid.middleware.PipelineMiddlware import PipelineMiddleware | ||
|
|
||
|
|
||
| class PipelineMetricsMiddleware(PipelineMiddleware): | ||
| def __init__(self, store: MetricsStore): | ||
| super().__init__() | ||
| self._store = store | ||
|
|
||
| def pre_pipeline(self, pipeline): | ||
| self._store.start_pipeline_monitoring(pipeline.get_pipeline_name()) | ||
|
|
||
| def post_pipeline(self, pipeline): | ||
| self._store.stop_pipeline_monitoring() |
This file contains hidden or 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,16 @@ | ||
| from watergrid.middleware.PipelineMiddlware import PipelineMiddleware | ||
| from watergrid.pipelines.pipeline_verifier import PipelineVerifier | ||
|
|
||
|
|
||
| class StepOrderingMiddleware(PipelineMiddleware): | ||
| def pre_pipeline(self, pipeline): | ||
| PipelineVerifier.verify_pipeline_dependencies_fulfilled( | ||
| pipeline._steps | ||
| ) | ||
| PipelineVerifier.verify_pipeline_step_ordering(pipeline._steps) | ||
|
|
||
| def post_pipeline(self, pipeline): | ||
| pass | ||
|
|
||
| def __init__(self): | ||
| super().__init__() |
Empty file.
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.