|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010, 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.orion.internal.server.search; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.net.URI; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import org.apache.solr.client.solrj.SolrQuery; |
| 20 |
import org.apache.solr.client.solrj.SolrServer; |
| 21 |
import org.apache.solr.client.solrj.SolrServerException; |
| 22 |
import org.apache.solr.client.solrj.response.QueryResponse; |
| 23 |
import org.apache.solr.common.SolrDocument; |
| 24 |
import org.apache.solr.common.SolrDocumentList; |
| 25 |
import org.apache.solr.common.params.CommonParams; |
| 26 |
import org.eclipse.core.filesystem.EFS; |
| 27 |
import org.eclipse.core.filesystem.IFileStore; |
| 28 |
import org.eclipse.core.filesystem.URIUtil; |
| 29 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 30 |
import org.eclipse.core.runtime.IStatus; |
| 31 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 32 |
import org.eclipse.core.runtime.Status; |
| 33 |
import org.eclipse.core.runtime.jobs.Job; |
| 34 |
import org.eclipse.orion.internal.server.servlets.ProtocolConstants; |
| 35 |
import org.eclipse.orion.server.core.LogHelper; |
| 36 |
import org.slf4j.Logger; |
| 37 |
import org.slf4j.LoggerFactory; |
| 38 |
|
| 39 |
/** |
| 40 |
* The IndexPurgeJob is responsible for cleaning up the indexes that that are |
| 41 |
* no longer required. The job crawls the indexes and purges those whose corresponding |
| 42 |
* resources no longer present in the file system. |
| 43 |
* |
| 44 |
*/ |
| 45 |
public class IndexPurgeJob extends Job { |
| 46 |
|
| 47 |
private static final long DEFAULT_DELAY = 180000;//3 minutes |
| 48 |
private static final long PAGE_SIZE = 1000; |
| 49 |
private final SolrServer server; |
| 50 |
private SolrQuery findAllQuery = null; |
| 51 |
|
| 52 |
public IndexPurgeJob(SolrServer server) { |
| 53 |
super("Purging Index"); |
| 54 |
this.server = server; |
| 55 |
this.findAllQuery = findAllQuery(); |
| 56 |
setSystem(true); |
| 57 |
} |
| 58 |
|
| 59 |
public void ensureUpdated() { |
| 60 |
schedule(DEFAULT_DELAY); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected IStatus run(IProgressMonitor monitor) { |
| 65 |
Logger logger = LoggerFactory.getLogger(Indexer.class); |
| 66 |
if (logger.isDebugEnabled()) |
| 67 |
logger.debug("Purging indexes"); //$NON-NLS-1$ |
| 68 |
long start = System.currentTimeMillis(); |
| 69 |
SolrQuery query = findAllQuery(); |
| 70 |
try { |
| 71 |
QueryResponse solrResponse = this.server.query(findAllQuery); |
| 72 |
SolrDocumentList result = solrResponse.getResults(); |
| 73 |
long numFound = result.getNumFound(); |
| 74 |
long processed = 0; |
| 75 |
List<String> listIds = new ArrayList<String>(); |
| 76 |
if (numFound > processed) { |
| 77 |
while (true) { |
| 78 |
checkCanceled(monitor); |
| 79 |
markStaleIndexes(result, listIds); |
| 80 |
processed += PAGE_SIZE; |
| 81 |
if (processed >= numFound) |
| 82 |
break; |
| 83 |
query.setParam(CommonParams.START, "" + processed); |
| 84 |
solrResponse = this.server.query(query); |
| 85 |
result = solrResponse.getResults(); |
| 86 |
// New indexes may have been added, perhaps |
| 87 |
numFound = result.getNumFound(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
checkCanceled(monitor); |
| 92 |
if (listIds.size() > 0) { |
| 93 |
this.server.deleteById(listIds); |
| 94 |
this.server.commit(); |
| 95 |
} |
| 96 |
if (logger.isDebugEnabled()) |
| 97 |
logger.debug("\tPurged: " + listIds.size()); //$NON-NLS-1$ |
| 98 |
|
| 99 |
} catch (Exception e) { |
| 100 |
handleIndexingFailure(e); |
| 101 |
} |
| 102 |
long duration = System.currentTimeMillis() - start; |
| 103 |
if (logger.isDebugEnabled()) |
| 104 |
logger.debug("Purge job took " + duration + "ms"); //$NON-NLS-1$ //$NON-NLS-3$ |
| 105 |
|
| 106 |
long delay = Math.max(DEFAULT_DELAY, duration); |
| 107 |
schedule(delay); |
| 108 |
return Status.OK_STATUS; |
| 109 |
} |
| 110 |
|
| 111 |
private void markStaleIndexes(SolrDocumentList list, List<String> listIds) throws IOException, SolrServerException { |
| 112 |
Iterator<SolrDocument> iterator = list.iterator(); |
| 113 |
while (iterator.hasNext()) { |
| 114 |
SolrDocument doc = iterator.next(); |
| 115 |
URI uri; |
| 116 |
try { |
| 117 |
uri = new URI((String) doc.getFieldValue(ProtocolConstants.KEY_ID)); |
| 118 |
IFileStore file = null; |
| 119 |
|
| 120 |
if (uri.isAbsolute()) { |
| 121 |
file = EFS.getLocalFileSystem().getStore(URIUtil.toPath(uri)); |
| 122 |
} else { |
| 123 |
file = EFS.getStore(uri); |
| 124 |
} |
| 125 |
|
| 126 |
if (!file.fetchInfo().exists()) |
| 127 |
listIds.add((String) doc.getFieldValue(ProtocolConstants.KEY_ID)); |
| 128 |
|
| 129 |
} catch (Exception e) { |
| 130 |
handleIndexingFailure(e); |
| 131 |
continue; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
private SolrQuery findAllQuery() { |
| 137 |
SolrQuery query = new SolrQuery(); |
| 138 |
query.setParam(CommonParams.ROWS, "" + PAGE_SIZE); |
| 139 |
String queryString = "*:*"; |
| 140 |
query.setQuery(queryString); |
| 141 |
return query; |
| 142 |
} |
| 143 |
|
| 144 |
private void checkCanceled(IProgressMonitor monitor) { |
| 145 |
if (monitor.isCanceled()) |
| 146 |
throw new OperationCanceledException(); |
| 147 |
} |
| 148 |
|
| 149 |
private void handleIndexingFailure(Throwable t) { |
| 150 |
LogHelper.log(new Status(IStatus.ERROR, SearchActivator.PI_SEARCH, "Error during search index purge", t)); //$NON-NLS-1$ |
| 151 |
} |
| 152 |
|
| 153 |
} |