ElasticSearchAnnotationService extends DefaultService implements AnnotationStorageService { @Override public List<Annotation> getAnnotations(List<AnnotationQuery> queries) { requireNotDisposed(); requireArgument(queries != null, "Annotation queries cannot be null."); List<Annotation> annotations = new ArrayList<>(); int from = 0, scrollSize = ANNOTATION_INDEX_MAX_RESULT_WINDOW; String requestUrl = String.format("/%s-*/_search", ANNOTATION_INDEX_TEMPLATE_PATTERN_START); try{ for (AnnotationQuery query : queries) { String queryJson = constructTermQuery(query, from, scrollSize); final long start = System.currentTimeMillis(); Request request = new Request(HttpMethod.POST.getName(), requestUrl); request.setEntity(new StringEntity(queryJson, ContentType.APPLICATION_JSON)); Response response = esRestClient.performRequest(request); final long time = System.currentTimeMillis() - start; logger.info("ES get request completed in {} ms", time); String str = ElasticSearchUtils.extractResponse(response); AnnotationRecordList list = ElasticSearchUtils.toEntity(str, new TypeReference<AnnotationRecordList>() {}, annotationMapper); annotations.addAll(list.getRecords()); if(annotations.size() == scrollSize) { logger.error("Maximum annotations limit execeeded for query- " + query.toString()); throw new RuntimeException(MessageFormat.format(EXCEPTION_MESSAGE, scrollSize)); } } } catch(IOException ex) { throw new SystemException(ex); } return annotations; } @Inject  ElasticSearchAnnotationService(SystemConfiguration config, MonitorService monitorService, ElasticSearchUtils esUtils); @Override void dispose(); @Override void putAnnotations(List<Annotation> annotations); @Override List<Annotation> getAnnotations(List<AnnotationQuery> queries); static final int ANNOTATION_INDEX_MAX_RESULT_WINDOW; static String ANNOTATION_INDEX_TEMPLATE_PATTERN_START; }
@Test public void testGetAnnotations() throws IOException { AnnotationQuery annotationQuery = new AnnotationQuery("scope1", "metric1", null, "unittest", 1557809359073L, 1557809599073L); List<AnnotationQuery> queries = new ArrayList<>(); queries.add(annotationQuery); ElasticSearchAnnotationService spyService = _initializeSpyService(esAnnotationService, getReply, false); List<Annotation> annotations = spyService.getAnnotations(queries); Annotation expectedAnnotation = new Annotation("unittest", "16ab4b56311", "unittest", "scope1", "metric1", 1557809559073L); assertEquals(expectedAnnotation, annotations.get(0)); ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); verify(restClient, times(1)).performRequest(requestCaptor.capture()); Request capturedRequest = requestCaptor.getValue(); String queryJson = convertToPrettyJson(EntityUtils.toString(capturedRequest.getEntity())); assertEquals(annotationQueryMustTermRange, queryJson); String expectedURL = "/argus-annotation-*/_search"; assertEquals(expectedURL, capturedRequest.getEndpoint()); }
@Test (expected = RuntimeException.class) public void testGetAnnotationsExceedingLimit(){ AnnotationQuery annotationQuery = new AnnotationQuery("scope1", "metric1", null, "unittest", 1557809359073L, 1557809599073L); List<AnnotationQuery> queries = new ArrayList<>(); queries.add(annotationQuery); ElasticSearchAnnotationService spyService = null; try { spyService = _initializeSpyService(esAnnotationService, getReply, false); } catch (IOException e) { fail(); } Whitebox.setInternalState(spyService, "ANNOTATION_INDEX_MAX_RESULT_WINDOW", 1); spyService.getAnnotations(queries); }