mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
A log file is named by its window's start second, taken from the writing broker's clock. A new leader whose clock is behind the previous one's wrote windows whose names sorted before older files holding lower offsets, so names no longer followed offsets. An offset read listing from an indexed file then skipped the new windows, and a read starting below such a file passed over it without notice; no reader-side check can see a file that sorts before where it starts listing. The partition fence now carries the newest log file time: the leader seeds it from the listing it already pages to continue the partition's offsets, which now also reports the newest log file name, legacy names included and compacted files, the fence and directories excluded. Each flush names its window no earlier than that time and advances it. Files sharing a second still sort by their min offset suffix. Timestamp reads resuming past the old leader's names now list such windows too, though entries timestamped before the resume point are still filtered out by timestamp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
3.7 KiB
Go
122 lines
3.7 KiB
Go
package mq
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
func rangeAttrs(min, max int64) map[string][]byte {
|
|
attrs := map[string][]byte{ExtendedAttrOffsetMin: make([]byte, 8), ExtendedAttrOffsetMax: make([]byte, 8)}
|
|
binary.BigEndian.PutUint64(attrs[ExtendedAttrOffsetMin], uint64(min))
|
|
binary.BigEndian.PutUint64(attrs[ExtendedAttrOffsetMax], uint64(max))
|
|
return attrs
|
|
}
|
|
|
|
func TestOffsetRangeBounds(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
files []map[string][]byte
|
|
earliest, hwm int64
|
|
}{
|
|
{"no files", nil, 0, 0},
|
|
{"no ranges", []map[string][]byte{{}}, 0, 0},
|
|
{"one message", []map[string][]byte{rangeAttrs(0, 0)}, 0, 1},
|
|
{"first window", []map[string][]byte{rangeAttrs(0, 199)}, 0, 200},
|
|
{"two windows", []map[string][]byte{rangeAttrs(200, 399), rangeAttrs(0, 199)}, 0, 400},
|
|
{"trimmed head", []map[string][]byte{rangeAttrs(500, 599)}, 500, 600},
|
|
} {
|
|
var r OffsetRange
|
|
for _, f := range tc.files {
|
|
r.Add(&filer_pb.Entry{Extended: f})
|
|
}
|
|
if earliest, hwm := r.Bounds(); earliest != tc.earliest || hwm != tc.hwm {
|
|
t.Errorf("%s: Bounds() = %d, %d; want %d, %d", tc.name, earliest, hwm, tc.earliest, tc.hwm)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Log files count by their start second under either naming; compacted files,
|
|
// the fence and directories do not count.
|
|
func TestOffsetRangeNewestLogFileTime(t *testing.T) {
|
|
newest := time.Date(2026, 11, 30, 10, 0, 7, 0, time.UTC)
|
|
var r OffsetRange
|
|
for _, entry := range []*filer_pb.Entry{
|
|
{Name: topic.PartitionFenceName},
|
|
{Name: topic.LogFileName(newest.Add(-2*time.Second), 0)},
|
|
{Name: newest.Format(topic.TIME_FORMAT)},
|
|
{Name: topic.LogFileName(newest.Add(-time.Second), 9)},
|
|
{Name: topic.LogFileName(newest.Add(time.Hour), 20) + ".parquet"},
|
|
{Name: newest.Add(time.Hour).Format(topic.TIME_FORMAT), IsDirectory: true},
|
|
} {
|
|
r.Add(entry)
|
|
}
|
|
if got := r.NewestLogFileTime(); !got.Equal(newest) {
|
|
t.Errorf("NewestLogFileTime() = %v, want %v", got, newest)
|
|
}
|
|
}
|
|
|
|
// listingFiler lists a directory the way the filer does: an unset limit means its
|
|
// default page of 1000.
|
|
type listingFiler struct {
|
|
filer_pb.UnimplementedSeaweedFilerServer
|
|
entries []*filer_pb.Entry
|
|
}
|
|
|
|
func (f *listingFiler) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) error {
|
|
limit := int(req.Limit)
|
|
if limit == 0 {
|
|
limit = 1000
|
|
}
|
|
sent := 0
|
|
for _, e := range f.entries {
|
|
if e.Name < req.StartFromFileName || (e.Name == req.StartFromFileName && !req.InclusiveStartFrom) {
|
|
continue
|
|
}
|
|
if sent == limit {
|
|
break
|
|
}
|
|
if err := stream.Send(&filer_pb.ListEntriesResponse{Entry: e}); err != nil {
|
|
return err
|
|
}
|
|
sent++
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestReadOffsetRangeScansPastTheDefaultPage(t *testing.T) {
|
|
filer := &listingFiler{}
|
|
for i := int64(0); i < 2500; i++ {
|
|
filer.entries = append(filer.entries, &filer_pb.Entry{Name: fmt.Sprintf("2026-11-30-00-00-%06d", i), Extended: rangeAttrs(i*10, i*10+9)})
|
|
}
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := grpc.NewServer()
|
|
filer_pb.RegisterSeaweedFilerServer(server, filer)
|
|
go server.Serve(listener)
|
|
defer server.Stop()
|
|
conn, err := grpc.NewClient(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
r, err := ReadOffsetRange(context.Background(), filer_pb.NewSeaweedFilerClient(conn), "/topics/test/t/v1/0000-2520")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if earliest, hwm := r.Bounds(); earliest != 0 || hwm != 25000 {
|
|
t.Fatalf("ReadOffsetRange = %d, %d; want 0, 25000", earliest, hwm)
|
|
}
|
|
}
|