io.deepsense.neptune.clientlibrary.services.channelvaluesender.BufferedChannelValueSender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neptune-client-library Show documentation
Show all versions of neptune-client-library Show documentation
Enables integration with Neptune in your Java code
/**
* 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.ChannelValues;
import io.deepsense.neptune.apiclient.model.ConfigInfo;
import io.deepsense.neptune.apiclient.model.Point;
import io.deepsense.neptune.clientlibrary.config.internal.ChannelValueBufferConfiguration;
import io.deepsense.neptune.clientlibrary.exceptions.common.NeptuneRuntimeException;
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 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.Optional;
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(ChannelValues 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 new NeptuneRuntimeException("Failed to send channel values!", exc);
}
}
}
private void addToBuffer(ChannelValues 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 (ChannelValues channelValuePackage: channelValuePackages) {
channelIdWithValues.compute(
channelValuePackage.getChannelId(),
(channelId, groupedChannelValues) -> {
if (!Optional.ofNullable(groupedChannelValues).isPresent()) {
return new ChannelValues().channelId(channelId).values(channelValuePackage.getValues());
} else {
List updatedChannelValues = Stream.concat(
groupedChannelValues.getValues().stream(),
channelValuePackage.getValues().stream())
.collect(Collectors.toList());
return new ChannelValues()
.channelId(groupedChannelValues.getChannelId())
.values(updatedChannelValues);
}
});
}
return new ArrayList<>(channelIdWithValues.values());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy