Skip to content
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

[ISSUE #7961] use BoundaryType in binarySearchInCQByTime #7968

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ public long getOffsetInQueueByTime(String topic, int queueId, long timestamp, Bo
if (high == null || high == -1) {
return 0;
}
return this.rocksDBConsumeQueueTable.binarySearchInCQByTime(topic, queueId, high, low, timestamp, minPhysicOffset);
return this.rocksDBConsumeQueueTable.binarySearchInCQByTime(topic, queueId, high, low, timestamp,
minPhysicOffset, boundaryType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.rocketmq.common.BoundaryType;
import org.apache.rocketmq.common.Pair;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.org.slf4j.Logger;
Expand Down Expand Up @@ -180,10 +181,10 @@ public void destroyCQ(final String topic, final int queueId, WriteBatch writeBat
}

public long binarySearchInCQByTime(String topic, int queueId, long high, long low, long timestamp,
long minPhysicOffset) throws RocksDBException {
long result = 0;
long minPhysicOffset, BoundaryType boundaryType) throws RocksDBException {
long result = -1L;
long targetOffset = -1L, leftOffset = -1L, rightOffset = -1L;
long leftValue = -1L, rightValue = -1L;
long ceiling = high, floor = low;
while (high >= low) {
long midOffset = low + ((high - low) >>> 1);
ByteBuffer byteBuffer = getCQInKV(topic, queueId, midOffset);
Expand All @@ -209,22 +210,64 @@ public long binarySearchInCQByTime(String topic, int queueId, long high, long lo
} else if (storeTime > timestamp) {
high = midOffset - 1;
rightOffset = midOffset;
rightValue = storeTime;
} else {
low = midOffset + 1;
leftOffset = midOffset;
leftValue = storeTime;
}
}
if (targetOffset != -1) {
// offset next to it might also share the same store-timestamp.
switch (boundaryType) {
case LOWER: {
while (true) {
long nextOffset = targetOffset - 1;
if (nextOffset < floor) {
break;
}
ByteBuffer byteBuffer = getCQInKV(topic, queueId, nextOffset);
long storeTime = byteBuffer.getLong(MSG_STORE_TIME_SIZE_OFFSET);
if (storeTime != timestamp) {
break;
}
targetOffset = nextOffset;
}
break;
}
case UPPER: {
while (true) {
long nextOffset = targetOffset + 1;
if (nextOffset > ceiling) {
break;
}
ByteBuffer byteBuffer = getCQInKV(topic, queueId, nextOffset);
long storeTime = byteBuffer.getLong(MSG_STORE_TIME_SIZE_OFFSET);
if (storeTime != timestamp) {
break;
}
targetOffset = nextOffset;
}
break;
}
default: {
log.warn("Unknown boundary type");
break;
}
}
result = targetOffset;
} else {
if (leftValue == -1) {
result = rightOffset;
} else if (rightValue == -1) {
result = leftOffset;
} else {
result = Math.abs(timestamp - leftValue) > Math.abs(timestamp - rightValue) ? rightOffset : leftOffset;
switch (boundaryType) {
case LOWER: {
result = rightOffset;
break;
}
case UPPER: {
result = leftOffset;
break;
}
default: {
log.warn("Unknown boundary type");
break;
}
}
}
return result;
Expand Down
Loading