-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Location mode example #23094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Location mode example #23094
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7358abc
Started outlining example
rickle-msft d991346
Finished and doc'ed example
rickle-msft 948ac6a
minor cleanup
rickle-msft 9c5ec7f
Deleted extra file
rickle-msft d781252
Merge remote-tracking branch 'upstream/main' into LocationModeExample
rickle-msft c23dca9
Pr feedback
rickle-msft 612fe53
ci fix
rickle-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...orage/azure-storage-blob/src/samples/java/com/azure/storage/blob/LocationModeExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.storage.blob; | ||
|
|
||
| import com.azure.storage.common.policy.RequestRetryOptions; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
|
|
||
| /** | ||
| * This example shows how to approximate the LocationMode behavior from the track 1 SDK. It is a general translation to | ||
| * achieve roughly the same results, but it is not an identical implementation. It may be modified to suit the use case. | ||
| * For more information on redundant storage, see here: | ||
| * https://docs.microsoft.com/en-us/azure/storage/common/storage-redundancy | ||
| * | ||
| * In a sense, the track 2 SDK is always primary-only or primary-then-secondary. However, by passing the secondary | ||
| * endpoint as the primary and the primary as the secondary, the behavior of secondary-only or secondary-then-primary | ||
| * can be achieved. To avoid confusion of terms in this example, 'preferred' and 'fallback' will refer to the location | ||
| * that is tried by the client first and then second respectively, whereas 'primary' and 'secondary' will refer to the | ||
| * Storage concept of primary read-write storage and back-up/redundant/read-only storage respectively. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this link to docs explaining back-up/redundant/read-only |
||
| * | ||
| * The general pattern is to create a BlobClient and pass the preferred location to the builder as the endpoint. To | ||
| * configure a fallback location, set it as the `secondaryEndpoint` on {@link RequestRetryOptions} and pass the | ||
| * configured options to {@link BlobClientBuilder#retryOptions(RequestRetryOptions)}. Switching LocationMode requires | ||
| * using a different client that is configured for the new request behavior. In this case, concurrency control should | ||
| * be carefully considered to prevent race conditions. | ||
| * | ||
| * Requests will always go first to the preferred location passed as the endpoint. If a request must be retried, it will | ||
| * and the error indicates it may be helped by checking the fallback, a request will immediately be reissued to the | ||
| * fallback. If that also fails and still a retry may be helpful, the client will wait for a backoff period specified by | ||
| * the retry options before retrying the initial location again. | ||
| * | ||
| * The client does not internally track the LocationMode or read it from an object that is passed because of how that | ||
| * might cause race conditions if it is shared between clients. | ||
| * | ||
| * Each of the clients constructed in this sample will have behavior according to the variable name. This sample does | ||
| * not demonstrate meaningful independent behavior, so running it will do nothing, but these clients can be copied and | ||
| * used as a component in other code. | ||
| * | ||
| * This example can be combined with the StorageEventExample to approximate the Circuit Breaker RAGRS sample here: | ||
| * https://github.com/Azure-Samples/storage-dotnet-circuit-breaker-ha-ra-grs/blob/master/storage-dotnet-circuit-breaker-ha-ra-grs/Program.cs | ||
| * In this case, in the StorageEvent callback, rather than switching the LocationMode on the DefaultRequestOptions, the | ||
| * client should be swapped out to the client with the appropriate LocationMode and the request reissued, alternating | ||
| * between a primary only and secondary only client. | ||
| * | ||
| * The main areas of divergence from the original LocationMode behavior are: | ||
| * - There is no LocationMode type | ||
| * - The v12 analogue of LocationMode is configured at client build time and is static for a given client; a new client | ||
| * must be used if different location behavior is desired. | ||
| * - Changing LocationMode entails changing the client being used to issue requests | ||
| */ | ||
| public class LocationModeExample { | ||
|
|
||
| public static void main(String[] args) { | ||
| String primaryEndpoint = "<primary-endpoint>"; | ||
| String secondaryEndpoint = "<secondary-endpoint>"; | ||
|
|
||
| BlobClient primaryOnlyClient; | ||
| BlobClient secondaryOnlyClient; | ||
| BlobClient primaryThenSecondaryClient; | ||
| BlobClient secondaryThenPrimaryClient; | ||
|
|
||
| BlobClientBuilder builder = new BlobClientBuilder() | ||
| .containerName("<container-name>") | ||
| .blobName("<blob-name>"); | ||
|
|
||
| /* | ||
| This could be refactored into a helper methods, but it is written out explicitly here for clarity and ease of | ||
| comparison. | ||
| Null in all cases indicates accepting the default value. | ||
| A distinct set of options must be created for each client to prevent overwriting the options held by another | ||
| client. | ||
| */ | ||
| // Create a primary only client by passing the primary endpoint as the preferred and passing no fallback. | ||
| RequestRetryOptions primaryOnlyRetryOptions = new RequestRetryOptions(null, null, (Duration) null, null, null, | ||
| null); | ||
| primaryOnlyClient = builder | ||
| .endpoint(primaryEndpoint) | ||
| .retryOptions(primaryOnlyRetryOptions) | ||
| .buildClient(); | ||
|
|
||
| // Create a secondary only client by passing the secondary as the preferred and passing no fallback. | ||
| RequestRetryOptions secondaryOnlyRetryOptions = new RequestRetryOptions(null, null, (Duration) null, null, null, | ||
| null); | ||
| secondaryOnlyClient = builder | ||
| .endpoint(secondaryEndpoint) | ||
| .retryOptions(secondaryOnlyRetryOptions) | ||
| .buildClient(); | ||
|
|
||
| // Create a primary then secondary by passing a primary as the preferred and secondary as a fallback. | ||
| RequestRetryOptions primaryThenSecondaryRetryOptions = new RequestRetryOptions(null, null, (Duration) null, | ||
| null, null, secondaryEndpoint); | ||
| primaryThenSecondaryClient = builder | ||
| .endpoint(primaryEndpoint) | ||
| .retryOptions(primaryThenSecondaryRetryOptions) | ||
| .buildClient(); | ||
|
|
||
| // Create a secondary then primary by passing a secondary as the preferred and a primary as a fallback. | ||
| RequestRetryOptions secondaryThenPrimaryRetryOptions = new RequestRetryOptions(null, null, (Duration) null, | ||
| null, null, primaryEndpoint); | ||
| secondaryThenPrimaryClient = builder | ||
| .endpoint(secondaryEndpoint) | ||
| .retryOptions(secondaryThenPrimaryRetryOptions) | ||
| .buildClient(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing license header