3131import org .apache .http .impl .client .CloseableHttpClient ;
3232import org .apache .http .impl .client .HttpClients ;
3333import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
34+ import org .apache .http .impl .execchain .RequestAbortedException ;
3435import org .commonjava .indy .service .archive .config .PreSeedConfig ;
3536import org .commonjava .indy .service .archive .model .ArchiveStatus ;
3637import org .commonjava .indy .service .archive .model .dto .HistoricalContentDTO ;
@@ -124,20 +125,8 @@ public class ArchiveController
124125 public void init ()
125126 throws IOException
126127 {
127- int threads = preSeedConfig .threadMultiplier ().orElse ( 4 ) * Runtime .getRuntime ().availableProcessors ();
128- downloadExecutor = Executors .newFixedThreadPool ( threads , ( final Runnable r ) -> {
129- final Thread t = new Thread ( r );
130- t .setName ( "Download-" + t .getName () );
131- t .setDaemon ( true );
132- return t ;
133- } );
134- generateExecutor = Executors .newFixedThreadPool ( threads , ( final Runnable r ) -> {
135- final Thread t = new Thread ( r );
136- t .setName ( "Generate-" + t .getName () );
137- t .setDaemon ( true );
138- return t ;
139- } );
140-
128+ newGenerateExecutor ();
129+ newDownloadExecutor ();
141130 final PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager ();
142131 ccm .setMaxTotal ( 500 );
143132 RequestConfig rc = RequestConfig .custom ().build ();
@@ -163,13 +152,24 @@ public void generate( HistoricalContentDTO content )
163152 {
164153 if ( isInProgress ( buildConfigId ) )
165154 {
166- logger .info ( "There is already generation process in progress for buildConfigId {}, this request will skip." ,
167- buildConfigId );
155+ logger .info (
156+ "There is already generation process in progress for buildConfigId {}, this request will skip." ,
157+ buildConfigId );
168158 // Conflicted generation, just return immediately
169159 return ;
170160 }
171161
172162 recordInProgress ( buildConfigId );
163+ if ( generateExecutor .isShutdown () || generateExecutor .isTerminated () )
164+ {
165+ logger .info ( "new generateExecutor" );
166+ newGenerateExecutor ();
167+ }
168+ if ( downloadExecutor .isShutdown () || downloadExecutor .isTerminated () )
169+ {
170+ logger .info ( "new downloadExecutor" );
171+ newDownloadExecutor ();
172+ }
173173 CompletableFuture <Void > future = CompletableFuture .runAsync ( () -> {
174174 try
175175 {
@@ -195,7 +195,13 @@ public void generate( HistoricalContentDTO content )
195195 // If timeout happens on generation, cancel and remove the status to make sure following generation
196196 removeStatus ( buildConfigId );
197197 cleanupBCWorkspace ( buildConfigId );
198- logger .error ( "Generation timeout for buildConfigId {}" , buildConfigId );
198+ logger .warn ( "Generation timeout for buildConfigId {}, try to shut down the generation executor" ,
199+ buildConfigId );
200+ generateExecutor .shutdownNow ();
201+ downloadExecutor .shutdownNow ();
202+
203+ buildConfigLocks .remove ( buildConfigId );
204+ logger .info ( "<<<Lock released for buildConfigId {}" , buildConfigId );
199205 return null ;
200206 } );
201207 }
@@ -218,17 +224,18 @@ protected Boolean doGenerate( HistoricalContentDTO content )
218224 }
219225 catch ( final InterruptedException e )
220226 {
221- logger .error ( "Artifacts downloading is interrupted, build config id: " + buildConfigId , e );
227+ logger .error ( "Artifacts downloading is interrupted, build config id: {}." , buildConfigId , e );
222228 return false ;
223229 }
224230 catch ( final ExecutionException e )
225231 {
226- logger .error ( "Artifacts download execution manager failed, build config id: " + buildConfigId , e );
232+ logger .error ( "Artifacts download execution manager failed, build config id: {}." , buildConfigId , e );
227233 return false ;
228234 }
229235 catch ( final IOException e )
230236 {
231- logger .error ( "Failed to generate historical archive from content, build config id: " + buildConfigId , e );
237+ logger .error ( "Failed to generate historical archive from content, build config id: {}." , buildConfigId ,
238+ e );
232239 return false ;
233240 }
234241
@@ -342,6 +349,28 @@ public boolean isInProgress( final String buildConfigId )
342349 ArchiveStatus .inProgress .getArchiveStatus () );
343350 }
344351
352+ private void newGenerateExecutor ()
353+ {
354+ int threads = preSeedConfig .threadMultiplier ().orElse ( 4 ) * Runtime .getRuntime ().availableProcessors ();
355+ generateExecutor = Executors .newFixedThreadPool ( threads , ( final Runnable r ) -> {
356+ final Thread t = new Thread ( r );
357+ t .setName ( "Generate-" + t .getName () );
358+ t .setDaemon ( true );
359+ return t ;
360+ } );
361+ }
362+
363+ private void newDownloadExecutor ()
364+ {
365+ int threads = preSeedConfig .threadMultiplier ().orElse ( 4 ) * Runtime .getRuntime ().availableProcessors ();
366+ downloadExecutor = Executors .newFixedThreadPool ( threads , ( final Runnable r ) -> {
367+ final Thread t = new Thread ( r );
368+ t .setName ( "Download-" + t .getName () );
369+ t .setDaemon ( true );
370+ return t ;
371+ } );
372+ }
373+
345374 private void downloadArtifacts ( final Map <String , HistoricalEntryDTO > entryDTOs ,
346375 final Map <String , String > downloadPaths , final HistoricalContentDTO content )
347376 throws InterruptedException , ExecutionException , IOException
@@ -497,7 +526,7 @@ private void fileTrackedContent( String contentBuildDir, final HistoricalContent
497526 }
498527 catch ( final IOException e )
499528 {
500- logger .error ( "Failed to file tracked content, path: " + tracked .getPath (), e );
529+ logger .error ( "Failed to file tracked content, path: {}" , tracked .getPath (), e );
501530 }
502531 finally
503532 {
@@ -623,9 +652,14 @@ else if ( statusCode == 404 )
623652 return false ;
624653 }
625654 }
655+ catch ( final RequestAbortedException e )
656+ {
657+ logger .warn ( "<<<Request aborted: Download failed for path: {}" , path );
658+ return false ;
659+ }
626660 catch ( final Exception e )
627661 {
628- logger .error ( "Download failed for path: " + path , e );
662+ logger .error ( "Download failed for path: {}" , path , e );
629663 }
630664 finally
631665 {
0 commit comments