add single chunk file copying to new filer

This commit is contained in:
Chris Lu 2018-05-29 23:46:45 -07:00
parent 78aabf66ce
commit 8a48baa056

View file

@ -11,6 +11,8 @@ import (
"github.com/chrislusf/seaweedfs/weed/operation" "github.com/chrislusf/seaweedfs/weed/operation"
filer_operation "github.com/chrislusf/seaweedfs/weed/operation/filer" filer_operation "github.com/chrislusf/seaweedfs/weed/operation/filer"
"github.com/chrislusf/seaweedfs/weed/security" "github.com/chrislusf/seaweedfs/weed/security"
"path"
"net/http"
) )
var ( var (
@ -68,20 +70,20 @@ func runCopy(cmd *Command, args []string) bool {
return false return false
} }
filerDestination := args[len(args)-1] filerDestination := args[len(args)-1]
fileOrDirs := args[0 : len(args)-1] fileOrDirs := args[0: len(args)-1]
filerUrl, err := url.Parse(filerDestination) filerUrl, err := url.Parse(filerDestination)
if err != nil { if err != nil {
fmt.Printf("The last argument should be a URL on filer: %v\n", err) fmt.Printf("The last argument should be a URL on filer: %v\n", err)
return false return false
} }
path := filerUrl.Path urlPath := filerUrl.Path
if !strings.HasSuffix(path, "/") { if !strings.HasSuffix(urlPath, "/") {
path = path + "/" urlPath = urlPath + "/"
} }
for _, fileOrDir := range fileOrDirs { for _, fileOrDir := range fileOrDirs {
if !doEachCopy(fileOrDir, filerUrl.Host, path) { if !doEachCopy(fileOrDir, filerUrl.Host, urlPath) {
return false return false
} }
} }
@ -120,29 +122,67 @@ func doEachCopy(fileOrDir string, host string, path string) bool {
} }
} }
parts, err := operation.NewFileParts([]string{fileOrDir}) // find the chunk count
chunkSize := int64(*copy.maxMB * 1024 * 1024)
chunkCount := 1
if chunkSize > 0 && fi.Size() > chunkSize {
chunkCount = int(fi.Size()/chunkSize) + 1
}
// assign a volume
assignResult, err := operation.Assign(*copy.master, &operation.VolumeAssignRequest{
Count: uint64(chunkCount),
Replication: *copy.replication,
Collection: *copy.collection,
Ttl: *copy.ttl,
})
if err != nil { if err != nil {
fmt.Printf("Failed to read file %s: %v\n", fileOrDir, err) fmt.Printf("Failed to assign from %s: %v\n", *copy.master, err)
} }
results, err := operation.SubmitFiles(*copy.master, parts, if chunkCount == 1 {
*copy.replication, *copy.collection, "", return uploadFileAsOne(host, path, assignResult, f, fi)
*copy.ttl, *copy.maxMB, copy.secret) }
return uploadFileInChunks(host, path, assignResult, f, chunkCount)
}
func uploadFileAsOne(filerUrl string, urlPath string, assignResult *operation.AssignResult, f *os.File, fi os.FileInfo) bool {
// upload the file content
ext := strings.ToLower(path.Ext(f.Name()))
head := make([]byte, 512)
f.Seek(0, 0)
n, err := f.Read(head)
if err != nil { if err != nil {
fmt.Printf("Failed to submit file %s: %v\n", fileOrDir, err) fmt.Printf("read head of %v: %v\n", f.Name(), err)
return false
} }
f.Seek(0, 0)
mimeType := http.DetectContentType(head[:n])
isGzipped := ext == ".gz"
if strings.HasSuffix(path, "/") { targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
path = path + fi.Name()
uploadResult, err := operation.Upload(targetUrl, f.Name(), f, isGzipped, mimeType, nil, "")
if err != nil {
fmt.Printf("upload data %v to %s: %v\n", f.Name(), targetUrl, err)
return false
} }
if uploadResult.Error != "" {
if err = filer_operation.RegisterFile(host, path, results[0].Fid, parts[0].FileSize, fmt.Printf("upload %v to %s result: %v\n", f.Name(), targetUrl, uploadResult.Error)
os.Getuid(), os.Getgid(), copy.secret); err != nil {
fmt.Printf("Failed to register file %s on %s: %v\n", fileOrDir, host, err)
return false return false
} }
fmt.Printf("Copy %s => http://%s%s\n", fileOrDir, host, path) if err = filer_operation.RegisterFile(filerUrl, filepath.Join(urlPath, f.Name()), assignResult.Fid, fi.Size(),
os.Getuid(), os.Getgid(), copy.secret); err != nil {
fmt.Printf("Failed to register file %s on %s: %v\n", f.Name(), filerUrl, err)
return false
}
fmt.Printf("Copied %s => http://%s%s\n", f.Name(), filerUrl, urlPath)
return true return true
} }
func uploadFileInChunks(filerUrl string, path string, assignResult *operation.AssignResult, f *os.File, chunkCount int) bool {
return false
}