DefaultAnnotationService extends DefaultService implements AnnotationService { @Override public void updateAnnotations(Map<Annotation, PrincipalUser> annotations) { requireNotDisposed(); requireArgument(annotations != null, "The set of annotations cannot be null."); List<Annotation> putAnnotationList = new LinkedList<>(); for (Entry<Annotation, PrincipalUser> entry : annotations.entrySet()) { PrincipalUser user = entry.getValue(); Annotation annotation = entry.getKey(); requireArgument(annotation != null, "The annotation cannot be null."); if (annotation.computeSizeBytes() > MAX_ANNOTATION_SIZE_BYTES) { _logger.debug("Annotation size of {} bytes exceeded max size {} allowed for annotation {}.", annotation.computeSizeBytes(), MAX_ANNOTATION_SIZE_BYTES, annotation); Map<String, String> tags = new HashMap<>(); tags.put("source", annotation.getSource()); _monitorService.modifyCounter(Counter.ANNOTATION_DROPS_MAXSIZEEXCEEDED, 1, tags); continue; } Map<String, String> fields = new HashMap<>(annotation.getFields()); String userName; if (user == null || (userName = user.getUserName()) == null || userName.isEmpty()) { fields.remove(USER_FIELD_NAME); } else { fields.put(USER_FIELD_NAME, userName); } annotation.setFields(fields); putAnnotationList.add(annotation); } _monitorService.modifyCounter(Counter.ANNOTATION_WRITES, putAnnotationList.size(), null); if (!putAnnotationList.isEmpty()) { _annotationStorageService.putAnnotations(putAnnotationList); } } @Inject  DefaultAnnotationService(AnnotationStorageService annotationStorageService, MonitorService monitorService, SystemConfiguration config); @Override List<Annotation> getAnnotations(String expression); @Override List<AnnotationQuery> getQueries(String expression); @Override void updateAnnotations(Map<Annotation, PrincipalUser> annotations); @Override void updateAnnotation(PrincipalUser user, Annotation annotation); @Override void dispose();  }
@Test public void updateAnnotations_testAnnotationSizeLessThanMax() { defaultAnnotationService.updateAnnotations(ImmutableMap.of(a, pu)); verify(monitorService).modifyCounter(Counter.ANNOTATION_WRITES, 1, null); ArgumentCaptor<List> annotationListCaptor = ArgumentCaptor.forClass(List.class); verify(tsdbService).putAnnotations(annotationListCaptor.capture()); assertEquals(1, annotationListCaptor.getValue().size()); assertTrue(annotationListCaptor.getValue().contains(a)); }
@Test public void updateAnnotations_testListContainingOneAnnotationSizeGreaterThanMax() { defaultAnnotationService.updateAnnotations(ImmutableMap.of(a, pu, tooLargeAnnotation, pu)); verify(monitorService).modifyCounter(Counter.ANNOTATION_DROPS_MAXSIZEEXCEEDED, 1, ImmutableMap.of("source", tooLargeAnnotation.getSource())); verify(monitorService).modifyCounter(Counter.ANNOTATION_WRITES, 1, null); ArgumentCaptor<List> annotationListCaptor = ArgumentCaptor.forClass(List.class); verify(tsdbService).putAnnotations(annotationListCaptor.capture()); assertEquals(1, annotationListCaptor.getValue().size()); }
@Test public void updateAnnotations_testOnlyOneAnnotationSizeGreaterThanMax() { defaultAnnotationService.updateAnnotations(ImmutableMap.of(tooLargeAnnotation, pu)); verify(monitorService).modifyCounter(Counter.ANNOTATION_DROPS_MAXSIZEEXCEEDED, 1, ImmutableMap.of("source", tooLargeAnnotation.getSource())); verify(monitorService).modifyCounter(Counter.ANNOTATION_WRITES, 0, null); verify(tsdbService, never()).putAnnotations(any()); }