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

[connector] Fluss support scan.startup.timestamp is larger than max timestamp of bucket. #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -21,6 +21,8 @@
import com.alibaba.fluss.client.table.snapshot.KvSnapshotInfo;
import com.alibaba.fluss.client.table.snapshot.PartitionSnapshotInfo;
import com.alibaba.fluss.client.utils.ClientRpcMessageUtils;
import com.alibaba.fluss.exception.ApiException;
import com.alibaba.fluss.exception.InvalidTimestampException;
import com.alibaba.fluss.lakehouse.LakeStorageInfo;
import com.alibaba.fluss.metadata.PartitionInfo;
import com.alibaba.fluss.metadata.PhysicalTablePath;
Expand Down Expand Up @@ -57,6 +59,9 @@
import com.alibaba.fluss.rpc.messages.TableExistsResponse;
import com.alibaba.fluss.rpc.protocol.ApiError;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;

import java.util.ArrayList;
Expand All @@ -68,6 +73,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

import static com.alibaba.fluss.client.admin.ListOffsetsResult.UNKNOWN_OFFSET;
import static com.alibaba.fluss.client.utils.ClientRpcMessageUtils.makeListOffsetsRequest;

/**
Expand All @@ -77,6 +83,7 @@
*/
public class FlussAdmin implements Admin {

private static final Logger log = LoggerFactory.getLogger(FlussAdmin.class);
private final AdminGateway gateway;
private final MetadataUpdater metadataUpdater;
private final RpcClient client;
Expand Down Expand Up @@ -323,11 +330,21 @@ private static void sendListOffsetsRequest(
for (PbListOffsetsRespForBucket resp :
r.getBucketsRespsList()) {
if (resp.hasErrorCode()) {
bucketToOffsetMap
.get(resp.getBucketId())
.completeExceptionally(
ApiError.fromErrorMessage(resp)
.exception());
ApiException exception =
ApiError.fromErrorMessage(resp).exception();
if (exception
instanceof InvalidTimestampException) {
log.warn(
"Invalid timestamp: "
+ exception.getMessage());
bucketToOffsetMap
.get(resp.getBucketId())
.complete(UNKNOWN_OFFSET);
} else {
bucketToOffsetMap
.get(resp.getBucketId())
.completeExceptionally(exception);
}
} else {
bucketToOffsetMap
.get(resp.getBucketId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
@PublicEvolving
public class ListOffsetsResult {
public static final long UNKNOWN_OFFSET = -1L;
private final Map<Integer, CompletableFuture<Long>> futures;

public ListOffsetsResult(Map<Integer, CompletableFuture<Long>> futures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,26 @@ public Map<Integer, Long> earliestOffsets(
@Override
public Map<Integer, Long> offsetsFromTimestamp(
@Nullable String partitionName, Collection<Integer> buckets, long timestamp) {
return listOffsets(partitionName, buckets, new OffsetSpec.TimestampSpec(timestamp));

// First get the current end offsets of the buckets. This is going to be used in case we
// cannot find a suitable offsets based on the timestamp, i.e. the message meeting the
// requirement of the timestamp have not been produced to fluss yet. In this case, we just
// use the latest offset. We need to get the latest offsets before querying offsets by time
// to ensure that no message is going to be missed.
Map<Integer, Long> endOffsets = latestOffsets(partitionName, buckets);
Map<Integer, Long> offsetsOfTimestamp =
listOffsets(partitionName, buckets, new OffsetSpec.TimestampSpec(timestamp));

Map<Integer, Long> initialOffsets = new HashMap<>();
for (Integer bucket : buckets) {
if (offsetsOfTimestamp.containsKey(bucket) && offsetsOfTimestamp.get(bucket) > 0) {
initialOffsets.put(bucket, offsetsOfTimestamp.get(bucket));
} else {
initialOffsets.put(bucket, endOffsets.get(bucket));
}
}

return initialOffsets;
}

private Map<Integer, Long> listOffsets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void testPkTableReadOnlySnapshot() throws Exception {

@Test
void testNonPkTableRead() throws Exception {
tEnv.executeSql("create table non_pk_table_test (a int, b varchar)");
tEnv.executeSql("create table f (a int, b varchar)");
TablePath tablePath = TablePath.of(DEFAULT_DB, "non_pk_table_test");

List<InternalRow> rows =
Expand Down Expand Up @@ -631,6 +631,49 @@ void testReadPrimaryKeyPartitionedTable() throws Exception {
assertResultsIgnoreOrder(rowIter, expectedRowValues, true);
}

@Test
void testReadTimestampOutOfBounds() throws Exception {

tEnv.executeSql("create table timestamp_table (a int, b varchar) ");
TablePath tablePath = TablePath.of(DEFAULT_DB, "timestamp_table");

// write first bath records
List<InternalRow> rows =
Arrays.asList(
row(DATA1_ROW_TYPE, new Object[] {1, "v1"}),
row(DATA1_ROW_TYPE, new Object[] {2, "v2"}),
row(DATA1_ROW_TYPE, new Object[] {3, "v3"}));

writeRows(tablePath, rows, true);
Thread.sleep(100);
// startup time between write first and second batch records.
long startupTimestamp = System.currentTimeMillis();

try (org.apache.flink.util.CloseableIterator<Row> rowIter =
tEnv.executeSql(
String.format(
"select * from timestamp_table /*+ OPTIONS('scan.startup.mode' = 'timestamp', 'scan.startup.timestamp' = '%s') */ ",
startupTimestamp))
.collect()) {
Thread.sleep(100);
// write second batch record.
rows =
Arrays.asList(
row(DATA1_ROW_TYPE, new Object[] {4, "v4"}),
row(DATA1_ROW_TYPE, new Object[] {5, "v5"}),
row(DATA1_ROW_TYPE, new Object[] {6, "v6"}));
writeRows(tablePath, rows, true);
List<String> expected = Arrays.asList("+I[4, v4]", "+I[5, v5]", "+I[6, v6]");
int expectRecords = expected.size();
List<String> actual = new ArrayList<>(expectRecords);
for (int i = 0; i < expectRecords; i++) {
String row = rowIter.next().toString();
actual.add(row);
}
assertThat(actual).containsExactlyElementsOf(expected);
}
}

// -------------------------------------------------------------------------------------
// Fluss look source tests
// -------------------------------------------------------------------------------------
Expand Down
Loading