-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add sync cli #151
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
feat: add sync cli #151
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5592d4c
feat: add sync cli
af08654
reuse SyncProces internals in sync cli
7514513
remove sync-no-parent flag
8e21998
implement sync using SyncProcess
8186d1e
Add progress bar
7a5d1fe
Add tests
9257e1d
change tqdm version range
9bde16e
Merge branch 'dev/persistent-queue' into ms/sync-cli
michalsosn b5f3baa
fix errors after merge
82a0bf3
add e2e tests
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,6 @@ repos: | |
- neptune-api==0.11.0 | ||
- more-itertools | ||
- backoff | ||
- types-click | ||
default_language_version: | ||
python: python3 |
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
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,56 @@ | ||
# | ||
# Copyright (c) 2022, Neptune Labs Sp. z o.o. | ||
# | ||
# 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. | ||
# | ||
|
||
__all__ = ["sync"] | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import click | ||
|
||
from neptune_scale.cli.sync import sync_all | ||
from neptune_scale.exceptions import NeptuneApiTokenNotProvided | ||
from neptune_scale.util.envs import API_TOKEN_ENV_NAME | ||
|
||
|
||
@click.group() | ||
def main() -> None: | ||
pass | ||
|
||
|
||
@main.command() | ||
@click.argument( | ||
"run_log_file", | ||
type=click.Path(exists=True, dir_okay=False, resolve_path=True), | ||
metavar="<run-log-file>", | ||
) | ||
@click.option( | ||
"--api-token", | ||
"api_token", | ||
multiple=False, | ||
default=os.environ.get(API_TOKEN_ENV_NAME), | ||
metavar="<api-token>", | ||
help="API token for authentication. Overrides NEPTUNE_API_TOKEN environment variable", | ||
) | ||
def sync( | ||
run_log_file: Path, | ||
api_token: Optional[str], | ||
) -> None: | ||
if api_token is None: | ||
raise NeptuneApiTokenNotProvided() | ||
|
||
sync_all(run_log_file, api_token) |
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,141 @@ | ||
# | ||
# Copyright (c) 2022, Neptune Labs Sp. z o.o. | ||
# | ||
# 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. | ||
# | ||
|
||
__all__ = ["sync_all"] | ||
|
||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from tqdm import tqdm | ||
|
||
from neptune_scale.sync import sync_process | ||
from neptune_scale.sync.errors_tracking import ( | ||
ErrorsMonitor, | ||
ErrorsQueue, | ||
) | ||
from neptune_scale.sync.operations_repository import ( | ||
OperationsRepository, | ||
SequenceId, | ||
) | ||
from neptune_scale.sync.sync_process import SyncProcess | ||
from neptune_scale.util import ( | ||
SharedFloat, | ||
SharedInt, | ||
get_logger, | ||
) | ||
|
||
logger = get_logger() | ||
|
||
|
||
def sync_all( | ||
run_log_file: Path, | ||
api_token: str, | ||
) -> None: | ||
runner = SyncRunner(api_token=api_token, run_log_file=run_log_file) | ||
|
||
try: | ||
runner.start() | ||
runner.wait() | ||
finally: | ||
runner.stop() | ||
|
||
|
||
class SyncRunner: | ||
def __init__( | ||
self, | ||
api_token: str, | ||
run_log_file: Path, | ||
) -> None: | ||
self._api_token: str = api_token | ||
self._run_log_file: Path = run_log_file | ||
self._operations_repository: OperationsRepository = OperationsRepository(db_path=run_log_file) | ||
self._process_link = sync_process.ProcessLink() | ||
self._errors_queue: ErrorsQueue = ErrorsQueue() | ||
self._last_queued_seq = SharedInt(-1) | ||
self._last_ack_seq = SharedInt(-1) | ||
self._last_ack_timestamp = SharedFloat(-1) | ||
self._log_seq_id_range: Optional[tuple[SequenceId, SequenceId]] = None | ||
self._sync_process: Optional[SyncProcess] = None | ||
self._errors_monitor: Optional[ErrorsMonitor] = None | ||
|
||
def start( | ||
self, | ||
) -> None: | ||
self._log_seq_id_range = self._operations_repository.get_sequence_id_range() | ||
if self._log_seq_id_range is None: | ||
logger.info("No operations to process") | ||
return | ||
|
||
metadata = self._operations_repository.get_metadata() | ||
if metadata is None: | ||
logger.error("No run metadata found in log") | ||
return | ||
|
||
self._sync_process = sync_process.SyncProcess( | ||
operations_repository_path=self._run_log_file, | ||
errors_queue=self._errors_queue, | ||
process_link=self._process_link, | ||
api_token=self._api_token, | ||
project=metadata.project, | ||
family=metadata.run_id, | ||
mode="async", | ||
last_queued_seq=self._last_queued_seq, | ||
last_ack_seq=self._last_ack_seq, | ||
last_ack_timestamp=self._last_ack_timestamp, | ||
) | ||
self._errors_monitor = ErrorsMonitor(errors_queue=self._errors_queue) | ||
|
||
self._sync_process.start() | ||
self._process_link.start() | ||
|
||
self._errors_monitor.start() | ||
|
||
def wait(self, progress_bar_enabled: bool = True, wait_time: float = 0.1) -> None: | ||
if self._log_seq_id_range is None: | ||
return | ||
|
||
total_count = self._log_seq_id_range[1] - self._log_seq_id_range[0] + 1 | ||
with tqdm( | ||
desc="Syncing operations", total=total_count, unit="op", disable=not progress_bar_enabled | ||
) as progress_bar: | ||
while True: | ||
try: | ||
with self._last_ack_seq: | ||
self._last_ack_seq.wait(timeout=wait_time) | ||
last_ack_seq_id = self._last_ack_seq.value | ||
|
||
if last_ack_seq_id != -1: | ||
acked_count = last_ack_seq_id - self._log_seq_id_range[0] + 1 | ||
progress_bar.update(acked_count - progress_bar.n) | ||
|
||
if last_ack_seq_id >= self._log_seq_id_range[1]: | ||
break | ||
except KeyboardInterrupt: | ||
logger.warning("Waiting interrupted by user") | ||
return | ||
|
||
def stop(self) -> None: | ||
if self._errors_monitor is not None: | ||
self._errors_monitor.interrupt() | ||
self._errors_monitor.join() | ||
|
||
if self._sync_process is not None: | ||
self._sync_process.terminate() | ||
self._sync_process.join() | ||
self._process_link.stop() | ||
|
||
self._operations_repository.close() | ||
self._errors_queue.close() |
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
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.