use of org.komamitsu.fluency.format.RequestOption in project fluency by komamitsu.
the class PackedForwardBuffer method flushInternal.
@Override
public void flushInternal(Sender sender, boolean force) throws IOException {
moveRetentionBuffersToFlushable(force);
TaggableBuffer flushableBuffer;
ByteArrayOutputStream header = new ByteArrayOutputStream();
MessagePacker messagePacker = MessagePack.newDefaultPacker(header);
while (!Thread.currentThread().isInterrupted() && (flushableBuffer = flushableBuffers.poll()) != null) {
boolean keepBuffer = false;
try {
LOG.trace("flushInternal(): bufferUsage={}, flushableBuffer={}", getBufferUsage(), flushableBuffer);
String tag = flushableBuffer.getTag();
ByteBuffer dataBuffer = flushableBuffer.getByteBuffer();
int dataLength = dataBuffer.limit();
messagePacker.packArrayHeader(3);
messagePacker.packString(tag);
messagePacker.packRawStringHeader(dataLength);
messagePacker.flush();
ByteBuffer headerBuffer = ByteBuffer.wrap(header.toByteArray());
try {
if (config.isAckResponseMode()) {
byte[] uuidBytes = UUID.randomUUID().toString().getBytes(CHARSET);
ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, uuidBytes)));
List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
synchronized (sender) {
sender.sendWithAck(buffers, uuidBytes);
}
} else {
ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, null)));
List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
synchronized (sender) {
sender.send(buffers);
}
}
} catch (IOException e) {
LOG.warn("Failed to send data. The data is going to be saved into the buffer again: data={}", flushableBuffer);
keepBuffer = true;
throw e;
}
} finally {
header.reset();
if (keepBuffer) {
try {
flushableBuffers.put(flushableBuffer);
} catch (InterruptedException e1) {
LOG.warn("Failed to save the data into the buffer. Trying to save it in extra buffer: chunk={}", flushableBuffer);
backupBuffers.add(flushableBuffer);
}
} else {
bufferPool.returnBuffer(flushableBuffer.getByteBuffer());
}
}
}
}
Aggregations