/** * Iterates over each element in the RingBuffer, calling the {@link ForEachEvaluator#evaluate(Object) evaluate} method on each element in the RingBuffer. If the Evaluator returns {@code false}, * the method will skip all remaining elements in the RingBuffer; otherwise, the next element will be evaluated until all elements have been evaluated. * * @param evaluator used to evaluate each item in the ring buffer */ public void forEach(final ForEachEvaluator<T> evaluator) { forEach(evaluator, IterationDirection.FORWARD); }
@Override public void update(final ProvenanceEventRecord event, final StorageSummary storageSummary) { final String componentId = event.getComponentId(); final RingBuffer<Long> ringBuffer = latestRecords.computeIfAbsent(componentId, id -> new RingBuffer<>(1000)); ringBuffer.add(storageSummary.getEventId()); }
public List<T> asList() { return getSelectedElements(new Filter<T>() { @Override public boolean select(final T value) { return true; } }); }
final RingBuffer<ProvenanceEventRecord> latestRecords = new RingBuffer<>(1000); latestRecords.add(truncateAttributes(record)); records++; latestRecords.forEach(new ForEachEvaluator<ProvenanceEventRecord>() { @Override public boolean evaluate(final ProvenanceEventRecord event) {
@Override public void update(final Bulletin bulletin) { for (final RingBuffer<Bulletin> bulletinBuffer : getBulletinBuffers(bulletin)) { bulletinBuffer.add(bulletin); } } }
public VolatileComponentStatusRepository(final NiFiProperties nifiProperties) { numDataPoints = nifiProperties.getIntegerProperty(NUM_DATA_POINTS_PROPERTY, DEFAULT_NUM_DATA_POINTS); gcStatuses = new RingBuffer<>(numDataPoints); timestamps = new RingBuffer<>(numDataPoints); }
@Override public Optional<List<Long>> evaluate(final Query query) { if (latestRecords.getSize() < query.getMaxResults()) { return Optional.empty(); } if (query.getSearchTerms().isEmpty() && query.getStartDate() == null && query.getEndDate() == null) { final List<Long> eventList = latestRecords.asList(); if (eventList.size() > query.getMaxResults()) { return Optional.of(eventList.subList(0, query.getMaxResults())); } else { return Optional.of(eventList); } } else { return Optional.empty(); } }
@Override public long getContainerUsableSpace(String containerName) throws IOException { return maxSize - ringBuffer.getSize(); }
private synchronized StatusHistory getStatusHistory(final String componentId, final boolean includeCounters, final Set<MetricDescriptor<?>> defaultMetricDescriptors) { final ComponentStatusHistory history = componentStatusHistories.get(componentId); if (history == null) { return createEmptyStatusHistory(); } final List<Date> dates = timestamps.asList(); return history.toStatusHistory(dates, includeCounters, defaultMetricDescriptors); }
final RingBuffer<ProvenanceEventRecord> latestRecords = new RingBuffer<>(1000); latestRecords.add(truncateAttributes(record)); records++; latestRecords.forEach(new ForEachEvaluator<ProvenanceEventRecord>() { @Override public boolean evaluate(final ProvenanceEventRecord event) {
@Override public boolean evaluate(final ProvenanceEventRecord event) { latestRecordBuffer.add(event); return true; } });
RingBuffer<Bulletin> bulletinBuffer = componentMap.get(CONTROLLER_BULLETIN_STORE_KEY); if (bulletinBuffer == null) { bulletinBuffer = new RingBuffer<>(CONTROLLER_BUFFER_SIZE); final RingBuffer<Bulletin> existingBuffer = componentMap.putIfAbsent(CONTROLLER_BULLETIN_STORE_KEY, bulletinBuffer); if (existingBuffer != null) { RingBuffer<Bulletin> bulletinBuffer = componentMap.get(bulletin.getSourceId()); if (bulletinBuffer == null) { bulletinBuffer = new RingBuffer<>(COMPONENT_BUFFER_SIZE); final RingBuffer<Bulletin> existingBuffer = componentMap.putIfAbsent(bulletin.getSourceId(), bulletinBuffer); if (existingBuffer != null) {
@Override public Optional<List<Long>> evaluate(final Query query) { if (query.getMaxResults() > 1000) { // If query max results > 1000 then we know we don't have enough results. So just return empty. return Optional.empty(); } final List<SearchTerm> terms = query.getSearchTerms(); if (terms.size() != 1) { return Optional.empty(); } final SearchTerm term = terms.get(0); if (!COMPONENT_ID_FIELD_NAME.equals(term.getSearchableField().getSearchableFieldName())) { return Optional.empty(); } if (query.getEndDate() != null || query.getStartDate() != null) { return Optional.empty(); } final RingBuffer<Long> ringBuffer = latestRecords.get(term.getValue()); if (ringBuffer == null || ringBuffer.getSize() < query.getMaxResults()) { return Optional.empty(); } List<Long> eventIds = ringBuffer.asList(); if (eventIds.size() > query.getMaxResults()) { eventIds = eventIds.subList(0, query.getMaxResults()); } return Optional.of(eventIds); }
@Override public void run() { // Retrieve the most recent results and count the total number of matches final AtomicInteger matchingCount = new AtomicInteger(0); final List<ProvenanceEventRecord> matchingRecords = new ArrayList<>(maxRecords); ringBuffer.forEach(new ForEachEvaluator<ProvenanceEventRecord>() { @Override public boolean evaluate(final ProvenanceEventRecord record) { if (filter.select(record)) { if (matchingCount.incrementAndGet() <= maxRecords) { matchingRecords.add(record); } } return true; } }, IterationDirection.BACKWARD); submission.getResult().update(matchingRecords, matchingCount.get()); } }
public List<T> getSelectedElements(final Filter<T> filter) { return getSelectedElements(filter, Integer.MAX_VALUE); }
@Override public void registerEvent(final ProvenanceEventRecord event) { final long id = idGenerator.getAndIncrement(); ringBuffer.add(new IdEnrichedProvEvent(event, id)); }
public VolatileProvenanceRepository(final NiFiProperties nifiProperties) { maxSize = nifiProperties.getIntegerProperty(BUFFER_SIZE, DEFAULT_BUFFER_SIZE); ringBuffer = new RingBuffer<>(maxSize); final String indexedFieldString = nifiProperties.getProperty(NiFiProperties.PROVENANCE_INDEXED_FIELDS); final String indexedAttrString = nifiProperties.getProperty(NiFiProperties.PROVENANCE_INDEXED_ATTRIBUTES); searchableFields = Collections.unmodifiableList(SearchableFieldParser.extractSearchableFields(indexedFieldString, true)); searchableAttributes = Collections.unmodifiableList(SearchableFieldParser.extractSearchableFields(indexedAttrString, false)); final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); queryExecService = Executors.newFixedThreadPool(2, new ThreadFactory() { private final AtomicInteger counter = new AtomicInteger(0); @Override public Thread newThread(final Runnable r) { final Thread thread = defaultThreadFactory.newThread(r); thread.setName("Provenance Query Thread-" + counter.incrementAndGet()); return thread; } }); scheduledExecService = Executors.newScheduledThreadPool(2); }
final AsyncQuerySubmission result = new AsyncQuerySubmission(query, 1, userId); if (latestRecords.getSize() >= query.getMaxResults()) { final List<ProvenanceEventRecord> latestList = filterUnauthorizedEvents(latestRecords.asList(), user); final List<ProvenanceEventRecord> trimmed; if (latestList.size() > query.getMaxResults()) {
@Override public GarbageCollectionHistory getGarbageCollectionHistory(final Date start, final Date end) { final StandardGarbageCollectionHistory history = new StandardGarbageCollectionHistory(); gcStatuses.forEach(statusSet -> { for (final GarbageCollectionStatus gcStatus : statusSet) { if (gcStatus.getTimestamp().before(start)) { continue; } if (gcStatus.getTimestamp().after(end)) { continue; } history.addGarbageCollectionStatus(gcStatus); } return true; }); return history; } }
@Override public List<ProvenanceEventRecord> getEvents(final long firstRecordId, final int maxRecords, final NiFiUser user) throws IOException { return ringBuffer.getSelectedElements(new Filter<ProvenanceEventRecord>() { @Override public boolean select(final ProvenanceEventRecord value) { if (!isAuthorized(value, user)) { return false; } return value.getEventId() >= firstRecordId; } }, maxRecords); }