askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_state_replication_simple.py · 3.1 KB · 97 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Simple test: Use existing counter object to verify state replication |
| 4 | """ |
| 5 | import requests |
| 6 | import time |
| 7 | import json |
| 8 | from cluster_config import get_config |
| 9 | |
| 10 | print("=== Testing State Replication with Counter Object ===\n") |
| 11 | |
| 12 | # Step 1: Reset counter on station1 |
| 13 | print("Step 1: Resetting counter to 0 on station1...") |
| 14 | response = requests.post('http://localhost:8001/objects/basics_counter', json={'value': 0}) |
| 15 | print(f" Response: {response.json()}") |
| 16 | time.sleep(1) |
| 17 | |
| 18 | # Step 2: Increment counter multiple times on station1 |
| 19 | print("\nStep 2: Incrementing counter 5 times on station1...") |
| 20 | for 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 |
| 27 | print("\nStep 3: Waiting 5 seconds for state replication...") |
| 28 | time.sleep(5) |
| 29 | |
| 30 | # Step 4: Check state on all stations |
| 31 | print("\nStep 4: Checking counter state across cluster...") |
| 32 | |
| 33 | def 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 | |
| 49 | config = get_config() |
| 50 | master = config.get_master() |
| 51 | workers = config.get_workers() |
| 52 | |
| 53 | # Get state from all stations |
| 54 | master_url = config.get_url(master['station_id']) |
| 55 | count_master = get_counter_state(master_url, master['station_id']) |
| 56 | |
| 57 | counts = [count_master] |
| 58 | for 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 |
| 64 | print("\n" + "="*60) |
| 65 | print("RESULTS:") |
| 66 | print("="*60) |
| 67 | |
| 68 | if 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") |
| 72 | else: |
| 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 |
| 81 | print("\n" + "="*60) |
| 82 | print("Checking state files directly on workers:") |
| 83 | print("="*60) |
| 84 | |
| 85 | import subprocess |
| 86 | |
| 87 | for 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}") |