T-213: M2.4b — Add Python stockbot register_signal, batch feature runner, and M2 MarketDataStore methods
This commit is contained in:
parent
0da283e67f
commit
0cd1f815c8
2 changed files with 327 additions and 36 deletions
|
|
@ -1957,6 +1957,41 @@ def _validate_polars_feature_output(result: Any) -> pl.DataFrame:
|
|||
return df
|
||||
|
||||
|
||||
def _has_existing_feature_partitions(version_dir: str) -> bool:
|
||||
if not os.path.isdir(version_dir):
|
||||
return False
|
||||
for _root, _dirs, files in os.walk(version_dir):
|
||||
for name in files:
|
||||
if name.endswith(".parquet") and not name.startswith("."):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _verify_existing_version_metadata(
|
||||
*,
|
||||
meta_path: str,
|
||||
ref: str,
|
||||
definition_hash: str,
|
||||
) -> None:
|
||||
try:
|
||||
with open(meta_path, "r", encoding="utf-8") as fh:
|
||||
payload = json.load(fh)
|
||||
except Exception as exc:
|
||||
raise FeatureLibraryError(
|
||||
"FEATURE_VERSION_IMMUTABLE",
|
||||
f'Published feature "{ref}" has unreadable version metadata at "{meta_path}".',
|
||||
cause=exc,
|
||||
ref=ref,
|
||||
) from exc
|
||||
existing_hash = payload.get("definition_hash") if isinstance(payload, dict) else None
|
||||
if not isinstance(existing_hash, str) or existing_hash != definition_hash:
|
||||
raise FeatureLibraryError(
|
||||
"FEATURE_VERSION_IMMUTABLE",
|
||||
f'Feature "{ref}" is already published on disk with a different definition hash; bump the feature version instead of overwriting.',
|
||||
ref=ref,
|
||||
)
|
||||
|
||||
|
||||
def _verify_or_persist_version_definition(
|
||||
*,
|
||||
version_dir: str,
|
||||
|
|
@ -1965,31 +2000,32 @@ def _verify_or_persist_version_definition(
|
|||
defn: FeatureDefinition,
|
||||
persist: bool,
|
||||
) -> bool:
|
||||
"""Checks on-disk version immutability metadata and optionally persists it atomically.
|
||||
"""Checks on-disk version immutability metadata and atomically claims it if absent.
|
||||
|
||||
Returns True when `_definition.json` is verified on disk with matching `definition_hash`.
|
||||
Rejects existing published Parquet partitions that lack a verified `_definition.json`
|
||||
(e.g. sidecar-free partitions from another writer), and uses POSIX create-if-absent
|
||||
hard-link (`os.link`) so concurrent first-time writers race safely and always verify
|
||||
the winning definition hash before publishing any partition.
|
||||
"""
|
||||
meta_path = os.path.join(version_dir, "_definition.json")
|
||||
if os.path.exists(meta_path):
|
||||
try:
|
||||
with open(meta_path, "r", encoding="utf-8") as fh:
|
||||
payload = json.load(fh)
|
||||
except Exception as exc:
|
||||
raise FeatureLibraryError(
|
||||
"FEATURE_VERSION_IMMUTABLE",
|
||||
f'Published feature "{ref}" has unreadable version metadata at "{meta_path}".',
|
||||
cause=exc,
|
||||
ref=ref,
|
||||
) from exc
|
||||
existing_hash = payload.get("definition_hash") if isinstance(payload, dict) else None
|
||||
if not isinstance(existing_hash, str) or existing_hash != definition_hash:
|
||||
raise FeatureLibraryError(
|
||||
"FEATURE_VERSION_IMMUTABLE",
|
||||
f'Feature "{ref}" is already published on disk with a different definition hash; bump the feature version instead of overwriting.',
|
||||
ref=ref,
|
||||
)
|
||||
_verify_existing_version_metadata(
|
||||
meta_path=meta_path, ref=ref, definition_hash=definition_hash
|
||||
)
|
||||
return True
|
||||
|
||||
if _has_existing_feature_partitions(version_dir):
|
||||
if os.path.exists(meta_path):
|
||||
_verify_existing_version_metadata(
|
||||
meta_path=meta_path, ref=ref, definition_hash=definition_hash
|
||||
)
|
||||
return True
|
||||
raise FeatureLibraryError(
|
||||
"FEATURE_VERSION_IMMUTABLE",
|
||||
f'Feature "{ref}" already has published Parquet partitions at "{version_dir}" without verified version metadata ("_definition.json"); bump the feature version instead of overwriting or adopting unverified partitions.',
|
||||
ref=ref,
|
||||
)
|
||||
|
||||
if not persist:
|
||||
return False
|
||||
|
||||
|
|
@ -2006,14 +2042,24 @@ def _verify_or_persist_version_definition(
|
|||
with open(tmp_meta, "w", encoding="utf-8") as fh:
|
||||
json.dump(metadata, fh, sort_keys=True, separators=(",", ":"))
|
||||
fh.write("\n")
|
||||
os.replace(tmp_meta, meta_path)
|
||||
except Exception:
|
||||
fh.flush()
|
||||
os.fsync(fh.fileno())
|
||||
try:
|
||||
os.link(tmp_meta, meta_path)
|
||||
return True
|
||||
except FileExistsError:
|
||||
pass
|
||||
finally:
|
||||
if os.path.exists(tmp_meta):
|
||||
try:
|
||||
os.remove(tmp_meta)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
|
||||
# Another writer won the atomic create-if-absent claim; verify the winning hash.
|
||||
_verify_existing_version_metadata(
|
||||
meta_path=meta_path, ref=ref, definition_hash=definition_hash
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue