All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deepsense.neptune.clientlibrary.services.channelvaluesender.BufferedChannelValueSender Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.services.channelvaluesender;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.apiclient.model.ConfigInfo;
import io.deepsense.neptune.apiclient.model.InputChannelValues;
import io.deepsense.neptune.apiclient.model.Point;
import io.deepsense.neptune.clientlibrary.config.internal.ChannelValueBufferConfiguration;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverterFactory;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiServiceBuilder;
import io.deepsense.neptune.clientlibrary.utils.exceptions.ApiExceptionWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueUtils.getEstimatedChannelValuesSize;

public class BufferedChannelValueSender implements ChannelValueSender {

    private static final Logger logger = LoggerFactory.getLogger(BufferedChannelValueSender.class);

    private final UUID jobId;

    private final ApiService apiService;

    private final ChannelValueBufferConfiguration bufferConfiguration;

    private final ChannelValueSendingStrategy sendingStrategy;

    private final Queue channelValueBuffer = new LinkedList<>();

    private final long maxBatchSizeInBytes;

    private long estimatedSizeOfBuffer = 0;

    public BufferedChannelValueSender(
            ApiServiceBuilder apiServiceBuilder,
            ApiModelConverterFactory apiModelConverterFactory,
            UUID jobId,
            ConfigInfo serverConfigInfo,
            ChannelValueBufferConfiguration bufferConfiguration,
            ChannelValueSendingStrategy sendingStrategy) {
        this.apiService = Preconditions.checkNotNull(apiServiceBuilder)
                .withApiModelConverter(Preconditions.checkNotNull(apiModelConverterFactory).create())
                .build();
        this.jobId = Preconditions.checkNotNull(jobId);
        this.bufferConfiguration = Preconditions.checkNotNull(bufferConfiguration);
        this.sendingStrategy = Preconditions.checkNotNull(sendingStrategy);

        this.maxBatchSizeInBytes =
                (long) (bufferConfiguration.getMaxBatchSizeAsFractionOfRequestSize()
                        * Preconditions.checkNotNull(serverConfigInfo).getMaxFormContentSize());
    }

    @Override
    public void delegateSend(InputChannelValues channelValuePackage) {
        long estimatedPackageSize = getEstimatedChannelValuesSize(channelValuePackage);

        if (estimatedPackageSize > maxBatchSizeInBytes) {
            throw new RuntimeException(
                    "The channel value exceeds the limit. Channel value's size should be checked in Channel.send()!");
        } else if (canBeAppendedToBuffer(estimatedPackageSize)) {
            addToBuffer(channelValuePackage, estimatedPackageSize);
            applySendingStrategy();
        } else {
            flush();
            addToBuffer(channelValuePackage, estimatedPackageSize);
        }
    }

    @Override
    public void flush() {
        if (!channelValueBuffer.isEmpty()) {
            try {
                List groupedChannelValues = groupValuesByChannelId(channelValueBuffer);
                clearBuffer();
                apiService.sendChannelValues(jobId, groupedChannelValues);
                sendingStrategy.onSend();
            } catch (ApiException exc) {
                throw ApiExceptionWrapper.wrappedApiException(exc);
            }
        }
    }

    private void addToBuffer(InputChannelValues channelValuePackage, long estimatedPackageSize) {
        channelValueBuffer.add(channelValuePackage);
        estimatedSizeOfBuffer += estimatedPackageSize;
    }

    private void clearBuffer() {
        channelValueBuffer.clear();
        estimatedSizeOfBuffer = 0;
    }

    private void applySendingStrategy() {
        if (sendingStrategy.shouldValuesBeSent(channelValueBuffer.size())) {
            flush();
        }
    }

    private boolean canBeAppendedToBuffer(long estimatedPackageSize) {
        return channelValueBuffer.size() < bufferConfiguration.getMaxPackageCount()
                && estimatedSizeOfBuffer + estimatedPackageSize <= maxBatchSizeInBytes;
    }

    private static List groupValuesByChannelId(
            Collection channelValuePackages) {

        Map channelIdWithValues = new HashMap<>();
        for (InputChannelValues channelValuePackage: channelValuePackages) {
            channelIdWithValues.compute(
                    channelValuePackage.getChannelId(),
                    (channelId, groupedChannelValues) -> {
                        if (groupedChannelValues == null) {
                            return new InputChannelValues()
                                    .channelId(channelId)
                                    .values(channelValuePackage.getValues())
                                    .isMetric(channelValuePackage.getIsMetric());
                        } else {
                            List updatedChannelValues = Stream.concat(
                                    groupedChannelValues.getValues().stream(),
                                    channelValuePackage.getValues().stream())
                                    .collect(Collectors.toList());
                            return new InputChannelValues()
                                    .channelId(groupedChannelValues.getChannelId())
                                    .values(updatedChannelValues)
                                    .isMetric(groupedChannelValues.getIsMetric());
                        }
                    });
        }
        return new ArrayList<>(channelIdWithValues.values());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy