private void seekingWriteAll(TableId table,List<InsertAllRequest.RowToInsert> elements,String topic,Set<Schema> schemas) throws BigQueryConnectException, InterruptedException {
  logger.debug("Seeking best batch size...");
  int currentIndex=0;
  int successfulCallCount=0;
  while (currentIndex < elements.size()) {
    int endIndex=Math.min(currentIndex + currentBatchSize,elements.size());
    List<InsertAllRequest.RowToInsert> currentBatchElements=elements.subList(currentIndex,endIndex);
    try {
      writer.writeRows(table,currentBatchElements,topic,schemas);
      successfulCallCount++;
      currentIndex=endIndex;
      if (currentBatchSize == MAXIMUM_BATCH_SIZE) {
        seeking=false;
        logger.debug("Best batch size found (max): {}",currentBatchSize);
        establishedWriteAll(table,elements.subList(currentIndex,elements.size()),topic,schemas);
        return;
      }
      if (currentIndex < elements.size()) {
        increaseBatchSize(table);
      }
    }
 catch (    BigQueryException exception) {
      if (isBatchSizeError(exception)) {
        decreaseBatchSize(table);
        if (successfulCallCount > 0) {
          seeking=false;
          logger.debug("Best batch size found (error if higher): {}",currentBatchSize);
          establishedWriteAll(table,elements.subList(currentIndex,elements.size()),topic,schemas);
          return;
        }
      }
 else {
        throw new BigQueryConnectException(String.format("Failed to write to BigQuery table %s",table),exception);
      }
    }
  }
  if (successfulCallCount == 1) {
    seeking=false;
    logger.debug("Best batch size found (all elements): {}",currentBatchSize);
  }
}

private void seekingWriteAll ( TableId table , List < InsertAllRequest . RowToInsert > elements , String topic , Set < Schema > schemas ) throws BigQueryConnectException , InterruptedException { logger . debug ( "Seeking best batch size..." ) ; int currentIndex = 0 ; int successfulCallCount = 0 ; while ( currentIndex < elements . size ( ) ) { int endIndex = Math . min ( currentIndex + currentBatchSize , elements . size ( ) ) ; List < InsertAllRequest . RowToInsert > currentBatchElements = elements . subList ( currentIndex , endIndex ) ; try { writer . writeRows ( table , currentBatchElements , topic , schemas ) ; successfulCallCount ++ ; currentIndex = endIndex ; if ( currentBatchSize == MAXIMUM_BATCH_SIZE ) { seeking = false ; logger . debug ( "Best batch size found (max): {}" , currentBatchSize ) ; establishedWriteAll ( table , elements . subList ( currentIndex , elements . size ( ) ) , topic , schemas ) ; return ; } if ( currentIndex < elements . size ( ) ) { increaseBatchSize ( table ) ; } } catch ( BigQueryException exception ) { if ( isBatchSizeError ( exception ) ) { decreaseBatchSize ( table ) ; if ( successfulCallCount > 0 ) { seeking = false ; logger . debug ( "Best batch size found (error if higher): {}" , currentBatchSize ) ; establishedWriteAll ( table , elements . subList ( currentIndex , elements . size ( ) ) , topic , schemas ) ; return ; } } else { throw new BigQueryConnectException ( String . format ( "Failed to write to BigQuery table %s" , table ) , exception ) ; } } } if ( successfulCallCount == 1 ) { seeking = false ; logger . debug ( "Best batch size found (all elements): {}" , currentBatchSize ) ; } }
a writeall intended to quickly find the largest viable batchsize . < p > we start with the currentbatchsize and adjust up or down as necessary . seek mode ends when one of the following occurs : 1 . we get an error after at least 1 success . this is slightly inefficient if our currentbatchsize is too big , but we will only make 1 extra request in that case . successes do not carry over from previous seekingwriteall requests . 2 . we write all the given elements in one batch request without error . 3 . we write maximum_batch_size elements in one batch request without error . ( unlikely )
