* fix: use keyed fields in struct literals - Replace unsafe reflect.StringHeader/SliceHeader with safe unsafe.String/Slice (weed/query/sqltypes/unsafe.go) - Add field names to Type_ScalarType struct literals (weed/mq/schema/schema_builder.go) - Add Duration field name to FlexibleDuration struct literals across test files - Add field names to bson.D struct literals (weed/filer/mongodb/mongodb_store_kv.go) Fixes go vet warnings about unkeyed struct literals. * fix: remove unreachable code - Remove unreachable return statements after infinite for loops - Remove unreachable code after if/else blocks where all paths return - Simplify recursive logic by removing unnecessary for loop (inode_to_path.go) - Fix Type_ScalarType literal to use enum value directly (schema_builder.go) - Call onCompletionFn on stream error (subscribe_session.go) Files fixed: - weed/query/sqltypes/unsafe.go - weed/mq/schema/schema_builder.go - weed/mq/client/sub_client/connect_to_sub_coordinator.go - weed/filer/redis3/ItemList.go - weed/mq/client/agent_client/subscribe_session.go - weed/mq/broker/broker_grpc_pub_balancer.go - weed/mount/inode_to_path.go - weed/util/skiplist/name_list.go * fix: avoid copying lock values in protobuf messages - Use proto.Merge() instead of direct assignment to avoid copying sync.Mutex in S3ApiConfiguration (iamapi_server.go) - Add explicit comments noting that channel-received values are already copies before taking addresses (volume_grpc_client_to_master.go) The protobuf messages contain sync.Mutex fields from the message state, which should not be copied. Using proto.Merge() properly merges messages without copying the embedded mutex. * fix: correct byte array size for uint32 bit shift operations The generateAccountId() function only needs 4 bytes to create a uint32 value. Changed from allocating 8 bytes to 4 bytes to match the actual usage. This fixes go vet warning about shifting 8-bit values (bytes) by more than 8 bits. * fix: ensure context cancellation on all error paths In broker_client_subscribe.go, ensure subscriberCancel() is called on all error return paths: - When stream creation fails - When partition assignment fails - When sending initialization message fails This prevents context leaks when an error occurs during subscriber creation. * fix: ensure subscriberCancel called for CreateFreshSubscriber stream.Send error Ensure subscriberCancel() is called when stream.Send fails in CreateFreshSubscriber. * ci: add go vet step to prevent future lint regressions - Add go vet step to GitHub Actions workflow - Filter known protobuf lock warnings (MessageState sync.Mutex) These are expected in generated protobuf code and are safe - Prevents accumulation of go vet errors in future PRs - Step runs before build to catch issues early * fix: resolve remaining syntax and logic errors in vet fixes - Fixed syntax errors in filer_sync.go caused by missing closing braces - Added missing closing brace for if block and function - Synchronized fixes to match previous commits on branch * fix: add missing return statements to daemon functions - Add 'return false' after infinite loops in filer_backup.go and filer_meta_backup.go - Satisfies declared bool return type signatures - Maintains consistency with other daemon functions (runMaster, runFilerSynchronize, runWorker) - While unreachable, explicitly declares the return satisfies function signature contract * fix: add nil check for onCompletionFn in SubscribeMessageRecord - Check if onCompletionFn is not nil before calling it - Prevents potential panic if nil function is passed - Matches pattern used in other callback functions * docs: clarify unreachable return statements in daemon functions - Add comments documenting that return statements satisfy function signature - Explains that these returns follow infinite loops and are unreachable - Improves code clarity for future maintainers
258 lines
8.3 KiB
Go
258 lines
8.3 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/spf13/viper"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var (
|
|
metaBackup FilerMetaBackupOptions
|
|
)
|
|
|
|
type FilerMetaBackupOptions struct {
|
|
grpcDialOption grpc.DialOption
|
|
filerAddress *string
|
|
filerDirectory *string
|
|
restart *bool
|
|
backupFilerConfig *string
|
|
|
|
store filer.FilerStore
|
|
clientId int32
|
|
clientEpoch int32
|
|
}
|
|
|
|
func init() {
|
|
cmdFilerMetaBackup.Run = runFilerMetaBackup // break init cycle
|
|
metaBackup.filerAddress = cmdFilerMetaBackup.Flag.String("filer", "localhost:8888", "filer hostname:port")
|
|
metaBackup.filerDirectory = cmdFilerMetaBackup.Flag.String("filerDir", "/", "a folder on the filer")
|
|
metaBackup.restart = cmdFilerMetaBackup.Flag.Bool("restart", false, "copy the full metadata before async incremental backup")
|
|
metaBackup.backupFilerConfig = cmdFilerMetaBackup.Flag.String("config", "", "path to filer.toml specifying backup filer store")
|
|
metaBackup.clientId = util.RandomInt32()
|
|
}
|
|
|
|
var cmdFilerMetaBackup = &Command{
|
|
UsageLine: "filer.meta.backup [-filer=localhost:8888] [-filerDir=/] [-restart] -config=/path/to/backup_filer.toml",
|
|
Short: "continuously backup filer meta data changes to anther filer store specified in a backup_filer.toml",
|
|
Long: `continuously backup filer meta data changes.
|
|
The backup writes to another filer store specified in a backup_filer.toml.
|
|
|
|
weed filer.meta.backup -config=/path/to/backup_filer.toml -filer="localhost:8888"
|
|
weed filer.meta.backup -config=/path/to/backup_filer.toml -filer="localhost:8888" -restart
|
|
|
|
`,
|
|
}
|
|
|
|
func runFilerMetaBackup(cmd *Command, args []string) bool {
|
|
|
|
util.LoadSecurityConfiguration()
|
|
metaBackup.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
|
|
// load backup_filer.toml
|
|
v := viper.New()
|
|
v.SetConfigFile(*metaBackup.backupFilerConfig)
|
|
|
|
if err := v.ReadInConfig(); err != nil { // Handle errors reading the config file
|
|
glog.Fatalf("Failed to load %s file: %v\nPlease use this command to generate the a %s.toml file\n"+
|
|
" weed scaffold -config=%s -output=.\n\n\n",
|
|
*metaBackup.backupFilerConfig, err, "backup_filer", "filer")
|
|
}
|
|
|
|
if err := metaBackup.initStore(v); err != nil {
|
|
glog.V(0).Infof("init backup filer store: %v", err)
|
|
return true
|
|
}
|
|
|
|
missingPreviousBackup := false
|
|
_, err := metaBackup.getOffset()
|
|
if err != nil {
|
|
missingPreviousBackup = true
|
|
}
|
|
|
|
if *metaBackup.restart || missingPreviousBackup {
|
|
glog.V(0).Infof("traversing metadata tree...")
|
|
startTime := time.Now()
|
|
if err := metaBackup.traverseMetadata(); err != nil {
|
|
glog.Errorf("traverse meta data: %v", err)
|
|
return true
|
|
}
|
|
glog.V(0).Infof("metadata copied up to %v", startTime)
|
|
if err := metaBackup.setOffset(startTime); err != nil {
|
|
startTime = time.Now()
|
|
}
|
|
}
|
|
|
|
for {
|
|
err := metaBackup.streamMetadataBackup()
|
|
if err != nil {
|
|
glog.Errorf("filer meta backup from %s: %v", *metaBackup.filerAddress, err)
|
|
time.Sleep(1747 * time.Millisecond)
|
|
}
|
|
}
|
|
// Unreachable: satisfies bool return type signature for daemon function
|
|
return false
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) initStore(v *viper.Viper) error {
|
|
// load configuration for default filer store
|
|
hasDefaultStoreConfigured := false
|
|
for _, store := range filer.Stores {
|
|
if v.GetBool(store.GetName() + ".enabled") {
|
|
store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(filer.FilerStore)
|
|
if err := store.Initialize(v, store.GetName()+"."); err != nil {
|
|
glog.Fatalf("failed to initialize store for %s: %+v", store.GetName(), err)
|
|
}
|
|
glog.V(0).Infof("configured filer store to %s", store.GetName())
|
|
hasDefaultStoreConfigured = true
|
|
metaBackup.store = filer.NewFilerStoreWrapper(store)
|
|
break
|
|
}
|
|
}
|
|
if !hasDefaultStoreConfigured {
|
|
return fmt.Errorf("no filer store enabled in %s", v.ConfigFileUsed())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) traverseMetadata() (err error) {
|
|
var saveErr error
|
|
|
|
traverseErr := filer_pb.TraverseBfs(metaBackup, util.FullPath(*metaBackup.filerDirectory), func(parentPath util.FullPath, entry *filer_pb.Entry) {
|
|
|
|
println("+", parentPath.Child(entry.Name))
|
|
if err := metaBackup.store.InsertEntry(context.Background(), filer.FromPbEntry(string(parentPath), entry)); err != nil {
|
|
saveErr = fmt.Errorf("insert entry error: %w\n", err)
|
|
return
|
|
}
|
|
|
|
})
|
|
|
|
if traverseErr != nil {
|
|
return fmt.Errorf("traverse: %w", traverseErr)
|
|
}
|
|
return saveErr
|
|
}
|
|
|
|
var (
|
|
MetaBackupKey = []byte("metaBackup")
|
|
)
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) streamMetadataBackup() error {
|
|
|
|
startTime, err := metaBackup.getOffset()
|
|
if err != nil {
|
|
startTime = time.Now()
|
|
}
|
|
glog.V(0).Infof("streaming from %v", startTime)
|
|
|
|
store := metaBackup.store
|
|
|
|
eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
ctx := context.Background()
|
|
message := resp.EventNotification
|
|
|
|
if filer_pb.IsEmpty(resp) {
|
|
return nil
|
|
} else if filer_pb.IsCreate(resp) {
|
|
println("+", util.FullPath(message.NewParentPath).Child(message.NewEntry.Name))
|
|
entry := filer.FromPbEntry(message.NewParentPath, message.NewEntry)
|
|
return store.InsertEntry(ctx, entry)
|
|
} else if filer_pb.IsDelete(resp) {
|
|
println("-", util.FullPath(resp.Directory).Child(message.OldEntry.Name))
|
|
return store.DeleteEntry(ctx, util.FullPath(resp.Directory).Child(message.OldEntry.Name))
|
|
} else if filer_pb.IsUpdate(resp) {
|
|
println("~", util.FullPath(message.NewParentPath).Child(message.NewEntry.Name))
|
|
entry := filer.FromPbEntry(message.NewParentPath, message.NewEntry)
|
|
return store.UpdateEntry(ctx, entry)
|
|
} else {
|
|
// renaming
|
|
println("-", util.FullPath(resp.Directory).Child(message.OldEntry.Name))
|
|
if err := store.DeleteEntry(ctx, util.FullPath(resp.Directory).Child(message.OldEntry.Name)); err != nil {
|
|
return err
|
|
}
|
|
println("+", util.FullPath(message.NewParentPath).Child(message.NewEntry.Name))
|
|
return store.InsertEntry(ctx, filer.FromPbEntry(message.NewParentPath, message.NewEntry))
|
|
}
|
|
}
|
|
|
|
processEventFnWithOffset := pb.AddOffsetFunc(eachEntryFunc, 3*time.Second, func(counter int64, lastTsNs int64) error {
|
|
lastTime := time.Unix(0, lastTsNs)
|
|
glog.V(0).Infof("meta backup %s progressed to %v %0.2f/sec", *metaBackup.filerAddress, lastTime, float64(counter)/float64(3))
|
|
return metaBackup.setOffset(lastTime)
|
|
})
|
|
|
|
metaBackup.clientEpoch++
|
|
|
|
prefix := *metaBackup.filerDirectory
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
prefix = prefix + "/"
|
|
}
|
|
metadataFollowOption := &pb.MetadataFollowOption{
|
|
ClientName: "meta_backup",
|
|
ClientId: metaBackup.clientId,
|
|
ClientEpoch: metaBackup.clientEpoch,
|
|
SelfSignature: 0,
|
|
PathPrefix: prefix,
|
|
AdditionalPathPrefixes: nil,
|
|
DirectoriesToWatch: nil,
|
|
StartTsNs: startTime.UnixNano(),
|
|
StopTsNs: 0,
|
|
EventErrorType: pb.RetryForeverOnError,
|
|
}
|
|
|
|
return pb.FollowMetadata(pb.ServerAddress(*metaBackup.filerAddress), metaBackup.grpcDialOption, metadataFollowOption, processEventFnWithOffset)
|
|
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) getOffset() (lastWriteTime time.Time, err error) {
|
|
value, err := metaBackup.store.KvGet(context.Background(), MetaBackupKey)
|
|
if err != nil {
|
|
return
|
|
}
|
|
tsNs := util.BytesToUint64(value)
|
|
|
|
return time.Unix(0, int64(tsNs)), nil
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) setOffset(lastWriteTime time.Time) error {
|
|
valueBuf := make([]byte, 8)
|
|
util.Uint64toBytes(valueBuf, uint64(lastWriteTime.UnixNano()))
|
|
|
|
if err := metaBackup.store.KvPut(context.Background(), MetaBackupKey, valueBuf); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var _ = filer_pb.FilerClient(&FilerMetaBackupOptions{})
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
|
|
|
return pb.WithFilerClient(streamingMode, metaBackup.clientId, pb.ServerAddress(*metaBackup.filerAddress), metaBackup.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
return fn(client)
|
|
})
|
|
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) AdjustedUrl(location *filer_pb.Location) string {
|
|
return location.Url
|
|
}
|
|
|
|
func (metaBackup *FilerMetaBackupOptions) GetDataCenter() string {
|
|
return ""
|
|
}
|