circuitbreaker_1.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. def run_circuit_breaker():
  2. # Name of image to use for testing.
  3. image_to_upload = "HelloWorld.png"
  4. global blob_client
  5. global container_name
  6. try:
  7. # Create a reference to the blob client and container using the storage account name and key
  8. blob_client = BlockBlobService(account_name, account_key)
  9. # Make the container unique by using a UUID in the name.
  10. container_name = "democontainer" + str(uuid.uuid4())
  11. blob_client.create_container(container_name)
  12. except Exception as ex:
  13. print("Please make sure you have put the correct storage account name and key.")
  14. print(ex)
  15. # Define a reference to the actual blob and upload the block_blob to the newly created container
  16. full_path_to_file = os.path.join(os.path.dirname(__file__), image_to_upload)
  17. blob_client.create_blob_from_path(container_name, image_to_upload, full_path_to_file)
  18. # Set the location mode to secondary, so you can check just the secondary data center.
  19. blob_client.location_mode = LocationMode.SECONDARY
  20. blob_client.retry = LinearRetry(backoff=0).retry
  21. # Before proceeding, wait until the blob has been replicated to the secondary data center.
  22. # Loop and check for the presence of the blob once in a second until it hits 60 seconds
  23. # or until it finds it
  24. counter = 0
  25. while counter < 60:
  26. counter += 1
  27. sys.stdout.write("\nAttempt {0} to see if the blob has replicated to the secondary storage yet.".format(counter))
  28. sys.stdout.flush()
  29. if blob_client.exists(container_name, image_to_upload):
  30. break
  31. # Wait a second, then loop around and try again
  32. # When it's finished replicating to the secondary, continue.
  33. time.sleep(1)
  34. # Set the starting LocationMode to Primary, then Secondary.
  35. # Here we use the linear retry by default, but allow it to retry to secondary if
  36. # the initial request to primary fails.
  37. # Note that the default is Primary. You must have RA-GRS enabled to use this
  38. blob_client.location_mode = LocationMode.PRIMARY
  39. blob_client.retry = LinearRetry(max_attempts=retry_threshold, backoff=1).retry
  40. '''
  41. ************INSTRUCTIONS**************k
  42. To perform the test, first replace the 'accountname' and 'accountkey' with your storage account name and key.
  43. Every time it calls get_blob_to_path it will hit the response_callback function.
  44. Next, run this app. While this loop is running, pause the program by pressing any key, and
  45. put the intercept code in Fiddler (that will intercept and return a 503).
  46. For instructions on modifying Fiddler, look at the Fiddler_script.text file in this project.
  47. There are also full instructions in the ReadMe_Instructions.txt file included in this project.
  48. After adding the custom script to Fiddler, calls to primary storage will fail with a retryable
  49. error which will trigger the Retrying event (above).
  50. Then it will switch over and read the secondary. It will do that 20 times, then try to
  51. switch back to the primary.
  52. After seeing that happen, pause this again and remove the intercepting Fiddler code
  53. Then you'll see it return to the primary and finish.
  54. '''
  55. print("\n\nThe application will pause at 200 unit interval")
  56. for i in range(0, 1000):
  57. if blob_client.location_mode == LocationMode.SECONDARY:
  58. sys.stdout.write("S{0} ".format(str(i)))
  59. else:
  60. sys.stdout.write("P{0} ".format(str(i)))
  61. sys.stdout.flush()
  62. try:
  63. # These function is called immediately after retry evaluation is performed.
  64. # It is used to trigger the change from primary to secondary and back
  65. blob_client.retry_callback = retry_callback
  66. # Download the file
  67. blob_client.get_blob_to_path(container_name, image_to_upload,
  68. str.replace(full_path_to_file, ".png", "Copy.png"))
  69. # Set the application to pause at 200 unit intervals to implement simulated failures
  70. if i == 200 or i == 400 or i == 600 or i == 800:
  71. sys.stdout.write("\nPress the Enter key to resume")
  72. sys.stdout.flush()
  73. if sys.version_info[0] < 3:
  74. raw_input()
  75. else:
  76. input()
  77. except Exception as ex:
  78. print(ex)
  79. finally:
  80. # Force an exists call to succeed by resetting the status
  81. blob_client.response_callback = response_callback
  82. # Clean up resources
  83. blob_client.delete_container(container_name)