-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: wire the durable (L2) store seam — chdb-serverless[durable] #3
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
Open
ShawnChen-Sirius
wants to merge
1
commit into
main
Choose a base branch
from
feat/durable-store-seam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,84 @@ | ||
| """L2 store: a Durable Analytical Object as the engine. | ||
|
|
||
| The whole difference between the stateless L1 recipe and a durable per-user | ||
| analyst is this file being chosen by the store seam. The same app, image, and | ||
| deploy scripts run unchanged; `CHDB_STORE=durable:<url>?id=<who>` points the | ||
| engine at a `chdb.durable` object on object storage you own, so the analyst's | ||
| tables and history survive the instance and are portable across clouds. | ||
|
|
||
| Spec (the part after `durable:`): | ||
| durable:s3://bucket/prefix?id=user-123 | ||
| durable:local:/var/data?id=user-123 | ||
| Object id also readable from CHDB_DURABLE_ID; writer identity from | ||
| CHDB_DURABLE_OWNER (defaults to the hostname); database from CHDB_DURABLE_DB. | ||
|
|
||
| Reads go straight to the object; writes are logged to its WAL and flushed | ||
| immediately, so a committed write survives an instance dying mid-session. | ||
| chDB is single-writer per process, so this store IS the process's one engine. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import socket | ||
| import threading | ||
| import re | ||
| from urllib.parse import parse_qs | ||
|
|
||
| from .store import Store | ||
|
|
||
| _WRITE_KEYWORDS = { | ||
| "INSERT", "CREATE", "ALTER", "DROP", "RENAME", "TRUNCATE", | ||
| "DELETE", "UPDATE", "OPTIMIZE", "ATTACH", "DETACH", "BACKUP", "RESTORE", | ||
| } | ||
|
|
||
| # Leading SQL comment — a `-- line` comment or a `/* block */` comment. Stripped | ||
| # before reading the first keyword so `/* migration */ CREATE TABLE ...` and | ||
| # `-- note\nINSERT ...` are still classified as writes (and routed to the WAL), | ||
| # not misread as reads because the comment was the "first token". | ||
| _LEADING_COMMENT = re.compile(r"^\s*(?:--[^\n]*(?:\n|$)|/\*.*?\*/)", re.DOTALL) | ||
|
|
||
|
|
||
| def _is_write(sql: str) -> bool: | ||
| s = sql.strip() | ||
| while True: | ||
| m = _LEADING_COMMENT.match(s) | ||
| if not m: | ||
| break | ||
| s = s[m.end():].lstrip() | ||
| if not s: | ||
| return False | ||
| return s.split(None, 1)[0].upper() in _WRITE_KEYWORDS | ||
|
|
||
|
|
||
| class DurableStore(Store): | ||
| def __init__(self, target: str) -> None: | ||
| from chdb import durable as cd | ||
|
|
||
| url, _, query = target.partition("?") | ||
| params = parse_qs(query) | ||
| oid = params.get("id", [os.getenv("CHDB_DURABLE_ID", "default")])[0] | ||
| owner = os.getenv("CHDB_DURABLE_OWNER") or socket.gethostname() | ||
| db = os.getenv("CHDB_DURABLE_DB", "mem") | ||
|
|
||
| self._ns = cd.Namespace(url, owner=owner, db=db) | ||
| self._obj = self._ns.open(oid) | ||
| self._lock = threading.Lock() | ||
| self.db = db | ||
| self.oid = oid | ||
|
|
||
| def query(self, sql: str, fmt: str = "JSONCompact") -> str: | ||
| with self._lock: | ||
| if _is_write(sql): | ||
| self._obj.execute(sql) | ||
| self._obj.flush() # RPO≈0: a committed write is durable at return | ||
| return "" | ||
| return self._obj.query(sql, fmt).data() | ||
|
|
||
| def checkpoint(self) -> None: | ||
| """Fold the WAL into a fresh base (call periodically / before shutdown).""" | ||
| with self._lock: | ||
| self._obj.checkpoint() | ||
|
|
||
| def close(self) -> None: | ||
| with self._lock: | ||
| self._obj.close() | ||
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.