mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
A save that applied but lost its answer was retried on the next filer under a new request id, refused as changed by its own write, and redone from the saved topic.conf: a balance move then found the target already leading and failed after saving. The same id lets the owner replay the recorded outcome. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
6.9 KiB
Go
206 lines
6.9 KiB
Go
package topic
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
|
)
|
|
|
|
type Topic struct {
|
|
Namespace string
|
|
Name string
|
|
}
|
|
|
|
func NewTopic(namespace string, name string) Topic {
|
|
return Topic{
|
|
Namespace: namespace,
|
|
Name: name,
|
|
}
|
|
}
|
|
func FromPbTopic(topic *schema_pb.Topic) Topic {
|
|
return Topic{
|
|
Namespace: topic.Namespace,
|
|
Name: topic.Name,
|
|
}
|
|
}
|
|
|
|
func (t Topic) ToPbTopic() *schema_pb.Topic {
|
|
return &schema_pb.Topic{
|
|
Namespace: t.Namespace,
|
|
Name: t.Name,
|
|
}
|
|
}
|
|
|
|
func (t Topic) String() string {
|
|
return fmt.Sprintf("%s.%s", t.Namespace, t.Name)
|
|
}
|
|
|
|
func (t Topic) Dir() string {
|
|
return fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
|
|
}
|
|
|
|
func (t Topic) ReadConfFile(client filer_pb.SeaweedFilerClient) (*mq_pb.ConfigureTopicResponse, error) {
|
|
data, err := filer.ReadInsideFiler(context.Background(), client, t.Dir(), filer.TopicConfFile)
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read topic.conf of %v: %w", t, err)
|
|
}
|
|
// parse into filer conf object
|
|
conf := &mq_pb.ConfigureTopicResponse{}
|
|
if err = jsonpb.Unmarshal(data, conf); err != nil {
|
|
return nil, fmt.Errorf("unmarshal topic %v conf: %w", t, err)
|
|
}
|
|
return conf, nil
|
|
}
|
|
|
|
// ErrConfChanged is a topic.conf save refused because topic.conf is no longer what was read.
|
|
var ErrConfChanged = errors.New("topic.conf changed since it was read")
|
|
|
|
// ReadConfFileWithMetadata reads the topic configuration along with its entry,
|
|
// which a later WriteConfFile requires unchanged.
|
|
func (t Topic) ReadConfFileWithMetadata(ctx context.Context, client filer_pb.SeaweedFilerClient) (*mq_pb.ConfigureTopicResponse, *filer_pb.Entry, error) {
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: t.Dir(),
|
|
Name: filer.TopicConfFile,
|
|
}
|
|
|
|
resp, err := filer_pb.LookupEntry(ctx, client, request)
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
return nil, nil, err
|
|
}
|
|
return nil, nil, fmt.Errorf("lookup topic.conf of %v: %w", t, err)
|
|
}
|
|
|
|
conf := &mq_pb.ConfigureTopicResponse{}
|
|
if err = jsonpb.Unmarshal(resp.Entry.Content, conf); err != nil {
|
|
return nil, nil, fmt.Errorf("unmarshal topic %v conf: %w", t, err)
|
|
}
|
|
|
|
return conf, resp.Entry, nil
|
|
}
|
|
|
|
// WriteConfFile saves conf only while topic.conf is still the entry previous was
|
|
// read as, or still absent when previous is nil; otherwise it fails with ErrConfChanged.
|
|
// A retry with the same requestId gets the outcome the filer recorded for it.
|
|
func (t Topic) WriteConfFile(ctx context.Context, client filer_pb.SeaweedFilerClient, conf *mq_pb.ConfigureTopicResponse, previous *filer_pb.Entry, requestId string) error {
|
|
var buf bytes.Buffer
|
|
filer.ProtoToText(&buf, conf)
|
|
content := buf.Bytes()
|
|
now := time.Now().Unix()
|
|
entry := &filer_pb.Entry{
|
|
Name: filer.TopicConfFile,
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
Crtime: now,
|
|
Mtime: now,
|
|
FileMode: uint32(0644),
|
|
FileSize: uint64(len(content)),
|
|
Md5: util.Md5(content),
|
|
},
|
|
Content: content,
|
|
}
|
|
condition := &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_NOT_EXISTS}
|
|
if previous != nil {
|
|
condition = &filer_pb.WriteCondition_Clause{Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, ExpectedEntry: previous}
|
|
if crtime := previous.GetAttributes().GetCrtime(); crtime != 0 {
|
|
entry.Attributes.Crtime = crtime
|
|
}
|
|
}
|
|
path := t.Dir() + "/" + filer.TopicConfFile
|
|
resp, err := client.ObjectTransaction(ctx, &filer_pb.ObjectTransactionRequest{
|
|
LockKey: path,
|
|
RouteKey: s3_constants.ObjectWriteRouteKeyPrefix + path,
|
|
ConditionKey: path,
|
|
Condition: &filer_pb.WriteCondition{Clauses: []*filer_pb.WriteCondition_Clause{condition}},
|
|
Mutations: []*filer_pb.ObjectMutation{{Type: filer_pb.ObjectMutation_PUT, Directory: t.Dir(), Entry: entry}},
|
|
RequestId: requestId,
|
|
})
|
|
if status.Code(err) == codes.FailedPrecondition || resp.GetErrorCode() == filer_pb.FilerError_PRECONDITION_FAILED {
|
|
return fmt.Errorf("save topic %v conf: %w", t, ErrConfChanged)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("save topic %v conf: %w", t, err)
|
|
}
|
|
if resp.Error != "" {
|
|
return fmt.Errorf("save topic %v conf: %s", t, resp.Error)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DiscoverPartitions discovers all partition directories for a topic by scanning the filesystem
|
|
// This centralizes partition discovery logic used across query engine, shell commands, etc.
|
|
func (t Topic) DiscoverPartitions(ctx context.Context, filerClient filer_pb.FilerClient) ([]string, error) {
|
|
var partitionPaths []string
|
|
|
|
// Scan the topic directory for version directories (e.g., v2025-09-01-07-16-34)
|
|
err := filer_pb.ReadDirAllEntries(ctx, filerClient, util.FullPath(t.Dir()), "", func(versionEntry *filer_pb.Entry, isLast bool) error {
|
|
if !versionEntry.IsDirectory {
|
|
return nil // Skip non-directories
|
|
}
|
|
|
|
// Parse version timestamp from directory name (e.g., "v2025-09-01-07-16-34")
|
|
if !IsValidVersionDirectory(versionEntry.Name) {
|
|
// Skip directories that don't match the version format
|
|
return nil
|
|
}
|
|
|
|
// Scan partition directories within this version (e.g., 0000-0630)
|
|
versionDir := fmt.Sprintf("%s/%s", t.Dir(), versionEntry.Name)
|
|
return filer_pb.ReadDirAllEntries(ctx, filerClient, util.FullPath(versionDir), "", func(partitionEntry *filer_pb.Entry, isLast bool) error {
|
|
if !partitionEntry.IsDirectory {
|
|
return nil // Skip non-directories
|
|
}
|
|
|
|
// Parse partition boundary from directory name (e.g., "0000-0630")
|
|
if !IsValidPartitionDirectory(partitionEntry.Name) {
|
|
return nil // Skip invalid partition names
|
|
}
|
|
|
|
// Add this partition path to the list
|
|
partitionPath := fmt.Sprintf("%s/%s", versionDir, partitionEntry.Name)
|
|
partitionPaths = append(partitionPaths, partitionPath)
|
|
return nil
|
|
})
|
|
})
|
|
|
|
return partitionPaths, err
|
|
}
|
|
|
|
// IsValidVersionDirectory checks if a directory name matches the topic version format
|
|
// Format: v2025-09-01-07-16-34
|
|
func IsValidVersionDirectory(name string) bool {
|
|
if !strings.HasPrefix(name, "v") || len(name) != 20 {
|
|
return false
|
|
}
|
|
|
|
// Try to parse the timestamp part
|
|
timestampStr := name[1:] // Remove 'v' prefix
|
|
_, err := time.Parse("2006-01-02-15-04-05", timestampStr)
|
|
return err == nil
|
|
}
|
|
|
|
// IsValidPartitionDirectory checks if a directory name matches the partition boundary format
|
|
// Format: 0000-0630 (rangeStart-rangeStop)
|
|
func IsValidPartitionDirectory(name string) bool {
|
|
// Use existing ParsePartitionBoundary function to validate
|
|
start, stop := ParsePartitionBoundary(name)
|
|
|
|
// Valid partition ranges should have start < stop (and not both be 0, which indicates parse error)
|
|
return start < stop && start >= 0
|
|
}
|