diff --git a/weed/mq/logstore/compaction_test.go b/weed/mq/logstore/compaction_test.go index 71b35a2123..96b3d3a4ae 100644 --- a/weed/mq/logstore/compaction_test.go +++ b/weed/mq/logstore/compaction_test.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "net/http/httptest" + "slices" "sort" "strings" "sync" @@ -397,3 +398,39 @@ func TestParquetReadStopsWhenTheCallbackIsDone(t *testing.T) { t.Errorf("read %v done %v, want three rows and done", keys, isDone) } } + +func readParquetTimestamps(t *testing.T, read log_buffer.LogReadFromDiskFuncType, startTsNs, stopTsNs int64) (got []int64) { + t.Helper() + if _, _, err := read(log_buffer.NewMessagePosition(startTsNs, -2), stopTsNs, func(entry *filer_pb.LogEntry) (bool, error) { + got = append(got, entry.TsNs) + return false, nil + }); err != nil { + t.Fatal(err) + } + return got +} + +// A parquet file is named after its first source log, which can start seconds before +// rows it holds; a timestamp resume after its name still reads those rows, and rows +// out of time order are each kept or dropped by the stop time. +func TestParquetReadResumesInsideAnEarlierFile(t *testing.T) { + f, client := newCompactionFiler(t) + f.putTopicConf(t) + base := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC) + at := func(sec int) int64 { return base.Add(time.Duration(sec) * time.Second).UnixNano() } + f.putLogFile(t, base, 0, at(5), at(9)) + f.putLogFile(t, base.Add(time.Second), 2, at(1), at(7)) + compactSchemaless(t, client) + f.removeLogFiles() + read := GenParquetReadFunc(client, compactionTopic, compactionPartition) + + // once from cold and once with the reader having listed the partition before + for range 2 { + if got, want := readParquetTimestamps(t, read, at(6), 0), []int64{at(9), at(7)}; !slices.Equal(got, want) { + t.Errorf("resumed at 6s, read %v, want %v", got, want) + } + } + if got, want := readParquetTimestamps(t, read, 1, at(8)), []int64{at(5), at(1), at(7)}; !slices.Equal(got, want) { + t.Errorf("stopping at 8s read %v, want %v", got, want) + } +} diff --git a/weed/mq/logstore/read_parquet_to_log.go b/weed/mq/logstore/read_parquet_to_log.go index d9de50b288..09ebbae7e3 100644 --- a/weed/mq/logstore/read_parquet_to_log.go +++ b/weed/mq/logstore/read_parquet_to_log.go @@ -2,7 +2,6 @@ package logstore import ( "context" - "encoding/binary" "fmt" "io" "math" @@ -134,13 +133,11 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic if err != nil { return processedTsNs, false, fmt.Errorf("ToRecordValue failed: %w", err) } - processedTsNs = recordValue.Fields[SW_COLUMN_NAME_TS].GetInt64Value() - if processedTsNs <= starTsNs { + // rows of windows flushed across a reload are not in time order + tsNs := recordValue.Fields[SW_COLUMN_NAME_TS].GetInt64Value() + if tsNs <= starTsNs || (stopTsNs != 0 && tsNs >= stopTsNs) { continue } - if stopTsNs != 0 && processedTsNs >= stopTsNs { - return processedTsNs, false, nil - } data, marshalErr := proto.Marshal(recordValue) if marshalErr != nil { @@ -154,7 +151,7 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic logEntry := &filer_pb.LogEntry{ Key: recordValue.Fields[SW_COLUMN_NAME_KEY].GetBytesValue(), - TsNs: processedTsNs, + TsNs: tsNs, Data: data, Offset: offset, } @@ -169,6 +166,7 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic if isDone, err = eachLogEntryFn(logEntry); err != nil { return processedTsNs, false, fmt.Errorf("process log entry %v: %w", logEntry, err) } + processedTsNs = max(processedTsNs, tsNs) if isDone { return processedTsNs, true, nil } @@ -187,14 +185,15 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic } } + index := &logFileIndex{} return func(startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastReadPosition log_buffer.MessagePosition, isDone bool, err error) { - startFileName := startPosition.Time.UTC().Format(topic.TIME_FORMAT) startTsNs := startPosition.Time.UnixNano() var processedTsNs int64 - var candidateFiles []*filer_pb.Entry + var candidateFiles, listed []*filer_pb.Entry + // a file is named after its first source log, so one named before the resume + // second can still hold later rows; list from where every earlier file ends before it err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { - return filer_pb.SeaweedList(context.Background(), client, partitionDir, "", func(entry *filer_pb.Entry, isLast bool) error { if entry.IsDirectory { return nil @@ -202,24 +201,13 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic if !strings.HasSuffix(entry.Name, ".parquet") { return nil } - if len(entry.Extended) == 0 { + listed = append(listed, entry) + minTsNs, hasMin := logFileTsNs(entry, mq.ExtendedAttrTimestampMin) + maxTsNs, hasMax := logFileTsNs(entry, mq.ExtendedAttrTimestampMax) + if !hasMin || !hasMax { return nil } - // read minTs from the parquet file - minTsBytes := entry.Extended[mq.ExtendedAttrTimestampMin] - if len(minTsBytes) != 8 { - return nil - } - minTsNs := int64(binary.BigEndian.Uint64(minTsBytes)) - - // read max ts - maxTsBytes := entry.Extended[mq.ExtendedAttrTimestampMax] - if len(maxTsBytes) != 8 { - return nil - } - maxTsNs := int64(binary.BigEndian.Uint64(maxTsBytes)) - if stopTsNs != 0 && stopTsNs <= minTsNs { return nil } @@ -231,13 +219,17 @@ func GenParquetReadFunc(filerClient filer_pb.FilerClient, t topic.Topic, p topic candidateFiles = append(candidateFiles, entry) return nil - }, startFileName, true, math.MaxInt32) + }, index.markBefore(startTsNs), true, math.MaxInt32) }) if err != nil { return startPosition, false, err } + index.add(listed) for _, entry := range candidateFiles { - if processedTsNs, isDone, err = eachFileFn(entry, eachLogEntryFn, startTsNs, stopTsNs); err != nil || isDone { + var fileTsNs int64 + fileTsNs, isDone, err = eachFileFn(entry, eachLogEntryFn, startTsNs, stopTsNs) + processedTsNs = max(processedTsNs, fileTsNs) + if err != nil || isDone { break } }