-
Notifications
You must be signed in to change notification settings - Fork 29
Fixed double compression of one message #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 77 additions & 41 deletions
118
topic/src/main/java/tech/ydb/topic/write/impl/EnqueuedMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,114 @@ | ||
package tech.ydb.topic.write.impl; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.protobuf.UnsafeByteOperations; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import tech.ydb.common.transaction.YdbTransaction; | ||
import tech.ydb.core.utils.ProtobufUtils; | ||
import tech.ydb.proto.topic.YdbTopic; | ||
import tech.ydb.topic.description.Codec; | ||
import tech.ydb.topic.description.MetadataItem; | ||
import tech.ydb.topic.settings.SendSettings; | ||
import tech.ydb.topic.utils.Encoder; | ||
import tech.ydb.topic.write.Message; | ||
import tech.ydb.topic.write.WriteAck; | ||
|
||
public class EnqueuedMessage { | ||
private final Message message; | ||
|
||
// use logger from WriterImpl | ||
private static final Logger logger = LoggerFactory.getLogger(WriterImpl.class); | ||
|
||
private Long seqNo; | ||
private byte[] bytes; | ||
private final long originLength; | ||
private final Instant createdAt; | ||
private final List<MetadataItem> items; | ||
|
||
private final CompletableFuture<WriteAck> future = new CompletableFuture<>(); | ||
private final AtomicBoolean isCompressed = new AtomicBoolean(); | ||
private final AtomicBoolean isProcessingFailed = new AtomicBoolean(); | ||
private final long uncompressedSizeBytes; | ||
private final YdbTransaction transaction; | ||
private long compressedSizeBytes; | ||
private Long seqNo; | ||
|
||
public EnqueuedMessage(Message message, SendSettings sendSettings) { | ||
this.message = message; | ||
this.uncompressedSizeBytes = message.getData().length; | ||
this.transaction = sendSettings != null ? sendSettings.getTransaction() : null; | ||
} | ||
private volatile boolean isReady = false; | ||
private volatile IOException compressError = null; | ||
|
||
public Message getMessage() { | ||
return message; | ||
} | ||
public EnqueuedMessage(Message message, SendSettings sendSettings, boolean noCompression) { | ||
this.bytes = message.getData(); | ||
this.createdAt = message.getCreateTimestamp(); | ||
this.items = message.getMetadataItems(); | ||
this.seqNo = message.getSeqNo(); | ||
|
||
public CompletableFuture<WriteAck> getFuture() { | ||
return future; | ||
} | ||
|
||
public boolean isCompressed() { | ||
return isCompressed.get(); | ||
this.originLength = bytes.length; | ||
this.transaction = sendSettings != null ? sendSettings.getTransaction() : null; | ||
this.isReady = noCompression; | ||
} | ||
|
||
public void setCompressed(boolean compressed) { | ||
this.isCompressed.set(compressed); | ||
public boolean isReady() { | ||
return isReady; | ||
} | ||
|
||
public boolean isProcessingFailed() { | ||
return isProcessingFailed.get(); | ||
public long getOriginalSize() { | ||
return originLength; | ||
} | ||
|
||
public void setProcessingFailed(boolean processingFailed) { | ||
isProcessingFailed.set(processingFailed); | ||
} | ||
|
||
public long getUncompressedSizeBytes() { | ||
return uncompressedSizeBytes; | ||
public long getSize() { | ||
return bytes.length; | ||
} | ||
|
||
public long getCompressedSizeBytes() { | ||
return compressedSizeBytes; | ||
public IOException getCompressError() { | ||
return compressError; | ||
} | ||
|
||
public void setCompressedSizeBytes(long compressedSizeBytes) { | ||
this.compressedSizeBytes = compressedSizeBytes; | ||
public void encode(String writeId, Codec codec) { | ||
logger.trace("[{}] Started encoding message", writeId); | ||
|
||
try { | ||
bytes = Encoder.encode(codec, bytes); | ||
isReady = true; | ||
logger.trace("[{}] Successfully finished encoding message", writeId); | ||
} catch (IOException ex) { | ||
logger.error("[{}] Exception while encoding message: ", writeId, ex); | ||
isReady = true; | ||
future.completeExceptionally(ex); | ||
} | ||
} | ||
|
||
public long getSizeBytes() { | ||
return isCompressed() ? getCompressedSizeBytes() : getUncompressedSizeBytes(); | ||
public CompletableFuture<WriteAck> getFuture() { | ||
return future; | ||
} | ||
|
||
public Long getSeqNo() { | ||
return seqNo; | ||
} | ||
|
||
public void setSeqNo(long seqNo) { | ||
this.seqNo = seqNo; | ||
} | ||
|
||
public YdbTransaction getTransaction() { | ||
return transaction; | ||
} | ||
|
||
long updateSeqNo(long lastSeqNo) { | ||
if (seqNo == null) { | ||
seqNo = lastSeqNo + 1; | ||
return seqNo; | ||
} | ||
return Math.max(lastSeqNo, seqNo); | ||
} | ||
|
||
YdbTopic.StreamWriteMessage.WriteRequest.MessageData toMessageData() { | ||
return YdbTopic.StreamWriteMessage.WriteRequest.MessageData.newBuilder() | ||
.setSeqNo(seqNo) | ||
.setData(UnsafeByteOperations.unsafeWrap(bytes)) | ||
.setCreatedAt(ProtobufUtils.instantToProto(createdAt)) | ||
.setUncompressedSize(originLength) | ||
.addAllMetadataItems(items.stream().map(it -> YdbTopic.MetadataItem.newBuilder() | ||
.setKey(it.getKey()) | ||
.setValue(UnsafeByteOperations.unsafeWrap(it.getValue())) | ||
.build() | ||
).collect(Collectors.toList())) | ||
.build(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
originalSize?