1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Add pushed_at timestamps and per-topic retention

- Every message is stamped with the time its push arrived (UTC ms since
  1970, the server's clock), carried as is by replication. Exposed in
  LatestMessage and Messages, BakaShrot_GetMessagePushedAt() in the DLL,
  Message.pushed_at / .pushed in Python, GetMessages --time,
  GetLatestMessage, and the messages view (pushed_at, pushed_utc).
- [BakaShrot.Retention] in each server's config: a max age per topic, by
  name, id, pattern or *, ages like 30d or 1d12h. A background pass
  (Server/Storage/Retention.pbi, every RetentionInterval seconds) deletes
  what's older, in batches through the writer. node_heads is left as is,
  so ids never go back and peers don't resend what was deleted.
- Replicated messages already past the receiver's max age are skipped on
  arrival, their head still raised so they aren't sent again.
- Schema v6: data.pushed_at (older databases stamped at upgrade time),
  a (topic_id, pushed_at) index, the unused archives table dropped. The
  writer takes the next id from node_heads.
- Folded into protocol 3, not released yet.
- Tests/Retention-Test.ps1 (52 checks), Python checks for pushed_at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVPXGLs3KK1MAQn3ouP
This commit is contained in:
2026-11-08 17:01:48 +01:00
parent 12fbe5f4da
commit 9caf816dca
26 changed files with 1327 additions and 54 deletions
+13 -2
View File
@@ -39,6 +39,7 @@ import os
import threading
import time
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Iterator, Optional, Union
__all__ = [
@@ -73,7 +74,9 @@ class ConnectionLostError(BakaShrotError):
@dataclass(frozen=True)
class Message:
"""A stored message. data is as stored: JSON or not (the server's
is_json column tells which, json() parses it)."""
is_json column tells which, json() parses it). pushed_at: when it was
pushed, to whichever server it was pushed to, in milliseconds since
1970-01-01 UTC (pushed is the same as a datetime)."""
id: int
topic: str
@@ -81,11 +84,17 @@ class Message:
topic_id: int
node_id: int
data: str
pushed_at: int = 0
def json(self) -> Any:
"""The data parsed as JSON, raises ValueError if it isn't."""
return json.loads(self.data)
@property
def pushed(self) -> datetime:
"""When it was pushed, as an aware datetime in UTC."""
return datetime.fromtimestamp(self.pushed_at / 1000, tz=timezone.utc)
@dataclass(frozen=True)
class MessagesPage:
@@ -181,6 +190,7 @@ class _Dll:
"BakaShrot_SelectMessage": (I, [L]),
"BakaShrot_HasMoreMessages": (I, []),
"BakaShrot_GetMessageId": (Q, []),
"BakaShrot_GetMessagePushedAt": (Q, []),
"BakaShrot_GetMessageTopicId": (L, []),
"BakaShrot_GetMessageNodeId": (L, []),
"BakaShrot_GetMessageTopicA": (I, [S, I]),
@@ -334,7 +344,8 @@ class Client:
def _current(self) -> Message:
dll = self._dll
return Message(dll.GetMessageId(), dll.string(dll.GetMessageTopicA), dll.string(dll.GetMessageNodeA),
dll.GetMessageTopicId(), dll.GetMessageNodeId(), dll.string(dll.GetMessageDataA))
dll.GetMessageTopicId(), dll.GetMessageNodeId(), dll.string(dll.GetMessageDataA),
dll.GetMessagePushedAt())
def latest(self, topic: Ref, node: Ref) -> Optional[Message]:
"""The latest message of topic/node, None if there's none yet."""
+9
View File
@@ -57,6 +57,12 @@ with kitchen:
latest = kitchen.latest("logs", "kitchen")
check("latest(): #250", latest is not None and latest.id == 250 and latest.json() == {"i": 250}, latest)
now_ms = int(time.time() * 1000)
check("latest().pushed_at: just now, UTC ms", latest is not None and now_ms - 60000 < latest.pushed_at <= now_ms + 1000,
f"{latest.pushed_at if latest else None} vs {now_ms}")
check("latest().pushed: the same, an aware datetime in UTC",
latest is not None and latest.pushed.tzinfo is not None and latest.pushed.utcoffset().total_seconds() == 0
and int(latest.pushed.timestamp() * 1000) == latest.pushed_at, latest.pushed if latest else None)
topic_id, topic_name = kitchen.resolve_topic("logs")
node_id, node_name = kitchen.resolve_node("kitchen")
@@ -70,6 +76,9 @@ with kitchen:
first = page.messages[0]
check("A message's fields", (first.topic, first.node, first.topic_id, first.node_id, first.data, first.json())
== ("logs", "kitchen", 1, 5, '{"i":1}', {"i": 1}), first)
stamps = [m.pushed_at for m in page]
check("Each message of a page has its pushed_at, in push order", all(stamps) and stamps == sorted(stamps),
f"{stamps[0]} .. {stamps[-1]}")
page = kitchen.get_messages("logs", "kitchen", after=200, limit=100)
check("Last page: 201-250, no more", [m.id for m in page] == list(range(201, 251)) and not page.more,