mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-08-21 06:36:54 +00:00
* feat(s3/lifecycle): filer-backed cursor Persister FilerPersister persists per-shard cursor maps as JSON to /etc/s3/lifecycle/cursors/shard-NN.json via filer.SaveInsideFiler. One file per shard keeps Save atomic — the filer writes the entry in a single mutation, so a crash mid-write doesn't leak partial state. Pipeline.Run loads on start; the periodic checkpoint and graceful-shutdown save go through this implementation. A small FilerStore interface wraps the SeaweedFilerClient surface the persister needs, so tests inject an in-memory fake instead of mocking the full gRPC client. * refactor(s3/lifecycle): drop BlockerStore — durable cursor IS the block A frozen cursor doesn't advance, so the durable cursor (FilerPersister) encodes the blocked state on its own. On worker restart the reader re-encounters the poison event at MinTsNs, the dispatcher walks the same retry budget to BLOCKED, and the cursor freezes at the same EventTs. Other in-flight events between freeze tsNs and prior cursor positions self-resolve via NOOP_RESOLVED (STALE_IDENTITY) since the underlying objects were already deleted on the prior pass. Removed: - BlockerStore interface + InMemoryBlockerStore + BlockerRecord - Dispatcher.Blockers + Dispatcher.ReplayBlockers - the BlockerStore.Put call in handleBlocked - Pipeline.Blockers field + the ReplayBlockers call on startup Added a TestDispatchRestartReFreezesNaturally that pins the self-recovery property: a fresh Dispatcher with a fresh Cursor, fed the same poison event, reaches the same frozen state at the same EventTs without any durable blocker store. Operator visibility: a cursor whose MinTsNs hasn't advanced is the signal — surfaced via the durable cursor file. * refactor(filer): SaveInsideFiler accepts ctx ReadInsideFiler already takes ctx; SaveInsideFiler used context.Background() internally and silently dropped the caller's ctx. Symmetric API now; cancellation/deadlines propagate through LookupEntry / CreateEntry / UpdateEntry. Mechanical update of all callers — most pass context.Background() since the existing call sites have no ctx in scope. * fix(s3/lifecycle): deterministic order in cursor save Iterating Go maps yields random order, so json.Encode produced a different byte sequence on each save even when the state hadn't changed. Sort entries by (Bucket, ActionKind, RuleHash) before encoding so the on-disk file diffs cleanly. New test pins byte-identical output across two saves of the same map. * fix(s3/lifecycle): log reason when freezing cursor in handleBlocked handleBlocked dropped the reason via _ = reason with a comment claiming the caller logged it; none of the three callers do. A frozen cursor is the only surface where the operator finds out something stuck, so the reason has to land somewhere. glog.Warningf with shard, key, eventTs, and the original reason — same shape the rest of the package uses.
182 lines
7.4 KiB
Go
182 lines
7.4 KiB
Go
package offset
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer_client"
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
|
|
)
|
|
|
|
// ConsumerGroupPosition represents a consumer's position in a partition
|
|
// This can be either a timestamp or an offset
|
|
type ConsumerGroupPosition struct {
|
|
Type string `json:"type"` // "offset" or "timestamp"
|
|
Value int64 `json:"value"` // The actual offset or timestamp value
|
|
OffsetType string `json:"offset_type"` // Optional: OffsetType enum name (e.g., "EXACT_OFFSET")
|
|
CommittedAt int64 `json:"committed_at"` // Unix timestamp in milliseconds when committed
|
|
Metadata string `json:"metadata"` // Optional: application-specific metadata
|
|
}
|
|
|
|
// ConsumerGroupOffsetStorage handles consumer group offset persistence
|
|
// Each consumer group gets its own offset file in a dedicated consumers/ subfolder:
|
|
// Path: /topics/{namespace}/{topic}/{version}/{partition}/consumers/{consumer_group}.offset
|
|
type ConsumerGroupOffsetStorage interface {
|
|
// SaveConsumerGroupOffset saves the committed offset for a consumer group
|
|
SaveConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string, offset int64) error
|
|
|
|
// SaveConsumerGroupPosition saves the committed position (offset or timestamp) for a consumer group
|
|
SaveConsumerGroupPosition(t topic.Topic, p topic.Partition, consumerGroup string, position *ConsumerGroupPosition) error
|
|
|
|
// LoadConsumerGroupOffset loads the committed offset for a consumer group (backward compatible)
|
|
LoadConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string) (int64, error)
|
|
|
|
// LoadConsumerGroupPosition loads the committed position for a consumer group
|
|
LoadConsumerGroupPosition(t topic.Topic, p topic.Partition, consumerGroup string) (*ConsumerGroupPosition, error)
|
|
|
|
// ListConsumerGroups returns all consumer groups for a topic partition
|
|
ListConsumerGroups(t topic.Topic, p topic.Partition) ([]string, error)
|
|
|
|
// DeleteConsumerGroupOffset removes the offset file for a consumer group
|
|
DeleteConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string) error
|
|
}
|
|
|
|
// FilerConsumerGroupOffsetStorage implements ConsumerGroupOffsetStorage using SeaweedFS filer
|
|
type FilerConsumerGroupOffsetStorage struct {
|
|
filerClientAccessor *filer_client.FilerClientAccessor
|
|
}
|
|
|
|
// NewFilerConsumerGroupOffsetStorageWithAccessor creates storage using a shared filer client accessor
|
|
func NewFilerConsumerGroupOffsetStorageWithAccessor(filerClientAccessor *filer_client.FilerClientAccessor) *FilerConsumerGroupOffsetStorage {
|
|
return &FilerConsumerGroupOffsetStorage{
|
|
filerClientAccessor: filerClientAccessor,
|
|
}
|
|
}
|
|
|
|
// SaveConsumerGroupOffset saves the committed offset for a consumer group
|
|
// Stores as: /topics/{namespace}/{topic}/{version}/{partition}/consumers/{consumer_group}.offset
|
|
// This is a convenience method that wraps SaveConsumerGroupPosition
|
|
func (f *FilerConsumerGroupOffsetStorage) SaveConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string, offset int64) error {
|
|
position := &ConsumerGroupPosition{
|
|
Type: "offset",
|
|
Value: offset,
|
|
OffsetType: schema_pb.OffsetType_EXACT_OFFSET.String(),
|
|
CommittedAt: time.Now().UnixMilli(),
|
|
}
|
|
return f.SaveConsumerGroupPosition(t, p, consumerGroup, position)
|
|
}
|
|
|
|
// SaveConsumerGroupPosition saves the committed position (offset or timestamp) for a consumer group
|
|
// Stores as JSON: /topics/{namespace}/{topic}/{version}/{partition}/consumers/{consumer_group}.offset
|
|
func (f *FilerConsumerGroupOffsetStorage) SaveConsumerGroupPosition(t topic.Topic, p topic.Partition, consumerGroup string, position *ConsumerGroupPosition) error {
|
|
partitionDir := topic.PartitionDir(t, p)
|
|
consumersDir := fmt.Sprintf("%s/consumers", partitionDir)
|
|
offsetFileName := fmt.Sprintf("%s.offset", consumerGroup)
|
|
|
|
// Marshal position to JSON
|
|
jsonBytes, err := json.Marshal(position)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal position to JSON: %w", err)
|
|
}
|
|
|
|
return f.filerClientAccessor.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
return filer.SaveInsideFiler(context.Background(), client, consumersDir, offsetFileName, jsonBytes)
|
|
})
|
|
}
|
|
|
|
// LoadConsumerGroupOffset loads the committed offset for a consumer group
|
|
// This method provides backward compatibility and returns just the offset value
|
|
func (f *FilerConsumerGroupOffsetStorage) LoadConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string) (int64, error) {
|
|
position, err := f.LoadConsumerGroupPosition(t, p, consumerGroup)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
return position.Value, nil
|
|
}
|
|
|
|
// LoadConsumerGroupPosition loads the committed position for a consumer group
|
|
func (f *FilerConsumerGroupOffsetStorage) LoadConsumerGroupPosition(t topic.Topic, p topic.Partition, consumerGroup string) (*ConsumerGroupPosition, error) {
|
|
partitionDir := topic.PartitionDir(t, p)
|
|
consumersDir := fmt.Sprintf("%s/consumers", partitionDir)
|
|
offsetFileName := fmt.Sprintf("%s.offset", consumerGroup)
|
|
|
|
var position *ConsumerGroupPosition
|
|
err := f.filerClientAccessor.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
data, err := filer.ReadInsideFiler(context.Background(), client, consumersDir, offsetFileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Parse JSON format
|
|
position = &ConsumerGroupPosition{}
|
|
if err := json.Unmarshal(data, position); err != nil {
|
|
return fmt.Errorf("invalid consumer group offset file format: %w", err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return position, nil
|
|
}
|
|
|
|
// ListConsumerGroups returns all consumer groups for a topic partition
|
|
func (f *FilerConsumerGroupOffsetStorage) ListConsumerGroups(t topic.Topic, p topic.Partition) ([]string, error) {
|
|
partitionDir := topic.PartitionDir(t, p)
|
|
consumersDir := fmt.Sprintf("%s/consumers", partitionDir)
|
|
var consumerGroups []string
|
|
|
|
err := f.filerClientAccessor.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
// Use ListEntries to get directory contents
|
|
stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{
|
|
Directory: consumersDir,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for {
|
|
resp, err := stream.Recv()
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
return err
|
|
}
|
|
|
|
entry := resp.Entry
|
|
if entry != nil && !entry.IsDirectory && entry.Name != "" {
|
|
// Check if this is a consumer group offset file (ends with .offset)
|
|
if len(entry.Name) > 7 && entry.Name[len(entry.Name)-7:] == ".offset" {
|
|
// Extract consumer group name (remove .offset suffix)
|
|
consumerGroup := entry.Name[:len(entry.Name)-7]
|
|
consumerGroups = append(consumerGroups, consumerGroup)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return consumerGroups, err
|
|
}
|
|
|
|
// DeleteConsumerGroupOffset removes the offset file for a consumer group
|
|
func (f *FilerConsumerGroupOffsetStorage) DeleteConsumerGroupOffset(t topic.Topic, p topic.Partition, consumerGroup string) error {
|
|
partitionDir := topic.PartitionDir(t, p)
|
|
consumersDir := fmt.Sprintf("%s/consumers", partitionDir)
|
|
offsetFileName := fmt.Sprintf("%s.offset", consumerGroup)
|
|
|
|
return f.filerClientAccessor.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
return filer_pb.DoRemove(context.Background(), client, consumersDir, offsetFileName, false, false, false, false, nil)
|
|
})
|
|
}
|