1717
1818import  com .google .common .annotations .VisibleForTesting ;
1919import  com .sforce .async .AsyncApiException ;
20- import  com .sforce .async .AsyncExceptionCode ;
2120import  com .sforce .async .BatchInfo ;
2221import  com .sforce .async .BatchStateEnum ;
2322import  com .sforce .async .BulkConnection ;
24- import  dev .failsafe .Failsafe ;
25- import  dev .failsafe .FailsafeException ;
26- import  dev .failsafe .RetryPolicy ;
27- import  dev .failsafe .TimeoutExceededException ;
2823import  io .cdap .cdap .api .data .schema .Schema ;
2924import  io .cdap .plugin .salesforce .BulkAPIBatchException ;
3025import  io .cdap .plugin .salesforce .SalesforceConnectionUtil ;
3126import  io .cdap .plugin .salesforce .SalesforceConstants ;
3227import  io .cdap .plugin .salesforce .authenticator .Authenticator ;
3328import  io .cdap .plugin .salesforce .authenticator .AuthenticatorCredentials ;
3429import  io .cdap .plugin .salesforce .plugin .source .batch .util .BulkConnectionRetryWrapper ;
35- import  io .cdap .plugin .salesforce .plugin .source .batch .util .SalesforceQueryExecutionException ;
3630import  io .cdap .plugin .salesforce .plugin .source .batch .util .SalesforceSourceConstants ;
37- import  io .cdap .plugin .salesforce .plugin .source .batch .util .SalesforceSplitUtil ;
3831import  org .apache .commons .csv .CSVFormat ;
3932import  org .apache .commons .csv .CSVParser ;
4033import  org .apache .commons .csv .CSVRecord ;
@@ -69,7 +62,6 @@ public class SalesforceBulkRecordReader extends RecordReader<Schema, Map<String,
6962  private  String [] resultIds ;
7063  private  int  resultIdIndex ;
7164  private  BulkConnectionRetryWrapper  bulkConnectionRetryWrapper ;
72-   private  RetryPolicy <Object > retryPolicy ;
7365
7466  public  SalesforceBulkRecordReader (Schema  schema ) {
7567    this (schema , null , null , null , null );
@@ -112,20 +104,16 @@ public SalesforceBulkRecordReader initialize(InputSplit inputSplit, Authenticato
112104    jobId  = salesforceSplit .getJobId ();
113105    batchId  = salesforceSplit .getBatchId ();
114106    LOG .debug ("Executing Salesforce Batch Id: '{}' for Job Id: '{}'" , batchId , jobId );
115-     retryPolicy  = SalesforceSplitUtil .getRetryPolicy (credentials .getInitialRetryDuration (),
116-                                                      credentials .getMaxRetryDuration (),
117-                                                      credentials .getMaxRetryCount (),
118-                                                      credentials .isRetryOnBackendError ());
119107    try  {
120108      bulkConnection  = new  BulkConnection (Authenticator .createConnectorConfig (credentials ));
121109      bulkConnectionRetryWrapper  = new  BulkConnectionRetryWrapper (bulkConnection , credentials .isRetryOnBackendError (),
122110                                                                  credentials .getInitialRetryDuration (),
123111                                                                  credentials .getMaxRetryDuration (),
124112                                                                  credentials .getMaxRetryCount ());
125-       resultIds  = waitForBatchResults (bulkConnection );
113+       resultIds  = waitForBatchResults (bulkConnectionRetryWrapper );
126114      LOG .debug ("Batch {} returned {} results" , batchId , resultIds .length );
127115      setupParser ();
128-     } catch  (AsyncApiException  |  SalesforceQueryExecutionException   e ) {
116+     } catch  (AsyncApiException  e ) {
129117      throw  new  RuntimeException (
130118        String .format ("Failed to wait for the result of a batch: %s" , e .getMessage ()),
131119        e );
@@ -193,7 +181,7 @@ public void close() throws IOException {
193181  }
194182
195183  @ VisibleForTesting 
196-   void  setupParser () throws  IOException , AsyncApiException ,  InterruptedException  {
184+   void  setupParser () throws  IOException , AsyncApiException  {
197185    if  (resultIdIndex  >= resultIds .length ) {
198186      throw  new  IllegalArgumentException (String .format ("Invalid resultIdIndex %d, should be less than %d" ,
199187        resultIdIndex , resultIds .length ));
@@ -212,32 +200,11 @@ void setupParser() throws IOException, AsyncApiException, InterruptedException {
212200      }
213201      parserIterator  = csvParser .iterator ();
214202      resultIdIndex ++;
215-     } catch  (TimeoutExceededException  e ) {
216-       throw  new  AsyncApiException ("Exhausted retries trying to get query result stream" , AsyncExceptionCode .Timeout );
217-     } catch  (FailsafeException  e ) {
218-       if  (e .getCause () instanceof  InterruptedException ) {
219-         throw  (InterruptedException ) e .getCause ();
220-       }
221-       if  (e .getCause () instanceof  AsyncApiException ) {
222-         throw  (AsyncApiException ) e .getCause ();
223-       }
203+     } catch  (AsyncApiException  e ) {
224204      throw  e ;
225205    }
226206  }
227207
228-   public  InputStream  getQueryResultStream (BulkConnection  bulkConnection )
229-     throws  SalesforceQueryExecutionException , AsyncApiException  {
230-     try  {
231-       return  bulkConnection .getQueryResultStream (jobId , batchId , resultIds [resultIdIndex ]);
232-     } catch  (AsyncApiException  exception ) {
233-       LOG .warn ("The bulk query job {} failed." , jobId );
234-       if  (BulkConnectionRetryWrapper .RETRY_ON_REASON .contains (exception .getExceptionCode ())) {
235-         throw  new  SalesforceQueryExecutionException (exception );
236-       }
237-       throw  exception ;
238-     }
239-   }
240- 
241208  /** 
242209   * Wait until a batch with given batchId succeeds, or throw an exception 
243210   * 
@@ -246,8 +213,8 @@ public InputStream getQueryResultStream(BulkConnection bulkConnection)
246213   * @throws AsyncApiException    if there is an issue creating the job 
247214   * @throws InterruptedException sleep interrupted 
248215   */ 
249-   private  String [] waitForBatchResults (BulkConnection  bulkConnection )
250-     throws  AsyncApiException , InterruptedException ,  SalesforceQueryExecutionException  {
216+   private  String [] waitForBatchResults (BulkConnectionRetryWrapper  bulkConnection )
217+     throws  AsyncApiException , InterruptedException  {
251218    BatchInfo  info  = null ;
252219    for  (int  i  = 0 ; i  < SalesforceSourceConstants .GET_BATCH_RESULTS_TRIES ; i ++) {
253220      try  {
@@ -261,20 +228,7 @@ private String[] waitForBatchResults(BulkConnection bulkConnection)
261228      }
262229
263230      if  (info .getState () == BatchStateEnum .Completed ) {
264-         try  {
265-           return  Failsafe .with (retryPolicy )
266-               .get (() -> getQueryResultList (bulkConnection ));
267-         } catch  (TimeoutExceededException  e ) {
268-           throw  new  AsyncApiException ("Exhausted retries trying to get query result list" , AsyncExceptionCode .Timeout );
269-         } catch  (FailsafeException  e ) {
270-           if  (e .getCause () instanceof  InterruptedException ) {
271-             throw  (InterruptedException ) e .getCause ();
272-           }
273-           if  (e .getCause () instanceof  AsyncApiException ) {
274-             throw  (AsyncApiException ) e .getCause ();
275-           }
276-           throw  e ;
277-         }
231+         return  bulkConnection .getQueryResultList (jobId , batchId );
278232      } else  if  (info .getState () == BatchStateEnum .Failed ) {
279233        throw  new  BulkAPIBatchException ("Batch failed" , info );
280234      } else  {
@@ -284,17 +238,4 @@ private String[] waitForBatchResults(BulkConnection bulkConnection)
284238    }
285239    throw  new  BulkAPIBatchException ("Timeout waiting for batch results" , info );
286240  }
287- 
288-   private  String [] getQueryResultList (BulkConnection  bulkConnection )
289-     throws  SalesforceQueryExecutionException , AsyncApiException  {
290-     try  {
291-       return  bulkConnection .getQueryResultList (jobId , batchId ).getResult ();
292-     } catch  (AsyncApiException  exception ) {
293-       LOG .warn ("The bulk query job {} failed." , jobId );
294-       if  (BulkConnectionRetryWrapper .RETRY_ON_REASON .contains (exception .getExceptionCode ())) {
295-         throw  new  SalesforceQueryExecutionException (exception );
296-       }
297-       throw  exception ;
298-     }
299-   }
300241}
0 commit comments