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

0 downloads

dbbasic-object-primitive/test_state_replication_simple.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_simple.py · 3.1 KB · 97 lines
1#!/usr/bin/env python3
2"""
3Simple test: Use existing counter object to verify state replication
4"""
5import requests
6import time
7import json
8from cluster_config import get_config
9
10print("=== Testing State Replication with Counter Object ===\n")
11
12# Step 1: Reset counter on station1
13print("Step 1: Resetting counter to 0 on station1...")
14response = requests.post('http://localhost:8001/objects/basics_counter', json={'value': 0})
15print(f" Response: {response.json()}")
16time.sleep(1)
17
18# Step 2: Increment counter multiple times on station1
19print("\nStep 2: Incrementing counter 5 times on station1...")
20for i in range(5):
21 response = requests.get('http://localhost:8001/objects/basics_counter')
22 data = response.json()
23 print(f" Increment {i+1}: count={data.get('count')}")
24 time.sleep(0.5)
25
26# Step 3: Wait for replication
27print("\nStep 3: Waiting 5 seconds for state replication...")
28time.sleep(5)
29
30# Step 4: Check state on all stations
31print("\nStep 4: Checking counter state across cluster...")
32
33def get_counter_state(url, name):
34 try:
35 response = requests.get(f"{url}/objects/basics_counter", params={'state': 'true'}, timeout=5)
36 if response.status_code == 200:
37 data = response.json()
38 state = data.get('state', {})
39 count = state.get('count', 'N/A')
40 print(f" {name}: count={count}")
41 return count
42 else:
43 print(f" {name}: Error - {response.status_code}")
44 return None
45 except Exception as e:
46 print(f" {name}: Error - {e}")
47 return None
48
49config = get_config()
50master = config.get_master()
51workers = config.get_workers()
52
53# Get state from all stations
54master_url = config.get_url(master['station_id'])
55count_master = get_counter_state(master_url, master['station_id'])
56
57counts = [count_master]
58for worker in workers:
59 worker_url = config.get_url(worker['station_id'])
60 count = get_counter_state(worker_url, worker['station_id'])
61 counts.append(count)
62
63# Step 5: Verify
64print("\n" + "="*60)
65print("RESULTS:")
66print("="*60)
67
68if all(c == 5 for c in counts):
69 print("✓ STATE REPLICATION WORKING")
70 print(f"✓ All {len(counts)} stations have count=5")
71 print("✓ Replace-based replication confirmed")
72else:
73 print("✗ STATE MISMATCH")
74 print(f" {master['station_id']}: {counts[0]}")
75 for i, worker in enumerate(workers, 1):
76 print(f" {worker['station_id']}: {counts[i]}")
77 print("\nNote: State replication may require manual triggering")
78 print(" Objects need to explicitly call replication endpoint")
79
80# Check state files directly via SSH on worker stations
81print("\n" + "="*60)
82print("Checking state files directly on workers:")
83print("="*60)
84
85import subprocess
86
87for worker in workers:
88 ssh_target = config.get_ssh_target(worker['station_id'])
89 try:
90 result = subprocess.run(
91 ['ssh', ssh_target, 'cat ~/multiplexing/data/state/basics_counter/state.tsv 2>&1'],
92 capture_output=True, text=True, timeout=5
93 )
94 print(f"{worker['station_id']} state file:")
95 print(f" {result.stdout.strip()}")
96 except Exception as e:
97 print(f" Error reading {worker['station_id']}: {e}")