Commit f2cb641e by skoritala Committed by Sarath Subramanian

ATLAS-3275 Basic Search with "query" string throws "Problem accessing /solr/vertex_index/freetext".

Made the return objects from V2 SOLR apis are handled correctly for error scenario. Made the code change to re-use solr client object. to avoid excessive Zookeeper Connections that are not getting closed. Signed-off-by: 's avatarSarath Subramanian <sarath@apache.org>
parent e230aad8
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.atlas.repository.graphdb.janus; package org.apache.atlas.repository.graphdb.janus;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasGraphIndexClient; import org.apache.atlas.repository.graphdb.AtlasGraphIndexClient;
import org.apache.atlas.repository.graphdb.AtlasGraphManagement; import org.apache.atlas.repository.graphdb.AtlasGraphManagement;
...@@ -27,6 +28,8 @@ import org.apache.solr.client.solrj.SolrClient; ...@@ -27,6 +28,8 @@ import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.V2Request; import org.apache.solr.client.solrj.request.V2Request;
import org.apache.solr.client.solrj.response.V2Response;
import org.apache.solr.common.util.NamedList;
import org.janusgraph.diskstorage.solr.Solr6Index; import org.janusgraph.diskstorage.solr.Solr6Index;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -83,7 +86,6 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient { ...@@ -83,7 +86,6 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
LOG.info("Attempting to update free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName); LOG.info("Attempting to update free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName);
updateFreeTextRequestHandler(solrClient, collectionName, attributeName2SearchWeightMap); updateFreeTextRequestHandler(solrClient, collectionName, attributeName2SearchWeightMap);
LOG.info("Successfully updated free text request handler {} for collection {}..", FREETEXT_REQUEST_HANDLER, collectionName); LOG.info("Successfully updated free text request handler {} for collection {}..", FREETEXT_REQUEST_HANDLER, collectionName);
return; return;
...@@ -97,7 +99,6 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient { ...@@ -97,7 +99,6 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
LOG.info("Attempting to create free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName); LOG.info("Attempting to create free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName);
createFreeTextRequestHandler(solrClient, collectionName, attributeName2SearchWeightMap); createFreeTextRequestHandler(solrClient, collectionName, attributeName2SearchWeightMap);
LOG.info("Successfully created free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName); LOG.info("Successfully created free text request handler {} for collection {}", FREETEXT_REQUEST_HANDLER, collectionName);
return; return;
...@@ -112,22 +113,53 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient { ...@@ -112,22 +113,53 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
throw lastExcp != null ? new RuntimeException(msg, lastExcp) : new RuntimeException(msg); throw lastExcp != null ? new RuntimeException(msg, lastExcp) : new RuntimeException(msg);
} finally { } finally {
LOG.debug("Releasing the solr client from usage.");
Solr6Index.releaseSolrClient(solrClient); Solr6Index.releaseSolrClient(solrClient);
} }
} }
private void updateFreeTextRequestHandler(SolrClient solrClient, String collectionName, Map<String, Integer> attributeName2SearchWeightMap) throws IOException, SolrServerException { private V2Response validateResponseForSuccess(V2Response v2Response) throws AtlasBaseException {
if(v2Response == null) {
String msg = "Received in valid response .";
LOG.error(msg);
throw new AtlasBaseException(msg);
}
if(LOG.isDebugEnabled()) {
LOG.debug("V2 Response is {}", v2Response.toString());
}
NamedList<Object> response = v2Response.getResponse();
Object errorMessages = response.get("errorMessages");
if(errorMessages != null) {
LOG.error("Error encountered in performing response handler action.");
List<Object> errorObjects = (List<Object>) errorMessages;
Map<Object, Object> errObject = (Map<Object, Object>) errorObjects.get(0);
List<String> msgs = (List<String>) errObject.get("errorMessages");
StringBuilder sb = new StringBuilder();
for(String msg: msgs) {
sb.append(msg);
}
String errors = sb.toString();
String msg = String.format("Error encountered in performing response handler action. %s.", errors);
LOG.error(msg);
throw new AtlasBaseException(msg);
} else {
LOG.debug("Successfully performed response handler action. V2 Response is {}", v2Response.toString());
}
return v2Response;
}
private V2Response updateFreeTextRequestHandler(SolrClient solrClient, String collectionName, Map<String, Integer> attributeName2SearchWeightMap) throws IOException, SolrServerException, AtlasBaseException {
String searchWeightString = generateSearchWeightString(graph.getManagementSystem(), collectionName, attributeName2SearchWeightMap); String searchWeightString = generateSearchWeightString(graph.getManagementSystem(), collectionName, attributeName2SearchWeightMap);
String payLoadString = generatePayLoadForFreeText("update-requesthandler", FREETEXT_REQUEST_HANDLER, searchWeightString); String payLoadString = generatePayLoadForFreeText("update-requesthandler", FREETEXT_REQUEST_HANDLER, searchWeightString);
performRequestHandlerAction(collectionName, solrClient, payLoadString); return performRequestHandlerAction(collectionName, solrClient, payLoadString);
} }
private void createFreeTextRequestHandler(SolrClient solrClient, String collectionName, Map<String, Integer> attributeName2SearchWeightMap) throws IOException, SolrServerException { private V2Response createFreeTextRequestHandler(SolrClient solrClient, String collectionName, Map<String, Integer> attributeName2SearchWeightMap) throws IOException, SolrServerException, AtlasBaseException {
String searchWeightString = generateSearchWeightString(graph.getManagementSystem(), collectionName, attributeName2SearchWeightMap); String searchWeightString = generateSearchWeightString(graph.getManagementSystem(), collectionName, attributeName2SearchWeightMap);
String payLoadString = generatePayLoadForFreeText("create-requesthandler", FREETEXT_REQUEST_HANDLER, searchWeightString); String payLoadString = generatePayLoadForFreeText("create-requesthandler", FREETEXT_REQUEST_HANDLER, searchWeightString);
performRequestHandlerAction(collectionName, solrClient, payLoadString); return performRequestHandlerAction(collectionName, solrClient, payLoadString);
} }
private String generateSearchWeightString(AtlasGraphManagement management, String indexName, Map<String, Integer> searchWeightsMap) { private String generateSearchWeightString(AtlasGraphManagement management, String indexName, Map<String, Integer> searchWeightsMap) {
...@@ -167,12 +199,12 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient { ...@@ -167,12 +199,12 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
"}", action, handlerName, qfValue); "}", action, handlerName, qfValue);
} }
private void performRequestHandlerAction(String collectionName, SolrClient solrClient, private V2Response performRequestHandlerAction(String collectionName, SolrClient solrClient,
String actionPayLoad) throws IOException, SolrServerException { String actionPayLoad) throws IOException, SolrServerException, AtlasBaseException {
V2Request v2Request = new V2Request.Builder(String.format("/collections/%s/config", collectionName)) V2Request v2Request = new V2Request.Builder(String.format("/collections/%s/config", collectionName))
.withMethod(SolrRequest.METHOD.POST) .withMethod(SolrRequest.METHOD.POST)
.withPayload(actionPayLoad) .withPayload(actionPayLoad)
.build(); .build();
v2Request.process(solrClient); return validateResponseForSuccess(v2Request.process(solrClient));
} }
} }
...@@ -139,6 +139,7 @@ public class Solr6Index implements IndexProvider { ...@@ -139,6 +139,7 @@ public class Solr6Index implements IndexProvider {
private static final char CHROOT_START_CHAR = '/'; private static final char CHROOT_START_CHAR = '/';
private static Solr6Index instance = null; private static Solr6Index instance = null;
public static final ConfigOption<Boolean> CREATE_SOLR_CLIENT_PER_REQUEST = new ConfigOption(SOLR_NS, "create-client-per-request", "when false, allows the sharing of solr client across other components.", org.janusgraph.diskstorage.configuration.ConfigOption.Type.LOCAL, true);
private enum Mode { private enum Mode {
HTTP, CLOUD; HTTP, CLOUD;
...@@ -168,6 +169,7 @@ public class Solr6Index implements IndexProvider { ...@@ -168,6 +169,7 @@ public class Solr6Index implements IndexProvider {
.build(); .build();
private static final Map<Geo, String> SPATIAL_PREDICATES = spatialPredicates(); private static final Map<Geo, String> SPATIAL_PREDICATES = spatialPredicates();
private static boolean createSolrClientPerRequest;
private final SolrClient solrClient; private final SolrClient solrClient;
private final Configuration configuration; private final Configuration configuration;
...@@ -179,6 +181,7 @@ public class Solr6Index implements IndexProvider { ...@@ -179,6 +181,7 @@ public class Solr6Index implements IndexProvider {
private final boolean waitSearcher; private final boolean waitSearcher;
private final boolean kerberosEnabled; private final boolean kerberosEnabled;
public Solr6Index(final Configuration config) throws BackendException { public Solr6Index(final Configuration config) throws BackendException {
// Add Kerberos-enabled SolrHttpClientBuilder // Add Kerberos-enabled SolrHttpClientBuilder
HttpClientUtil.setHttpClientBuilder(new Krb5HttpClientBuilder().getBuilder()); HttpClientUtil.setHttpClientBuilder(new Krb5HttpClientBuilder().getBuilder());
...@@ -203,21 +206,43 @@ public class Solr6Index implements IndexProvider { ...@@ -203,21 +206,43 @@ public class Solr6Index implements IndexProvider {
} }
solrClient = createSolrClient(); solrClient = createSolrClient();
createSolrClientPerRequest = config.get(CREATE_SOLR_CLIENT_PER_REQUEST);
if(createSolrClientPerRequest) {
logger.info("A new Solr Client will be created for direct interation with SOLR.");
} else {
logger.info("Solr Client will be shared for direct interation with SOLR.");
}
Solr6Index.instance = this; Solr6Index.instance = this;
} }
public static SolrClient getSolrClient() { public static SolrClient getSolrClient() {
return Solr6Index.instance != null ? Solr6Index.instance.createSolrClient() : null; if (Solr6Index.instance != null) {
if (createSolrClientPerRequest) {
logger.debug("Creating a new Solr Client.");
return Solr6Index.instance.createSolrClient();
} else {
logger.debug("Returning the solr client owned by Solr6Index.");
return Solr6Index.instance.solrClient;
}
} else {
logger.debug(" No Solr6Index available. Will return null");
return null;
}
} }
public static void releaseSolrClient(SolrClient solrClient) { public static void releaseSolrClient(SolrClient solrClient) {
if (solrClient != null) { if(createSolrClientPerRequest) {
try { if (solrClient != null) {
solrClient.close(); try {
} catch (IOException excp) { solrClient.close();
logger.warn("Failed to close SolrClient", excp); } catch (IOException excp) {
logger.warn("Failed to close SolrClient", excp);
}
} }
logger.debug("Closed the solr client successfully.");
} else {
logger.debug("Ignoring the closing of solr client as it is owned by Solr6Index.");
return;
} }
} }
private SolrClient createSolrClient() { private SolrClient createSolrClient() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment