From 64357bc913f6dfe2475294accced833a83b44340 Mon Sep 17 00:00:00 2001 From: Munendra S N Date: Mon, 29 Jul 2019 09:40:29 +0530 Subject: [PATCH] SOLR-13643:add Getter/Setter in ResponseBuilder to handle analytic res --- solr/CHANGES.txt | 2 + .../handler/component/AnalyticsComponent.java | 55 +++++++++++-------- .../handler/component/ResponseBuilder.java | 42 +++++++++++--- .../component/ResponseBuilderTest.java | 33 +++++++++-- 4 files changed, 96 insertions(+), 36 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 3dca1b6fa0c..dbaa013eabe 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -71,6 +71,8 @@ Other Changes * SOLR-13629: Clean trailing whitespace from 'analytics' contrib (Neal Sidhwaney via Jason Gerlowski) +* SOLR-13643: Add Getters/Setters in ResponseBuilder for analytics response handling (Neal Sidhwaney via Munendra S N) + ================== 8.2.0 ================== Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. diff --git a/solr/contrib/analytics/src/java/org/apache/solr/handler/component/AnalyticsComponent.java b/solr/contrib/analytics/src/java/org/apache/solr/handler/component/AnalyticsComponent.java index d48fe7858c9..6c69623157c 100644 --- a/solr/contrib/analytics/src/java/org/apache/solr/handler/component/AnalyticsComponent.java +++ b/solr/contrib/analytics/src/java/org/apache/solr/handler/component/AnalyticsComponent.java @@ -23,10 +23,10 @@ import org.apache.solr.analytics.AnalyticsRequestManager; import org.apache.solr.analytics.AnalyticsRequestParser; import org.apache.solr.analytics.ExpressionFactory; import org.apache.solr.analytics.stream.AnalyticsShardRequestManager; +import org.apache.solr.analytics.util.AnalyticsResponseHeadings; import org.apache.solr.analytics.util.OldAnalyticsParams; import org.apache.solr.analytics.util.OldAnalyticsRequestConverter; import org.apache.solr.common.util.NamedList; -import org.apache.solr.analytics.util.AnalyticsResponseHeadings; /** * Computes analytics requests. @@ -43,23 +43,31 @@ public class AnalyticsComponent extends SearchComponent { public void prepare(ResponseBuilder rb) throws IOException { // First check to see if there is an analytics request using the current format String analyticsRequest = rb.req.getParams().get(AnalyticsRequestParser.analyticsParamName); - rb._isOlapAnalytics = false; - rb.doAnalytics = false; + rb.setOlapAnalytics(false); + rb.setAnalytics(false); + boolean isDistributed = rb.isDistributed(); if (analyticsRequest != null) { - rb.doAnalytics = true; - rb._analyticsRequestManager = AnalyticsRequestParser.parse(analyticsRequest, new ExpressionFactory(rb.req.getSchema()), rb.isDistrib); + rb.setAnalytics(true); + rb.setAnalyticsRequestManager( + AnalyticsRequestParser.parse( + analyticsRequest, + new ExpressionFactory(rb.req.getSchema()), isDistributed)); } // If there is no request in the current format, check for the old olap-style format else if (rb.req.getParams().getBool(OldAnalyticsParams.OLD_ANALYTICS,false)) { - rb._analyticsRequestManager = AnalyticsRequestParser.parse(OldAnalyticsRequestConverter.convert(rb.req.getParams()), new ExpressionFactory(rb.req.getSchema()), rb.isDistrib); - rb._isOlapAnalytics = true; - rb.doAnalytics = true; + rb.setAnalyticsRequestManager( + AnalyticsRequestParser.parse( + OldAnalyticsRequestConverter.convert(rb.req.getParams()), + new ExpressionFactory(rb.req.getSchema()), isDistributed)); + rb.setOlapAnalytics(true); + rb.setAnalytics(true); } - if (rb.doAnalytics) { - AnalyticsRequestManager reqManager = (AnalyticsRequestManager)rb._analyticsRequestManager; + if (rb.isAnalytics()) { + AnalyticsRequestManager reqManager = getAnalyticsRequestManager(rb); + // Check to see if the request is distributed - if (rb.isDistrib) { + if (isDistributed) { reqManager.sendShards = true; reqManager.shardStream = new AnalyticsShardRequestManager(rb.req.getParams(), reqManager); } else { @@ -71,29 +79,29 @@ public class AnalyticsComponent extends SearchComponent { @Override public void process(ResponseBuilder rb) throws IOException { - if (!rb.doAnalytics) { + if (!rb.isAnalytics()) { return; } - AnalyticsRequestManager reqManager = (AnalyticsRequestManager)rb._analyticsRequestManager; + AnalyticsRequestManager reqManager = getAnalyticsRequestManager(rb); // Collect the data and generate a response AnalyticsDriver.drive(reqManager, rb.req.getSearcher(), rb.getResults().docSet.getTopFilter(), rb.req); - if (rb._isOlapAnalytics) { + if (rb.isOlapAnalytics()) { rb.rsp.add(AnalyticsResponseHeadings.COMPLETED_OLD_HEADER, reqManager.createOldResponse()); } else { rb.rsp.add(AnalyticsResponseHeadings.COMPLETED_HEADER, reqManager.createResponse()); } - rb.doAnalytics = false; + rb.setAnalytics(false); + rb.setOlapAnalytics(false); } - @Override public int distributedProcess(ResponseBuilder rb) throws IOException { - if (!rb.doAnalytics || rb.stage != ResponseBuilder.STAGE_EXECUTE_QUERY) { + if (!rb.isAnalytics() || rb.stage != ResponseBuilder.STAGE_EXECUTE_QUERY) { return ResponseBuilder.STAGE_DONE; } - AnalyticsRequestManager reqManager = (AnalyticsRequestManager)rb._analyticsRequestManager; + AnalyticsRequestManager reqManager = getAnalyticsRequestManager(rb); if (!reqManager.sendShards){ return ResponseBuilder.STAGE_DONE; } @@ -127,10 +135,10 @@ public class AnalyticsComponent extends SearchComponent { @Override public void finishStage(ResponseBuilder rb) { - if (rb.doAnalytics && rb.stage == ResponseBuilder.STAGE_GET_FIELDS) { - AnalyticsRequestManager reqManager = (AnalyticsRequestManager)rb._analyticsRequestManager; + if (rb.isAnalytics() && rb.stage == ResponseBuilder.STAGE_GET_FIELDS) { + AnalyticsRequestManager reqManager = getAnalyticsRequestManager(rb); // Generate responses from the merged shard data - if (rb._isOlapAnalytics) { + if (rb.isOlapAnalytics()) { rb.rsp.add(AnalyticsResponseHeadings.COMPLETED_OLD_HEADER, reqManager.createOldResponse()); } else { rb.rsp.add(AnalyticsResponseHeadings.COMPLETED_HEADER, reqManager.createResponse()); @@ -140,7 +148,6 @@ public class AnalyticsComponent extends SearchComponent { super.finishStage(rb); } - @Override public String getName() { return COMPONENT_NAME; @@ -151,6 +158,10 @@ public class AnalyticsComponent extends SearchComponent { return "Perform analytics"; } + private AnalyticsRequestManager getAnalyticsRequestManager(ResponseBuilder rb) { + return (AnalyticsRequestManager)rb.getAnalyticsRequestManager(); + } + /*@Override public NamedList getStatistics() { return analyticsCollector.getStatistics(); diff --git a/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java b/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java index 913c7104e99..61b10139a21 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java +++ b/solr/core/src/java/org/apache/solr/handler/component/ResponseBuilder.java @@ -16,6 +16,13 @@ */ package org.apache.solr.handler.component; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + import org.apache.lucene.search.Query; import org.apache.lucene.search.grouping.SearchGroup; import org.apache.lucene.search.grouping.TopGroups; @@ -23,7 +30,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.util.NamedList; -import org.apache.solr.util.RTimer; import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrRequestInfo; @@ -34,17 +40,11 @@ import org.apache.solr.search.DocSlice; import org.apache.solr.search.QParser; import org.apache.solr.search.QueryCommand; import org.apache.solr.search.QueryResult; -import org.apache.solr.search.SortSpec; import org.apache.solr.search.RankQuery; +import org.apache.solr.search.SortSpec; import org.apache.solr.search.grouping.GroupingSpecification; import org.apache.solr.search.grouping.distributed.command.QueryCommandResult; - -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.Set; +import org.apache.solr.util.RTimer; /** * This class is experimental and will be changing in the future. @@ -497,4 +497,28 @@ public class ResponseBuilder public void setNextCursorMark(CursorMark nextCursorMark) { this.nextCursorMark = nextCursorMark; } + + public void setAnalytics(boolean doAnalytics) { + this.doAnalytics = doAnalytics; + } + + public boolean isAnalytics() { + return this.doAnalytics; + } + + public void setAnalyticsRequestManager(Object analyticsRequestManager) { + this._analyticsRequestManager = analyticsRequestManager; + } + + public Object getAnalyticsRequestManager() { + return this._analyticsRequestManager; + } + + public void setOlapAnalytics(boolean isOlapAnalytics) { + this._isOlapAnalytics = isOlapAnalytics; + } + + public boolean isOlapAnalytics() { + return this._isOlapAnalytics; + } } diff --git a/solr/core/src/test/org/apache/solr/handler/component/ResponseBuilderTest.java b/solr/core/src/test/org/apache/solr/handler/component/ResponseBuilderTest.java index 8445054c405..28bad7a6cb7 100644 --- a/solr/core/src/test/org/apache/solr/handler/component/ResponseBuilderTest.java +++ b/solr/core/src/test/org/apache/solr/handler/component/ResponseBuilderTest.java @@ -26,6 +26,9 @@ import org.junit.BeforeClass; public class ResponseBuilderTest extends SolrTestCaseJ4 { + SolrQueryRequest req = req("q", "title:test"); + SolrQueryResponse rsp = new SolrQueryResponse(); + @BeforeClass public static void beforeClass() throws Exception { initCore("solrconfig.xml", "schema.xml"); @@ -33,13 +36,33 @@ public class ResponseBuilderTest extends SolrTestCaseJ4 { //This test is being added to verify responseBuilder.isDistributed() exists and is visible. public void testIsDistrib(){ - - SolrQueryRequest req = req("q", "title:test"); - SolrQueryResponse rsp = new SolrQueryResponse(); - ResponseBuilder responseBuilder = new ResponseBuilder(req, rsp, new ArrayList<>(0)); assertFalse(responseBuilder.isDistributed()); - } + public void testDoAnalyticsAccessors() { + ResponseBuilder rb = new ResponseBuilder(req, rsp, new ArrayList<>(0)); + assertFalse(rb.isAnalytics()); + rb.setAnalytics(true); + assertTrue(rb.isAnalytics()); + rb.setAnalytics(false); + assertFalse(rb.isAnalytics()); + } + + public void testIsOlapAnalyticsAccessors() { + ResponseBuilder rb = new ResponseBuilder(req, rsp, new ArrayList<>(0)); + assertFalse(rb.isOlapAnalytics()); + rb.setOlapAnalytics(true); + assertTrue(rb.isOlapAnalytics()); + rb.setOlapAnalytics(false); + assertFalse(rb.isOlapAnalytics()); + } + + public void testAnalyticsRequestManagerAccessors() { + ResponseBuilder rb = new ResponseBuilder(req, rsp, new ArrayList<>(0)); + assertNull(rb.getAnalyticsRequestManager()); + rb.setAnalyticsRequestManager(this); + assertNotNull(rb.getAnalyticsRequestManager()); + assertEquals(rb.getAnalyticsRequestManager(), this); + } }