@@ -3653,13 +3653,11 @@ public ResultSet getSchemas(String catalog, String schemaPattern) {
36533653 }
36543654
36553655 ExecutorService apiExecutor = null ;
3656+ final List <Future <List <Dataset >>> apiFutures = new ArrayList <>();
36563657 try {
36573658 apiExecutor = Executors .newFixedThreadPool (API_EXECUTOR_POOL_SIZE );
3658- List <Future <List <Dataset >>> apiFutures = new ArrayList <>();
36593659 for (String currentProjectToScan : projectsToScanList ) {
3660- if (Thread .currentThread ().isInterrupted ()) {
3661- break ;
3662- }
3660+ checkInterrupted (apiFutures );
36633661 Callable <List <Dataset >> apiCallable =
36643662 () ->
36653663 findMatchingBigQueryObjects (
@@ -3678,40 +3676,35 @@ public ResultSet getSchemas(String catalog, String schemaPattern) {
36783676 apiExecutor .shutdown ();
36793677
36803678 for (Future <List <Dataset >> apiFuture : apiFutures ) {
3681- if (Thread .currentThread ().isInterrupted ()) {
3682- break ;
3683- }
3679+ checkInterrupted (apiFutures );
36843680 try {
36853681 List <Dataset > datasetsInProject = apiFuture .get ();
36863682 if (datasetsInProject != null ) {
36873683 for (Dataset dataset : datasetsInProject ) {
3688- if (Thread .currentThread ().isInterrupted ()) break ;
36893684 processSchemaInfo (dataset , collectedResults , localResultSchemaFields );
36903685 }
36913686 }
36923687 } catch (InterruptedException e ) {
36933688 Thread .currentThread ().interrupt ();
3694- LOG .warning ("Fetcher thread interrupted while waiting for API future result." );
3695- break ;
3689+ checkInterrupted (apiFutures );
36963690 } catch (ExecutionException e ) {
36973691 LOG .warning ("Error executing findMatchingDatasets task: " + e .getMessage ());
36983692 } catch (CancellationException e ) {
36993693 LOG .warning ("A findMatchingDatasets task was cancelled." );
37003694 }
37013695 }
37023696
3703- if (!Thread .currentThread ().isInterrupted ()) {
3704- Comparator <FieldValueList > comparator =
3705- defineGetSchemasComparator (localResultSchemaFields );
3706- sortResults (collectedResults , comparator , "getSchemas" , LOG );
3707- }
3708-
3709- if (!Thread .currentThread ().isInterrupted ()) {
3710- populateQueue (collectedResults , queue , localResultSchemaFields );
3711- }
3697+ checkInterrupted (apiFutures );
3698+ Comparator <FieldValueList > comparator =
3699+ defineGetSchemasComparator (localResultSchemaFields );
3700+ sortResults (collectedResults , comparator , "getSchemas" , LOG );
3701+ populateQueue (collectedResults , queue , localResultSchemaFields );
37123702
3703+ } catch (CancellationException e ) {
3704+ LOG .warning ("Schema fetcher task was cancelled/interrupted." );
37133705 } catch (Throwable t ) {
37143706 LOG .severe ("Unexpected error in schema fetcher runnable: " + t .getMessage ());
3707+ apiFutures .forEach (f -> f .cancel (true ));
37153708 } finally {
37163709 shutdownExecutor (apiExecutor );
37173710 signalEndOfData (queue , localResultSchemaFields );
@@ -5155,6 +5148,13 @@ private void signalEndOfData(
51555148 }
51565149 }
51575150
5151+ private void checkInterrupted (List <? extends Future <?>> futures ) {
5152+ if (Thread .currentThread ().isInterrupted ()) {
5153+ futures .forEach (f -> f .cancel (true ));
5154+ throw new CancellationException ("Fetcher thread was interrupted." );
5155+ }
5156+ }
5157+
51585158 private void shutdownExecutor (ExecutorService executor ) {
51595159 if (executor == null || executor .isShutdown ()) {
51605160 return ;
0 commit comments