Repository navigation
feat: keep the DS's watcher state on its own Drive and restore it at startup - #9613
Conversation
- WatcherCheckpoint and WatcherRollingState in syft/sync/checkpoints/watcher_state.py hold the files received from one DO, the changes since then and the cursor in its outbox. They refuse a state that a later client wrote. - parse_versioned_json() checks the version of JSON text or bytes before the fields. read_versioned_json() now calls it for a file. - DriveWatcherStoreConnection keeps one folder per DO in /SyftBox/<me>-<version>-watcher-state, which is shared with nobody. A save replaces the content of the checkpoint file or the rolling-state file in place. - put_checkpoint() and put_rolling_state() take no file name, because each DO has one file of each kind.
…crypted with the DS key - watcher_save_checkpoint() and watcher_save_rolling_state() encrypt the state for the DS when encryption is required, then store it. - watcher_load_checkpoint() and watcher_load_rolling_state() return None only when nothing is saved. They raise when the state cannot be decrypted or a later client saved it. - When the DS key cannot decrypt the state, the error tells the DS to log in with the key_file of the key that saved it.
…at startup - At startup, a DS in on-disk mode restores every DO that has saved state. It writes the files of the checkpoint and the rolling state, and records their hashes and the received message ids. - After a pull that receives messages, the DS saves the rolling state of that DO, then marks the messages processed. At 50 changed paths, it folds them into the checkpoint. - The DS keeps the received message ids per DO. An event that pre_write_filter refuses is not saved, so a restore does not write it. - WatcherRollingState has no last_msg_id, because the cursor is the newest received id. - peers_with_saved_state() lists the DOs that have saved state.
… holds its state - In on-disk mode, the DS keeps its record of received messages in memory, as in-memory mode does. A restart restores only from the saved state on Drive. - The code that rebuilt the cursor and the file hashes from the local folder is gone, and so is the unused events_base_path. - The local cleanup no longer deletes a -event-messages folder. - The three tests that restarted from the local folder now check the state restored from Drive.
…checkpoint models - Watcher state is not encrypted until key recovery exists, as the ticket says. With the DS key, an enclave could not boot twice: its new key could not read the state of the last boot. - The DS saves a Checkpoint and a RollingState from syft/sync/checkpoints/, as the ticket says. watcher_state.py and parse_versioned_json are gone. - CheckpointFile.content holds str or bytes, so a binary file survives a checkpoint. RollingState.received_msg_ids holds the ids that a DS received. - The DS saves and loads the state of a DO under its email without case, so two spellings share one Drive folder. A restore writes the checkpoint files under the email as the DO's events spell it.
- Such a change blocked every later sync of the DS. A change with no hash made each fold raise, and a change with no content raised when it was applied. - The DS now drops it with a warning, as it does for an event aimed at another datasite.
…e DO and the DS - syft/sync/utils/file_content.py holds the text-or-bytes content code that ProposedFileChange, FileChangeEvent and CheckpointFile used to repeat. The JSON shape does not change. - Checkpoint.with_events() applies events to a checkpoint. The DO compaction and the DS fold both use it, and compact_incremental_checkpoints is gone. - The DO compaction now raises on a change without content or hash. Before, it dropped the file and could write "None" as a hash.
- decode_content() dropped characters outside the base64 alphabet and returned other bytes, with no error. It now raises, so the model refuses the content.
- DEFAULT_CHECKPOINT_EVENT_THRESHOLD moved to syft/sync/checkpoints/checkpoint.py. The DO syncer, the DS cache and SyftboxManager all read it. - WATCHER_CHECKPOINT_THRESHOLD and three literal defaults of 50 in SyftboxManager are gone.
- DriveWatcherStoreConnection.clone_for_thread() returned the same object, so another thread would share one Drive client. It now returns a store over a new client, as the other Drive connections do.
…ndle - WatcherStoreConnection.latest_checkpoint() is now get_checkpoint(), because each DO has one checkpoint. put_checkpoint() and put_rolling_state() return nothing, because no caller read the file id. - DriveWatcherStoreConnection names its helpers after the watcher folder and the files: _watcher_folder_id(), _save_file(), _read_file() and _find_id(). - DataSiteWatcherCache has _save_watcher_state() and _restore_watcher_state(). The DO compaction names its sorted checkpoints incrementals_in_order.
…test - test_ds_cache_handles_deletions_correctly reloaded over an empty mock Drive, so it passed even with no restore. It now reloads over the DS's own router. - The test also checks that a kept file comes back, so a restore that does nothing fails it.
| def _save_rolling_state(self, peer_email: str) -> None: | ||
| key = _peer_key(peer_email) |
There was a problem hiding this comment.
The watcher state of a DO is saved under _peer_key(), the lowercased email, while restored checkpoint files go under the email as the DO's events spell it.
Let's make a ticket in Asana to lowercase this everywhere, and only when we first read it into an object.
… helpers - _restore_watcher_state() calls _restore_checkpoint() and then _restore_rolling_state() for each DO with saved state. The checkpoint goes first, because the rolling state holds the changes after it.
…al() - _rolling_state() is now _rolling_state_local(), so its name says that it returns the copy in memory. _save_rolling_state() writes that copy to the DS's storage.
- DriveWatcherStoreConnection keeps the ids of the watcher folder, of each DO folder and of each file once it finds or creates them. After the first save, a save is one Drive call and a load is one download. Before, each save looked up three ids first. - reset(peer) drops the ids of that DO, reset_caches() drops all of them, and clone_for_thread() copies them.
…the DS - FileChangeEvent refuses a change that is not a delete and has no content or no hash. No code builds one, and a message that holds one cannot be read: the DS skips it with a warning, as any unreadable outbox message. - The DS no longer checks each event for content. _accepted_events() is _events_within_peer_datasite() again. - Checkpoint.with_events() keeps its check as a guard of that rule.
- CheckpointFile.content is always bytes, as base64 in JSON. A checkpoint built from text encodes it as UTF-8, so the hashes do not change. - The content_type field and its two hooks are gone from CheckpointFile. Events and proposals keep str or bytes, because peers exchange them.
- The router encrypts the watcher state with the DS key when the DS requires encryption, as it does the DO's own state. A key that cannot decrypt it raises an error that says to log in with the key_file of the key that saved it. - persist_watcher_state, next to persist_owner_state, turns the watcher state on or off, in both cache modes. An enclave gets a new key at each boot, so for_enclave() sets both from the same setting, which is off by default. - The in-memory cache now saves and restores the watcher state too, so test clients run it as a real client does.
- peers_with_saved_state() no longer fills the folder-id cache from its listing. That listing skipped the check that a DO has exactly one folder, so two folders made at the same time were read as one without an error. - The test of the id cache counts Drive calls with _count_drive_calls(), which sees every list request.
…_content - file_content.content_bytes() encodes text as UTF-8. The checkpoint and the send queue use it, instead of a copy each. - The send queue reads ContentType from file_content, instead of its own copy.
- With freeze_private_datasets on, the watcher does not change or delete a file that it already has in an immutable collection. - The enclave flow turns the flag on, so syft_enclaves.immutability is gone.
|
lets get this fixed: Lets also add a test for that |
Summary
PR 2 of "connsplit v1.3: Scientist durability", stacked on #9611.
<syftbox>-event-messagesfolder. Now the DS saves its state for that DO on its own Drive, in/SyftBox/<email>-<version>-watcher-state/<do email>/, before it marks the messages processed. Nobody else has access to that folder. Each save replacesrolling_state.tar.gz, which holds the changes since the checkpoint and the ids of the received messages. At 50 changed paths, the DS folds those changes intocheckpoint.tar.gz. With encryption on, both files are encrypted with the DS's own key.-event-messagesfolder, so a lost local disk, such as a Colab runtime reset, lost them. Now the DS restores the files, hashes and received message ids of each DO from its Drive. When the DS's key cannot decrypt the saved state, startup raises and says to log in with thekey_fileof the key that saved it.persist_watcher_statefollowspersist_owner_state, which is off, because an enclave gets a new key at each boot.pre_write_filtercallback thatSyftEnclaveClientset on the cache, and it checked whether the file was on disk. Now it is thefreeze_private_datasetsconfig flag, on by default, because only an enclave ever receives private dataset files. It checks whether the DS has the file's hash, and the refused change stays out of the saved state.FileChangeEventrefuses it, and it also refuses content that is not valid base64. The DS skips such a message as unreadable, and the DO's compaction raises.Checkpoint.with_events, which the DS's fold uses too.CheckpointFile.contentis now bytes, stored as base64 in JSON. It used to be text, soCHECKPOINT_VERSIONis now 2, and a version 1 checkpoint loads with its text as UTF-8 bytes. Incremental checkpoints keep version 1, because their format did not change./SyftBox/<email>-<version>-watcher-statefolder. TheSyftboxManagerConfigbuilders takepersist_watcher_stateandfreeze_private_datasets, andDataSiteWatcherCache.pre_write_filteris gone.Guidelines
High level
freeze_private_datasetsflag, which replaces the enclave'spre_write_filter, could be its own PR. The rest belongs together.Mid level
CheckpointFile.contentis now base64 bytes, soCHECKPOINT_VERSIONis now 2, and a version 1 checkpoint loads with its text content as UTF-8 bytes.Low level
Security
Testing
tests/unit/test_watcher_state.pycovers these cases:tests/unit/test_watcher_immutable_collections.pycoversfreeze_private_datasetson and off, and a refused change that is not restored after a restart.test_drive_connections.pytests the Drive watcher store.test_encryption.pytests encryption at rest.test_checkpoints.pytests later versions and a change without content.test_checkpoint_version.pyloads a version 1 checkpoint whose text,true, is also valid base64.test_file_change_events_serialization.pytests corrupt base64.test_ds_cache_aware_sync_gdrive.Asana task