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
|
# The MIT License (MIT)
# Copyright (c) Microsoft Corporation. All rights reserved.
import os
import unittest
import uuid
from time import sleep
import pytest
from azure.core.exceptions import ServiceResponseError
import test_config
from azure.cosmos import _location_cache, _partition_health_tracker
from azure.cosmos import CosmosClient
from azure.cosmos._partition_health_tracker import HEALTH_STATUS, UNHEALTHY_TENTATIVE, UNHEALTHY
from azure.cosmos.exceptions import CosmosHttpResponseError
from _fault_injection_transport import FaultInjectionTransport
REGION_1 = test_config.TestConfig.WRITE_LOCATION
REGION_2 = test_config.TestConfig.READ_LOCATION
CHANGE_FEED = "changefeed"
CHANGE_FEED_PK = "changefeed_pk"
CHANGE_FEED_EPK = "changefeed_epk"
READ = "read"
CREATE = "create"
READ_ALL_ITEMS = "read_all_items"
DELETE_ALL_ITEMS_BY_PARTITION_KEY = "delete_all_items_by_partition_key"
QUERY = "query"
QUERY_PK = "query_pk"
BATCH = "batch"
UPSERT = "upsert"
REPLACE = "replace"
PATCH = "patch"
DELETE = "delete"
PK_VALUE = "pk1"
def create_doc():
return {'id': str(uuid.uuid4()),
'pk': PK_VALUE,
'name': 'sample document',
'key': 'value'}
def read_operations_and_errors():
read_operations = [READ, QUERY_PK, CHANGE_FEED, CHANGE_FEED_PK, CHANGE_FEED_EPK]
errors = create_errors()
params = []
for read_operation in read_operations:
for error in errors:
params.append((read_operation, error))
return params
def write_operations_and_errors():
write_operations = [CREATE, UPSERT, REPLACE, DELETE, PATCH, BATCH]
errors = create_errors()
params = []
for write_operation in write_operations:
for error in errors:
params.append((write_operation, error))
return params
def operations():
write_operations = [CREATE, UPSERT, REPLACE, DELETE, PATCH, BATCH]
read_operations = [READ, QUERY_PK, CHANGE_FEED_PK, CHANGE_FEED_EPK]
operations = []
for i, write_operation in enumerate(write_operations):
operations.append((read_operations[i % len(read_operations)], write_operation))
return operations
def create_errors():
errors = []
error_codes = [408, 500, 502, 503]
for error_code in error_codes:
errors.append(CosmosHttpResponseError(
status_code=error_code,
message="Some injected error."))
errors.append(ServiceResponseError(message="Injected Service Response Error."))
return errors
def validate_unhealthy_partitions(global_endpoint_manager,
expected_unhealthy_partitions):
health_info_map = global_endpoint_manager.global_partition_endpoint_manager_core.partition_health_tracker.pk_range_wrapper_to_health_info
unhealthy_partitions = 0
for pk_range_wrapper, location_to_health_info in health_info_map.items():
for location, health_info in location_to_health_info.items():
health_status = health_info.unavailability_info.get(HEALTH_STATUS)
if health_status == UNHEALTHY_TENTATIVE or health_status == UNHEALTHY:
unhealthy_partitions += 1
else:
assert health_info.read_consecutive_failure_count < 10
assert health_info.write_consecutive_failure_count < 5
assert unhealthy_partitions == expected_unhealthy_partitions
def validate_response_uri(response, expected_uri):
request = response.get_response_headers()["_request"]
assert request.url.startswith(expected_uri)
def perform_write_operation(operation, container, fault_injection_container, doc_id, pk, expected_uri):
doc = {'id': doc_id,
'pk': pk,
'name': 'sample document',
'key': 'value'}
if operation == CREATE:
resp = fault_injection_container.create_item(body=doc)
elif operation == UPSERT:
resp = fault_injection_container.upsert_item(body=doc)
elif operation == REPLACE:
container.create_item(body=doc)
sleep(1)
new_doc = {'id': doc_id,
'pk': pk,
'name': 'sample document' + str(uuid),
'key': 'value'}
resp = fault_injection_container.replace_item(item=doc['id'], body=new_doc)
elif operation == DELETE:
container.create_item(body=doc)
sleep(1)
resp = fault_injection_container.delete_item(item=doc['id'], partition_key=doc['pk'])
elif operation == PATCH:
container.create_item(body=doc)
sleep(1)
operations = [{"op": "incr", "path": "/company", "value": 3}]
resp = fault_injection_container.patch_item(item=doc['id'], partition_key=doc['pk'], patch_operations=operations)
elif operation == BATCH:
batch_operations = [
("create", (doc, )),
("upsert", (doc,)),
("upsert", (doc,)),
("upsert", (doc,)),
]
resp = fault_injection_container.execute_item_batch(batch_operations, partition_key=doc['pk'])
# this will need to be emulator only
elif operation == DELETE_ALL_ITEMS_BY_PARTITION_KEY:
container.create_item(body=doc)
resp = fault_injection_container.delete_all_items_by_partition_key(pk)
if resp:
validate_response_uri(resp, expected_uri)
def perform_read_operation(operation, container, doc_id, pk, expected_uri):
if operation == READ:
read_resp = container.read_item(item=doc_id, partition_key=pk)
request = read_resp.get_response_headers()["_request"]
# Validate the response comes from "Read Region" (the most preferred read-only region)
assert request.url.startswith(expected_uri)
elif operation == QUERY_PK:
# partition key filtered query
query = "SELECT * FROM c WHERE c.id = @id AND c.pk = @pk"
parameters = [{"name": "@id", "value": doc_id}, {"name": "@pk", "value": pk}]
for item in container.query_items(query=query, partition_key=pk, parameters=parameters):
assert item['id'] == doc_id
# need to do query with no pk and with feed range
elif operation == QUERY:
# cross partition query
query = "SELECT * FROM c WHERE c.id = @id"
for item in container.query_items(query=query):
assert item['id'] == doc_id
elif operation == CHANGE_FEED:
for _ in container.query_items_change_feed():
pass
elif operation == CHANGE_FEED_PK:
# partition key filtered change feed
for _ in container.query_items_change_feed(partition_key=pk):
pass
elif operation == CHANGE_FEED_EPK:
# partition key filtered by feed range
feed_range = container.feed_range_from_partition_key(partition_key=pk)
for _ in container.query_items_change_feed(feed_range=feed_range):
pass
elif operation == READ_ALL_ITEMS:
for _ in container.read_all_items():
pass
@pytest.mark.cosmosCircuitBreaker
class TestPerPartitionCircuitBreakerMM:
host = test_config.TestConfig.host
master_key = test_config.TestConfig.masterKey
TEST_DATABASE_ID = test_config.TestConfig.TEST_DATABASE_ID
TEST_CONTAINER_MULTI_PARTITION_ID = test_config.TestConfig.TEST_MULTI_PARTITION_CONTAINER_ID
def setup_method_with_custom_transport(self, custom_transport, default_endpoint=host, **kwargs):
container_id = kwargs.pop("container_id", None)
if not container_id:
container_id = self.TEST_CONTAINER_MULTI_PARTITION_ID
client = CosmosClient(default_endpoint, self.master_key,
preferred_locations=[REGION_1, REGION_2],
multiple_write_locations=True,
transport=custom_transport, **kwargs)
db = client.get_database_client(self.TEST_DATABASE_ID)
container = db.get_container_client(container_id)
return {"client": client, "db": db, "col": container}
@pytest.mark.parametrize("write_operation, error", write_operations_and_errors())
def test_write_consecutive_failure_threshold(self, write_operation, error):
error_lambda = lambda r: FaultInjectionTransport.error_after_delay(
0,
error
)
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = self.setup_info(error_lambda)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
with pytest.raises((CosmosHttpResponseError, ServiceResponseError)) as exc_info:
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
assert exc_info.value == error
validate_unhealthy_partitions(global_endpoint_manager, 0)
# writes should fail but still be tracked
for i in range(4):
with pytest.raises((CosmosHttpResponseError, ServiceResponseError)) as exc_info:
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
assert exc_info.value == error
# writes should now succeed because going to the other region
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
validate_unhealthy_partitions(global_endpoint_manager, 1)
# remove faults and reduce initial recover time and perform a write
original_unavailable_time = _partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = 1
custom_transport.faults = []
try:
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
uri_down)
finally:
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = original_unavailable_time
validate_unhealthy_partitions(global_endpoint_manager, 0)
@pytest.mark.cosmosCircuitBreakerMultiRegion
@pytest.mark.parametrize("read_operation, error", read_operations_and_errors())
def test_read_consecutive_failure_threshold(self, read_operation, error):
error_lambda = lambda r: FaultInjectionTransport.error_after_delay(
0,
error
)
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = self.setup_info(error_lambda)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
# create a document to read
container.create_item(body=doc)
# reads should fail over and only the relevant partition should be marked as unavailable
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
doc['pk'],
expected_uri)
# partition should not have been marked unavailable after one error
validate_unhealthy_partitions(global_endpoint_manager, 0)
for i in range(10):
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
doc['pk'],
expected_uri)
# the partition should have been marked as unavailable after breaking read threshold
if read_operation in (CHANGE_FEED, QUERY, READ_ALL_ITEMS):
# these operations are cross partition so they would mark both partitions as unavailable
expected_unhealthy_partitions = 5
else:
expected_unhealthy_partitions = 1
validate_unhealthy_partitions(global_endpoint_manager, expected_unhealthy_partitions)
# remove faults and reduce initial recover time and perform a read
original_unavailable_time = _partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = 1
custom_transport.faults = []
try:
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
doc['pk'],
uri_down)
finally:
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = original_unavailable_time
validate_unhealthy_partitions(global_endpoint_manager, 0)
@pytest.mark.parametrize("write_operation, error", write_operations_and_errors())
def test_write_failure_rate_threshold(self, write_operation, error):
error_lambda = lambda r: FaultInjectionTransport.error_after_delay(
0,
error
)
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = self.setup_info(error_lambda)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
# lower minimum requests for testing
_partition_health_tracker.MINIMUM_REQUESTS_FOR_FAILURE_RATE = 10
os.environ["AZURE_COSMOS_FAILURE_PERCENTAGE_TOLERATED"] = "80"
try:
# writes should fail but still be tracked and mark unavailable a partition after crossing threshold
for i in range(10):
validate_unhealthy_partitions(global_endpoint_manager, 0)
if i == 4 or i == 8:
# perform some successful creates to reset consecutive counter
# remove faults and perform a write
custom_transport.faults = []
fault_injection_container.upsert_item(body=doc)
custom_transport.add_fault(predicate,
lambda r: FaultInjectionTransport.error_after_delay(
0,
error
))
else:
with pytest.raises((CosmosHttpResponseError, ServiceResponseError)) as exc_info:
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
assert exc_info.value == error
validate_unhealthy_partitions(global_endpoint_manager, 1)
finally:
os.environ["AZURE_COSMOS_FAILURE_PERCENTAGE_TOLERATED"] = "90"
# restore minimum requests
_partition_health_tracker.MINIMUM_REQUESTS_FOR_FAILURE_RATE = 100
@pytest.mark.cosmosCircuitBreakerMultiRegion
@pytest.mark.parametrize("read_operation, error", read_operations_and_errors())
def test_read_failure_rate_threshold(self, read_operation, error):
error_lambda = lambda r: FaultInjectionTransport.error_after_delay(
0,
error
)
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = self.setup_info(error_lambda)
container.upsert_item(body=doc)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
# lower minimum requests for testing
_partition_health_tracker.MINIMUM_REQUESTS_FOR_FAILURE_RATE = 8
os.environ["AZURE_COSMOS_FAILURE_PERCENTAGE_TOLERATED"] = "80"
try:
if isinstance(error, ServiceResponseError):
# service response error retries in region 3 additional times before failing over
num_operations = 2
else:
num_operations = 8
for i in range(num_operations):
validate_unhealthy_partitions(global_endpoint_manager, 0)
# read will fail and retry in other region
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
PK_VALUE,
expected_uri)
if read_operation in (CHANGE_FEED, QUERY, READ_ALL_ITEMS):
# these operations are cross partition so they would mark both partitions as unavailable
expected_unhealthy_partitions = 5
else:
expected_unhealthy_partitions = 1
validate_unhealthy_partitions(global_endpoint_manager, expected_unhealthy_partitions)
finally:
os.environ["AZURE_COSMOS_FAILURE_PERCENTAGE_TOLERATED"] = "90"
# restore minimum requests
_partition_health_tracker.MINIMUM_REQUESTS_FOR_FAILURE_RATE = 100
def setup_info(self, error, **kwargs):
expected_uri = _location_cache.LocationCache.GetLocationalEndpoint(self.host, REGION_2)
uri_down = _location_cache.LocationCache.GetLocationalEndpoint(self.host, REGION_1)
custom_transport = FaultInjectionTransport()
# two documents targeted to same partition, one will always fail and the other will succeed
doc = create_doc()
predicate = lambda r: (FaultInjectionTransport.predicate_is_document_operation(r) and
FaultInjectionTransport.predicate_targets_region(r, uri_down))
custom_transport.add_fault(predicate,
error)
custom_setup = self.setup_method_with_custom_transport(custom_transport, default_endpoint=self.host, **kwargs)
fault_injection_container = custom_setup['col']
setup = self.setup_method_with_custom_transport(None, default_endpoint=self.host, **kwargs)
container = setup['col']
return container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate
def test_stat_reset(self):
error_lambda = lambda r: FaultInjectionTransport.error_after_delay(
0,
CosmosHttpResponseError(
status_code=503,
message="Some injected error.")
)
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = \
self.setup_info(error_lambda, container_id=test_config.TestConfig.TEST_SINGLE_PARTITION_CONTAINER_ID)
container.upsert_item(body=doc)
sleep(1)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
# lower refresh interval for testing
_partition_health_tracker.REFRESH_INTERVAL_MS = 10 * 1000
try:
for i in range(2):
validate_unhealthy_partitions(global_endpoint_manager, 0)
# read will fail and retry in other region
perform_read_operation(READ,
fault_injection_container,
doc['id'],
PK_VALUE,
expected_uri)
try:
perform_write_operation(CREATE,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
except CosmosHttpResponseError as e:
assert e.status_code == 503
validate_unhealthy_partitions(global_endpoint_manager, 0)
validate_stats(global_endpoint_manager, 2, 2, 2, 2, 0, 0)
sleep(25)
perform_read_operation(READ,
fault_injection_container,
doc['id'],
PK_VALUE,
expected_uri)
validate_stats(global_endpoint_manager, 2, 3, 1, 0, 0, 0)
finally:
_partition_health_tracker.REFRESH_INTERVAL_MS = 60 * 1000
@pytest.mark.parametrize("read_operation, write_operation", operations())
def test_service_request_error(self, read_operation, write_operation):
# the region should be tried 4 times before failing over and mark the partition as unavailable
# the region should not be marked as unavailable
error_lambda = lambda r: FaultInjectionTransport.error_region_down()
container, doc, expected_uri, uri_down, fault_injection_container, custom_transport, predicate = self.setup_info(error_lambda)
container.upsert_item(body=doc)
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
PK_VALUE,
expected_uri)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
validate_unhealthy_partitions(global_endpoint_manager, 0)
# there shouldn't be region marked as unavailable
assert len(global_endpoint_manager.location_cache.location_unavailability_info_by_endpoint) == 1
# recover partition
# remove faults and reduce initial recover time and perform a write
original_unavailable_time = _partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = 1
custom_transport.faults = []
try:
perform_read_operation(read_operation,
fault_injection_container,
doc['id'],
PK_VALUE,
expected_uri)
finally:
_partition_health_tracker.INITIAL_UNAVAILABLE_TIME_MS = original_unavailable_time
validate_unhealthy_partitions(global_endpoint_manager, 0)
custom_transport.add_fault(predicate,
lambda r: FaultInjectionTransport.error_region_down())
perform_write_operation(write_operation,
container,
fault_injection_container,
str(uuid.uuid4()),
PK_VALUE,
expected_uri)
global_endpoint_manager = fault_injection_container.client_connection._global_endpoint_manager
validate_unhealthy_partitions(global_endpoint_manager, 0)
# there shouldn't be region marked as unavailable
assert len(global_endpoint_manager.location_cache.location_unavailability_info_by_endpoint) == 1
# test cosmos client timeout
if __name__ == '__main__':
unittest.main()
def validate_stats(global_endpoint_manager,
expected_write_consecutive_failure_count,
expected_read_consecutive_failure_count,
expected_read_failure_count,
expected_write_failure_count,
expected_write_success_count,
expected_read_success_count):
health_info_map = global_endpoint_manager.global_partition_endpoint_manager_core.partition_health_tracker.pk_range_wrapper_to_health_info
for pk_range_wrapper, location_to_health_info in health_info_map.items():
health_info = location_to_health_info[REGION_1]
assert health_info.read_consecutive_failure_count == expected_read_consecutive_failure_count
assert health_info.write_consecutive_failure_count == expected_write_consecutive_failure_count
assert health_info.read_failure_count == expected_read_failure_count
assert health_info.write_failure_count == expected_write_failure_count
assert health_info.read_success_count == expected_read_success_count
assert health_info.write_success_count == expected_write_success_count
|