2019-04-16 16:37:11 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2022-08-07 16:42:15 +08:00
|
|
|
"flag"
|
2019-04-16 16:37:11 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-04-16 02:41:34 +08:00
|
|
|
"strings"
|
2023-12-29 03:54:37 +08:00
|
|
|
"sync"
|
2022-08-07 16:42:15 +08:00
|
|
|
"time"
|
2019-04-16 16:37:11 +08:00
|
|
|
|
2022-08-18 03:05:07 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-02-26 13:50:12 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2019-04-16 16:37:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 16:30:24 +08:00
|
|
|
Commands = append(Commands, &commandFsMetaLoad{})
|
2019-04-16 16:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsMetaLoad struct {
|
2022-09-28 00:58:46 +08:00
|
|
|
dirPrefix *string
|
2019-04-16 16:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaLoad) Name() string {
|
|
|
|
return "fs.meta.load"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaLoad) Help() string {
|
|
|
|
return `load saved filer meta data to restore the directory and file structure
|
|
|
|
|
2019-04-17 00:40:27 +08:00
|
|
|
fs.meta.load <filer_host>-<port>-<time>.meta
|
2022-08-07 16:42:15 +08:00
|
|
|
fs.meta.load -v=false <filer_host>-<port>-<time>.meta // skip printing out the verbose output
|
2024-05-22 12:42:48 +08:00
|
|
|
fs.meta.load -concurrency=1 <filer_host>-<port>-<time>.meta // number of parallel meta load to filer
|
2024-05-22 12:45:29 +08:00
|
|
|
fs.meta.load -dirPrefix=/buckets/important <filer_host>.meta // load any dirs with prefix "important"
|
2019-04-16 16:37:11 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2024-09-30 01:38:22 +08:00
|
|
|
func (c *commandFsMetaLoad) HasTag(CommandTag) bool {
|
2024-09-29 11:22:57 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-04-16 16:37:11 +08:00
|
|
|
|
2019-04-27 00:31:55 +08:00
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprintf(writer, "missing a metadata file\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:37:11 +08:00
|
|
|
fileName := args[len(args)-1]
|
|
|
|
|
2022-08-07 16:42:15 +08:00
|
|
|
metaLoadCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2022-09-28 00:58:46 +08:00
|
|
|
c.dirPrefix = metaLoadCommand.String("dirPrefix", "", "load entries only with directories matching prefix")
|
2023-12-29 03:54:37 +08:00
|
|
|
concurrency := metaLoadCommand.Int("concurrency", 1, "number of parallel meta load to filer")
|
2022-08-07 16:42:15 +08:00
|
|
|
verbose := metaLoadCommand.Bool("v", true, "verbose mode")
|
|
|
|
if err = metaLoadCommand.Parse(args[0 : len(args)-1]); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:37:11 +08:00
|
|
|
dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
var dirCount, fileCount uint64
|
2022-08-07 16:42:15 +08:00
|
|
|
lastLogTime := time.Now()
|
2019-04-16 16:37:11 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2019-04-16 16:37:11 +08:00
|
|
|
|
|
|
|
sizeBuf := make([]byte, 4)
|
2023-12-29 03:54:37 +08:00
|
|
|
waitChan := make(chan struct{}, *concurrency)
|
|
|
|
defer close(waitChan)
|
|
|
|
var wg sync.WaitGroup
|
2019-04-16 16:37:11 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
if n, err := dst.Read(sizeBuf); n != 4 {
|
2019-04-18 15:19:18 +08:00
|
|
|
if err == io.EOF {
|
2019-04-16 16:37:11 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size := util.BytesToUint32(sizeBuf)
|
|
|
|
|
|
|
|
data := make([]byte, int(size))
|
|
|
|
|
|
|
|
if n, err := dst.Read(data); n != len(data) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullEntry := &filer_pb.FullEntry{}
|
|
|
|
if err = proto.Unmarshal(data, fullEntry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:58:46 +08:00
|
|
|
// check collection name pattern
|
|
|
|
entryFullName := string(util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
|
|
|
|
if *c.dirPrefix != "" {
|
|
|
|
if !strings.HasPrefix(fullEntry.Dir, *c.dirPrefix) {
|
|
|
|
if *verbose {
|
|
|
|
fmt.Fprintf(writer, "not match dir prefix %s\n", entryFullName)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 04:37:05 +08:00
|
|
|
if *verbose || lastLogTime.Add(time.Second).Before(time.Now()) {
|
|
|
|
if !*verbose {
|
|
|
|
lastLogTime = time.Now()
|
|
|
|
}
|
2022-09-28 00:58:46 +08:00
|
|
|
fmt.Fprintf(writer, "load %s\n", entryFullName)
|
2022-08-31 04:37:05 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 02:41:34 +08:00
|
|
|
fullEntry.Entry.Name = strings.ReplaceAll(fullEntry.Entry.Name, "/", "x")
|
2019-04-16 16:37:11 +08:00
|
|
|
if fullEntry.Entry.IsDirectory {
|
2023-12-29 03:54:37 +08:00
|
|
|
wg.Wait()
|
|
|
|
if errEntry := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: fullEntry.Dir,
|
|
|
|
Entry: fullEntry.Entry,
|
|
|
|
}); errEntry != nil {
|
|
|
|
return errEntry
|
|
|
|
}
|
2019-04-16 16:37:11 +08:00
|
|
|
dirCount++
|
|
|
|
} else {
|
2023-12-29 03:54:37 +08:00
|
|
|
wg.Add(1)
|
|
|
|
waitChan <- struct{}{}
|
|
|
|
go func(entry *filer_pb.FullEntry) {
|
|
|
|
if errEntry := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: entry.Dir,
|
|
|
|
Entry: entry.Entry,
|
|
|
|
}); errEntry != nil {
|
|
|
|
err = errEntry
|
|
|
|
}
|
|
|
|
defer wg.Done()
|
|
|
|
<-waitChan
|
|
|
|
}(fullEntry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:37:11 +08:00
|
|
|
fileCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
|
2020-03-24 12:26:15 +08:00
|
|
|
fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
|
2019-04-16 16:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-04-18 15:19:18 +08:00
|
|
|
}
|