X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Fsearchdbabstraction%2Felasticsearch%2Fdao%2FElasticSearchHttpController.java;h=de38d28c1d1363d23f5461fd9eb165160bc86781;hb=0ed6c8a6bc6a2b067d11b14bb0a5be8e8680dd4f;hp=5fc1df0fa1a3268d052ced6027c03d734bf063f1;hpb=d5922af3f5edbb5ec083ca9b90c74c20a7a9328d;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java index 5fc1df0..de38d28 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java @@ -53,6 +53,7 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status.Family; +import org.eclipse.jetty.http.HttpStatus; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -76,6 +77,8 @@ import org.onap.aai.sa.searchdbabstraction.entity.Document; import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult; import org.onap.aai.sa.searchdbabstraction.entity.ErrorResult; import org.onap.aai.sa.searchdbabstraction.entity.OperationResult; +import org.onap.aai.sa.searchdbabstraction.entity.OperationResultBuilder; +import org.onap.aai.sa.searchdbabstraction.entity.OperationResultBuilder.Type; import org.onap.aai.sa.searchdbabstraction.entity.SearchHit; import org.onap.aai.sa.searchdbabstraction.entity.SearchHits; import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult; @@ -103,6 +106,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private static final String JSON_ATTR_ERROR = "error"; private static final String JSON_ATTR_REASON = "reason"; + private static final String DEFAULT_TYPE = "default"; private static final String QUERY_PARAM_VERSION = "?version="; private static final String MSG_RESOURCE_MISSING = "Specified resource does not exist: "; @@ -110,8 +114,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private static final String MSG_INVALID_DOCUMENT_URL = "Invalid document URL: "; private static final String MSG_HTTP_PUT_FAILED = "Failed to set HTTP request method to PUT."; private static final String MSG_HTTP_POST_FAILED = "Failed to set HTTP request method to POST."; - private static final String INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT = - "Internal Error: ElasticSearch operation fault occurred"; private static final String FAILED_TO_GET_THE_RESPONSE_CODE_FROM_THE_CONNECTION = "Failed to get the response code from the connection."; private static final String FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE = "Failed to parse Elastic Search response."; @@ -127,10 +129,23 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private final ElasticSearchConfig config; - private static final String DEFAULT_TYPE = "default"; - protected AnalysisConfiguration analysisConfig; + + public ElasticSearchHttpController(ElasticSearchConfig config) { + this.config = config; + analysisConfig = new AnalysisConfiguration(); + + try { + logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_ATTEMPT, getFullUrl("", false)); + checkConnection(); + logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_SUCCESS, getFullUrl("", false)); + } catch (Exception e) { + logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, getFullUrl("", false), + e.getMessage()); + } + } + public static ElasticSearchHttpController getInstance() { synchronized (ElasticSearchHttpController.class) { if (instance == null) { @@ -151,34 +166,15 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { return instance; } - public ElasticSearchHttpController(ElasticSearchConfig config) { - this.config = config; - analysisConfig = new AnalysisConfiguration(); - - try { - logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_ATTEMPT, getFullUrl("", false)); - checkConnection(); - logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_SUCCESS, getFullUrl("", false)); - } catch (Exception e) { - logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, getFullUrl("", false), - e.getMessage()); - } - } - public AnalysisConfiguration getAnalysisConfig() { return analysisConfig; } @Override public OperationResult createIndex(String index, DocumentSchema documentSchema) { - OperationResult result = new OperationResult(); - result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - try { - - // Submit the request to ElasticSearch to create the index using a - // default document type. - result = createTable(index, DEFAULT_TYPE, analysisConfig.getEsIndexSettings(), + // Submit the request to ElasticSearch to create the index using a default document type. + OperationResult result = createTable(index, DEFAULT_TYPE, analysisConfig.getEsIndexSettings(), DocumentSchemaUtil.generateDocumentMappings(documentSchema)); // ElasticSearch will return us a 200 code on success when we @@ -190,20 +186,18 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { if (isSuccess(result)) { result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}"); } + return result; } catch (DocumentStoreOperationException | IOException e) { - result.setFailureCause("Document store operation failure. Cause: " + e.getMessage()); + return new OperationResultBuilder().useDefaults() + .failureCause("Document store operation failure. Cause: " + e.getMessage()).build(); } - return result; } @Override public OperationResult createDynamicIndex(String index, String dynamicSchema) { - OperationResult result = new OperationResult(); - result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - try { - result = createTable(index, dynamicSchema); + OperationResult result = createTable(index, dynamicSchema); // ElasticSearch will return us a 200 code on success when we // want to report a 201, so translate the result here. @@ -213,21 +207,15 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { if (isSuccess(result)) { result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}"); } + return result; } catch (DocumentStoreOperationException e) { - result.setFailureCause("Document store operation failure. Cause: " + e.getMessage()); + return new OperationResultBuilder().useDefaults() + .failureCause("Document store operation failure. Cause: " + e.getMessage()).build(); } - - return result; } @Override public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException { - - // Initialize operation result with a failure codes / fault string - OperationResult opResult = new OperationResult(); - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -243,15 +231,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { throw new DocumentStoreOperationException("Failed to set HTTP request method to DELETE.", e); } - handleResponse(conn, opResult); - - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.DELETE_INDEX_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName); - + OperationResult opResult = handleResponse(conn); + logMetricsInfo(override, SearchDbMsgs.DELETE_INDEX_TIME, opResult, indexName); shutdownConnection(conn); return opResult; @@ -270,12 +251,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { int resultCode = conn.getResponseCode(); logger.debug("getClusterHealth() response Code : " + resultCode); - OperationResult opResult = new OperationResult(); - opResult.setResultCode(resultCode); shutdownConnection(conn); - return opResult; + return new OperationResultBuilder().resultCode(resultCode).build(); } private String getFullUrl(String resourceUrl, boolean isSecure) { @@ -333,7 +312,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { // @Override protected OperationResult createTable(String indexName, String typeName, String indexSettings, String indexMappings) throws DocumentStoreOperationException { - if (indexSettings == null) { logger.debug("No settings provided."); } @@ -342,12 +320,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("No mappings provided."); } - OperationResult opResult = new OperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -379,18 +351,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } logger.debug("\ncreateTable(), Sending 'PUT' request to URL : " + conn.getURL()); - logger.debug("Request content: " + sb.toString()); - - handleResponse(conn, opResult); + logger.debug("Request content: " + sb); + OperationResult opResult = handleResponse(conn); shutdownConnection(conn); - - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.CREATE_INDEX_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()), - override, indexName); + logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, opResult, indexName); return opResult; } @@ -406,10 +371,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { */ protected OperationResult createTable(String indexName, String settingsAndMappings) throws DocumentStoreOperationException { - OperationResult result = new OperationResult(); - result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - result.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -429,14 +390,9 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.error(SearchDbMsgs.INDEX_CREATE_FAILURE, e); throw new DocumentStoreOperationException(e.getMessage()); } - handleResponse(conn, result); - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.CREATE_INDEX_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()), - override, indexName); + OperationResult result = handleResponse(conn); + logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, result, indexName); return result; } @@ -452,12 +408,9 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { // which can lead to hard-to-debug behaviour with queries down the road. OperationResult indexExistsResult = checkIndexExistence(indexName); if (!isSuccess(indexExistsResult)) { - DocumentOperationResult opResult = new DocumentOperationResult(); - opResult.setResultCode(Status.NOT_FOUND.getStatusCode()); String resultMsg = "Document Index '" + indexName + "' does not exist."; - opResult.setResult(resultMsg); - opResult.setFailureCause(resultMsg); - return opResult; + return (DocumentOperationResult) new OperationResultBuilder(Type.DOCUMENT).status(Status.NOT_FOUND) + .result(resultMsg).failureCause(resultMsg).build(); } } @@ -483,11 +436,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { return opResult; } - opResult = new DocumentOperationResult(); - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -505,15 +453,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("Sending 'PUT' request to: " + conn.getURL()); - handleResponse(conn, opResult); + opResult = getOperationResult(conn); buildDocumentResult(opResult, indexName); - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.CREATE_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName); + logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, opResult, indexName); shutdownConnection(conn); @@ -522,12 +465,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private DocumentOperationResult createDocumentWithoutId(String indexName, DocumentStoreDataEntity document) throws DocumentStoreOperationException { - - DocumentOperationResult response = new DocumentOperationResult(); - // Initialize operation result with a failure codes / fault string - response.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - response.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -545,15 +482,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("Sending 'POST' request to: " + conn.getURL()); - handleResponse(conn, response); + DocumentOperationResult response = getOperationResult(conn); buildDocumentResult(response, indexName); - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.CREATE_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getResult()), - override, indexName); + logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, response, indexName); shutdownConnection(conn); @@ -568,11 +500,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { private DocumentOperationResult checkDocumentExistence(String indexName, String docId) throws DocumentStoreOperationException { - DocumentOperationResult opResult = new DocumentOperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -598,15 +525,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug(MSG_RESPONSE_CODE + resultCode); - opResult.setResultCode(resultCode); - - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.GET_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, docId); + DocumentOperationResult opResult = (DocumentOperationResult) new OperationResultBuilder(Type.DOCUMENT) + .useDefaults().resultCode(resultCode).build(); + logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, docId); shutdownConnection(conn); return opResult; @@ -632,12 +554,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } } - DocumentOperationResult opResult = new DocumentOperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -656,14 +572,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("Sending 'PUT' request to: " + conn.getURL()); - handleResponse(conn, opResult); + DocumentOperationResult opResult = getOperationResult(conn); buildDocumentResult(opResult, indexName); - // Generate a metrics log so we can track how long the operation took. - metricsLogger.info(SearchDbMsgs.UPDATE_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, document.getId()); + logMetricsInfo(override, SearchDbMsgs.UPDATE_DOCUMENT_TIME, opResult, indexName, document.getId()); shutdownConnection(conn); @@ -673,12 +585,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document) throws DocumentStoreOperationException { - DocumentOperationResult opResult = new DocumentOperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -695,7 +601,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("\nSending 'DELETE' request to " + conn.getURL()); - handleResponse(conn, opResult); + DocumentOperationResult opResult = getOperationResult(conn); buildDocumentResult(opResult, indexName); // supress the etag and url in response for delete as they are not required if (opResult.getDocument() != null) { @@ -703,11 +609,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { opResult.getDocument().setUrl(null); } - // Generate a metrics log so we can track how long the operation took. - metricsLogger.info(SearchDbMsgs.DELETE_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResult()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()), - override, indexName, document.getId()); + logMetricsInfo(override, SearchDbMsgs.DELETE_DOCUMENT_TIME, opResult, indexName, document.getId()); shutdownConnection(conn); @@ -717,12 +619,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document) throws DocumentStoreOperationException { - DocumentOperationResult opResult = new DocumentOperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -737,14 +633,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("\nSending 'GET' request to: " + conn.getURL()); - handleResponse(conn, opResult); + DocumentOperationResult opResult = getOperationResult(conn); buildDocumentResult(opResult, indexName); - // Generate a metrics log so we can track how long the operation took. - metricsLogger.info(SearchDbMsgs.GET_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, document.getId()); + logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, document.getId()); shutdownConnection(conn); @@ -753,11 +645,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public SearchOperationResult search(String indexName, String queryString) throws DocumentStoreOperationException { - SearchOperationResult opResult = new SearchOperationResult(); - - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); String fullUrl = getFullUrl("/" + indexName + "/_search" + "?" + queryString, false); @@ -775,14 +662,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("\nsearch(), Sending 'GET' request to URL : " + conn.getURL()); - handleResponse(conn, opResult); + SearchOperationResult opResult = getSearchOperationResult(conn); buildSearchResult(opResult, indexName); - - metricsLogger.info(SearchDbMsgs.QUERY_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, queryString); + logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, queryString); return opResult; } @@ -790,16 +673,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public SearchOperationResult searchWithPayload(String indexName, String query) throws DocumentStoreOperationException { - SearchOperationResult opResult = new SearchOperationResult(); - if (logger.isDebugEnabled()) { logger.debug("Querying index: " + indexName + " with query string: " + query); } - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - String fullUrl = getFullUrl("/" + indexName + "/_search", false); // Grab the current time so we can use it to generate a metrics log. @@ -819,14 +696,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL()); logger.debug("Request body = Elasticsearch query = " + query); - handleResponse(conn, opResult); + SearchOperationResult opResult = getSearchOperationResult(conn); buildSearchResult(opResult, indexName); - metricsLogger - .info(SearchDbMsgs.QUERY_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, query); + logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query); shutdownConnection(conn); @@ -837,17 +710,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public SearchOperationResult suggestionQueryWithPayload(String indexName, String query) throws DocumentStoreOperationException { - - SearchOperationResult opResult = new SearchOperationResult(); - if (logger.isDebugEnabled()) { logger.debug("Querying Suggestion index: " + indexName + " with query string: " + query); } - // Initialize operation result with a failure codes / fault string - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); - String fullUrl = getFullUrl("/" + indexName + "/_suggest", false); // Grab the current time so we can use it to generate a metrics log. @@ -867,14 +733,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL()); logger.debug("Request body = Elasticsearch query = " + query); - handleResponse(conn, opResult); + SearchOperationResult opResult = getSearchOperationResult(conn); buildSuggestResult(opResult, indexName); - metricsLogger - .info(SearchDbMsgs.QUERY_DOCUMENT_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()), - override, indexName, query); + logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query); shutdownConnection(conn); @@ -925,7 +787,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { return conn; } - private void handleResponse(HttpURLConnection conn, OperationResult opResult) + private OperationResult handleResponse(HttpURLConnection conn) throws DocumentStoreOperationException { + return handleResponse(conn, new OperationResultBuilder().useDefaults()); + } + + private OperationResult handleResponse(HttpURLConnection conn, OperationResultBuilder rb) throws DocumentStoreOperationException { int resultCode; @@ -967,15 +833,16 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } if (resultCode == Status.CONFLICT.getStatusCode()) { - opResult.setResultCode(Status.PRECONDITION_FAILED.getStatusCode()); + rb.resultCode(Status.PRECONDITION_FAILED.getStatusCode()); } else { - opResult.setResultCode(resultCode); + rb.resultCode(resultCode); } if (logger.isDebugEnabled()) { logger.debug("Raw result string from ElasticSearch = " + result.toString()); } - opResult.setResult(result.toString()); - opResult.setResultVersion(extractVersion(result.toString())); + rb.result(result.toString()); + rb.resultVersion(extractVersion(result.toString())); + return rb.build(); } private String extractVersion(String result) { @@ -1032,7 +899,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { @Override public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException { - if (logger.isDebugEnabled()) { StringBuilder dbgString = new StringBuilder("ESController: performBulkOperations - Operations: "); @@ -1146,13 +1012,13 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } } - // Finally, build the operation result and return it to the caller. - OperationResult result = new OperationResult(); - result.setResultCode(207); - result.setResult(buildGenericBulkResultSet(opResult, rejected)); + OperationResult result = new OperationResultBuilder() // + .resultCode(HttpStatus.MULTI_STATUS_207) // + .result(buildGenericBulkResultSet(opResult, rejected)) // + .build(); - // In the success case we don't want the entire result string to be - // dumped into the metrics log, so concatenate it. + // In the success case we don't want the entire result string to be dumped into the metrics log, so concatenate + // it. String resultStringForMetricsLog = result.getResult(); if (isSuccess(result)) { resultStringForMetricsLog = @@ -1160,14 +1026,14 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } metricsLogger.info(SearchDbMsgs.BULK_OPERATIONS_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode()) + new LogFields() // + .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode()) .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, resultStringForMetricsLog), override); return result; } - /** * This method converts a {@link BulkRequest} object into a json structure which can be understood by ElasticSearch. * @@ -1482,11 +1348,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { * @throws DocumentStoreOperationException */ public OperationResult checkIndexExistence(String indexName) throws DocumentStoreOperationException { - - // Initialize operation result with a failure codes / fault string - OperationResult opResult = new OperationResult(); - opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); - // Grab the current time so we can use it to generate a metrics log. MdcOverride override = getStartTime(new MdcOverride()); @@ -1512,20 +1373,21 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } logger.debug(MSG_RESPONSE_CODE + resultCode); - opResult.setResultCode(resultCode); - - // Generate a metrics log so we can track how long the operation took. - metricsLogger - .info(SearchDbMsgs.CHECK_INDEX_TIME, - new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode()) - .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()), - override, indexName); - + OperationResult opResult = new OperationResultBuilder().useDefaults().resultCode(resultCode).build(); + logMetricsInfo(override, SearchDbMsgs.CHECK_INDEX_TIME, opResult, indexName); shutdownConnection(conn); return opResult; } + private DocumentOperationResult getOperationResult(HttpURLConnection conn) throws DocumentStoreOperationException { + return (DocumentOperationResult) handleResponse(conn, new OperationResultBuilder(Type.DOCUMENT).useDefaults()); + } + + private SearchOperationResult getSearchOperationResult(HttpURLConnection conn) + throws DocumentStoreOperationException { + return (SearchOperationResult) handleResponse(conn, new OperationResultBuilder(Type.SEARCH).useDefaults()); + } private void buildDocumentResult(DocumentOperationResult result, String index) throws DocumentStoreOperationException { @@ -1562,7 +1424,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } private void buildSearchResult(SearchOperationResult result, String index) throws DocumentStoreOperationException { - JSONParser parser = new JSONParser(); JSONObject root; @@ -1612,7 +1473,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { } catch (Exception e) { throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult()); } - } private void buildSuggestResult(SearchOperationResult result, String index) throws DocumentStoreOperationException { @@ -1669,4 +1529,17 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult()); } } + + /** + * Record the timing of the operation in the metrics log. + * + */ + private void logMetricsInfo(MdcOverride override, SearchDbMsgs message, OperationResult operationResult, + String... args) { + metricsLogger.info(message, + new LogFields() // + .setField(LogLine.DefinedFields.RESPONSE_CODE, operationResult.getResultCode()) + .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, operationResult.getResult()), + override, args); + } }