-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBulkWriteService.java
More file actions
405 lines (360 loc) · 14.7 KB
/
BulkWriteService.java
File metadata and controls
405 lines (360 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime;
import com.google.protobuf.ByteString;
import io.greptime.common.TimeoutCompletableFuture;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.arrow.flight.BulkFlightClient.ClientStreamListener;
import org.apache.arrow.flight.BulkFlightClient.PutListener;
import org.apache.arrow.flight.CallOption;
import org.apache.arrow.flight.FlightDescriptor;
import org.apache.arrow.flight.PutResult;
import org.apache.arrow.flight.grpc.StatusUtils;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.util.AutoCloseables;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.message.IpcOption;
import org.apache.arrow.vector.types.pojo.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* BulkWriteService is a specialized class for efficiently writing data to the server in bulk operations.
*
* It encapsulates two key components:
* 1. A ClientStreamListener that manages and controls the data upload process
* 2. A VectorSchemaRoot that contains the structured data to be transmitted
*
* This service handles the serialization and transfer of Arrow-formatted data in an optimized manner,
* providing a streamlined interface for bulk write operations.
*/
public class BulkWriteService implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(BulkWriteService.class);
private final AtomicLong idGenerator = new AtomicLong(0);
private final BulkWriteManager manager;
private final BufferAllocator allocator;
private final VectorSchemaRoot root;
private final ClientStreamListener listener;
private final AsyncPutListener metadataListener;
private final long timeoutMs;
/**
* Constructs a new BulkWriteService.
*
* @param manager The BulkWriteManager that manages this service
* @param allocator The BufferAllocator for memory management
* @param schema The Arrow schema defining the data structure
* @param descriptor The FlightDescriptor identifying the data stream
* @param timeoutMs The timeout in milliseconds for operations
* @param options Additional call options for the Flight client
*/
public BulkWriteService(
BulkWriteManager manager,
BufferAllocator allocator,
Schema schema,
FlightDescriptor descriptor,
long timeoutMs,
CallOption... options) {
this.manager = manager;
this.allocator = allocator;
this.root = manager.createSchemaRoot(schema);
this.metadataListener = new AsyncPutListener();
this.listener = manager.startPut(descriptor, this.metadataListener, options);
this.timeoutMs = timeoutMs;
}
/**
* Starts the bulk write stream with default IPC options.
*/
public void start() {
LOG.debug("Starting bulk write stream with default IPC options");
this.listener.start(this.root, this.manager.newDefaultDictionaryProvider());
}
/**
* Starts the bulk write stream with custom IPC options.
*
* @param ipcOption The IPC options to use for the Arrow data transfer
*/
public void start(IpcOption ipcOption) {
LOG.debug("Starting bulk write stream with custom IPC options: {}", ipcOption);
this.listener.start(this.root, this.manager.newDefaultDictionaryProvider(), ipcOption);
}
/**
* Gets the VectorSchemaRoot of the bulk stream.
*
* @return The VectorSchemaRoot containing the data to be written
*/
public VectorSchemaRoot getRoot() {
return this.root;
}
/**
* Enables zero-copy write mode for improved performance.
* This avoids unnecessary memory copies when sending data.
*/
public void tryUseZeroCopyWrite() {
LOG.info("Enabling zero-copy write mode for improved performance");
this.listener.setUseZeroCopy(true);
}
/**
* Checks if the stream is ready to send the next message.
*
* @return true if the stream is ready to send the next message, false otherwise
*/
public boolean isStreamReady() {
return this.listener.isReady();
}
/**
* Sends the current contents of the associated VectorSchemaRoot to the server.
* This method will:
* 1. Generate a unique ID for the request
* 2. Create a future to track the operation
* 3. Prepare and send the data with metadata
* 4. Clear the VectorSchemaRoot for the next batch
*
* @return A PutStage object containing the future and the number of in-flight requests
*/
public PutStage putNext() {
long id = nextId();
long totalRowCount = this.root.getRowCount();
LOG.debug("Starting putNext operation [id={}], total row count: {}", id, totalRowCount);
// Create future with timeout and attach to listener
IdentifiableCompletableFuture future = new IdentifiableCompletableFuture(id, this.timeoutMs);
this.metadataListener.attach(id, future);
// Prepare metadata buffer
byte[] metadata = new Metadata.RequestMetadata(id).toJsonBytesUtf8();
ArrowBuf metadataBuf = null;
try {
// The buffer will be closed in the putNext method, but if an error occurs during execution,
// we need to close it ourselves in the catch block to prevent memory leaks.
metadataBuf = this.allocator.buffer(metadata.length);
metadataBuf.writeBytes(metadata);
// Send data to the server
LOG.debug("Sending data to server [id={}]", id);
this.listener.putNext(metadataBuf);
int inFlightCount = this.metadataListener.numInFlight();
LOG.debug("Data sent successfully [id={}], in-flight requests: {}", id, inFlightCount);
return new PutStage(future, inFlightCount);
} catch (Throwable t) {
// Close the metadata buffer on error
if (metadataBuf != null) {
metadataBuf.close();
}
throw t;
} finally {
// Clear the root to prepare for next batch
this.root.clear();
LOG.debug("Cleared root for next batch [id={}], previous row count: {}", id, totalRowCount);
}
}
/**
* Completes the bulk write operation, indicating that transmission is finished.
* This signals to the server that no more data will be sent.
*/
public void completed() {
LOG.info("Completing bulk write operation, signaling end of transmission");
this.listener.completed();
}
/**
* Waits for the stream to finish processing on the server side.
* This method must be called to be notified of any errors that may have
* occurred during the upload process.
*/
public void waitServerCompleted() {
LOG.info("Waiting for server to complete processing");
this.listener.getResult();
}
@Override
public void close() throws Exception {
LOG.info("Closing BulkWriteService resources");
AutoCloseables.close(this.root, this.manager);
}
private long nextId() {
long id;
do {
id = this.idGenerator.incrementAndGet();
} while (id == 0); // Skip ID 0 as it's reserved for special cases
return id;
}
/**
* Represents the state of a put operation, containing both the future for tracking
* completion and the current count of in-flight requests.
*/
public static class PutStage {
private final CompletableFuture<Integer> future;
private final int numInFlight;
/**
* Creates a new PutStage.
*
* @param future The future that will be completed when the operation finishes
* @param numInFlight The current number of in-flight requests
*/
public PutStage(CompletableFuture<Integer> future, int numInFlight) {
this.future = future;
this.numInFlight = numInFlight;
}
/**
* Gets the future that will be completed when the operation finishes.
*
* @return The CompletableFuture for this operation
*/
public CompletableFuture<Integer> future() {
return this.future;
}
/**
* Gets the number of in-flight requests at the time this PutStage was created.
*
* @return The count of in-flight requests
*/
public int numInFlight() {
return this.numInFlight;
}
}
/**
* A CompletableFuture with an associated ID for tracking purposes.
* Extends TimeoutCompletableFuture to support operation timeouts.
*/
static class IdentifiableCompletableFuture extends TimeoutCompletableFuture<Integer> {
private final long id;
/**
* Creates a new IdentifiableCompletableFuture.
*
* @param id The unique identifier for this future
* @param timeoutMs The timeout in milliseconds
*/
public IdentifiableCompletableFuture(long id, long timeoutMs) {
super(timeoutMs, TimeUnit.MILLISECONDS);
this.id = id;
}
/**
* Gets the unique identifier for this future.
*
* @return The ID of this future
*/
public long getId() {
return this.id;
}
}
/**
* Listener for handling asynchronous responses from the server during bulk write operations.
* Manages the lifecycle of in-flight requests and their associated futures.
*/
static class AsyncPutListener implements PutListener {
private final ConcurrentMap<Long, IdentifiableCompletableFuture> futuresInFlight;
private final CompletableFuture<Void> completed;
/**
* Creates a new AsyncPutListener.
*/
AsyncPutListener() {
this.futuresInFlight = new ConcurrentHashMap<>();
this.completed = new CompletableFuture<>();
this.completed.whenComplete((r, t) -> {
if (t != null) {
// Also complete all the futures with the same exception
for (IdentifiableCompletableFuture future : this.futuresInFlight.values()) {
future.completeExceptionally(t);
}
}
// When completed, clear the futuresInFlight
this.futuresInFlight.clear();
});
}
/**
* Attaches a future to this listener for tracking.
*
* @param id The unique identifier for the request
* @param future The future to track
*/
public void attach(long id, IdentifiableCompletableFuture future) {
this.futuresInFlight.put(id, future);
future.whenComplete((r, t) -> {
// Remove the future from the map when it's completed
this.futuresInFlight.remove(id);
if (t != null) {
LOG.error("Put operation failed [id={}]: {}", id, t.getMessage(), t);
if (!(t instanceof TimeoutCompletableFuture.FutureDeadlineExceededException)) {
// If a put next operation fails, we complete the future with the exception
// and the stream will be terminated immediately to prevent further operations
onError(t);
}
} else {
LOG.debug("Put operation succeeded [id={}], affected rows: {}", id, r);
}
});
future.scheduleTimeout();
if (LOG.isDebugEnabled()) {
LOG.debug("Attached future [id={}], current in-flight count: {}", id, this.futuresInFlight.size());
}
}
/**
* Gets the number of currently in-flight requests.
*
* @return The count of in-flight requests
*/
public int numInFlight() {
return this.futuresInFlight.size();
}
@Override
public void onNext(PutResult val) {
ArrowBuf metadata = val.getApplicationMetadata();
if (metadata == null) {
LOG.warn("Received PutResult with null metadata");
return;
}
String metadataString = ByteString.copyFrom(metadata.nioBuffer()).toStringUtf8();
Metadata.ResponseMetadata responseMetadata = Metadata.ResponseMetadata.fromJson(metadataString);
long requestId = responseMetadata.getRequestId();
int affectedRows = responseMetadata.getAffectedRows();
LOG.debug("Received response [id={}], affected rows: {}", requestId, affectedRows);
IdentifiableCompletableFuture future = this.futuresInFlight.get(requestId);
if (future != null) {
future.complete(affectedRows);
} else if (requestId != 0) { // 0 is reserved for special cases
LOG.warn("A timeout response [id={}] finally received", requestId);
}
}
@Override
public void onError(Throwable t) {
LOG.error("Stream error occurred: {}", t.getMessage(), t);
this.completed.completeExceptionally(StatusUtils.fromThrowable(t));
}
@Override
public final void onCompleted() {
LOG.info("Server signaled stream completion");
this.completed.complete(null);
}
@Override
public boolean isCancelled() {
return this.completed.isCancelled();
}
@Override
public boolean isCompletedExceptionally() {
return this.completed.isCompletedExceptionally();
}
@Override
public void getResult() {
try {
this.completed.get();
} catch (InterruptedException e) {
throw StatusUtils.fromThrowable(e);
} catch (ExecutionException e) {
throw StatusUtils.fromThrowable(e.getCause());
}
}
}
}