@@ -143,7 +143,7 @@ public boolean isStreamReady() {
143143 * @return A PutStage object containing the future and the number of in-flight requests
144144 */
145145 public PutStage putNext () {
146- long id = this . idGenerator . incrementAndGet ();
146+ long id = nextId ();
147147 long totalRowCount = this .root .getRowCount ();
148148
149149 LOG .debug ("Starting putNext operation [id={}], total row count: {}" , id , totalRowCount );
@@ -159,7 +159,7 @@ public PutStage putNext() {
159159 // The buffer will be closed in the putNext method, but if an error occurs during execution,
160160 // we need to close it ourselves in the catch block to prevent memory leaks.
161161 metadataBuf = this .allocator .buffer (metadata .length );
162- metadataBuf .setBytes ( 0 , metadata );
162+ metadataBuf .writeBytes ( metadata );
163163
164164 // Send data to the server
165165 LOG .debug ("Sending data to server [id={}]" , id );
@@ -207,6 +207,14 @@ public void close() throws Exception {
207207 AutoCloseables .close (this .root , this .manager );
208208 }
209209
210+ private long nextId () {
211+ long id ;
212+ do {
213+ id = this .idGenerator .incrementAndGet ();
214+ } while (id == 0 ); // Skip ID 0 as it's reserved for special cases
215+ return id ;
216+ }
217+
210218 /**
211219 * Represents the state of a put operation, containing both the future for tracking
212220 * completion and the current count of in-flight requests.
@@ -277,7 +285,7 @@ public long getId() {
277285 * Listener for handling asynchronous responses from the server during bulk write operations.
278286 * Manages the lifecycle of in-flight requests and their associated futures.
279287 */
280- class AsyncPutListener implements PutListener {
288+ static class AsyncPutListener implements PutListener {
281289 private final ConcurrentMap <Long , IdentifiableCompletableFuture > futuresInFlight ;
282290 private final CompletableFuture <Void > completed ;
283291
@@ -313,9 +321,11 @@ public void attach(long id, IdentifiableCompletableFuture future) {
313321
314322 if (t != null ) {
315323 LOG .error ("Put operation failed [id={}]: {}" , id , t .getMessage (), t );
316- // If a put next operation fails, we complete the future with the exception
317- // and the stream will be terminated immediately to prevent further operations
318- onError (t );
324+ if (!(t instanceof TimeoutCompletableFuture .FutureDeadlineExceededException )) {
325+ // If a put next operation fails, we complete the future with the exception
326+ // and the stream will be terminated immediately to prevent further operations
327+ onError (t );
328+ }
319329 } else {
320330 LOG .debug ("Put operation succeeded [id={}], affected rows: {}" , id , r );
321331 }
@@ -338,26 +348,24 @@ public int numInFlight() {
338348
339349 @ Override
340350 public void onNext (PutResult val ) {
341- try (ArrowBuf metadata = val .getApplicationMetadata ()) {
342- if (metadata == null ) {
343- LOG .warn ("Received PutResult with null metadata" );
344- return ;
345- }
346- String metadataString =
347- ByteString .copyFrom (metadata .nioBuffer ()).toStringUtf8 ();
348- Metadata .ResponseMetadata responseMetadata = Metadata .ResponseMetadata .fromJson (metadataString );
351+ ArrowBuf metadata = val .getApplicationMetadata ();
352+ if (metadata == null ) {
353+ LOG .warn ("Received PutResult with null metadata" );
354+ return ;
355+ }
356+ String metadataString = ByteString .copyFrom (metadata .nioBuffer ()).toStringUtf8 ();
357+ Metadata .ResponseMetadata responseMetadata = Metadata .ResponseMetadata .fromJson (metadataString );
349358
350- long requestId = responseMetadata .getRequestId ();
351- int affectedRows = responseMetadata .getAffectedRows ();
359+ long requestId = responseMetadata .getRequestId ();
360+ int affectedRows = responseMetadata .getAffectedRows ();
352361
353- LOG .debug ("Received response [id={}], affected rows: {}" , requestId , affectedRows );
362+ LOG .debug ("Received response [id={}], affected rows: {}" , requestId , affectedRows );
354363
355- IdentifiableCompletableFuture future = this .futuresInFlight .get (requestId );
356- if (future != null ) {
357- future .complete (affectedRows );
358- } else {
359- LOG .warn ("A timeout response [id={}] finally received" , requestId );
360- }
364+ IdentifiableCompletableFuture future = this .futuresInFlight .get (requestId );
365+ if (future != null ) {
366+ future .complete (affectedRows );
367+ } else if (requestId != 0 ) { // 0 is reserved for special cases
368+ LOG .warn ("A timeout response [id={}] finally received" , requestId );
361369 }
362370 }
363371
0 commit comments