askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
replicate.py · 3.9 KB · 133 lines
| 1 | """ |
| 2 | Cluster State Replication |
| 3 | |
| 4 | POST /cluster/replicate - Receive state replication from another station |
| 5 | |
| 6 | Used for high availability: when an object updates its state on station1, |
| 7 | it automatically replicates to station2 and station3. If station1 fails, |
| 8 | objects can read state from replicas. |
| 9 | """ |
| 10 | import json |
| 11 | import os |
| 12 | import time |
| 13 | from pathlib import Path |
| 14 | from dbbasic_web.responses import json as json_response, json_error |
| 15 | |
| 16 | |
| 17 | def POST(request): |
| 18 | """ |
| 19 | Receive state replication from another station |
| 20 | |
| 21 | Request body: |
| 22 | { |
| 23 | "object_id": str, |
| 24 | "key": str, |
| 25 | "value": str, |
| 26 | "timestamp": float, |
| 27 | "source_station": str |
| 28 | } |
| 29 | |
| 30 | Response: |
| 31 | { |
| 32 | "status": "ok", |
| 33 | "message": "State replicated", |
| 34 | "object_id": str, |
| 35 | "key": str |
| 36 | } |
| 37 | """ |
| 38 | try: |
| 39 | if request.body: |
| 40 | data = json.loads(request.body.decode('utf-8')) |
| 41 | else: |
| 42 | return json_error('Request body required', status=400) |
| 43 | except json.JSONDecodeError: |
| 44 | return json_error('Invalid JSON', status=400) |
| 45 | |
| 46 | object_id = data.get('object_id') |
| 47 | key = data.get('key') |
| 48 | value = data.get('value') |
| 49 | timestamp = data.get('timestamp') |
| 50 | source_station = data.get('source_station') |
| 51 | |
| 52 | if not all([object_id, key, value is not None, timestamp, source_station]): |
| 53 | return json_error( |
| 54 | 'Missing required fields: object_id, key, value, timestamp, source_station', |
| 55 | status=400 |
| 56 | ) |
| 57 | |
| 58 | # Write to local state (replica) |
| 59 | state_dir = Path(f'data/state/{object_id}') |
| 60 | state_dir.mkdir(parents=True, exist_ok=True) |
| 61 | state_file = state_dir / 'state.tsv' |
| 62 | |
| 63 | # Read existing state |
| 64 | existing = {} |
| 65 | if state_file.exists(): |
| 66 | with open(state_file, 'r') as f: |
| 67 | for line_num, line in enumerate(f): |
| 68 | if line.strip(): |
| 69 | # Skip header row if present (detect by checking if timestamp is parseable) |
| 70 | if line_num == 0: |
| 71 | parts = line.strip().split('\t') |
| 72 | if len(parts) >= 3: |
| 73 | try: |
| 74 | float(parts[2]) # Try to parse timestamp |
| 75 | except ValueError: |
| 76 | # Header row detected, skip it |
| 77 | continue |
| 78 | |
| 79 | parts = line.strip().split('\t') |
| 80 | if len(parts) >= 3: |
| 81 | k, v, ts = parts[0], parts[1], float(parts[2]) |
| 82 | existing[k] = (v, ts) |
| 83 | |
| 84 | # Check if we should accept this update (last-write-wins) |
| 85 | if key in existing: |
| 86 | _, existing_ts = existing[key] |
| 87 | if timestamp <= existing_ts: |
| 88 | # Existing value is newer, reject this update |
| 89 | return json_response(json.dumps({ |
| 90 | 'status': 'ok', |
| 91 | 'message': 'Replica already has newer value', |
| 92 | 'object_id': object_id, |
| 93 | 'key': key, |
| 94 | 'rejected': True |
| 95 | })) |
| 96 | |
| 97 | # Update with new value |
| 98 | existing[key] = (value, timestamp) |
| 99 | |
| 100 | # Write back to file |
| 101 | with open(state_file, 'w') as f: |
| 102 | for k, (v, ts) in sorted(existing.items()): |
| 103 | f.write(f'{k}\t{v}\t{ts}\n') |
| 104 | |
| 105 | return json_response(json.dumps({ |
| 106 | 'status': 'ok', |
| 107 | 'message': 'State replicated', |
| 108 | 'object_id': object_id, |
| 109 | 'key': key, |
| 110 | 'source_station': source_station, |
| 111 | 'timestamp': timestamp |
| 112 | })) |
| 113 | |
| 114 | |
| 115 | def GET(request): |
| 116 | """ |
| 117 | Get replication status for monitoring |
| 118 | |
| 119 | Response: |
| 120 | { |
| 121 | "status": "ok", |
| 122 | "station_id": str, |
| 123 | "message": "Replication endpoint active" |
| 124 | } |
| 125 | """ |
| 126 | station_id = os.environ.get('STATION_ID', 'unknown') |
| 127 | |
| 128 | return json_response(json.dumps({ |
| 129 | 'status': 'ok', |
| 130 | 'station_id': station_id, |
| 131 | 'message': 'Replication endpoint active', |
| 132 | 'timestamp': time.time() |
| 133 | })) |