mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
mq: name compacted files and skip compacted windows by their source logs
Now that several windows can start in one second, compaction named every output after its first message's second, so groups starting in the same second overwrote each other, and it skipped any window whose start second was not after the newest compacted row, so a later window in that second was never compacted. A parquet file is now named after its first source log, which is unique, and a window is skipped when a parquet file lists it as a source; the start-second rule remains only for windows named by their second alone.
This commit is contained in:
1 parent
b0b34f41d1
commit
9964b86f8b
3 files changed
+81
-20
No files matched your search
@@ -99,13 +99,13 @@ func compactTopicPartition(filerClient filer_pb.FilerClient, t topic.Topic, time
|
||||
|
||||
func compactTopicPartitionDir(filerClient filer_pb.FilerClient, topicName, partitionDir string, timeAgo time.Duration, recordType *schema_pb.RecordType, preference *operation.StoragePreference) error {
|
||||
// read all existing parquet files
|
||||
minTsNs, maxTsNs, err := readAllParquetFiles(filerClient, partitionDir)
|
||||
maxTsNs, compacted, err := readAllParquetFiles(filerClient, partitionDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// read all log files
|
||||
logFiles, err := readAllLogFiles(filerClient, partitionDir, timeAgo, minTsNs, maxTsNs)
|
||||
logFiles, err := readAllLogFiles(filerClient, partitionDir, timeAgo, maxTsNs, compacted)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -156,7 +156,10 @@ func groupFilesBySize(logFiles []*filer_pb.Entry, maxGroupSize int64) (logFileGr
|
||||
return
|
||||
}
|
||||
|
||||
func readAllLogFiles(filerClient filer_pb.FilerClient, partitionDir string, timeAgo time.Duration, minTsNs, maxTsNs int64) (logFiles []*filer_pb.Entry, err error) {
|
||||
// readAllLogFiles lists the log files no parquet file holds yet. A compacted file is
|
||||
// known by name from the parquet sources; a log file named by its start second only
|
||||
// is also taken as compacted when that second is not after the newest parquet row.
|
||||
func readAllLogFiles(filerClient filer_pb.FilerClient, partitionDir string, timeAgo time.Duration, maxTsNs int64, compacted map[string]bool) (logFiles []*filer_pb.Entry, err error) {
|
||||
err = filer_pb.ReadDirAllEntries(context.Background(), filerClient, util.FullPath(partitionDir), "", func(entry *filer_pb.Entry, isLast bool) error {
|
||||
if strings.HasSuffix(entry.Name, ".parquet") {
|
||||
return nil
|
||||
@@ -169,7 +172,10 @@ func readAllLogFiles(filerClient filer_pb.FilerClient, partitionDir string, time
|
||||
// glog.Warningf("parse log time %s: %v", entry.Name, err)
|
||||
return nil
|
||||
}
|
||||
if maxTsNs > 0 && logTime.UnixNano() <= maxTsNs {
|
||||
if compacted[entry.Name] {
|
||||
return nil
|
||||
}
|
||||
if entry.Name == topic.LogFileTimePrefix(entry.Name) && maxTsNs > 0 && logTime.UnixNano() <= maxTsNs {
|
||||
return nil
|
||||
}
|
||||
logFiles = append(logFiles, entry)
|
||||
@@ -178,7 +184,10 @@ func readAllLogFiles(filerClient filer_pb.FilerClient, partitionDir string, time
|
||||
return
|
||||
}
|
||||
|
||||
func readAllParquetFiles(filerClient filer_pb.FilerClient, partitionDir string) (minTsNs, maxTsNs int64, err error) {
|
||||
// readAllParquetFiles reports the newest row any parquet file holds and the log
|
||||
// files the parquet files were made from.
|
||||
func readAllParquetFiles(filerClient filer_pb.FilerClient, partitionDir string) (maxTsNs int64, compacted map[string]bool, err error) {
|
||||
compacted = make(map[string]bool)
|
||||
err = filer_pb.ReadDirAllEntries(context.Background(), filerClient, util.FullPath(partitionDir), "", func(entry *filer_pb.Entry, isLast bool) error {
|
||||
if !strings.HasSuffix(entry.Name, ".parquet") {
|
||||
return nil
|
||||
@@ -186,15 +195,11 @@ func readAllParquetFiles(filerClient filer_pb.FilerClient, partitionDir string)
|
||||
if len(entry.Extended) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// read min ts
|
||||
minTsBytes := entry.Extended[mq.ExtendedAttrTimestampMin]
|
||||
if len(minTsBytes) != 8 {
|
||||
return nil
|
||||
}
|
||||
minTs := int64(binary.BigEndian.Uint64(minTsBytes))
|
||||
if minTsNs == 0 || minTs < minTsNs {
|
||||
minTsNs = minTs
|
||||
var sources []string
|
||||
if json.Unmarshal(entry.Extended[mq.ExtendedAttrSources], &sources) == nil {
|
||||
for _, source := range sources {
|
||||
compacted[source] = true
|
||||
}
|
||||
}
|
||||
|
||||
// read max ts
|
||||
@@ -380,7 +385,8 @@ func writeLogFilesToParquet(filerClient filer_pb.FilerClient, partitionDir strin
|
||||
}
|
||||
|
||||
// write to parquet file to partitionDir
|
||||
parquetFileName := fmt.Sprintf("%s.parquet", time.Unix(0, startTsNs).UTC().Format("2006-01-02-15-04-05"))
|
||||
// log file names are unique, so groups that start in one second keep apart
|
||||
parquetFileName := logFileGroups[0].Name + ".parquet"
|
||||
|
||||
// Collect source log file names and buffer_start metadata for deduplication
|
||||
var sourceLogFiles []string
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package logstore
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/mq"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// A window starting in an already compacted second is compacted unless a parquet
|
||||
// file lists it as a source.
|
||||
func TestCompactionSelectsWindowsBySourceName(t *testing.T) {
|
||||
second := time.Date(2026, 11, 30, 10, 0, 5, 0, time.UTC)
|
||||
compactedLog := "2026-11-30-10-00-05.00000000000000000100"
|
||||
sources, _ := json.Marshal([]string{compactedLog})
|
||||
log := func(name string) *filer_pb.Entry {
|
||||
return &filer_pb.Entry{Name: name, Attributes: &filer_pb.FuseAttributes{Crtime: 1}}
|
||||
}
|
||||
filer := &listingFiler{entries: []*filer_pb.Entry{
|
||||
log("2026-11-30-10-00-04"),
|
||||
log(compactedLog),
|
||||
{Name: compactedLog + ".parquet", Attributes: &filer_pb.FuseAttributes{Crtime: 1}, Extended: map[string][]byte{
|
||||
mq.ExtendedAttrTimestampMax: binary.BigEndian.AppendUint64(nil, uint64(second.Add(200*time.Millisecond).UnixNano())),
|
||||
mq.ExtendedAttrSources: sources,
|
||||
}},
|
||||
log("2026-11-30-10-00-05.00000000000000000200"),
|
||||
}}
|
||||
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()
|
||||
client := listingFilerClient{address: pb.NewServerAddressWithGrpcPort(listener.Addr().String(), listener.Addr().(*net.TCPAddr).Port)}
|
||||
|
||||
maxTsNs, compacted, err := readAllParquetFiles(client, "/topics/test/t/v1/0000-2520")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logFiles, err := readAllLogFiles(client, "/topics/test/t/v1/0000-2520", 0, maxTsNs, compacted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(logFiles) != 1 || logFiles[0].Name != "2026-11-30-10-00-05.00000000000000000200" {
|
||||
var names []string
|
||||
for _, f := range logFiles {
|
||||
names = append(names, f.Name)
|
||||
}
|
||||
t.Fatalf("selected %v, want only the window not yet compacted", names)
|
||||
}
|
||||
}
|
||||
@@ -4094,11 +4094,7 @@ func (e *SQLEngine) convertLogEntryToRecordValue(logEntry *filer_pb.LogEntry) (*
|
||||
// extractTimestampFromFilename extracts timestamp from parquet filename
|
||||
// Format: YYYY-MM-DD-HH-MM-SS.parquet
|
||||
func (e *SQLEngine) extractTimestampFromFilename(filename string) int64 {
|
||||
// Remove .parquet extension
|
||||
filename = strings.TrimSuffix(filename, ".parquet")
|
||||
|
||||
// Parse timestamp format: 2006-01-02-15-04-05
|
||||
t, err := time.Parse("2006-01-02-15-04-05", filename)
|
||||
t, err := topic.ParseLogFileTime(strings.TrimSuffix(filename, ".parquet"))
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user