You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

518 lines
15 KiB

5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/chrislusf/seaweedfs/weed/util/grace"
  16. "google.golang.org/grpc"
  17. "github.com/chrislusf/seaweedfs/weed/operation"
  18. "github.com/chrislusf/seaweedfs/weed/pb"
  19. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  20. "github.com/chrislusf/seaweedfs/weed/security"
  21. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  22. "github.com/chrislusf/seaweedfs/weed/util"
  23. "github.com/chrislusf/seaweedfs/weed/wdclient"
  24. )
  25. var (
  26. copy CopyOptions
  27. waitGroup sync.WaitGroup
  28. )
  29. type CopyOptions struct {
  30. include *string
  31. replication *string
  32. collection *string
  33. ttl *string
  34. maxMB *int
  35. masterClient *wdclient.MasterClient
  36. concurrenctFiles *int
  37. concurrenctChunks *int
  38. grpcDialOption grpc.DialOption
  39. masters []string
  40. cipher bool
  41. ttlSec int32
  42. }
  43. func init() {
  44. cmdCopy.Run = runCopy // break init cycle
  45. cmdCopy.IsDebug = cmdCopy.Flag.Bool("debug", false, "verbose debug information")
  46. copy.include = cmdCopy.Flag.String("include", "", "pattens of files to copy, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
  47. copy.replication = cmdCopy.Flag.String("replication", "", "replication type")
  48. copy.collection = cmdCopy.Flag.String("collection", "", "optional collection name")
  49. copy.ttl = cmdCopy.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
  50. copy.maxMB = cmdCopy.Flag.Int("maxMB", 32, "split files larger than the limit")
  51. copy.concurrenctFiles = cmdCopy.Flag.Int("c", 8, "concurrent file copy goroutines")
  52. copy.concurrenctChunks = cmdCopy.Flag.Int("concurrentChunks", 8, "concurrent chunk copy goroutines for each file")
  53. }
  54. var cmdCopy = &Command{
  55. UsageLine: "filer.copy file_or_dir1 [file_or_dir2 file_or_dir3] http://localhost:8888/path/to/a/folder/",
  56. Short: "copy one or a list of files to a filer folder",
  57. Long: `copy one or a list of files, or batch copy one whole folder recursively, to a filer folder
  58. It can copy one or a list of files or folders.
  59. If copying a whole folder recursively:
  60. All files under the folder and subfolders will be copyed.
  61. Optional parameter "-include" allows you to specify the file name patterns.
  62. If "maxMB" is set to a positive number, files larger than it would be split into chunks.
  63. `,
  64. }
  65. func runCopy(cmd *Command, args []string) bool {
  66. util.LoadConfiguration("security", false)
  67. if len(args) <= 1 {
  68. return false
  69. }
  70. filerDestination := args[len(args)-1]
  71. fileOrDirs := args[0 : len(args)-1]
  72. filerUrl, err := url.Parse(filerDestination)
  73. if err != nil {
  74. fmt.Printf("The last argument should be a URL on filer: %v\n", err)
  75. return false
  76. }
  77. urlPath := filerUrl.Path
  78. if !strings.HasSuffix(urlPath, "/") {
  79. fmt.Printf("The last argument should be a folder and end with \"/\": %v\n", err)
  80. return false
  81. }
  82. if filerUrl.Port() == "" {
  83. fmt.Printf("The filer port should be specified.\n")
  84. return false
  85. }
  86. filerPort, parseErr := strconv.ParseUint(filerUrl.Port(), 10, 64)
  87. if parseErr != nil {
  88. fmt.Printf("The filer port parse error: %v\n", parseErr)
  89. return false
  90. }
  91. filerGrpcPort := filerPort + 10000
  92. filerGrpcAddress := fmt.Sprintf("%s:%d", filerUrl.Hostname(), filerGrpcPort)
  93. copy.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  94. masters, collection, replication, maxMB, cipher, err := readFilerConfiguration(copy.grpcDialOption, filerGrpcAddress)
  95. if err != nil {
  96. fmt.Printf("read from filer %s: %v\n", filerGrpcAddress, err)
  97. return false
  98. }
  99. if *copy.collection == "" {
  100. *copy.collection = collection
  101. }
  102. if *copy.replication == "" {
  103. *copy.replication = replication
  104. }
  105. if *copy.maxMB == 0 {
  106. *copy.maxMB = int(maxMB)
  107. }
  108. copy.masters = masters
  109. copy.cipher = cipher
  110. ttl, err := needle.ReadTTL(*copy.ttl)
  111. if err != nil {
  112. fmt.Printf("parsing ttl %s: %v\n", *copy.ttl, err)
  113. return false
  114. }
  115. copy.ttlSec = int32(ttl.Minutes()) * 60
  116. if *cmdCopy.IsDebug {
  117. grace.SetupProfiling("filer.copy.cpu.pprof", "filer.copy.mem.pprof")
  118. }
  119. fileCopyTaskChan := make(chan FileCopyTask, *copy.concurrenctFiles)
  120. go func() {
  121. defer close(fileCopyTaskChan)
  122. for _, fileOrDir := range fileOrDirs {
  123. if err := genFileCopyTask(fileOrDir, urlPath, fileCopyTaskChan); err != nil {
  124. fmt.Fprintf(os.Stderr, "gen file list error: %v\n", err)
  125. break
  126. }
  127. }
  128. }()
  129. for i := 0; i < *copy.concurrenctFiles; i++ {
  130. waitGroup.Add(1)
  131. go func() {
  132. defer waitGroup.Done()
  133. worker := FileCopyWorker{
  134. options: &copy,
  135. filerHost: filerUrl.Host,
  136. filerGrpcAddress: filerGrpcAddress,
  137. }
  138. if err := worker.copyFiles(fileCopyTaskChan); err != nil {
  139. fmt.Fprintf(os.Stderr, "copy file error: %v\n", err)
  140. return
  141. }
  142. }()
  143. }
  144. waitGroup.Wait()
  145. return true
  146. }
  147. func readFilerConfiguration(grpcDialOption grpc.DialOption, filerGrpcAddress string) (masters []string, collection, replication string, maxMB uint32, cipher bool, err error) {
  148. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  149. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  150. if err != nil {
  151. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  152. }
  153. masters, collection, replication, maxMB = resp.Masters, resp.Collection, resp.Replication, resp.MaxMb
  154. cipher = resp.Cipher
  155. return nil
  156. })
  157. return
  158. }
  159. func genFileCopyTask(fileOrDir string, destPath string, fileCopyTaskChan chan FileCopyTask) error {
  160. fi, err := os.Stat(fileOrDir)
  161. if err != nil {
  162. fmt.Fprintf(os.Stderr, "Failed to get stat for file %s: %v\n", fileOrDir, err)
  163. return nil
  164. }
  165. mode := fi.Mode()
  166. if mode.IsDir() {
  167. files, _ := ioutil.ReadDir(fileOrDir)
  168. for _, subFileOrDir := range files {
  169. if err = genFileCopyTask(fileOrDir+"/"+subFileOrDir.Name(), destPath+fi.Name()+"/", fileCopyTaskChan); err != nil {
  170. return err
  171. }
  172. }
  173. return nil
  174. }
  175. uid, gid := util.GetFileUidGid(fi)
  176. fileCopyTaskChan <- FileCopyTask{
  177. sourceLocation: fileOrDir,
  178. destinationUrlPath: destPath,
  179. fileSize: fi.Size(),
  180. fileMode: fi.Mode(),
  181. uid: uid,
  182. gid: gid,
  183. }
  184. return nil
  185. }
  186. type FileCopyWorker struct {
  187. options *CopyOptions
  188. filerHost string
  189. filerGrpcAddress string
  190. }
  191. func (worker *FileCopyWorker) copyFiles(fileCopyTaskChan chan FileCopyTask) error {
  192. for task := range fileCopyTaskChan {
  193. if err := worker.doEachCopy(task); err != nil {
  194. return err
  195. }
  196. }
  197. return nil
  198. }
  199. type FileCopyTask struct {
  200. sourceLocation string
  201. destinationUrlPath string
  202. fileSize int64
  203. fileMode os.FileMode
  204. uid uint32
  205. gid uint32
  206. }
  207. func (worker *FileCopyWorker) doEachCopy(task FileCopyTask) error {
  208. f, err := os.Open(task.sourceLocation)
  209. if err != nil {
  210. fmt.Printf("Failed to open file %s: %v\n", task.sourceLocation, err)
  211. if _, ok := err.(*os.PathError); ok {
  212. fmt.Printf("skipping %s\n", task.sourceLocation)
  213. return nil
  214. }
  215. return err
  216. }
  217. defer f.Close()
  218. // this is a regular file
  219. if *worker.options.include != "" {
  220. if ok, _ := filepath.Match(*worker.options.include, filepath.Base(task.sourceLocation)); !ok {
  221. return nil
  222. }
  223. }
  224. // find the chunk count
  225. chunkSize := int64(*worker.options.maxMB * 1024 * 1024)
  226. chunkCount := 1
  227. if chunkSize > 0 && task.fileSize > chunkSize {
  228. chunkCount = int(task.fileSize/chunkSize) + 1
  229. }
  230. if chunkCount == 1 {
  231. return worker.uploadFileAsOne(task, f)
  232. }
  233. return worker.uploadFileInChunks(task, f, chunkCount, chunkSize)
  234. }
  235. func (worker *FileCopyWorker) uploadFileAsOne(task FileCopyTask, f *os.File) error {
  236. // upload the file content
  237. fileName := filepath.Base(f.Name())
  238. mimeType := detectMimeType(f)
  239. data, err := ioutil.ReadAll(f)
  240. if err != nil {
  241. return err
  242. }
  243. var chunks []*filer_pb.FileChunk
  244. var assignResult *filer_pb.AssignVolumeResponse
  245. var assignError error
  246. if task.fileSize > 0 {
  247. // assign a volume
  248. err := pb.WithGrpcFilerClient(worker.filerGrpcAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  249. request := &filer_pb.AssignVolumeRequest{
  250. Count: 1,
  251. Replication: *worker.options.replication,
  252. Collection: *worker.options.collection,
  253. TtlSec: worker.options.ttlSec,
  254. ParentPath: task.destinationUrlPath,
  255. }
  256. assignResult, assignError = client.AssignVolume(context.Background(), request)
  257. if assignError != nil {
  258. return fmt.Errorf("assign volume failure %v: %v", request, assignError)
  259. }
  260. if assignResult.Error != "" {
  261. return fmt.Errorf("assign volume failure %v: %v", request, assignResult.Error)
  262. }
  263. return nil
  264. })
  265. if err != nil {
  266. return fmt.Errorf("Failed to assign from %v: %v\n", worker.options.masters, err)
  267. }
  268. targetUrl := "http://" + assignResult.Url + "/" + assignResult.FileId
  269. uploadResult, err := operation.UploadData(targetUrl, fileName, worker.options.cipher, data, false, mimeType, nil, security.EncodedJwt(assignResult.Auth))
  270. if err != nil {
  271. return fmt.Errorf("upload data %v to %s: %v\n", fileName, targetUrl, err)
  272. }
  273. if uploadResult.Error != "" {
  274. return fmt.Errorf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
  275. }
  276. fmt.Printf("uploaded %s to %s\n", fileName, targetUrl)
  277. chunks = append(chunks, &filer_pb.FileChunk{
  278. FileId: assignResult.FileId,
  279. Offset: 0,
  280. Size: uint64(uploadResult.Size),
  281. Mtime: time.Now().UnixNano(),
  282. ETag: uploadResult.Md5,
  283. CipherKey: uploadResult.CipherKey,
  284. IsGzipped: uploadResult.Gzip > 0,
  285. })
  286. fmt.Printf("copied %s => http://%s%s%s\n", fileName, worker.filerHost, task.destinationUrlPath, fileName)
  287. }
  288. if err := pb.WithGrpcFilerClient(worker.filerGrpcAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  289. request := &filer_pb.CreateEntryRequest{
  290. Directory: task.destinationUrlPath,
  291. Entry: &filer_pb.Entry{
  292. Name: fileName,
  293. Attributes: &filer_pb.FuseAttributes{
  294. Crtime: time.Now().Unix(),
  295. Mtime: time.Now().Unix(),
  296. Gid: task.gid,
  297. Uid: task.uid,
  298. FileSize: uint64(task.fileSize),
  299. FileMode: uint32(task.fileMode),
  300. Mime: mimeType,
  301. Replication: *worker.options.replication,
  302. Collection: *worker.options.collection,
  303. TtlSec: worker.options.ttlSec,
  304. },
  305. Chunks: chunks,
  306. },
  307. }
  308. if err := filer_pb.CreateEntry(client, request); err != nil {
  309. return fmt.Errorf("update fh: %v", err)
  310. }
  311. return nil
  312. }); err != nil {
  313. return fmt.Errorf("upload data %v to http://%s%s%s: %v\n", fileName, worker.filerHost, task.destinationUrlPath, fileName, err)
  314. }
  315. return nil
  316. }
  317. func (worker *FileCopyWorker) uploadFileInChunks(task FileCopyTask, f *os.File, chunkCount int, chunkSize int64) error {
  318. fileName := filepath.Base(f.Name())
  319. mimeType := detectMimeType(f)
  320. chunksChan := make(chan *filer_pb.FileChunk, chunkCount)
  321. concurrentChunks := make(chan struct{}, *worker.options.concurrenctChunks)
  322. var wg sync.WaitGroup
  323. var uploadError error
  324. var collection, replication string
  325. fmt.Printf("uploading %s in %d chunks ...\n", fileName, chunkCount)
  326. for i := int64(0); i < int64(chunkCount) && uploadError == nil; i++ {
  327. wg.Add(1)
  328. concurrentChunks <- struct{}{}
  329. go func(i int64) {
  330. defer func() {
  331. wg.Done()
  332. <-concurrentChunks
  333. }()
  334. // assign a volume
  335. var assignResult *filer_pb.AssignVolumeResponse
  336. var assignError error
  337. err := pb.WithGrpcFilerClient(worker.filerGrpcAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  338. request := &filer_pb.AssignVolumeRequest{
  339. Count: 1,
  340. Replication: *worker.options.replication,
  341. Collection: *worker.options.collection,
  342. TtlSec: worker.options.ttlSec,
  343. ParentPath: task.destinationUrlPath,
  344. }
  345. assignResult, assignError = client.AssignVolume(context.Background(), request)
  346. if assignError != nil {
  347. return fmt.Errorf("assign volume failure %v: %v", request, assignError)
  348. }
  349. if assignResult.Error != "" {
  350. return fmt.Errorf("assign volume failure %v: %v", request, assignResult.Error)
  351. }
  352. return nil
  353. })
  354. if err != nil {
  355. fmt.Printf("Failed to assign from %v: %v\n", worker.options.masters, err)
  356. }
  357. if err != nil {
  358. fmt.Printf("Failed to assign from %v: %v\n", worker.options.masters, err)
  359. }
  360. targetUrl := "http://" + assignResult.Url + "/" + assignResult.FileId
  361. if collection == "" {
  362. collection = assignResult.Collection
  363. }
  364. if replication == "" {
  365. replication = assignResult.Replication
  366. }
  367. uploadResult, err, _ := operation.Upload(targetUrl, fileName+"-"+strconv.FormatInt(i+1, 10), worker.options.cipher, io.NewSectionReader(f, i*chunkSize, chunkSize), false, "", nil, security.EncodedJwt(assignResult.Auth))
  368. if err != nil {
  369. uploadError = fmt.Errorf("upload data %v to %s: %v\n", fileName, targetUrl, err)
  370. return
  371. }
  372. if uploadResult.Error != "" {
  373. uploadError = fmt.Errorf("upload %v to %s result: %v\n", fileName, targetUrl, uploadResult.Error)
  374. return
  375. }
  376. chunksChan <- &filer_pb.FileChunk{
  377. FileId: assignResult.FileId,
  378. Offset: i * chunkSize,
  379. Size: uint64(uploadResult.Size),
  380. Mtime: time.Now().UnixNano(),
  381. ETag: uploadResult.ETag,
  382. CipherKey: uploadResult.CipherKey,
  383. IsGzipped: uploadResult.Gzip > 0,
  384. }
  385. fmt.Printf("uploaded %s-%d to %s [%d,%d)\n", fileName, i+1, targetUrl, i*chunkSize, i*chunkSize+int64(uploadResult.Size))
  386. }(i)
  387. }
  388. wg.Wait()
  389. close(chunksChan)
  390. var chunks []*filer_pb.FileChunk
  391. for chunk := range chunksChan {
  392. chunks = append(chunks, chunk)
  393. }
  394. if uploadError != nil {
  395. var fileIds []string
  396. for _, chunk := range chunks {
  397. fileIds = append(fileIds, chunk.FileId)
  398. }
  399. operation.DeleteFiles(copy.masters[0], false, worker.options.grpcDialOption, fileIds)
  400. return uploadError
  401. }
  402. if err := pb.WithGrpcFilerClient(worker.filerGrpcAddress, worker.options.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  403. request := &filer_pb.CreateEntryRequest{
  404. Directory: task.destinationUrlPath,
  405. Entry: &filer_pb.Entry{
  406. Name: fileName,
  407. Attributes: &filer_pb.FuseAttributes{
  408. Crtime: time.Now().Unix(),
  409. Mtime: time.Now().Unix(),
  410. Gid: task.gid,
  411. Uid: task.uid,
  412. FileSize: uint64(task.fileSize),
  413. FileMode: uint32(task.fileMode),
  414. Mime: mimeType,
  415. Replication: replication,
  416. Collection: collection,
  417. TtlSec: worker.options.ttlSec,
  418. },
  419. Chunks: chunks,
  420. },
  421. }
  422. if err := filer_pb.CreateEntry(client, request); err != nil {
  423. return fmt.Errorf("update fh: %v", err)
  424. }
  425. return nil
  426. }); err != nil {
  427. return fmt.Errorf("upload data %v to http://%s%s%s: %v\n", fileName, worker.filerHost, task.destinationUrlPath, fileName, err)
  428. }
  429. fmt.Printf("copied %s => http://%s%s%s\n", fileName, worker.filerHost, task.destinationUrlPath, fileName)
  430. return nil
  431. }
  432. func detectMimeType(f *os.File) string {
  433. head := make([]byte, 512)
  434. f.Seek(0, io.SeekStart)
  435. n, err := f.Read(head)
  436. if err == io.EOF {
  437. return ""
  438. }
  439. if err != nil {
  440. fmt.Printf("read head of %v: %v\n", f.Name(), err)
  441. return ""
  442. }
  443. f.Seek(0, io.SeekStart)
  444. mimeType := http.DetectContentType(head[:n])
  445. if mimeType == "application/octet-stream" {
  446. return ""
  447. }
  448. return mimeType
  449. }