From 30c27cea26a7dd2f42d78a13746f9d823616e934 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Wed, 6 Nov 2024 09:49:20 +0100 Subject: [PATCH] handle the error upon commit --- .../eu/dnetlib/dhp/solr/SolrClientTest.java | 63 +++++++++++++++++++ .../dhp/RecordImporterApplication.java | 5 -- .../eu/dnetlib/dhp/solr/RecordImporter.java | 9 ++- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 openaire-solr-test/src/test/java/eu/dnetlib/dhp/solr/SolrClientTest.java diff --git a/openaire-solr-test/src/test/java/eu/dnetlib/dhp/solr/SolrClientTest.java b/openaire-solr-test/src/test/java/eu/dnetlib/dhp/solr/SolrClientTest.java new file mode 100644 index 0000000..d5b3e49 --- /dev/null +++ b/openaire-solr-test/src/test/java/eu/dnetlib/dhp/solr/SolrClientTest.java @@ -0,0 +1,63 @@ +package eu.dnetlib.dhp.solr; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.Http2SolrClient; +import org.apache.solr.client.solrj.impl.LBHttp2SolrClient; +import org.apache.solr.client.solrj.impl.LBSolrClient; +import org.apache.solr.client.solrj.response.QueryResponse; +import org.apache.solr.client.solrj.response.SolrPingResponse; +import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.junit.jupiter.api.*; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class SolrClientTest { + + private static final String DEFAULT_COLLECTION = "DMF-index-openaire"; + + public static final String SOLR_HTTP_URL_LB1 = String.format("http://localhost:8981/solr/%s", DEFAULT_COLLECTION); + + public static final String SOLR_HTTP_URL_LB2 = String.format("http://localhost:8982/solr/%s", DEFAULT_COLLECTION); + + public static final String SOLR_HTTP_URL_LB3 = String.format("http://localhost:8983/solr/%s", DEFAULT_COLLECTION); + + private static SolrClient client; + + @BeforeAll + static void setUp() { + final Http2SolrClient http2SolrClient = new Http2SolrClient.Builder().build(); + client = new LBHttp2SolrClient.Builder( + http2SolrClient, + new LBSolrClient.Endpoint(SOLR_HTTP_URL_LB1), + new LBSolrClient.Endpoint(SOLR_HTTP_URL_LB2), + new LBSolrClient.Endpoint(SOLR_HTTP_URL_LB3)) + .build(); + } + + @AfterAll + static void tearDown() throws IOException { + client.close(); + } + + @Test + @Order(1) + void testPing() throws SolrServerException, IOException { + SolrPingResponse rsp = client.ping(); + assertEquals(0, rsp.getStatus()); + } + + @Test + @Order(2) + void testQuery() throws SolrServerException, IOException { + + final ModifiableSolrParams query = new SolrQuery().add(CommonParams.Q, "*:*"); + QueryResponse rsp = client.query(query); + assertEquals(52247, rsp.getResults().getNumFound()); + } +} diff --git a/solr-importer/src/main/java/eu/dnetlib/dhp/RecordImporterApplication.java b/solr-importer/src/main/java/eu/dnetlib/dhp/RecordImporterApplication.java index 587f756..dd0f292 100644 --- a/solr-importer/src/main/java/eu/dnetlib/dhp/RecordImporterApplication.java +++ b/solr-importer/src/main/java/eu/dnetlib/dhp/RecordImporterApplication.java @@ -35,8 +35,6 @@ public class RecordImporterApplication { } } - System.out.println(); - log.info("**** EXECUTING - {} ***", APPLICATION_TITLE); Boolean isSparkSessionManaged = Optional @@ -71,11 +69,8 @@ public class RecordImporterApplication { RecordImporter.importRecords(conf, zkHost, collection, path, RecordImporter.BATCH_SIZE); log.info("**** DONE ***"); - System.out.println(); } - - private static ArgumentApplicationParser parseArguments(String[] args) throws IOException, ParseException { return ArgumentApplicationParser.parse( IOUtils.toString( diff --git a/solr-importer/src/main/java/eu/dnetlib/dhp/solr/RecordImporter.java b/solr-importer/src/main/java/eu/dnetlib/dhp/solr/RecordImporter.java index 44aa824..922dc15 100644 --- a/solr-importer/src/main/java/eu/dnetlib/dhp/solr/RecordImporter.java +++ b/solr-importer/src/main/java/eu/dnetlib/dhp/solr/RecordImporter.java @@ -8,6 +8,7 @@ import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.apache.solr.client.solrj.request.UpdateRequest; +import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrInputDocument; import org.apache.spark.SparkConf; @@ -36,7 +37,13 @@ public class RecordImporter implements Serializable { CloudClientParams params = new CloudClientParams(zkHost, collection); indexDocs(params, batchSize, spark.read().json(path)); CloudSolrClient client = CacheCloudSolrClient.getCachedCloudClient(params); - client.commit(collection); + UpdateResponse commitRsp = client.commit(collection); + if (commitRsp.getStatus() != 0) { + log.error("got exception during commit operation", commitRsp.getException()); + throw commitRsp.getException(); + } else { + log.info("Done"); + } }); }