kailash 0.5.0__py3-none-any.whl → 0.6.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. kailash/__init__.py +1 -1
  2. kailash/access_control/__init__.py +1 -1
  3. kailash/client/__init__.py +12 -0
  4. kailash/client/enhanced_client.py +306 -0
  5. kailash/core/actors/__init__.py +16 -0
  6. kailash/core/actors/adaptive_pool_controller.py +630 -0
  7. kailash/core/actors/connection_actor.py +566 -0
  8. kailash/core/actors/supervisor.py +364 -0
  9. kailash/core/ml/__init__.py +1 -0
  10. kailash/core/ml/query_patterns.py +544 -0
  11. kailash/core/monitoring/__init__.py +19 -0
  12. kailash/core/monitoring/connection_metrics.py +488 -0
  13. kailash/core/optimization/__init__.py +1 -0
  14. kailash/core/resilience/__init__.py +17 -0
  15. kailash/core/resilience/circuit_breaker.py +382 -0
  16. kailash/edge/__init__.py +16 -0
  17. kailash/edge/compliance.py +834 -0
  18. kailash/edge/discovery.py +659 -0
  19. kailash/edge/location.py +582 -0
  20. kailash/gateway/__init__.py +33 -0
  21. kailash/gateway/api.py +289 -0
  22. kailash/gateway/enhanced_gateway.py +357 -0
  23. kailash/gateway/resource_resolver.py +217 -0
  24. kailash/gateway/security.py +227 -0
  25. kailash/middleware/auth/access_control.py +6 -6
  26. kailash/middleware/auth/models.py +2 -2
  27. kailash/middleware/communication/ai_chat.py +7 -7
  28. kailash/middleware/communication/api_gateway.py +5 -15
  29. kailash/middleware/database/base_models.py +1 -7
  30. kailash/middleware/gateway/__init__.py +22 -0
  31. kailash/middleware/gateway/checkpoint_manager.py +398 -0
  32. kailash/middleware/gateway/deduplicator.py +382 -0
  33. kailash/middleware/gateway/durable_gateway.py +417 -0
  34. kailash/middleware/gateway/durable_request.py +498 -0
  35. kailash/middleware/gateway/event_store.py +499 -0
  36. kailash/middleware/mcp/enhanced_server.py +2 -2
  37. kailash/nodes/admin/permission_check.py +817 -33
  38. kailash/nodes/admin/role_management.py +1242 -108
  39. kailash/nodes/admin/schema_manager.py +438 -0
  40. kailash/nodes/admin/user_management.py +1124 -1582
  41. kailash/nodes/code/__init__.py +8 -1
  42. kailash/nodes/code/async_python.py +1035 -0
  43. kailash/nodes/code/python.py +1 -0
  44. kailash/nodes/data/async_sql.py +9 -3
  45. kailash/nodes/data/query_pipeline.py +641 -0
  46. kailash/nodes/data/query_router.py +895 -0
  47. kailash/nodes/data/sql.py +20 -11
  48. kailash/nodes/data/workflow_connection_pool.py +1071 -0
  49. kailash/nodes/monitoring/__init__.py +3 -5
  50. kailash/nodes/monitoring/connection_dashboard.py +822 -0
  51. kailash/nodes/rag/__init__.py +2 -7
  52. kailash/resources/__init__.py +40 -0
  53. kailash/resources/factory.py +533 -0
  54. kailash/resources/health.py +319 -0
  55. kailash/resources/reference.py +288 -0
  56. kailash/resources/registry.py +392 -0
  57. kailash/runtime/async_local.py +711 -302
  58. kailash/testing/__init__.py +34 -0
  59. kailash/testing/async_test_case.py +353 -0
  60. kailash/testing/async_utils.py +345 -0
  61. kailash/testing/fixtures.py +458 -0
  62. kailash/testing/mock_registry.py +495 -0
  63. kailash/workflow/__init__.py +8 -0
  64. kailash/workflow/async_builder.py +621 -0
  65. kailash/workflow/async_patterns.py +766 -0
  66. kailash/workflow/cyclic_runner.py +107 -16
  67. kailash/workflow/graph.py +7 -2
  68. kailash/workflow/resilience.py +11 -1
  69. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/METADATA +19 -4
  70. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/RECORD +74 -28
  71. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/WHEEL +0 -0
  72. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/entry_points.txt +0 -0
  73. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/licenses/LICENSE +0 -0
  74. {kailash-0.5.0.dist-info → kailash-0.6.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,382 @@
1
+ """Circuit Breaker pattern implementation for connection management.
2
+
3
+ This module implements the Circuit Breaker pattern to prevent cascading failures
4
+ in connection pools and database operations. It provides automatic failure detection,
5
+ recovery testing, and graceful degradation.
6
+
7
+ The circuit breaker has three states:
8
+ - CLOSED: Normal operation, requests pass through
9
+ - OPEN: Failures detected, requests fail fast
10
+ - HALF_OPEN: Testing recovery, limited requests allowed
11
+
12
+ Example:
13
+ >>> breaker = ConnectionCircuitBreaker(
14
+ ... failure_threshold=5,
15
+ ... recovery_timeout=60,
16
+ ... half_open_requests=3
17
+ ... )
18
+ >>>
19
+ >>> # Wrap connection operations
20
+ >>> async with breaker.call() as protected:
21
+ ... result = await connection.execute(query)
22
+ """
23
+
24
+ import asyncio
25
+ import logging
26
+ import time
27
+ from collections import deque
28
+ from dataclasses import dataclass, field
29
+ from datetime import datetime, timedelta
30
+ from enum import Enum
31
+ from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar
32
+
33
+ logger = logging.getLogger(__name__)
34
+
35
+ T = TypeVar("T")
36
+
37
+
38
+ class CircuitState(Enum):
39
+ """Circuit breaker states."""
40
+
41
+ CLOSED = "closed" # Normal operation
42
+ OPEN = "open" # Failing fast
43
+ HALF_OPEN = "half_open" # Testing recovery
44
+
45
+
46
+ class CircuitBreakerError(Exception):
47
+ """Raised when circuit breaker is open."""
48
+
49
+ pass
50
+
51
+
52
+ @dataclass
53
+ class CircuitBreakerConfig:
54
+ """Configuration for circuit breaker behavior."""
55
+
56
+ failure_threshold: int = 5 # Failures before opening
57
+ success_threshold: int = 3 # Successes to close from half-open
58
+ recovery_timeout: int = 60 # Seconds before trying half-open
59
+ half_open_requests: int = 3 # Requests allowed in half-open
60
+ error_rate_threshold: float = 0.5 # Error rate to trigger open
61
+ window_size: int = 100 # Rolling window for error rate
62
+ excluded_exceptions: List[type] = field(default_factory=list) # Don't count these
63
+
64
+
65
+ @dataclass
66
+ class CircuitBreakerMetrics:
67
+ """Metrics tracking for circuit breaker."""
68
+
69
+ total_calls: int = 0
70
+ successful_calls: int = 0
71
+ failed_calls: int = 0
72
+ rejected_calls: int = 0
73
+ state_transitions: List[Dict[str, Any]] = field(default_factory=list)
74
+ last_failure_time: Optional[float] = None
75
+ consecutive_failures: int = 0
76
+ consecutive_successes: int = 0
77
+
78
+ def record_success(self):
79
+ """Record successful call."""
80
+ self.total_calls += 1
81
+ self.successful_calls += 1
82
+ self.consecutive_successes += 1
83
+ self.consecutive_failures = 0
84
+
85
+ def record_failure(self):
86
+ """Record failed call."""
87
+ self.total_calls += 1
88
+ self.failed_calls += 1
89
+ self.consecutive_failures += 1
90
+ self.consecutive_successes = 0
91
+ self.last_failure_time = time.time()
92
+
93
+ def record_rejection(self):
94
+ """Record rejected call (circuit open)."""
95
+ self.rejected_calls += 1
96
+
97
+ def get_error_rate(self) -> float:
98
+ """Calculate current error rate."""
99
+ if self.total_calls == 0:
100
+ return 0.0
101
+ return self.failed_calls / self.total_calls
102
+
103
+
104
+ class ConnectionCircuitBreaker(Generic[T]):
105
+ """Circuit breaker for database connections and operations.
106
+
107
+ Monitors failures and prevents cascading failures by failing fast
108
+ when error threshold is reached. Automatically tests recovery
109
+ after timeout period.
110
+ """
111
+
112
+ def __init__(self, config: Optional[CircuitBreakerConfig] = None):
113
+ """Initialize circuit breaker with configuration."""
114
+ self.config = config or CircuitBreakerConfig()
115
+ self.state = CircuitState.CLOSED
116
+ self.metrics = CircuitBreakerMetrics()
117
+ self._lock = asyncio.Lock()
118
+ self._half_open_requests = 0
119
+ self._last_state_change = time.time()
120
+ self._rolling_window = deque(maxlen=self.config.window_size)
121
+ self._listeners: List[Callable] = []
122
+
123
+ async def call(self, func: Callable[..., T], *args, **kwargs) -> T:
124
+ """Execute function with circuit breaker protection.
125
+
126
+ Args:
127
+ func: Async function to protect
128
+ *args: Function arguments
129
+ **kwargs: Function keyword arguments
130
+
131
+ Returns:
132
+ Function result
133
+
134
+ Raises:
135
+ CircuitBreakerError: If circuit is open
136
+ Exception: If function fails
137
+ """
138
+ async with self._lock:
139
+ # Check if we should transition states
140
+ await self._check_state_transition()
141
+
142
+ if self.state == CircuitState.OPEN:
143
+ self.metrics.record_rejection()
144
+ raise CircuitBreakerError(
145
+ f"Circuit breaker is OPEN. "
146
+ f"Rejected after {self.metrics.consecutive_failures} failures. "
147
+ f"Will retry in {self._time_until_recovery():.1f}s"
148
+ )
149
+
150
+ if self.state == CircuitState.HALF_OPEN:
151
+ if self._half_open_requests >= self.config.half_open_requests:
152
+ self.metrics.record_rejection()
153
+ raise CircuitBreakerError(
154
+ "Circuit breaker is HALF_OPEN but request limit reached"
155
+ )
156
+ self._half_open_requests += 1
157
+
158
+ # Execute the function
159
+ start_time = time.time()
160
+ try:
161
+ result = await func(*args, **kwargs)
162
+ await self._record_success()
163
+ return result
164
+ except Exception as e:
165
+ # Check if this exception should be counted
166
+ if not any(
167
+ isinstance(e, exc_type) for exc_type in self.config.excluded_exceptions
168
+ ):
169
+ await self._record_failure(e)
170
+ raise
171
+
172
+ async def _check_state_transition(self):
173
+ """Check if state should transition based on metrics."""
174
+ current_time = time.time()
175
+
176
+ if self.state == CircuitState.CLOSED:
177
+ # Check if we should open
178
+ if self._should_open():
179
+ await self._transition_to(CircuitState.OPEN)
180
+
181
+ elif self.state == CircuitState.OPEN:
182
+ # Check if we should try recovery
183
+ time_since_open = current_time - self._last_state_change
184
+ if time_since_open >= self.config.recovery_timeout:
185
+ await self._transition_to(CircuitState.HALF_OPEN)
186
+ self._half_open_requests = 0
187
+
188
+ elif self.state == CircuitState.HALF_OPEN:
189
+ # This is handled after request execution
190
+ pass
191
+
192
+ def _should_open(self) -> bool:
193
+ """Determine if circuit should open based on failures."""
194
+ # Check consecutive failures
195
+ if self.metrics.consecutive_failures >= self.config.failure_threshold:
196
+ return True
197
+
198
+ # Check error rate in rolling window
199
+ if len(self._rolling_window) >= self.config.window_size / 2:
200
+ error_count = sum(1 for success in self._rolling_window if not success)
201
+ error_rate = error_count / len(self._rolling_window)
202
+ if error_rate >= self.config.error_rate_threshold:
203
+ return True
204
+
205
+ return False
206
+
207
+ async def _record_success(self):
208
+ """Record successful execution."""
209
+ async with self._lock:
210
+ self.metrics.record_success()
211
+ self._rolling_window.append(True)
212
+
213
+ if self.state == CircuitState.HALF_OPEN:
214
+ if self.metrics.consecutive_successes >= self.config.success_threshold:
215
+ await self._transition_to(CircuitState.CLOSED)
216
+
217
+ async def _record_failure(self, error: Exception):
218
+ """Record failed execution."""
219
+ async with self._lock:
220
+ self.metrics.record_failure()
221
+ self._rolling_window.append(False)
222
+
223
+ if self.state == CircuitState.HALF_OPEN:
224
+ # Single failure in half-open goes back to open
225
+ await self._transition_to(CircuitState.OPEN)
226
+ elif self.state == CircuitState.CLOSED:
227
+ # Check if we should open the circuit
228
+ if self._should_open():
229
+ await self._transition_to(CircuitState.OPEN)
230
+
231
+ logger.warning(
232
+ f"Circuit breaker recorded failure: {type(error).__name__}: {error}"
233
+ )
234
+
235
+ async def _transition_to(self, new_state: CircuitState):
236
+ """Transition to new state and notify listeners."""
237
+ old_state = self.state
238
+ self.state = new_state
239
+ self._last_state_change = time.time()
240
+
241
+ # Reset counters on state change
242
+ if new_state == CircuitState.CLOSED:
243
+ self.metrics.consecutive_failures = 0
244
+ elif new_state == CircuitState.OPEN:
245
+ self.metrics.consecutive_successes = 0
246
+
247
+ # Record transition
248
+ self.metrics.state_transitions.append(
249
+ {
250
+ "from": old_state.value,
251
+ "to": new_state.value,
252
+ "timestamp": datetime.now().isoformat(),
253
+ "reason": self._get_transition_reason(old_state, new_state),
254
+ }
255
+ )
256
+
257
+ logger.info(
258
+ f"Circuit breaker transitioned from {old_state.value} to {new_state.value}"
259
+ )
260
+
261
+ # Notify listeners
262
+ for listener in self._listeners:
263
+ try:
264
+ await listener(old_state, new_state, self.metrics)
265
+ except Exception as e:
266
+ logger.error(f"Error notifying circuit breaker listener: {e}")
267
+
268
+ def _get_transition_reason(
269
+ self, old_state: CircuitState, new_state: CircuitState
270
+ ) -> str:
271
+ """Get human-readable reason for state transition."""
272
+ if old_state == CircuitState.CLOSED and new_state == CircuitState.OPEN:
273
+ return f"Failure threshold reached ({self.metrics.consecutive_failures} failures)"
274
+ elif old_state == CircuitState.OPEN and new_state == CircuitState.HALF_OPEN:
275
+ return f"Recovery timeout elapsed ({self.config.recovery_timeout}s)"
276
+ elif old_state == CircuitState.HALF_OPEN and new_state == CircuitState.CLOSED:
277
+ return f"Success threshold reached ({self.metrics.consecutive_successes} successes)"
278
+ elif old_state == CircuitState.HALF_OPEN and new_state == CircuitState.OPEN:
279
+ return "Failure during recovery test"
280
+ return "Unknown reason"
281
+
282
+ def _time_until_recovery(self) -> float:
283
+ """Calculate seconds until recovery attempt."""
284
+ if self.state != CircuitState.OPEN:
285
+ return 0.0
286
+ elapsed = time.time() - self._last_state_change
287
+ remaining = self.config.recovery_timeout - elapsed
288
+ return max(0.0, remaining)
289
+
290
+ async def force_open(self, reason: str = "Manual override"):
291
+ """Manually open the circuit breaker."""
292
+ async with self._lock:
293
+ if self.state != CircuitState.OPEN:
294
+ logger.warning(f"Manually opening circuit breaker: {reason}")
295
+ await self._transition_to(CircuitState.OPEN)
296
+
297
+ async def force_close(self, reason: str = "Manual override"):
298
+ """Manually close the circuit breaker."""
299
+ async with self._lock:
300
+ if self.state != CircuitState.CLOSED:
301
+ logger.warning(f"Manually closing circuit breaker: {reason}")
302
+ self.metrics.consecutive_failures = 0
303
+ self.metrics.consecutive_successes = 0
304
+ await self._transition_to(CircuitState.CLOSED)
305
+
306
+ async def reset(self):
307
+ """Reset circuit breaker to initial state."""
308
+ async with self._lock:
309
+ self.state = CircuitState.CLOSED
310
+ self.metrics = CircuitBreakerMetrics()
311
+ self._rolling_window.clear()
312
+ self._half_open_requests = 0
313
+ self._last_state_change = time.time()
314
+ logger.info("Circuit breaker reset to initial state")
315
+
316
+ def add_listener(self, listener: Callable):
317
+ """Add state change listener."""
318
+ self._listeners.append(listener)
319
+
320
+ def remove_listener(self, listener: Callable):
321
+ """Remove state change listener."""
322
+ if listener in self._listeners:
323
+ self._listeners.remove(listener)
324
+
325
+ def get_status(self) -> Dict[str, Any]:
326
+ """Get current circuit breaker status."""
327
+ return {
328
+ "state": self.state.value,
329
+ "metrics": {
330
+ "total_calls": self.metrics.total_calls,
331
+ "successful_calls": self.metrics.successful_calls,
332
+ "failed_calls": self.metrics.failed_calls,
333
+ "rejected_calls": self.metrics.rejected_calls,
334
+ "error_rate": self.metrics.get_error_rate(),
335
+ "consecutive_failures": self.metrics.consecutive_failures,
336
+ "consecutive_successes": self.metrics.consecutive_successes,
337
+ },
338
+ "config": {
339
+ "failure_threshold": self.config.failure_threshold,
340
+ "success_threshold": self.config.success_threshold,
341
+ "recovery_timeout": self.config.recovery_timeout,
342
+ "error_rate_threshold": self.config.error_rate_threshold,
343
+ },
344
+ "time_until_recovery": (
345
+ self._time_until_recovery() if self.state == CircuitState.OPEN else None
346
+ ),
347
+ "state_transitions": self.metrics.state_transitions[
348
+ -5:
349
+ ], # Last 5 transitions
350
+ }
351
+
352
+
353
+ class CircuitBreakerManager:
354
+ """Manages multiple circuit breakers for different resources."""
355
+
356
+ def __init__(self):
357
+ """Initialize circuit breaker manager."""
358
+ self._breakers: Dict[str, ConnectionCircuitBreaker] = {}
359
+ self._default_config = CircuitBreakerConfig()
360
+
361
+ def get_or_create(
362
+ self, name: str, config: Optional[CircuitBreakerConfig] = None
363
+ ) -> ConnectionCircuitBreaker:
364
+ """Get existing or create new circuit breaker."""
365
+ if name not in self._breakers:
366
+ self._breakers[name] = ConnectionCircuitBreaker(
367
+ config or self._default_config
368
+ )
369
+ return self._breakers[name]
370
+
371
+ def get_all_status(self) -> Dict[str, Dict[str, Any]]:
372
+ """Get status of all circuit breakers."""
373
+ return {name: breaker.get_status() for name, breaker in self._breakers.items()}
374
+
375
+ async def reset_all(self):
376
+ """Reset all circuit breakers."""
377
+ for breaker in self._breakers.values():
378
+ await breaker.reset()
379
+
380
+ def set_default_config(self, config: CircuitBreakerConfig):
381
+ """Set default configuration for new breakers."""
382
+ self._default_config = config
@@ -0,0 +1,16 @@
1
+ """Edge computing infrastructure for global distribution.
2
+
3
+ This module provides edge computing capabilities for Kailash SDK,
4
+ enabling global distribution of compute and data with sub-10ms latency.
5
+ """
6
+
7
+ from .compliance import ComplianceRouter
8
+ from .discovery import EdgeDiscovery, EdgeSelectionStrategy
9
+ from .location import EdgeLocation
10
+
11
+ __all__ = [
12
+ "EdgeLocation",
13
+ "EdgeDiscovery",
14
+ "EdgeSelectionStrategy",
15
+ "ComplianceRouter",
16
+ ]