func writeFiles(idChan chan int, fileIdLineChan chan string, s *stat) { defer wait.Done() delayedDeleteChan := make(chan *delayedFile, 100) var waitForDeletions sync.WaitGroup secret := security.Secret(*b.secretKey) for i := 0; i < 7; i++ { waitForDeletions.Add(1) go func() { defer waitForDeletions.Done() for df := range delayedDeleteChan { if df.enterTime.After(time.Now()) { time.Sleep(df.enterTime.Sub(time.Now())) } if e := util.Delete("http://"+df.fp.Server+"/"+df.fp.Fid, security.GenJwt(secret, df.fp.Fid)); e == nil { s.completed++ } else { s.failed++ } } }() } for id := range idChan { start := time.Now() fileSize := int64(*b.fileSize + rand.Intn(64)) fp := &operation.FilePart{Reader: &FakeReader{id: uint64(id), size: fileSize}, FileSize: fileSize} ar := &operation.VolumeAssignRequest{ Count: 1, Collection: *b.collection, } if assignResult, err := operation.Assign(*b.server, ar); err == nil { fp.Server, fp.Fid, fp.Collection = assignResult.Url, assignResult.Fid, *b.collection if _, err := fp.Upload(0, *b.server, secret); err == nil { if rand.Intn(100) < *b.deletePercentage { s.total++ delayedDeleteChan <- &delayedFile{time.Now().Add(time.Second), fp} } else { fileIdLineChan <- fp.Fid } s.completed++ s.transferred += fileSize } else { s.failed++ fmt.Printf("Failed to write with error:%v\n", err) } writeStats.addSample(time.Now().Sub(start)) if *cmdBenchmark.IsDebug { fmt.Printf("writing %d file %s\n", id, fp.Fid) } } else { s.failed++ println("writing file error:", err.Error()) } } close(delayedDeleteChan) waitForDeletions.Wait() }
func (fi FilePart) Upload(maxMB int, master string, secret security.Secret) (retSize uint32, err error) { jwt := security.GenJwt(secret, fi.Fid) fileUrl := "http://" + fi.Server + "/" + fi.Fid if fi.ModTime != 0 { fileUrl += "?ts=" + strconv.Itoa(int(fi.ModTime)) } if closer, ok := fi.Reader.(io.Closer); ok { defer closer.Close() } baseName := path.Base(fi.FileName) if maxMB > 0 && fi.FileSize > int64(maxMB*1024*1024) { chunkSize := int64(maxMB * 1024 * 1024) chunks := fi.FileSize/chunkSize + 1 cm := ChunkManifest{ Name: baseName, Size: fi.FileSize, Mime: fi.MimeType, Chunks: make([]*ChunkInfo, 0, chunks), } for i := int64(0); i < chunks; i++ { id, count, e := upload_one_chunk( baseName+"-"+strconv.FormatInt(i+1, 10), io.LimitReader(fi.Reader, chunkSize), master, fi.Replication, fi.Collection, fi.Ttl, jwt) if e != nil { // delete all uploaded chunks cm.DeleteChunks(master) return 0, e } cm.Chunks = append(cm.Chunks, &ChunkInfo{ Offset: i * chunkSize, Size: int64(count), Fid: id, }, ) retSize += count } err = upload_chunked_file_manifest(fileUrl, &cm, jwt) if err != nil { // delete all uploaded chunks cm.DeleteChunks(master) } } else { ret, e := Upload(fileUrl, baseName, fi.Reader, fi.IsGzipped, fi.MimeType, jwt) if e != nil { return 0, e } return ret.Size, e } return }
func RegisterFile(filer string, path string, fileId string, secret security.Secret) error { // TODO: jwt need to be used _ = security.GenJwt(secret, fileId) values := make(url.Values) values.Add("path", path) values.Add("fileId", fileId) _, err := util.Post("http://"+filer+"/admin/register", values) if err != nil { return fmt.Errorf("Failed to register path:%s on filer:%s to file id:%s", path, filer, fileId) } return nil }
func (fs *FilerServer) jwt(fileId string) security.EncodedJwt { return security.GenJwt(fs.secret, fileId) }
func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt { return security.GenJwt(vs.guard.SecretKey, fileId) }