Skip to content

Commit f0a1656

Browse files
authored
Location mode example (Azure#23094)
* Started outlining example * Finished and doc'ed example * minor cleanup * Deleted extra file * Pr feedback * ci fix
1 parent c84de5d commit f0a1656

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

sdk/storage/azure-storage-blob/migrationGuides/V8_V12.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,10 @@ final BlockBlobClient blobClient = new BlobClientBuilder()
306306
OutputStream blobOutputStream = blobClient.getBlobOutputStream();
307307
// insert your method of choice to write to an OutputStream
308308
```
309+
310+
## Miscellaneous
311+
- Examples for how to approximate v8 StorageEvent behavior can be found [here][storage event sample].
312+
- Examples for how to approximate v8 LocationMode behavior can be found [here][location mode sample].
313+
314+
[storage event sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/StorageEventExample.java
315+
[location mode sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/LocationModeExample.java
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.storage.blob;
5+
6+
import com.azure.storage.common.policy.RequestRetryOptions;
7+
8+
import java.time.Duration;
9+
10+
11+
/**
12+
* This example shows how to approximate the LocationMode behavior from the track 1 SDK. It is a general translation to
13+
* achieve roughly the same results, but it is not an identical implementation. It may be modified to suit the use case.
14+
* For more information on redundant storage, see here:
15+
* https://docs.microsoft.com/en-us/azure/storage/common/storage-redundancy
16+
*
17+
* In a sense, the track 2 SDK is always primary-only or primary-then-secondary. However, by passing the secondary
18+
* endpoint as the primary and the primary as the secondary, the behavior of secondary-only or secondary-then-primary
19+
* can be achieved. To avoid confusion of terms in this example, 'preferred' and 'fallback' will refer to the location
20+
* that is tried by the client first and then second respectively, whereas 'primary' and 'secondary' will refer to the
21+
* Storage concept of primary read-write storage and back-up/redundant/read-only storage respectively.
22+
*
23+
* The general pattern is to create a BlobClient and pass the preferred location to the builder as the endpoint. To
24+
* configure a fallback location, set it as the `secondaryEndpoint` on {@link RequestRetryOptions} and pass the
25+
* configured options to {@link BlobClientBuilder#retryOptions(RequestRetryOptions)}. Switching LocationMode requires
26+
* using a different client that is configured for the new request behavior. In this case, concurrency control should
27+
* be carefully considered to prevent race conditions.
28+
*
29+
* Requests will always go first to the preferred location passed as the endpoint. If a request must be retried, it will
30+
* and the error indicates it may be helped by checking the fallback, a request will immediately be reissued to the
31+
* fallback. If that also fails and still a retry may be helpful, the client will wait for a backoff period specified by
32+
* the retry options before retrying the initial location again.
33+
*
34+
* The client does not internally track the LocationMode or read it from an object that is passed because of how that
35+
* might cause race conditions if it is shared between clients.
36+
*
37+
* Each of the clients constructed in this sample will have behavior according to the variable name. This sample does
38+
* not demonstrate meaningful independent behavior, so running it will do nothing, but these clients can be copied and
39+
* used as a component in other code.
40+
*
41+
* This example can be combined with the StorageEventExample to approximate the Circuit Breaker RAGRS sample here:
42+
* https://github.com/Azure-Samples/storage-dotnet-circuit-breaker-ha-ra-grs/blob/master/storage-dotnet-circuit-breaker-ha-ra-grs/Program.cs
43+
* In this case, in the StorageEvent callback, rather than switching the LocationMode on the DefaultRequestOptions, the
44+
* client should be swapped out to the client with the appropriate LocationMode and the request reissued, alternating
45+
* between a primary only and secondary only client.
46+
*
47+
* The main areas of divergence from the original LocationMode behavior are:
48+
* - There is no LocationMode type
49+
* - The v12 analogue of LocationMode is configured at client build time and is static for a given client; a new client
50+
* must be used if different location behavior is desired.
51+
* - Changing LocationMode entails changing the client being used to issue requests
52+
*/
53+
public class LocationModeExample {
54+
55+
public static void main(String[] args) {
56+
String primaryEndpoint = "<primary-endpoint>";
57+
String secondaryEndpoint = "<secondary-endpoint>";
58+
59+
BlobClient primaryOnlyClient;
60+
BlobClient secondaryOnlyClient;
61+
BlobClient primaryThenSecondaryClient;
62+
BlobClient secondaryThenPrimaryClient;
63+
64+
BlobClientBuilder builder = new BlobClientBuilder()
65+
.containerName("<container-name>")
66+
.blobName("<blob-name>");
67+
68+
/*
69+
This could be refactored into a helper methods, but it is written out explicitly here for clarity and ease of
70+
comparison.
71+
Null in all cases indicates accepting the default value.
72+
A distinct set of options must be created for each client to prevent overwriting the options held by another
73+
client.
74+
*/
75+
// Create a primary only client by passing the primary endpoint as the preferred and passing no fallback.
76+
RequestRetryOptions primaryOnlyRetryOptions = new RequestRetryOptions(null, null, (Duration) null, null, null,
77+
null);
78+
primaryOnlyClient = builder
79+
.endpoint(primaryEndpoint)
80+
.retryOptions(primaryOnlyRetryOptions)
81+
.buildClient();
82+
83+
// Create a secondary only client by passing the secondary as the preferred and passing no fallback.
84+
RequestRetryOptions secondaryOnlyRetryOptions = new RequestRetryOptions(null, null, (Duration) null, null, null,
85+
null);
86+
secondaryOnlyClient = builder
87+
.endpoint(secondaryEndpoint)
88+
.retryOptions(secondaryOnlyRetryOptions)
89+
.buildClient();
90+
91+
// Create a primary then secondary by passing a primary as the preferred and secondary as a fallback.
92+
RequestRetryOptions primaryThenSecondaryRetryOptions = new RequestRetryOptions(null, null, (Duration) null,
93+
null, null, secondaryEndpoint);
94+
primaryThenSecondaryClient = builder
95+
.endpoint(primaryEndpoint)
96+
.retryOptions(primaryThenSecondaryRetryOptions)
97+
.buildClient();
98+
99+
// Create a secondary then primary by passing a secondary as the preferred and a primary as a fallback.
100+
RequestRetryOptions secondaryThenPrimaryRetryOptions = new RequestRetryOptions(null, null, (Duration) null,
101+
null, null, primaryEndpoint);
102+
secondaryThenPrimaryClient = builder
103+
.endpoint(secondaryEndpoint)
104+
.retryOptions(secondaryThenPrimaryRetryOptions)
105+
.buildClient();
106+
}
107+
}

0 commit comments

Comments
 (0)