Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Commit 86eb13c

Browse files
committed
docs(samples): add zonal bucket pre-warmed writer pool sample
Adds OptimizeWriteLatencyPool sample (region tag: storage_optimize_write_latency_pool) demonstrating a pre-warmed pool of appendable object writers with CloseAction.CLOSE_WITHOUT_FINALIZING to avoid object creation and finalization metadata overhead on the critical write path. Verified with both mock unit tests and live integration testing against a Rapid (zonal) bucket in us-central1-a: Running live Java test against bucket=<zonal-bucket>, prefix=live_java_pool_1790087142149 Java read back: "0123456789", pool size after refill: 3 Time: 3.94s OK (1 test)
1 parent 679cddd commit 86eb13c

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_optimize_write_latency_pool]
20+
21+
import com.google.cloud.storage.BlobAppendableUpload.AppendableUploadWriteableByteChannel;
22+
import com.google.cloud.storage.BlobAppendableUploadConfig;
23+
import com.google.cloud.storage.BlobAppendableUploadConfig.CloseAction;
24+
import com.google.cloud.storage.BlobId;
25+
import com.google.cloud.storage.BlobInfo;
26+
import com.google.cloud.storage.BlobReadSession;
27+
import com.google.cloud.storage.RangeSpec;
28+
import com.google.cloud.storage.ReadProjectionConfigs;
29+
import com.google.cloud.storage.Storage;
30+
import com.google.cloud.storage.StorageOptions;
31+
import java.nio.ByteBuffer;
32+
import java.nio.charset.StandardCharsets;
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.List;
36+
import java.util.concurrent.ExecutorService;
37+
import java.util.concurrent.Executors;
38+
import java.util.concurrent.Future;
39+
import java.util.concurrent.TimeUnit;
40+
41+
public class OptimizeWriteLatencyPool {
42+
public static void optimizeWriteLatencyPool(String bucketName, String keyPrefix)
43+
throws Exception {
44+
// The ID of your GCS zonal bucket
45+
// String bucketName = "your-unique-bucket-name";
46+
47+
// The prefix for your pooled GCS objects
48+
// String keyPrefix = "pooled-object";
49+
50+
int poolSize = 3;
51+
String nextObjectName = keyPrefix + "_" + poolSize;
52+
byte[] payload = "0123456789".getBytes(StandardCharsets.UTF_8);
53+
ExecutorService executor = Executors.newSingleThreadExecutor();
54+
55+
try (Storage storage = StorageOptions.grpc().build().getService()) {
56+
BlobAppendableUploadConfig config =
57+
BlobAppendableUploadConfig.of().withCloseAction(CloseAction.CLOSE_WITHOUT_FINALIZING);
58+
59+
// 1. Init pool: Flushing incurs operation charges, so size the pool carefully.
60+
List<AppendableUploadWriteableByteChannel> pool =
61+
Collections.synchronizedList(new ArrayList<>(poolSize));
62+
for (int i = 0; i < poolSize; i++) {
63+
BlobInfo info = BlobInfo.newBuilder(bucketName, keyPrefix + "_" + i).build();
64+
AppendableUploadWriteableByteChannel ch =
65+
storage
66+
.blobAppendableUpload(info, config, Storage.BlobWriteOption.doesNotExist())
67+
.open();
68+
ch.flush(); // Forces 0-byte object creation in the background.
69+
pool.add(ch);
70+
}
71+
72+
// 2. Write: Pop a pre-warmed writer and commit with flush() (~1-2 ms)
73+
// instead of blocking on close().
74+
AppendableUploadWriteableByteChannel channel = pool.remove(0);
75+
channel.write(ByteBuffer.wrap(payload));
76+
channel.flush();
77+
78+
// 3. Pool maintenance (run asynchronously off the critical write path):
79+
// Close the used channel without finalizing, refill the pool, and discard stale writers.
80+
Future<Void> maintenanceFuture =
81+
executor.submit(
82+
() -> {
83+
channel.closeWithoutFinalizing();
84+
BlobInfo nextInfo = BlobInfo.newBuilder(bucketName, nextObjectName).build();
85+
AppendableUploadWriteableByteChannel replacement =
86+
storage
87+
.blobAppendableUpload(
88+
nextInfo, config, Storage.BlobWriteOption.doesNotExist())
89+
.open();
90+
replacement.flush();
91+
pool.add(replacement);
92+
return null;
93+
});
94+
95+
// 4. Read: Unfinalized objects are readable after flush().
96+
try (BlobReadSession readSession =
97+
storage
98+
.blobReadSession(BlobId.of(bucketName, keyPrefix + "_0"))
99+
.get(10, TimeUnit.SECONDS)) {
100+
byte[] bytes =
101+
readSession
102+
.readAs(
103+
ReadProjectionConfigs.asFutureBytes()
104+
.withRangeSpec(RangeSpec.of(0, payload.length)))
105+
.get();
106+
System.out.printf(
107+
"Read unfinalized object %s_0: %s%n",
108+
keyPrefix, new String(bytes, StandardCharsets.UTF_8));
109+
}
110+
111+
maintenanceFuture.get(10, TimeUnit.SECONDS);
112+
for (AppendableUploadWriteableByteChannel rem : pool) {
113+
rem.closeWithoutFinalizing();
114+
}
115+
} finally {
116+
executor.shutdown();
117+
}
118+
}
119+
}
120+
121+
// [END storage_optimize_write_latency_pool]

0 commit comments

Comments
 (0)