diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 441d85ee85c..1054ede2316 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -143,6 +143,9 @@ New Features value for the fetchMailsSince filter. (Peter Sturge, Timothy Potter) +* SOLR-6258: Added onRollback event handler hook to Data Import Handler (DIH). + (ehatcher) + Bug Fixes ---------------------- diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java index 97b763a43d2..6524041550c 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DocBuilder.java @@ -308,6 +308,9 @@ public class DocBuilder { writer.rollback(); statusMessages.put("", "Indexing failed. Rolled back all changes."); addStatusMessage("Rolledback"); + if ((config != null) && (config.getOnRollback() != null)) { + invokeEventListener(config.getOnRollback()); + } } private void doFullDump() { diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/DIHConfiguration.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/DIHConfiguration.java index ce302f9f2f7..a623d90a51f 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/DIHConfiguration.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/config/DIHConfiguration.java @@ -56,6 +56,7 @@ public class DIHConfiguration { private final List entities; private final String onImportStart; private final String onImportEnd; + private final String onRollback; private final List> functions; private final Script script; private final Map> dataSources; @@ -71,6 +72,7 @@ public class DIHConfiguration { this.deleteQuery = ConfigParseUtil.getStringAttribute(element, "deleteQuery", null); this.onImportStart = ConfigParseUtil.getStringAttribute(element, "onImportStart", null); this.onImportEnd = ConfigParseUtil.getStringAttribute(element, "onImportEnd", null); + this.onRollback = ConfigParseUtil.getStringAttribute(element, "onRollback", null); List modEntities = new ArrayList<>(); List l = ConfigParseUtil.getChildNodes(element, "entity"); boolean docRootFound = false; @@ -163,6 +165,9 @@ public class DIHConfiguration { public String getOnImportEnd() { return onImportEnd; } + public String getOnRollback() { + return onRollback; + } public List> getFunctions() { return functions; } diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestDocBuilder2.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestDocBuilder2.java index faa7dcfac29..59a6e35b997 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestDocBuilder2.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestDocBuilder2.java @@ -75,6 +75,17 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTestCase { assertTrue("Update request processor finish was not called", TestUpdateRequestProcessor.finishCalled); } + @Test + public void testRollbackHandler() throws Exception { + List rows = new ArrayList(); + rows.add(createMap("id", "1", "FORCE_ROLLBACK", "true")); + MockDataSource.setIterator("select * from x", rows.iterator()); + + runFullImport(dataConfigWithRollbackHandler); + + assertTrue("Rollback event listener was not called", RollbackEventListener.executed); + } + @Test @SuppressWarnings("unchecked") public void testDynamicFields() throws Exception { @@ -276,6 +287,13 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTestCase { } } + public static class ForcedExceptionTransformer extends Transformer { + @Override + public Object transformRow(Map row, Context context) { + throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "ForcedException"); + } + } + public static class MockDataSource2 extends MockDataSource { } @@ -298,6 +316,15 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTestCase { } } + public static class RollbackEventListener implements EventListener { + public static boolean executed = false; + + @Override + public void onEvent(Context ctx) { + executed = true; + } + } + private final String requestParamAsVariable = "\n" + " \n" + " \n" + @@ -350,6 +377,15 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTestCase { " \n" + ""; + private final String dataConfigWithRollbackHandler = " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + private final String dataConfigWithTemplatizedFieldNames = "\n" + " \n" + " \n" +