-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11_cb_async_operations.py
More file actions
660 lines (540 loc) · 25.3 KB
/
11_cb_async_operations.py
File metadata and controls
660 lines (540 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
"""
Demonstrates asynchronous operations in Couchbase with observability features.
This script shows how to:
1. Create a reusable async Couchbase client class
2. Define individual async methods for upsert, get, and remove
3. Perform multiple concurrent operations using asyncio.gather()
4. Enable performance tracking with timing metrics
5. Configure slow operations logging and orphan response tracking
6. Toggle debug output with a global flag
Async operations are beneficial for:
- High-throughput applications
- I/O-bound workloads
- Concurrent operations on multiple documents
- Improved resource utilization
- Better scalability
WHAT TO EXPECT WHEN YOU RUN THIS:
==================================
With DEBUG=True, you'll see:
1. INFO logs: Connection status, batch summaries, timing
2. WARNING logs: Connection attempts to unavailable ports (normal for localhost)
3. Performance metrics: Throughput (ops/sec) for each example
4. Final metrics JSON: Operation percentiles and counts (p50, p90, p99, p100)
5. Slow operations: JSON output if any operation exceeds thresholds
6. Orphaned responses: Logged if requests timeout (10s intervals)
The final "Metrics" JSON shows:
- "percentiles_us": Operation latency at 50th, 90th, 99th, 100th percentiles (microseconds)
- "total_count": Number of operations performed
- Operations grouped by service: kv (get/upsert/remove), query, search, management
Observability Features:
- Operation timing with DEBUG flag
- Slow operations threshold logging (JSON output)
- Orphaned response tracking (timeout detection)
- Performance metrics per operation
- Exponential backoff retry for transient failures (timeouts, server errors)
- Configurable retry attempts and backoff intervals
Retry Strategy:
- Attempt 1: 0.1s delay (100ms)
- Attempt 2: 0.2s delay (200ms)
- Attempt 3: 0.4s delay (400ms)
- Formula: delay = initial_backoff * (2 ** attempt)
- Retries: Timeouts, ServiceUnavailable, InternalServerFailure
- No Retry: DocumentNotFound (not a transient error)
Learn More:
- Slow Operations Logging: https://docs.couchbase.com/python-sdk/current/howtos/slow-operations-logging.html
- Orphaned Response Tracking: https://docs.couchbase.com/python-sdk/current/howtos/observability-orphan-logger.html
- Async Operations: https://docs.couchbase.com/sdk-api/couchbase-python-client/acouchbase_api/acouchbase_core.html
"""
import asyncio
import time
import logging
import sys
from datetime import timedelta
# Async Couchbase imports
from acouchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator
from couchbase.options import ClusterOptions, ClusterTracingOptions
from couchbase.exceptions import (
CouchbaseException,
DocumentNotFoundException,
DocumentExistsException,
TimeoutException,
AmbiguousTimeoutException,
UnAmbiguousTimeoutException,
AuthenticationException,
BucketNotFoundException,
ServiceUnavailableException,
InternalServerFailureException
)
import couchbase
# Global debug flag - set to True to enable detailed timing and logging
DEBUG = True
# Configure logging for Couchbase operations
if DEBUG:
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='%(levelname)s::%(asctime)s::%(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger()
couchbase.configure_logging(logger.name, level=logger.level)
else:
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger()
class AsyncCouchbaseClient:
"""
Async Couchbase client with built-in observability and performance tracking.
Features:
- Individual async methods for each operation type
- Optional timing metrics controlled by DEBUG flag
- Exponential backoff retry for transient failures
- Comprehensive exception handling
- Slow operations and orphan response logging
"""
def __init__(self, endpoint, username, password, bucket_name, scope_name, collection_name, max_retries=3, initial_backoff=0.1):
"""
Initialize the async Couchbase client.
Args:
endpoint: Couchbase server endpoint (e.g., "localhost")
username: Couchbase username
password: Couchbase password
bucket_name: Name of the bucket
scope_name: Name of the scope
collection_name: Name of the collection
max_retries: Maximum retry attempts for transient failures (default: 3)
initial_backoff: Initial backoff delay in seconds (default: 0.1s)
"""
self.endpoint = endpoint
self.username = username
self.password = password
self.bucket_name = bucket_name
self.scope_name = scope_name
self.collection_name = collection_name
self.max_retries = max_retries
self.initial_backoff = initial_backoff
self.cluster = None
self.bucket = None
self.collection = None
async def connect(self, use_tls=False, wan_profile=False):
"""
Connect to the Couchbase cluster asynchronously with observability features.
Args:
use_tls: Use couchbases:// (for Capella/cloud)
wan_profile: Apply wan_development profile (for Capella)
Raises:
AuthenticationException: If credentials are invalid
TimeoutException: If connection times out
BucketNotFoundException: If bucket doesn't exist
ServiceUnavailableException: If cluster is unreachable
"""
if DEBUG:
logger.info(f"Connecting to Couchbase cluster at {self.endpoint}...")
start_time = time.time() if DEBUG else None
try:
auth = PasswordAuthenticator(self.username, self.password)
# Configure tracing options for slow operations and orphan logging
tracing_opts = ClusterTracingOptions(
# Slow operations thresholds
tracing_threshold_kv=timedelta(milliseconds=100),
tracing_threshold_query=timedelta(milliseconds=500),
tracing_threshold_search=timedelta(milliseconds=500),
tracing_threshold_analytics=timedelta(seconds=1),
tracing_threshold_view=timedelta(seconds=1),
tracing_threshold_queue_size=10,
# Orphaned response logging
tracing_orphaned_queue_flush_interval=timedelta(seconds=10),
tracing_orphaned_queue_size=10
)
options = ClusterOptions(
authenticator=auth,
tracing_options=tracing_opts
)
if wan_profile:
options.apply_profile('wan_development')
protocol = 'couchbases' if use_tls else 'couchbase'
connection_string = f'{protocol}://{self.endpoint}'
self.cluster = await Cluster.connect(connection_string, options)
await self.cluster.wait_until_ready(timedelta(seconds=10))
self.bucket = self.cluster.bucket(self.bucket_name)
self.collection = self.bucket.scope(self.scope_name).collection(self.collection_name)
if DEBUG:
elapsed = time.time() - start_time
logger.info(f"✓ Connected to cluster in {elapsed:.4f}s")
logger.info(f" Slow operations logging enabled (KV > 100ms, Query > 500ms)")
logger.info(f" Orphaned response tracking enabled (10s intervals)")
return self
except AuthenticationException as e:
logger.error(f"✗ Authentication failed: Invalid credentials")
raise
except BucketNotFoundException as e:
logger.error(f"✗ Bucket '{self.bucket_name}' not found")
raise
except TimeoutException as e:
logger.error(f"✗ Connection timeout - cluster may be unreachable")
raise
except ServiceUnavailableException as e:
logger.error(f"✗ Service unavailable - check cluster status")
raise
async def close(self):
"""Close the cluster connection."""
if self.cluster:
if DEBUG:
logger.info("Closing cluster connection...")
await self.cluster.close()
if DEBUG:
logger.info("✓ Cluster connection closed")
async def upsert_document(self, key, document, enable_retry=True):
"""
Upsert a single document asynchronously with exponential backoff retry.
Args:
key: Document key
document: Document content (dict)
enable_retry: Enable retry with exponential backoff (default: True)
Returns:
Result with CAS value or None on error
"""
start_time = time.time() if DEBUG else None
last_exception = None
for attempt in range(self.max_retries + 1):
try:
result = await self.collection.upsert(key, document)
if DEBUG:
elapsed = time.time() - start_time
retry_info = f" (after {attempt} retries)" if attempt > 0 else ""
logger.debug(f"Upsert {key}{retry_info}: {elapsed:.4f}s (CAS: {result.cas})")
return result
except (TimeoutException, AmbiguousTimeoutException, UnAmbiguousTimeoutException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Timeout upserting {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
if DEBUG:
elapsed = time.time() - start_time
logger.warning(f"Timeout upserting {key} after {elapsed:.4f}s and {attempt} retries")
print(f" ✗ Timeout upserting {key} after {attempt} retries")
return None
except (ServiceUnavailableException, InternalServerFailureException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Transient error upserting {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
logger.error(f"Server error upserting {key} after {attempt} retries")
print(f" ✗ Server error upserting {key}")
return None
except CouchbaseException as e:
logger.error(f"Upsert failed for {key}: {e}")
print(f" ✗ Upsert failed for {key}: {e}")
return None
return None
async def get_document(self, key, enable_retry=True):
"""
Get a single document asynchronously with exponential backoff retry.
Args:
key: Document key
enable_retry: Enable retry with exponential backoff (default: True)
Returns:
Document content (dict) or None if not found
"""
start_time = time.time() if DEBUG else None
last_exception = None
for attempt in range(self.max_retries + 1):
try:
result = await self.collection.get(key)
if DEBUG:
elapsed = time.time() - start_time
retry_info = f" (after {attempt} retries)" if attempt > 0 else ""
logger.debug(f"Get {key}{retry_info}: {elapsed:.4f}s")
return result.content_as[dict]
except DocumentNotFoundException:
# Document doesn't exist - don't retry
if DEBUG:
elapsed = time.time() - start_time
logger.debug(f"Document not found {key} ({elapsed:.4f}s)")
print(f" ✗ Document not found: {key}")
return None
except (TimeoutException, AmbiguousTimeoutException, UnAmbiguousTimeoutException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Timeout getting {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
if DEBUG:
elapsed = time.time() - start_time
logger.warning(f"Timeout getting {key} after {elapsed:.4f}s and {attempt} retries")
print(f" ✗ Timeout getting {key} after {attempt} retries")
return None
except (ServiceUnavailableException, InternalServerFailureException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Transient error getting {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
logger.error(f"Server error getting {key} after {attempt} retries")
print(f" ✗ Server error getting {key}")
return None
except CouchbaseException as e:
logger.error(f"Get failed for {key}: {e}")
print(f" ✗ Get failed for {key}: {e}")
return None
return None
async def remove_document(self, key, enable_retry=True):
"""
Remove a single document asynchronously with exponential backoff retry.
Args:
key: Document key
enable_retry: Enable retry with exponential backoff (default: True)
Returns:
True if removed, False otherwise
"""
start_time = time.time() if DEBUG else None
last_exception = None
for attempt in range(self.max_retries + 1):
try:
await self.collection.remove(key)
if DEBUG:
elapsed = time.time() - start_time
retry_info = f" (after {attempt} retries)" if attempt > 0 else ""
logger.debug(f"Remove {key}{retry_info}: {elapsed:.4f}s")
return True
except DocumentNotFoundException:
# Document doesn't exist - don't retry
if DEBUG:
logger.debug(f"Document not found (already removed): {key}")
return False
except (TimeoutException, AmbiguousTimeoutException, UnAmbiguousTimeoutException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Timeout removing {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
if DEBUG:
elapsed = time.time() - start_time
logger.warning(f"Timeout removing {key} after {elapsed:.4f}s and {attempt} retries")
print(f" ✗ Timeout removing {key} after {attempt} retries")
return False
except (ServiceUnavailableException, InternalServerFailureException) as e:
last_exception = e
if enable_retry and attempt < self.max_retries:
backoff_delay = self.initial_backoff * (2 ** attempt)
if DEBUG:
logger.warning(f"Transient error removing {key}, retry {attempt + 1} in {backoff_delay:.2f}s")
await asyncio.sleep(backoff_delay)
continue
else:
logger.error(f"Server error removing {key} after {attempt} retries")
print(f" ✗ Server error removing {key}")
return False
except CouchbaseException as e:
logger.error(f"Remove failed for {key}: {e}")
print(f" ✗ Remove failed for {key}: {e}")
return False
return False
async def demo_concurrent_operations(client):
"""
Demonstrate various concurrent operation patterns with timing.
Args:
client: AsyncCouchbaseClient instance
"""
# Example 1: Concurrent upserts
print("\n" + "=" * 70)
print("EXAMPLE 1: 20 Concurrent Upserts")
print("=" * 70)
overall_start = time.time()
# Create 20 documents to upsert
upsert_tasks = []
for i in range(20):
doc_id = 9000 + i
key = f"async_airline_{doc_id}"
doc = {
"type": "airline",
"id": doc_id,
"callsign": f"ASYNC{i:03d}",
"name": f"Async Airways #{i}",
"country": "United States",
"timestamp": time.time()
}
upsert_tasks.append(client.upsert_document(key, doc))
# Execute all upserts concurrently
results = await asyncio.gather(*upsert_tasks)
successful = sum(1 for r in results if r is not None)
elapsed = time.time() - overall_start
print(f"\n✓ Completed {successful}/20 upserts in {elapsed:.4f}s")
print(f" Throughput: {20/elapsed:.2f} ops/sec")
if DEBUG:
logger.info(f"Upsert batch completed: {successful}/20 successful, {elapsed:.4f}s total")
# Small delay to ensure persistence
await asyncio.sleep(0.5)
# Example 2: Concurrent gets
print("\n" + "=" * 70)
print("EXAMPLE 2: 20 Concurrent Gets")
print("=" * 70)
overall_start = time.time()
# Create 20 get tasks
get_tasks = []
for i in range(20):
doc_id = 9000 + i
key = f"async_airline_{doc_id}"
get_tasks.append(client.get_document(key))
# Execute all gets concurrently
documents = await asyncio.gather(*get_tasks)
successful_docs = [d for d in documents if d is not None]
elapsed = time.time() - overall_start
print(f"\n✓ Retrieved {len(successful_docs)}/20 documents in {elapsed:.4f}s")
print(f" Throughput: {20/elapsed:.2f} ops/sec")
if DEBUG:
logger.info(f"Get batch completed: {len(successful_docs)}/20 successful, {elapsed:.4f}s total")
# Show sample documents
if successful_docs:
print(f"\nSample documents:")
for doc in successful_docs[:3]:
print(f" - {doc['name']} ({doc['callsign']})")
# Example 3: Mixed concurrent operations (upsert, get, remove)
print("\n" + "=" * 70)
print("EXAMPLE 3: Mixed Concurrent Operations (15 total)")
print("=" * 70)
print("Performing upserts, gets, and removes simultaneously\n")
overall_start = time.time()
mixed_tasks = []
# Add 5 upserts
for i in range(5):
doc_id = 9100 + i
key = f"async_airline_{doc_id}"
doc = {"type": "airline", "id": doc_id, "name": f"Mixed Test {i}"}
mixed_tasks.append(('upsert', client.upsert_document(key, doc)))
# Add 5 gets (from previously created docs)
for i in range(5):
doc_id = 9000 + i
key = f"async_airline_{doc_id}"
mixed_tasks.append(('get', client.get_document(key)))
# Add 5 removes (cleanup from Example 1/2)
for i in range(5, 10):
doc_id = 9000 + i
key = f"async_airline_{doc_id}"
mixed_tasks.append(('remove', client.remove_document(key)))
# Execute all mixed operations concurrently
if DEBUG:
logger.info("Executing 15 mixed operations concurrently (5 upserts, 5 gets, 5 removes)...")
results = await asyncio.gather(*[task for _, task in mixed_tasks])
elapsed = time.time() - overall_start
# Count results by operation type
upsert_count = sum(1 for i, (op, _) in enumerate(mixed_tasks) if op == 'upsert' and results[i] is not None)
get_count = sum(1 for i, (op, _) in enumerate(mixed_tasks) if op == 'get' and results[i] is not None)
remove_count = sum(1 for i, (op, _) in enumerate(mixed_tasks) if op == 'remove' and results[i])
print(f"✓ Mixed operations completed in {elapsed:.4f}s:")
print(f" - Upserts: {upsert_count}/5")
print(f" - Gets: {get_count}/5")
print(f" - Removes: {remove_count}/5")
print(f" Total throughput: {15/elapsed:.2f} ops/sec")
if DEBUG:
logger.info(f"Mixed batch: U={upsert_count}/5, G={get_count}/5, R={remove_count}/5, {elapsed:.4f}s")
# Example 4: Final cleanup
print("\n" + "=" * 70)
print("EXAMPLE 4: Final Cleanup (Concurrent Removal)")
print("=" * 70)
overall_start = time.time()
cleanup_tasks = []
# Clean up all test documents created (9000-9019 and 9100-9104)
for doc_id in list(range(9000, 9020)) + list(range(9100, 9105)):
key = f"async_airline_{doc_id}"
cleanup_tasks.append(client.remove_document(key))
results = await asyncio.gather(*cleanup_tasks)
removed = sum(1 for r in results if r)
elapsed = time.time() - overall_start
print(f"✓ Cleaned up {removed} test documents in {elapsed:.4f}s")
if DEBUG:
logger.info(f"Cleanup completed: {removed} documents removed, {elapsed:.4f}s")
async def main():
"""
Main async function demonstrating concurrent Couchbase operations.
"""
print("=" * 70)
print("ASYNC COUCHBASE OPERATIONS WITH OBSERVABILITY")
print("=" * 70)
print(f"\nDEBUG Mode: {'ON' if DEBUG else 'OFF'}")
if DEBUG:
print(" ✓ Detailed timing for each operation")
print(" ✓ Slow operations logging (JSON format)")
print(" ✓ Orphaned response tracking")
print(" ✓ Performance metrics")
print("\nThis demo shows:")
print(" - AsyncCouchbaseClient class for clean encapsulation")
print(" - Individual async methods (upsert, get, remove)")
print(" - Concurrent execution with asyncio.gather()")
print(" - Mixed operation types running simultaneously")
print(" - Comprehensive observability and error tracking\n")
# Configuration
# For local/self-hosted Couchbase Server:
ENDPOINT = "localhost"
USERNAME = "Administrator"
PASSWORD = "password"
USE_TLS = False
WAN_PROFILE = False
# For Capella (cloud), uncomment and update these:
# ENDPOINT = "cb.your-endpoint.cloud.couchbase.com"
# USERNAME = "your-capella-username"
# PASSWORD = "your-capella-password"
# USE_TLS = True
# WAN_PROFILE = True
BUCKET_NAME = "travel-sample"
CB_SCOPE = "inventory"
CB_COLLECTION = "airline"
# Create async client instance
client = AsyncCouchbaseClient(
endpoint=ENDPOINT,
username=USERNAME,
password=PASSWORD,
bucket_name=BUCKET_NAME,
scope_name=CB_SCOPE,
collection_name=CB_COLLECTION
)
try:
# Connect to Couchbase cluster
await client.connect(use_tls=USE_TLS, wan_profile=WAN_PROFILE)
# Run demonstration of concurrent operations
await demo_concurrent_operations(client)
print("\n" + "=" * 70)
print("KEY TAKEAWAYS")
print("=" * 70)
print(" ✓ Class-based design provides clean encapsulation")
print(" ✓ Individual async methods compose easily")
print(" ✓ asyncio.gather() enables true concurrency")
print(" ✓ Can mix different operation types simultaneously")
print(" ✓ Significantly better performance than sequential operations")
print(" ✓ Built-in observability with timing and logging")
print(" ✓ Slow operations and orphaned responses auto-logged")
if DEBUG:
print("\n Check logs above for:")
print(" - Individual operation timings")
print(" - Slow operations (if any exceeded thresholds)")
print(" - Orphaned responses (if any timeouts occurred)")
except CouchbaseException as e:
logger.error(f"Couchbase error: {e}")
print(f"\n✗ Couchbase error: {e}")
except Exception as e:
logger.error(f"Unexpected error: {e}")
print(f"\n✗ Unexpected error: {e}")
finally:
# Always close the cluster connection
await client.close()
if __name__ == "__main__":
print("\nTo disable debug output, set DEBUG = False at the top of the script\n")
# Run the async main function
asyncio.run(main())