The GQLDB Python driver provides methods for monitoring server health, managing caches, and gathering statistics.
| Method | Description |
|---|---|
health_check(service) | Check the health status of the server |
watch(service, callback) | Stream health status changes |
watch_iter(service) | Iterate over health status changes |
| Method | Description |
|---|---|
warmup_parser(count) | Pre-allocate parser instances |
get_cache_stats(cache_type) | Get cache statistics |
clear_cache(cache_type) | Clear specified caches |
get_statistics(graph_name) | Get database statistics |
invalidate_permission_cache(username) | Invalidate RBAC permission cache |
compact(graph_name) | Trigger compaction for a graph |
get_system_metrics() | Get system-level metrics |
wait_for_compute_topology(graph_name, timeout) | Wait for compute engine ready |
| Method | Description |
|---|---|
show_tasks() | List running/completed algorithm tasks |
stop_task(task_id) | Stop a running task |
delete_task(task_id) | Delete a task |
show_algos() | List available algorithms |
top() | List running queries |
kill(query_id) | Terminate a running query |
stats() | Get graph statistics (node/edge counts by label) |
test() | Connectivity test (ping) |
Check the current health status of the server:
Pythonfrom gqldb import GqldbClient, GqldbConfig from gqldb.types import HealthStatus config = GqldbConfig(hosts=["localhost:9000"]) with GqldbClient(config) as client: client.login("admin", "password") # Check overall server health status = client.health_check() if status == HealthStatus.SERVING: print("Server is healthy and serving requests") elif status == HealthStatus.NOT_SERVING: print("Server is not serving requests") elif status == HealthStatus.SERVICE_UNKNOWN: print("Service status is unknown") else: print("Health status is unknown") # Check specific service health query_status = client.health_check("query") print(f"Query service: {query_status.name}")
Pythonfrom gqldb.types import HealthStatus HealthStatus.UNKNOWN # Status unknown HealthStatus.SERVING # Healthy and serving HealthStatus.NOT_SERVING # Not serving requests HealthStatus.SERVICE_UNKNOWN # Service status unknown
Monitor health status changes with a callback:
Pythonfrom gqldb.types import HealthStatus def on_health_change(status: HealthStatus): print(f"Health status changed: {status.name}") if status != HealthStatus.SERVING: print("WARNING: Server is not healthy!") # Trigger alerts, failover logic, etc. # Watch health status client.watch(callback=on_health_change)
Iterate over health status changes:
Pythonfrom gqldb.types import HealthStatus # Using generator for status in client.watch_iter(): print(f"Health status: {status.name}") if status != HealthStatus.SERVING: print("Server unhealthy, breaking...") break
Pythonfrom gqldb.types import CacheType CacheType.ALL # All caches CacheType.AST # Abstract Syntax Tree cache CacheType.PLAN # Query plan cache
Get statistics about caches:
Pythonfrom gqldb.types import CacheType # Get all cache stats all_stats = client.get_cache_stats(CacheType.ALL) print(f"All cache stats: {all_stats}") # Get AST cache stats ast_stats = client.get_cache_stats(CacheType.AST) print(f"AST cache: {ast_stats}") # Get plan cache stats plan_stats = client.get_cache_stats(CacheType.PLAN) print(f"Plan cache: {plan_stats}")
Python@dataclass class CacheStats: ast_stats: ASTCacheStats plan_stats: PlanCacheStats @dataclass class ASTCacheStats: hits: int misses: int evictions: int entries: int hit_rate: float @dataclass class PlanCacheStats: hits: int misses: int size: int capacity: int hit_rate: float
Clear caches to free memory or force recompilation:
Pythonfrom gqldb.types import CacheType # Clear all caches client.clear_cache(CacheType.ALL) print("All caches cleared") # Clear only AST cache client.clear_cache(CacheType.AST) print("AST cache cleared") # Clear only plan cache client.clear_cache(CacheType.PLAN) print("Plan cache cleared") # Clear all (default) client.clear_cache() print("All caches cleared (default)")
Pre-allocate parser instances for better performance:
Python# Pre-allocate 10 parser instances client.warmup_parser(10) print("Parsers warmed up")
This is useful before high-load periods to reduce latency from parser initialization.
Get statistics about the database or a specific graph:
Python# Get overall database statistics db_stats = client.get_statistics() print(f"Database statistics: {db_stats}") # Get statistics for a specific graph graph_stats = client.get_statistics("myGraph") print(f"Graph statistics: {graph_stats}")
Python@dataclass class Statistics: node_count: int edge_count: int label_counts: Dict[str, int] edge_label_counts: Dict[str, int]
Invalidate the RBAC (Role-Based Access Control) permission cache:
Python# Invalidate all permission caches client.invalidate_permission_cache() print("All permission caches invalidated") # Invalidate cache for a specific user client.invalidate_permission_cache("johndoe") print("Permission cache invalidated for johndoe")
Use this after changing user permissions to ensure changes take effect immediately.
Get system-level metrics including CPU, memory, disk, and network:
Pythonmetrics = client.get_system_metrics() if metrics.cpu: print(f"CPU - Process: {metrics.cpu.process_percent}%, System: {metrics.cpu.system_percent}%") if metrics.memory: print(f"Memory - Used: {metrics.memory.system_used_percent}%") if metrics.storage: print(f"Storage - DB size: {metrics.storage.db_size_bytes} bytes")
Wait for the compute engine to be ready:
Pythonready, message = client.wait_for_compute_topology("myGraph", timeout=60) print(f"Compute topology: {'ready' if ready else 'not ready'} - {message}")
List running and completed algorithm tasks:
Pythonclient.use_graph("myGraph") tasks = client.show_tasks() for t in tasks: print(f"{t.task_id}: {t.status} ({t.progress})")
Pythonclient.stop_task(task_id) client.delete_task(task_id)
List available algorithms:
Pythonalgos = client.show_algos() for a in algos: print(f"{a.name}: {a.description}")
List currently running queries:
Pythonprocs = client.top() for p in procs: print(f"Query {p.query_id}: {p.query_text} ({p.duration_ms}ms)")
Terminate a running query:
Pythonclient.kill(query_id)
Get graph statistics with node/edge counts by label:
Pythonstats = client.stats() print(f"Nodes: {stats.node_count}, Edges: {stats.edge_count}") print(f"Label counts: {stats.label_counts}")
Test connectivity (returns latency in nanoseconds):
Pythonlatency_ns = client.test() print(f"Latency: {latency_ns / 1_000_000:.2f}ms")
Pythonfrom gqldb import GqldbClient, GqldbConfig from gqldb.types import CacheType, HealthStatus from gqldb.errors import GqldbError import time def main(): config = GqldbConfig( hosts=["localhost:9000"], timeout=30 ) with GqldbClient(config) as client: client.login("admin", "password") # Health check print("=== Health Check ===") health = client.health_check() print(f"Server status: {health.name}") # Warmup parsers print("\n=== Parser Warmup ===") client.warmup_parser(5) print("Warmed up 5 parser instances") # Execute some queries to populate cache print("\n=== Executing Queries ===") client.create_graph("healthDemo") client.use_graph("healthDemo") client.gql("MATCH (n) RETURN count(n)") client.gql("MATCH (n) RETURN count(n)") # Should hit cache client.gql("MATCH (n)-[e]->(m) RETURN count(e)") # Check cache stats print("\n=== Cache Statistics ===") cache_stats = client.get_cache_stats(CacheType.ALL) print(f"Cache stats: {cache_stats}") # Get database statistics print("\n=== Database Statistics ===") db_stats = client.get_statistics() print(f"Database stats: {db_stats}") # Start health monitoring (with timeout) print("\n=== Health Monitoring ===") print("Monitoring health for 5 seconds...") start_time = time.time() for status in client.watch_iter(): print(f" Health update: {status.name}") if time.time() - start_time > 5: break print("Health monitoring stopped") # Clear caches print("\n=== Clear Caches ===") client.clear_cache(CacheType.ALL) print("All caches cleared") # Verify caches are cleared cleared_stats = client.get_cache_stats(CacheType.ALL) print(f"Cache stats after clear: {cleared_stats}") # Invalidate permission cache print("\n=== Permission Cache ===") client.invalidate_permission_cache() print("Permission cache invalidated") # Cleanup client.drop_graph("healthDemo") if __name__ == "__main__": main()