Skip to content

Commit

Permalink
112307: Solr soft commits performance increase & test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Atmire-Kristof committed Feb 21, 2024
1 parent 5a43e6b commit 2685fbd
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void cleanIndex() throws Exception {
@Override
public void commit() {
try {
getSolr().commit();
getSolr().commit(false, false);
} catch (SolrServerException e) {
log.error("Error while committing authority solr server", e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void unIndexContent(Context context, IndexableObject indexableObject, boo
log.info("Try to delete uniqueID:" + uniqueID);
indexObjectServiceFactory.getIndexableObjectFactory(indexableObject).delete(indexableObject);
if (commit) {
solrSearchCore.getSolr().commit();
solrSearchCore.getSolr().commit(false, false);
}
} catch (IOException | SolrServerException exception) {
log.error(exception.getMessage(), exception);
Expand Down Expand Up @@ -263,7 +263,7 @@ public void unIndexContent(Context context, String searchUniqueID, boolean commi
log.warn("Object not found in Solr index: " + searchUniqueID);
}
if (commit) {
solrSearchCore.getSolr().commit();
solrSearchCore.getSolr().commit(false, false);
}
}
} catch (SolrServerException e) {
Expand Down Expand Up @@ -352,7 +352,7 @@ public void updateIndex(Context context, boolean force, String type) {
}
}
if (solrSearchCore.getSolr() != null) {
solrSearchCore.getSolr().commit();
solrSearchCore.getSolr().commit(false, false);
}

} catch (IOException | SQLException | SolrServerException e) {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ protected DiscoverResult retrieveResult(Context context, DiscoverQuery query)
log.info("ZombieDocs ");
zombieDocs.forEach(log::info);
solrSearchCore.getSolr().deleteById(zombieDocs);
solrSearchCore.getSolr().commit();
solrSearchCore.getSolr().commit(false, false);
} else {
valid = true;
}
Expand Down Expand Up @@ -1548,7 +1548,7 @@ public void indexContent(Context context, IndexableObject indexableObject, boole
public void commit() throws SearchServiceException {
try {
if (solrSearchCore.getSolr() != null) {
solrSearchCore.getSolr().commit();
solrSearchCore.getSolr().commit(false, false);
}
} catch (IOException | SolrServerException e) {
throw new SearchServiceException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public boolean isUseProxies() {
public void removeIndex(String query) throws IOException,
SolrServerException {
solr.deleteByQuery(query);
solr.commit();
solr.commit(false, false);
}

@Override
Expand Down Expand Up @@ -730,7 +730,7 @@ public void execute(String query) throws SolrServerException, IOException {
}

public void commit() throws IOException, SolrServerException {
solr.commit();
solr.commit(false, false);
}

/**
Expand Down Expand Up @@ -780,7 +780,7 @@ public void process(SolrInputDocument doc) throws IOException, SolrServerExcepti
/* query for ip, exclude results previously set as bots. */
processor.execute("ip:" + ip + "* AND -isBot:true");

solr.commit();
solr.commit(false, false);

} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down Expand Up @@ -808,7 +808,7 @@ public void process(SolrInputDocument doc) throws IOException, SolrServerExcepti
/* query for ip, exclude results previously set as bots. */
processor.execute("userAgent:" + agent + " AND -isBot:true");

solr.commit();
solr.commit(false, false);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
Expand Down Expand Up @@ -1340,12 +1340,12 @@ public void shardSolrIndex() throws IOException, SolrServerException {
statisticsYearServer.request(contentStreamUpdateRequest);
}

statisticsYearServer.commit(true, true);
statisticsYearServer.commit(false, false);


//Delete contents of this year from our year query !
solr.deleteByQuery(filterQuery.toString());
solr.commit(true, true);
solr.commit(false, false);

log.info("Moved {} records into core: {}", totalRecords, coreName);
}
Expand Down Expand Up @@ -1530,7 +1530,7 @@ public void reindexBitstreamHits(boolean removeDeletedBitstreams) throws Excepti
//Now that all our new bitstream stats are in place, delete all the old ones !
solr.deleteByQuery("-bundleName:[* TO *] AND type:" + Constants.BITSTREAM);
//Commit everything to wrap up
solr.commit(true, true);
solr.commit(false, false);
//Clean up our directory !
FileUtils.deleteDirectory(tempDirectory);
} catch (Exception e) {
Expand Down Expand Up @@ -1587,7 +1587,7 @@ public void exportHits() throws Exception {

@Override
public void commit() throws IOException, SolrServerException {
solr.commit();
solr.commit(false, false);
}

protected void addDocumentsToFile(Context context, SolrDocumentList docs, File exportOutput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ protected void load(String filename, Context context, boolean verbose) {

// Commit at the end because it takes a while
try {
solr.commit();
solr.commit(false, false);
} catch (SolrServerException sse) {
System.err.println("Error committing statistics to solr server!");
sse.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private static void reindex(String indexName, String exportDirName, boolean keep

// commit changes
HttpSolrClient origSolr = new HttpSolrClient.Builder(origSolrUrl).build();
origSolr.commit();
origSolr.commit(false, false);

// swap back (statistics now going to actual core name in actual data dir)
swapRequest = new CoreAdminRequest();
Expand All @@ -364,7 +364,7 @@ private static void reindex(String indexName, String exportDirName, boolean keep
importIndex(tempIndexName, exportDir, origSolrUrl, false);

// commit changes
origSolr.commit();
origSolr.commit(false, false);

// unload now-temp core (temp core name)
CoreAdminRequest.unloadCore(tempIndexName, false, false, adminSolr);
Expand Down Expand Up @@ -465,7 +465,7 @@ public boolean accept(File dir, String name) {
solr.request(contentStreamUpdateRequest);
}

solr.commit(true, true);
solr.commit(false, false);
}

/**
Expand Down Expand Up @@ -502,7 +502,7 @@ private static List<String> getMultiValuedFields(HttpSolrClient solr) {
public static void clearIndex(String solrUrl) throws IOException, SolrServerException {
HttpSolrClient solr = new HttpSolrClient.Builder(solrUrl).build();
solr.deleteByQuery("*:*");
solr.commit();
solr.commit(false, false);
solr.optimize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public SolrUpgradePre6xStatistics(String indexName, int numRec, int batchSize)
private void batchUpdateStats() throws SolrServerException, IOException {
if (docs.size() > 0) {
server.add(docs);
server.commit(true, true);
server.commit(false, false);
docs.clear();
}
}
Expand Down
105 changes: 105 additions & 0 deletions dspace-api/src/test/java/org/dspace/discovery/SolrServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.discovery;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataValueService;
import org.dspace.discovery.indexobject.IndexableItem;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Test;

public class SolrServiceTest extends AbstractIntegrationTestWithDatabase {
protected ItemService itemService = ContentServiceFactory.getInstance().getItemService();
protected MetadataValueService metadataValueService = ContentServiceFactory.getInstance().getMetadataValueService();
protected SearchService searchService = SearchUtils.getSearchService();
protected SolrSearchCore solrSearchCore = DSpaceServicesFactory.getInstance().getServiceManager()
.getServicesByType(SolrSearchCore.class).get(0);

private Community community;
private Collection collection;
private Item item;

@Override
public void setUp() throws Exception {
super.setUp();

context.turnOffAuthorisationSystem();

community = CommunityBuilder.createCommunity(context).withName("Top Community").build();
collection = CollectionBuilder.createCollection(context, community).withName("Test Collection").build();
item = ItemBuilder.createItem(context, collection).withTitle("Test Item").build();

context.restoreAuthSystemState();
}

@Test
public void directDatabaseNoCommitTest() throws Exception {
setTitleDatabase("Test Item MODIFIED");
indexItemNoCommit();
assertSolrTitle("Test Item");
}

@Test
public void directDatabaseSoftCommitTest() throws Exception {
setTitleDatabase("Test Item MODIFIED");
indexItemNoCommit();
solrSearchCore.getSolr().commit(false, false);
assertSolrTitle("Test Item MODIFIED");
}

@Test
public void directDatabaseHardCommitTest() throws Exception {
setTitleDatabase("Test Item MODIFIED");
indexItemNoCommit();
solrSearchCore.getSolr().commit(true, true);
assertSolrTitle("Test Item MODIFIED");
}

private void assertSolrTitle(String expectedTitle) throws Exception {
// Retrieve item document from solr
SolrQuery query = new SolrQuery();
query.setQuery("search.resourceid:\"" + item.getID() + "\"");
SolrDocumentList results = solrSearchCore.getSolr().query(query, solrSearchCore.REQUEST_METHOD).getResults();
assertEquals(1, results.getNumFound());
SolrDocument result = results.get(0);

// Compare the expected value of the title with the actual one
assertEquals("[" + expectedTitle + "]", result.get("title").toString());
}

private void setTitleDatabase(String newTitle) throws Exception {
// Retrieve the current title
List<MetadataValue> titles = itemService.getMetadata(item, "dc", "title", null, Item.ANY);
assertEquals(1, titles.size());
MetadataValue title = titles.get(0);

// Modify the title directly in the database, this shouldn't trigger a solr update
title.setValue(newTitle);
metadataValueService.update(context, title);
}

private void indexItemNoCommit() throws Exception {
solrSearchCore.indexingService.indexContent(context, new IndexableItem(item), true, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static synchronized SolrClient initSolrServerForCore(final String coreNa
//Start with an empty index
try {
server.deleteByQuery("*:*");
server.commit();
server.commit(false, false);
} catch (SolrServerException | IOException e) {
log.error("Failed to empty Solr index: {}", e.getMessage(), e);
}
Expand Down
8 changes: 4 additions & 4 deletions dspace-oai/src/main/java/org/dspace/xoai/app/XOAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public int index() throws DSpaceSolrIndexerException {
}

}
solrServerResolver.getServer().commit();
solrServerResolver.getServer().commit(false, false);

if (optimize) {
println("Optimizing Index");
Expand Down Expand Up @@ -340,7 +340,7 @@ private int index(Iterator<Item> iterator) throws DSpaceSolrIndexerException {
if (i % batchSize == 0) {
System.out.println(i + " items imported so far...");
server.add(list);
server.commit();
server.commit(false, false);
list.clear();
}
}
Expand All @@ -349,7 +349,7 @@ private int index(Iterator<Item> iterator) throws DSpaceSolrIndexerException {
if (!list.isEmpty()) {
server.add(list);
}
server.commit(true, true);
server.commit(false, false);
list.clear();
}
return i;
Expand Down Expand Up @@ -553,7 +553,7 @@ private void clearIndex() throws DSpaceSolrIndexerException {
try {
System.out.println("Clearing index");
solrServerResolver.getServer().deleteByQuery("*:*");
solrServerResolver.getServer().commit();
solrServerResolver.getServer().commit(false, false);
System.out.println("Index cleared");
} catch (SolrServerException | IOException ex) {
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
Expand Down

0 comments on commit 2685fbd

Please sign in to comment.