You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaIndexingTaskExecutor.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package org.apache.archiva.maven.scheduler.indexing;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.indexer.ArchivaIndexingContext;
  20. import org.apache.archiva.indexer.UnsupportedBaseContextException;
  21. import org.apache.archiva.components.taskqueue.Task;
  22. import org.apache.archiva.components.taskqueue.execution.TaskExecutionException;
  23. import org.apache.archiva.components.taskqueue.execution.TaskExecutor;
  24. import org.apache.archiva.repository.ManagedRepository;
  25. import org.apache.archiva.repository.features.IndexCreationFeature;
  26. import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
  27. import org.apache.maven.index.ArtifactContext;
  28. import org.apache.maven.index.ArtifactContextProducer;
  29. import org.apache.maven.index.DefaultScannerListener;
  30. import org.apache.maven.index.FlatSearchRequest;
  31. import org.apache.maven.index.FlatSearchResponse;
  32. import org.apache.maven.index.Indexer;
  33. import org.apache.maven.index.IndexerEngine;
  34. import org.apache.maven.index.MAVEN;
  35. import org.apache.maven.index.Scanner;
  36. import org.apache.maven.index.ScanningRequest;
  37. import org.apache.maven.index.ScanningResult;
  38. import org.apache.maven.index.context.IndexingContext;
  39. import org.apache.maven.index.expr.SourcedSearchExpression;
  40. import org.apache.maven.index.packer.IndexPacker;
  41. import org.apache.maven.index.packer.IndexPackingRequest;
  42. import org.apache.maven.index_shaded.lucene.search.BooleanClause;
  43. import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
  44. import org.slf4j.Logger;
  45. import org.slf4j.LoggerFactory;
  46. import org.springframework.stereotype.Service;
  47. import javax.inject.Inject;
  48. import java.io.IOException;
  49. import java.nio.file.Path;
  50. /**
  51. * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
  52. * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
  53. * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
  54. */
  55. @Service( "taskExecutor#indexing" )
  56. public class ArchivaIndexingTaskExecutor
  57. implements TaskExecutor
  58. {
  59. private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
  60. @Inject
  61. private IndexPacker indexPacker;
  62. @Inject
  63. private ArtifactContextProducer artifactContextProducer;
  64. @Inject
  65. private Indexer indexer;
  66. @Inject
  67. private Scanner scanner;
  68. @Inject
  69. IndexerEngine indexerEngine;
  70. /**
  71. * depending on current {@link Task} you have.
  72. * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action#FINISH} && isExecuteOnEntireRepo:
  73. * repository will be scanned.
  74. *
  75. * @param task
  76. * @throws TaskExecutionException
  77. */
  78. @Override
  79. public void executeTask( Task task )
  80. throws TaskExecutionException
  81. {
  82. ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
  83. ManagedRepository repository = indexingTask.getRepository( );
  84. ArchivaIndexingContext archivaContext = indexingTask.getContext( );
  85. IndexingContext context = null;
  86. try
  87. {
  88. context = archivaContext.getBaseContext( IndexingContext.class );
  89. }
  90. catch ( UnsupportedBaseContextException e )
  91. {
  92. throw new TaskExecutionException( "Bad repository type.", e );
  93. }
  94. if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction( ) )
  95. && indexingTask.isExecuteOnEntireRepo( ) )
  96. {
  97. long start = System.currentTimeMillis( );
  98. try
  99. {
  100. context.updateTimestamp( );
  101. DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, null );
  102. ScanningRequest request = new ScanningRequest( context, listener );
  103. ScanningResult result = scanner.scan( request );
  104. if ( result.hasExceptions( ) )
  105. {
  106. log.error( "Exceptions occured during index scan of " + context.getId( ) );
  107. result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
  108. s -> log.error( "Message: " + s )
  109. );
  110. }
  111. }
  112. catch ( IOException e )
  113. {
  114. log.error( "Error during context scan {}: {}", context.getId( ), context.getIndexDirectory( ) );
  115. }
  116. long end = System.currentTimeMillis( );
  117. log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId( ),
  118. indexingTask.isOnlyUpdate( ), ( end - start ) );
  119. log.debug( "Finishing indexing task on repo: {}", repository.getId( ) );
  120. finishIndexingTask( indexingTask, repository, context );
  121. }
  122. else
  123. {
  124. // create context if not a repo scan request
  125. if ( !indexingTask.isExecuteOnEntireRepo( ) )
  126. {
  127. try
  128. {
  129. log.debug( "Creating indexing context on resource: {}", //
  130. ( indexingTask.getResourceFile( ) == null
  131. ? "none"
  132. : indexingTask.getResourceFile( ) ) );
  133. archivaContext = repository.getIndexingContext( );
  134. context = archivaContext.getBaseContext( IndexingContext.class );
  135. }
  136. catch ( UnsupportedBaseContextException e )
  137. {
  138. log.error( "Error occurred while creating context: {}", e.getMessage( ) );
  139. throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage( ), e );
  140. }
  141. }
  142. if ( context == null || context.getIndexDirectory( ) == null )
  143. {
  144. throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
  145. }
  146. try
  147. {
  148. Path artifactFile = indexingTask.getResourceFile( );
  149. if ( artifactFile == null )
  150. {
  151. log.debug( "no artifact pass in indexing task so skip it" );
  152. }
  153. else
  154. {
  155. ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile( ) );
  156. if ( ac != null )
  157. {
  158. // MRM-1779 pom must be indexed too
  159. // TODO make that configurable?
  160. if ( artifactFile.getFileName( ).toString( ).endsWith( ".pom" ) )
  161. {
  162. ac.getArtifactInfo( ).setFileExtension( "pom" );
  163. ac.getArtifactInfo( ).setPackaging( "pom" );
  164. ac.getArtifactInfo( ).setClassifier( "pom" );
  165. }
  166. if ( indexingTask.getAction( ).equals( ArtifactIndexingTask.Action.ADD ) )
  167. {
  168. //IndexSearcher s = context.getIndexSearcher();
  169. //String uinfo = ac.getArtifactInfo().getUinfo();
  170. //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
  171. BooleanQuery.Builder qb = new BooleanQuery.Builder();
  172. qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
  173. ac.getArtifactInfo( ).getGroupId( ) ) ), BooleanClause.Occur.MUST );
  174. qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
  175. ac.getArtifactInfo( ).getArtifactId( ) ) ), BooleanClause.Occur.MUST );
  176. qb.add( indexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
  177. ac.getArtifactInfo( ).getVersion( ) ) ), BooleanClause.Occur.MUST );
  178. if ( ac.getArtifactInfo( ).getClassifier( ) != null )
  179. {
  180. qb.add( indexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
  181. ac.getArtifactInfo( ).getClassifier( ) ) ), BooleanClause.Occur.MUST );
  182. }
  183. if ( ac.getArtifactInfo( ).getPackaging( ) != null )
  184. {
  185. qb.add( indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
  186. ac.getArtifactInfo( ).getPackaging( ) ) ), BooleanClause.Occur.MUST );
  187. }
  188. FlatSearchRequest flatSearchRequest = new FlatSearchRequest( qb.build(), context );
  189. FlatSearchResponse flatSearchResponse = indexer.searchFlat( flatSearchRequest );
  190. if ( flatSearchResponse.getResults( ).isEmpty( ) )
  191. {
  192. log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo( ) );
  193. indexerEngine.index( context, ac );
  194. }
  195. else
  196. {
  197. log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo( ) );
  198. // TODO check if update exists !!
  199. indexerEngine.update( context, ac );
  200. }
  201. context.updateTimestamp( );
  202. context.commit( );
  203. }
  204. else
  205. {
  206. log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo( ) );
  207. indexerEngine.remove( context, ac );
  208. }
  209. }
  210. }
  211. // close the context if not a repo scan request
  212. if ( !indexingTask.isExecuteOnEntireRepo( ) )
  213. {
  214. log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile( ) != null
  215. ? indexingTask.getResourceFile( )
  216. : " none " );
  217. finishIndexingTask( indexingTask, repository, context );
  218. }
  219. }
  220. catch ( IOException e )
  221. {
  222. log.error( "Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage( ),
  223. e );
  224. throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
  225. e );
  226. }
  227. }
  228. }
  229. private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
  230. IndexingContext context )
  231. throws TaskExecutionException
  232. {
  233. try
  234. {
  235. log.debug( "Finishing indexing" );
  236. context.optimize( );
  237. if ( repository.supportsFeature( IndexCreationFeature.class ) )
  238. {
  239. IndexCreationFeature icf = repository.getFeature( IndexCreationFeature.class );
  240. if ( !icf.isSkipPackedIndexCreation( ) && icf.getLocalPackedIndexPath( ) != null && icf.getLocalIndexPath().getFilePath()!=null )
  241. {
  242. log.debug( "Creating packed index from {} on {}", context.getIndexDirectoryFile( ), icf.getLocalPackedIndexPath( ) );
  243. IndexPackingRequest request = new IndexPackingRequest( context, //
  244. context.acquireIndexSearcher( ).getIndexReader( ),
  245. //
  246. icf.getLocalPackedIndexPath( ).getFilePath().toFile( ) );
  247. indexPacker.packIndex( request );
  248. context.updateTimestamp( true );
  249. log.debug( "Index file packed at '{}'.", icf.getLocalPackedIndexPath( ) );
  250. }
  251. else
  252. {
  253. log.debug( "skip packed index creation" );
  254. }
  255. }
  256. else
  257. {
  258. log.debug( "skip packed index creation" );
  259. }
  260. }
  261. catch ( IOException e )
  262. {
  263. log.error( "Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage( ) );
  264. throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
  265. e );
  266. }
  267. }
  268. public void setIndexPacker( IndexPacker indexPacker )
  269. {
  270. this.indexPacker = indexPacker;
  271. }
  272. }