Search in sources :

Example 1 with DataSubmissionTask

use of com.wavefront.agent.data.DataSubmissionTask in project java by wavefrontHQ.

the class QueueingFactoryImpl method getQueueController.

@SuppressWarnings("unchecked")
@Override
public <T extends DataSubmissionTask<T>> QueueController<T> getQueueController(@Nonnull HandlerKey handlerKey, int numThreads) {
    ScheduledExecutorService executor = executors.computeIfAbsent(handlerKey, x -> Executors.newScheduledThreadPool(numThreads, new NamedThreadFactory("queueProcessor-" + handlerKey.getEntityType() + "-" + handlerKey.getHandle())));
    List<QueueProcessor<T>> queueProcessors = IntStream.range(0, numThreads).mapToObj(i -> (QueueProcessor<T>) getQueueProcessor(handlerKey, executor, i)).collect(Collectors.toList());
    return (QueueController<T>) queueControllers.computeIfAbsent(handlerKey, x -> new QueueController<>(handlerKey, queueProcessors, backlogSize -> entityPropsFactory.get(handlerKey.getEntityType()).reportBacklogSize(handlerKey.getHandle(), backlogSize)));
}
Also used : IntStream(java.util.stream.IntStream) EventDataSubmissionTask(com.wavefront.agent.data.EventDataSubmissionTask) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EntityPropertiesFactory(com.wavefront.agent.data.EntityPropertiesFactory) DataSubmissionTask(com.wavefront.agent.data.DataSubmissionTask) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ReportableEntityType(com.wavefront.data.ReportableEntityType) NamedThreadFactory(com.wavefront.common.NamedThreadFactory) List(java.util.List) TaskInjector(com.wavefront.agent.data.TaskInjector) TreeMap(java.util.TreeMap) SourceTagSubmissionTask(com.wavefront.agent.data.SourceTagSubmissionTask) Map(java.util.Map) APIContainer(com.wavefront.agent.api.APIContainer) HandlerKey(com.wavefront.agent.handlers.HandlerKey) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LineDelimitedDataSubmissionTask(com.wavefront.agent.data.LineDelimitedDataSubmissionTask) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nonnull(javax.annotation.Nonnull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(com.wavefront.common.NamedThreadFactory)

Example 2 with DataSubmissionTask

use of com.wavefront.agent.data.DataSubmissionTask in project java by wavefrontHQ.

the class TaskQueueFactoryImpl method createTaskQueue.

private <T extends DataSubmissionTask<T>> TaskQueue<T> createTaskQueue(@Nonnull HandlerKey handlerKey, int threadNum) {
    String fileName = bufferFile + "." + handlerKey.getEntityType().toString() + "." + handlerKey.getHandle() + "." + threadNum;
    String lockFileName = fileName + ".lck";
    String spoolFileName = fileName + ".spool";
    // iron-clad guarantee, but it works well in most cases.
    try {
        File lockFile = new File(lockFileName);
        if (lockFile.exists()) {
            Files.deleteIfExists(lockFile.toPath());
        }
        FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
        if (channel.tryLock() == null) {
            throw new OverlappingFileLockException();
        }
    } catch (SecurityException e) {
        logger.severe("Error writing to the buffer lock file " + lockFileName + " - please make sure write permissions are correct for this file path and restart the " + "proxy: " + e);
        return new TaskQueueStub<>();
    } catch (OverlappingFileLockException e) {
        logger.severe("Error requesting exclusive access to the buffer " + "lock file " + lockFileName + " - please make sure that no other processes " + "access this file and restart the proxy: " + e);
        return new TaskQueueStub<>();
    } catch (IOException e) {
        logger.severe("Error requesting access to buffer lock file " + lockFileName + " Channel is " + "closed or an I/O error has occurred - please restart the proxy: " + e);
        return new TaskQueueStub<>();
    }
    try {
        File buffer = new File(spoolFileName);
        if (purgeBuffer) {
            if (buffer.delete()) {
                logger.warning("Retry buffer has been purged: " + spoolFileName);
            }
        }
        BiConsumer<Integer, Long> statsUpdater = (bytes, millis) -> {
            bytesWritten.inc(bytes);
            ioTimeWrites.inc(millis);
        };
        com.wavefront.agent.queueing.QueueFile queueFile = disableSharding ? new ConcurrentQueueFile(new TapeQueueFile(new QueueFile.Builder(new File(spoolFileName)).build(), statsUpdater)) : new ConcurrentShardedQueueFile(spoolFileName, ".spool", shardSize * 1024 * 1024, s -> new TapeQueueFile(new QueueFile.Builder(new File(s)).build(), statsUpdater));
        // TODO: allow configurable compression types and levels
        return new InstrumentedTaskQueueDelegate<>(new FileBasedTaskQueue<>(queueFile, new RetryTaskConverter<T>(handlerKey.getHandle(), TaskConverter.CompressionType.LZ4)), "buffer", ImmutableMap.of("port", handlerKey.getHandle()), handlerKey.getEntityType());
    } catch (Exception e) {
        logger.severe("WF-006: Unable to open or create queue file " + spoolFileName + ": " + e.getMessage());
        return new TaskQueueStub<>();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) Counter(com.yammer.metrics.core.Counter) TaggedMetricName(com.wavefront.common.TaggedMetricName) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DataSubmissionTask(com.wavefront.agent.data.DataSubmissionTask) IOException(java.io.IOException) Logger(java.util.logging.Logger) File(java.io.File) Objects(java.util.Objects) QueueFile(com.squareup.tape2.QueueFile) ExpectedAgentMetric(com.wavefront.metrics.ExpectedAgentMetric) TreeMap(java.util.TreeMap) Gauge(com.yammer.metrics.core.Gauge) Map(java.util.Map) HandlerKey(com.wavefront.agent.handlers.HandlerKey) BiConsumer(java.util.function.BiConsumer) Metrics(com.yammer.metrics.Metrics) Nonnull(javax.annotation.Nonnull) FileChannel(java.nio.channels.FileChannel) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) QueueFile(com.squareup.tape2.QueueFile) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Aggregations

DataSubmissionTask (com.wavefront.agent.data.DataSubmissionTask)2 HandlerKey (com.wavefront.agent.handlers.HandlerKey)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nonnull (javax.annotation.Nonnull)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 QueueFile (com.squareup.tape2.QueueFile)1 APIContainer (com.wavefront.agent.api.APIContainer)1 EntityPropertiesFactory (com.wavefront.agent.data.EntityPropertiesFactory)1 EventDataSubmissionTask (com.wavefront.agent.data.EventDataSubmissionTask)1 LineDelimitedDataSubmissionTask (com.wavefront.agent.data.LineDelimitedDataSubmissionTask)1 SourceTagSubmissionTask (com.wavefront.agent.data.SourceTagSubmissionTask)1 TaskInjector (com.wavefront.agent.data.TaskInjector)1 NamedThreadFactory (com.wavefront.common.NamedThreadFactory)1 TaggedMetricName (com.wavefront.common.TaggedMetricName)1 ReportableEntityType (com.wavefront.data.ReportableEntityType)1 ExpectedAgentMetric (com.wavefront.metrics.ExpectedAgentMetric)1 Metrics (com.yammer.metrics.Metrics)1