-
Notifications
You must be signed in to change notification settings - Fork 11
Implement Monitor and MonitoringThreadContainer #58
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,51 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from threading import Lock | ||
|
||
|
||
class AtomicInt: | ||
def __init__(self, initial_value: int = 0): | ||
self._value = initial_value | ||
self._lock: Lock = Lock() | ||
|
||
def get(self): | ||
with self._lock: | ||
return self._value | ||
|
||
def set(self, value: int): | ||
with self._lock: | ||
self._value = value | ||
|
||
def get_and_increment(self): | ||
with self._lock: | ||
value = self._value | ||
self._value += 1 | ||
return value | ||
|
||
def increment_and_get(self): | ||
with self._lock: | ||
self._value += 1 | ||
return self._value | ||
|
||
def get_and_decrement(self): | ||
with self._lock: | ||
value = self._value | ||
self._value -= 1 | ||
return value | ||
|
||
def decrement_and_get(self): | ||
with self._lock: | ||
self._value -= 1 | ||
return self._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,83 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from threading import Lock | ||
from typing import Callable, Generic, List, Optional, TypeVar | ||
|
||
K = TypeVar('K') | ||
V = TypeVar('V') | ||
|
||
|
||
class ConcurrentDict(Generic[K, V]): | ||
def __init__(self): | ||
self._dict = dict() | ||
self._lock = Lock() | ||
|
||
def __len__(self): | ||
return len(self._dict) | ||
|
||
def get(self, key: K, default_value: Optional[V] = None) -> Optional[V]: | ||
return self._dict.get(key, default_value) | ||
|
||
def clear(self): | ||
self._dict.clear() | ||
|
||
def compute_if_present(self, key: K, remapping_func: Callable) -> Optional[V]: | ||
with self._lock: | ||
existing_value = self._dict.get(key) | ||
if existing_value is None: | ||
return None | ||
new_value = remapping_func(key, existing_value) | ||
if new_value is not None: | ||
self._dict[key] = new_value | ||
return new_value | ||
else: | ||
self._dict.pop(key, None) | ||
return None | ||
|
||
def compute_if_absent(self, key: K, mapping_func: Callable) -> Optional[V]: | ||
with self._lock: | ||
value = self._dict.get(key) | ||
if value is None: | ||
new_value = mapping_func(key) | ||
if new_value is not None: | ||
self._dict[key] = new_value | ||
return new_value | ||
return value | ||
|
||
def put_if_absent(self, key: K, new_value: V) -> V: | ||
with self._lock: | ||
existing_value = self._dict.get(key) | ||
if existing_value is None: | ||
self._dict[key] = new_value | ||
return new_value | ||
return existing_value | ||
|
||
def remove_if(self, predicate: Callable) -> bool: | ||
with self._lock: | ||
original_len = len(self._dict) | ||
self._dict = {key: value for key, value in self._dict.items() if not predicate(key, value)} | ||
return len(self._dict) < original_len | ||
|
||
def remove_matching_values(self, removal_values: List[V]) -> bool: | ||
with self._lock: | ||
original_len = len(self._dict) | ||
self._dict = {key: value for key, value in self._dict.items() if value not in removal_values} | ||
return len(self._dict) < original_len | ||
|
||
def apply_if(self, predicate: Callable, apply: Callable): | ||
with self._lock: | ||
for key, value in self._dict.items(): | ||
if predicate(key, value): | ||
apply(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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
|
||
class Properties(Dict[str, str]): | ||
... | ||
pass | ||
|
||
|
||
class WrapperProperty: | ||
|
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,114 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from concurrent.futures import ThreadPoolExecutor | ||
congoamz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from aws_wrapper.utils.atomic import AtomicInt | ||
|
||
|
||
def test_set_and_get(): | ||
n = AtomicInt(1) | ||
assert 1 == n.get() | ||
n.set(3) | ||
assert 3 == n.get() | ||
|
||
|
||
def test_get_and_increment(): | ||
n = AtomicInt() | ||
assert 0 == n.get_and_increment() | ||
assert 1 == n.get_and_increment() | ||
n.set(5) | ||
assert 5 == n.get_and_increment() | ||
|
||
|
||
def test_get_and_increment__multithreaded(): | ||
n = AtomicInt() | ||
num_threads = 50 | ||
|
||
def get_and_increment_thread(atomic_num: AtomicInt): | ||
atomic_num.get_and_increment() | ||
|
||
with ThreadPoolExecutor(num_threads) as executor: | ||
for _ in range(num_threads): | ||
executor.submit(get_and_increment_thread, n) | ||
|
||
assert num_threads == n.get() | ||
|
||
|
||
def test_increment_and_get(): | ||
n = AtomicInt() | ||
assert 1 == n.increment_and_get() | ||
assert 2 == n.increment_and_get() | ||
assert 2 == n.get() | ||
n.set(5) | ||
assert 6 == n.increment_and_get() | ||
|
||
|
||
def test_increment_and_get__multithreaded(): | ||
n = AtomicInt() | ||
num_threads = 50 | ||
|
||
def increment_and_get_thread(atomic_num: AtomicInt): | ||
atomic_num.increment_and_get() | ||
|
||
with ThreadPoolExecutor(num_threads) as executor: | ||
for _ in range(num_threads): | ||
executor.submit(increment_and_get_thread, n) | ||
|
||
assert num_threads == n.get() | ||
|
||
|
||
def test_get_and_decrement(): | ||
n = AtomicInt() | ||
assert 0 == n.get_and_decrement() | ||
assert -1 == n.get_and_decrement() | ||
n.set(5) | ||
assert 5 == n.get_and_decrement() | ||
|
||
|
||
def test_get_and_decrement__multithreaded(): | ||
num_threads = 50 | ||
n = AtomicInt(num_threads) | ||
|
||
def get_and_decrement_thread(atomic_num: AtomicInt): | ||
atomic_num.get_and_decrement() | ||
|
||
with ThreadPoolExecutor(num_threads) as executor: | ||
for _ in range(num_threads): | ||
executor.submit(get_and_decrement_thread, n) | ||
|
||
assert 0 == n.get() | ||
|
||
|
||
def test_decrement_and_get(): | ||
n = AtomicInt() | ||
assert -1 == n.decrement_and_get() | ||
assert -2 == n.decrement_and_get() | ||
assert -2 == n.get() | ||
n.set(5) | ||
assert 4 == n.decrement_and_get() | ||
|
||
|
||
def test_decrement_and_get__multithreaded(): | ||
num_threads = 50 | ||
n = AtomicInt(num_threads) | ||
|
||
def decrement_and_get_thread(atomic_num: AtomicInt): | ||
atomic_num.decrement_and_get() | ||
|
||
with ThreadPoolExecutor(num_threads) as executor: | ||
for _ in range(num_threads): | ||
executor.submit(decrement_and_get_thread, n) | ||
|
||
assert 0 == n.get() |
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.
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.
Does there already exist a library that can do this for us?
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.
Looked around, found this. Looks like you can use pypy but it looks like pypy is a different implementation of python instead of just a library. Not sure if we want to switch to get their implementation. Maybe we could create another task to investigate if it is viable or if there is a different library out there