Skip to content

Commit e1acee5

Browse files
authored
[Storage] Cache specialized clients (Azure#24615)
* Revert "Increment version for core releases (Azure#24504)" This reverts commit 8a10ae6 * recording. * Revert "Revert "Increment version for core releases (Azure#24504)"" This reverts commit ad5155d. * cache specialized client. * checkstyle. * Update BlobAPITest.groovy
1 parent 9e0ef9b commit e1acee5

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobAsyncClient.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public class BlobAsyncClient extends BlobAsyncClientBase {
101101
static final long BLOB_MAX_UPLOAD_BLOCK_SIZE = 4000L * Constants.MB;
102102
private final ClientLogger logger = new ClientLogger(BlobAsyncClient.class);
103103

104+
private BlockBlobAsyncClient blockBlobAsyncClient;
105+
private AppendBlobAsyncClient appendBlobAsyncClient;
106+
private PageBlobAsyncClient pageBlobAsyncClient;
107+
104108
/**
105109
* Protected constructor for use by {@link BlobClientBuilder}.
106110
*
@@ -233,7 +237,10 @@ public BlobAsyncClient getCustomerProvidedKeyAsyncClient(CustomerProvidedKey cus
233237
* @return A {@link AppendBlobAsyncClient} associated with this blob.
234238
*/
235239
public AppendBlobAsyncClient getAppendBlobAsyncClient() {
236-
return prepareBuilder().buildAppendBlobAsyncClient();
240+
if (appendBlobAsyncClient == null) {
241+
appendBlobAsyncClient = prepareBuilder().buildAppendBlobAsyncClient();
242+
}
243+
return appendBlobAsyncClient;
237244
}
238245

239246
/**
@@ -242,7 +249,10 @@ public AppendBlobAsyncClient getAppendBlobAsyncClient() {
242249
* @return A {@link BlockBlobAsyncClient} associated with this blob.
243250
*/
244251
public BlockBlobAsyncClient getBlockBlobAsyncClient() {
245-
return prepareBuilder().buildBlockBlobAsyncClient();
252+
if (blockBlobAsyncClient == null) {
253+
blockBlobAsyncClient = prepareBuilder().buildBlockBlobAsyncClient();
254+
}
255+
return blockBlobAsyncClient;
246256
}
247257

248258
/**
@@ -251,7 +261,10 @@ public BlockBlobAsyncClient getBlockBlobAsyncClient() {
251261
* @return A {@link PageBlobAsyncClient} associated with this blob.
252262
*/
253263
public PageBlobAsyncClient getPageBlobAsyncClient() {
254-
return prepareBuilder().buildPageBlobAsyncClient();
264+
if (pageBlobAsyncClient == null) {
265+
pageBlobAsyncClient = prepareBuilder().buildPageBlobAsyncClient();
266+
}
267+
return pageBlobAsyncClient;
255268
}
256269

257270
private SpecializedBlobClientBuilder prepareBuilder() {

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobClient.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public class BlobClient extends BlobClientBase {
7575

7676
private final BlobAsyncClient client;
7777

78+
private BlockBlobClient blockBlobClient;
79+
private AppendBlobClient appendBlobClient;
80+
private PageBlobClient pageBlobClient;
81+
7882
/**
7983
* Protected constructor for use by {@link BlobClientBuilder}.
8084
* @param client the async blob client
@@ -136,9 +140,12 @@ public BlobClient getCustomerProvidedKeyClient(CustomerProvidedKey customerProvi
136140
* @return A {@link AppendBlobClient} associated with this blob.
137141
*/
138142
public AppendBlobClient getAppendBlobClient() {
139-
return new SpecializedBlobClientBuilder()
140-
.blobClient(this)
141-
.buildAppendBlobClient();
143+
if (appendBlobClient == null) {
144+
appendBlobClient = new SpecializedBlobClientBuilder()
145+
.blobClient(this)
146+
.buildAppendBlobClient();
147+
}
148+
return appendBlobClient;
142149
}
143150

144151
/**
@@ -147,9 +154,12 @@ public AppendBlobClient getAppendBlobClient() {
147154
* @return A {@link BlockBlobClient} associated with this blob.
148155
*/
149156
public BlockBlobClient getBlockBlobClient() {
150-
return new SpecializedBlobClientBuilder()
151-
.blobClient(this)
152-
.buildBlockBlobClient();
157+
if (blockBlobClient == null) {
158+
blockBlobClient = new SpecializedBlobClientBuilder()
159+
.blobClient(this)
160+
.buildBlockBlobClient();
161+
}
162+
return blockBlobClient;
153163
}
154164

155165
/**
@@ -158,9 +168,12 @@ public BlockBlobClient getBlockBlobClient() {
158168
* @return A {@link PageBlobClient} associated with this blob.
159169
*/
160170
public PageBlobClient getPageBlobClient() {
161-
return new SpecializedBlobClientBuilder()
162-
.blobClient(this)
163-
.buildPageBlobClient();
171+
if (pageBlobClient == null) {
172+
pageBlobClient = new SpecializedBlobClientBuilder()
173+
.blobClient(this)
174+
.buildPageBlobClient();
175+
}
176+
return pageBlobClient;
164177
}
165178

166179
/**

sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/BlobAPITest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ import java.util.concurrent.atomic.AtomicInteger
7373

7474
class BlobAPITest extends APISpec {
7575
BlobClient bc
76+
BlobAsyncClient bcAsync
7677
String blobName
7778

7879
def setup() {
7980
blobName = generateBlobName()
8081
bc = cc.getBlobClient(blobName)
82+
bcAsync = ccAsync.getBlobAsyncClient(blobName)
8183
bc.getBlockBlobClient().upload(data.defaultInputStream, data.defaultDataSize)
8284
}
8385

@@ -3183,4 +3185,14 @@ class BlobAPITest extends APISpec {
31833185
notThrown(BlobStorageException)
31843186
response.getHeaders().getValue("x-ms-version") == "2017-11-09"
31853187
}
3188+
3189+
def "Specialized child client gets cached"() {
3190+
expect:
3191+
bc.getBlockBlobClient() == bc.getBlockBlobClient()
3192+
bc.getAppendBlobClient() == bc.getAppendBlobClient()
3193+
bc.getPageBlobClient() == bc.getPageBlobClient()
3194+
bcAsync.getBlockBlobAsyncClient() == bcAsync.getBlockBlobAsyncClient()
3195+
bcAsync.getAppendBlobAsyncClient() == bcAsync.getAppendBlobAsyncClient()
3196+
bcAsync.getPageBlobAsyncClient() == bcAsync.getPageBlobAsyncClient()
3197+
}
31863198
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"networkCallRecords" : [ {
3+
"Method" : "PUT",
4+
"Uri" : "https://REDACTED.blob.core.windows.net/c4cb2b070c4cb2b0760d01980b5fbbd116ea5420dbb6?restype=container",
5+
"Headers" : {
6+
"x-ms-version" : "2020-10-02",
7+
"User-Agent" : "azsdk-java-azure-storage-blob/12.15.0-beta.1 (11; Windows 10; 10.0)",
8+
"x-ms-client-request-id" : "dfcc68cd-e621-4df0-ad8b-877bbbc6f8bf"
9+
},
10+
"Response" : {
11+
"content-length" : "0",
12+
"x-ms-version" : "2020-10-02",
13+
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
14+
"eTag" : "0x8D989A320D892D6",
15+
"Last-Modified" : "Thu, 07 Oct 2021 14:59:57 GMT",
16+
"retry-after" : "0",
17+
"StatusCode" : "201",
18+
"x-ms-request-id" : "eeb0684a-201e-005e-618b-bb2151000000",
19+
"x-ms-client-request-id" : "dfcc68cd-e621-4df0-ad8b-877bbbc6f8bf",
20+
"Date" : "Thu, 07 Oct 2021 14:59:57 GMT"
21+
},
22+
"Exception" : null
23+
}, {
24+
"Method" : "PUT",
25+
"Uri" : "https://REDACTED.blob.core.windows.net/c4cb2b070c4cb2b0760d01980b5fbbd116ea5420dbb6/c4cb2b071c4cb2b0760d56405bb0ce39faa19449d9f2",
26+
"Headers" : {
27+
"x-ms-version" : "2020-10-02",
28+
"User-Agent" : "azsdk-java-azure-storage-blob/12.15.0-beta.1 (11; Windows 10; 10.0)",
29+
"x-ms-client-request-id" : "577feec8-2ac2-45e2-b191-494f95feddcb",
30+
"Content-Type" : "application/octet-stream"
31+
},
32+
"Response" : {
33+
"content-length" : "0",
34+
"x-ms-version" : "2020-10-02",
35+
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
36+
"x-ms-content-crc64" : "6RYQPwaVsyQ=",
37+
"Last-Modified" : "Thu, 07 Oct 2021 14:59:57 GMT",
38+
"retry-after" : "0",
39+
"StatusCode" : "201",
40+
"x-ms-request-server-encrypted" : "true",
41+
"Date" : "Thu, 07 Oct 2021 14:59:57 GMT",
42+
"Content-MD5" : "wh+Wm18D0z1D4E+PE252gg==",
43+
"eTag" : "0x8D989A321051AC5",
44+
"x-ms-request-id" : "eeb069e3-201e-005e-588b-bb2151000000",
45+
"x-ms-client-request-id" : "577feec8-2ac2-45e2-b191-494f95feddcb"
46+
},
47+
"Exception" : null
48+
} ],
49+
"variables" : [ "c4cb2b070c4cb2b0760d01980b5fbbd116ea5420dbb6", "c4cb2b071c4cb2b0760d56405bb0ce39faa19449d9f2" ]
50+
}

0 commit comments

Comments
 (0)