Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

dbbasic-object-primitive/test_state_replication_live.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
test_state_replication_live.py · 5.7 KB · 206 lines
1#!/usr/bin/env python3
2"""
3Test state replication in live cluster
4"""
5import json
6import requests
7import time
8from cluster_config import get_config
9
10def create_counter_object():
11 """Create a simple counter object on station1"""
12 print("Creating counter object on station1...")
13
14 code = '''
15import json
16from pathlib import Path
17
18# Initialize counter state
19state_file = Path('data/state/test_counter/state.tsv')
20state_file.parent.mkdir(parents=True, exist_ok=True)
21
22# Create initial state
23state = {
24 'count': 0,
25 'last_update': '2025-11-16T00:00:00'
26}
27
28# Write state
29with open(state_file, 'w') as f:
30 f.write('timestamp\\tdata\\n')
31 f.write(f'2025-11-16T00:00:00\\t{json.dumps(state)}\\n')
32
33print(f"Created counter with state: {state}")
34'''
35
36 response = requests.post(
37 'http://localhost:8001/objects/test_counter/execute',
38 json={'code': code},
39 timeout=10
40 )
41
42 if response.status_code == 200:
43 print("✓ Counter object created")
44 return True
45 else:
46 print(f"✗ Failed to create counter: {response.text}")
47 return False
48
49def update_counter_state(count_value):
50 """Update counter state on station1"""
51 print(f"\nUpdating counter to {count_value}...")
52
53 code = f'''
54import json
55import time
56from pathlib import Path
57
58# Update counter state
59state_file = Path('data/state/test_counter/state.tsv')
60
61state = {{
62 'count': {count_value},
63 'last_update': time.strftime('%Y-%m-%dT%H:%M:%S')
64}}
65
66# Write state (this should trigger replication)
67timestamp = time.time()
68with open(state_file, 'w') as f:
69 f.write('timestamp\\tdata\\n')
70 f.write(f'{{timestamp}}\\t{{json.dumps(state)}}\\n')
71
72print(f"Updated counter to {{state}}")
73
74# Trigger replication manually
75import os
76import threading
77
78def replicate():
79 try:
80 import requests
81
82 # Get cluster stations
83 stations_response = requests.get('http://localhost:8001/cluster/stations', timeout=2)
84 stations_data = stations_response.json()
85
86 for station in stations_data.get('stations', []):
87 if station['station_id'] == os.environ.get('STATION_ID', 'station1'):
88 continue
89 if not station.get('is_active', False):
90 continue
91
92 # Replicate state to this station
93 url = f"{{station['url']}}/cluster/replicate"
94 payload = {{
95 'object_id': 'test_counter',
96 'timestamp': timestamp,
97 'state': state,
98 'source_station': os.environ.get('STATION_ID', 'station1')
99 }}
100 requests.post(url, json=payload, timeout=0.5)
101 except:
102 pass
103
104thread = threading.Thread(target=replicate, daemon=True)
105thread.start()
106'''
107
108 response = requests.post(
109 'http://localhost:8001/objects/test_counter/execute',
110 json={'code': code},
111 timeout=10
112 )
113
114 if response.status_code == 200:
115 print(f"✓ Counter updated to {count_value}")
116 return True
117 else:
118 print(f"✗ Failed to update counter: {response.text}")
119 return False
120
121def check_state_on_station(station_url, station_name):
122 """Check state file on a station"""
123 print(f"\nChecking state on {station_name}...")
124
125 try:
126 response = requests.get(
127 f"{station_url}/objects/test_counter",
128 params={'state': 'true'},
129 timeout=5
130 )
131
132 if response.status_code == 200:
133 data = response.json()
134 state = data.get('state')
135 if state:
136 print(f" State: count={state.get('count')}, last_update={state.get('last_update')}")
137 return state
138 else:
139 print(" No state found")
140 return None
141 else:
142 print(f" Error: {response.text}")
143 return None
144 except Exception as e:
145 print(f" Error: {e}")
146 return None
147
148def main():
149 print("=== Testing State Replication ===\n")
150
151 # Create counter
152 if not create_counter_object():
153 return
154
155 time.sleep(2)
156
157 # Update counter several times
158 for i in [1, 5, 10, 15]:
159 if not update_counter_state(i):
160 return
161 time.sleep(2)
162
163 print("\n" + "="*50)
164 print("Waiting 5 seconds for replication to complete...")
165 print("="*50)
166 time.sleep(5)
167
168 # Check state on all stations
169 print("\n=== Checking State Across Cluster ===")
170
171 config = get_config()
172 master = config.get_master()
173 workers = config.get_workers()
174
175 # Check master
176 master_url = config.get_url(master['station_id'])
177 state_master = check_state_on_station(master_url, master['station_id'])
178
179 # Check workers
180 states = [state_master]
181 for worker in workers:
182 worker_url = config.get_url(worker['station_id'])
183 state = check_state_on_station(worker_url, worker['station_id'])
184 states.append(state)
185
186 print("\n=== Results ===")
187
188 if all(states):
189 counts = [s.get('count') for s in states]
190 if all(c == 15 for c in counts):
191 print("✓ State replication PASSED")
192 print(f"✓ All {len(states)} stations have count=15 (latest state)")
193 print("✓ Replace-based replication working correctly")
194 else:
195 print("✗ State mismatch across stations")
196 print(f" {master['station_id']}: {counts[0]}")
197 for i, worker in enumerate(workers, 1):
198 print(f" {worker['station_id']}: {counts[i]}")
199 else:
200 print("✗ Some stations missing state")
201 print(f" {master['station_id']}: {'found' if states[0] else 'missing'}")
202 for i, worker in enumerate(workers, 1):
203 print(f" {worker['station_id']}: {'found' if states[i] else 'missing'}")
204
205if __name__ == '__main__':
206 main()