Beispiel #1
0
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
	m := make(map[string]interface{})
	if r.Method != "POST" {
		m["error"] = "Only submit via POST!"
		writeJsonQuiet(w, r, m)
		return
	}

	debug("parsing upload file...")
	fname, data, mimeType, isGzipped, lastModified, pe := storage.ParseUpload(r)
	if pe != nil {
		writeJsonError(w, r, pe)
		return
	}

	debug("assigning file id for", fname)
	assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"), r.FormValue("collection"))
	if ae != nil {
		writeJsonError(w, r, ae)
		return
	}

	url := "http://" + assignResult.PublicUrl + "/" + assignResult.Fid
	if lastModified != 0 {
		url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
	}

	debug("upload file to store", url)
	uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType)
	if err != nil {
		writeJsonError(w, r, err)
		return
	}

	m["fileName"] = fname
	m["fid"] = assignResult.Fid
	m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
	m["size"] = uploadResult.Size
	writeJsonQuiet(w, r, m)
	return
}
Beispiel #2
0
func writeFiles(idChan chan int, fileIdLineChan chan string, s *stats) {
	delayedDeleteChan := make(chan *delayedFile, 100)
	var waitForDeletions sync.WaitGroup
	for i := 0; i < 7; i++ {
		go func() {
			waitForDeletions.Add(1)
			for df := range delayedDeleteChan {
				if df == nil {
					break
				}
				if df.enterTime.After(time.Now()) {
					time.Sleep(df.enterTime.Sub(time.Now()))
				}
				fp := df.fp
				serverLimitChan[fp.Server] <- true
				if e := util.Delete("http://" + fp.Server + "/" + fp.Fid); e == nil {
					s.completed++
				} else {
					s.failed++
				}
				<-serverLimitChan[fp.Server]
			}
			waitForDeletions.Done()
		}()
	}

	for {
		if id, ok := <-idChan; ok {
			start := time.Now()
			fileSize := int64(*b.fileSize + rand.Intn(64))
			fp := &operation.FilePart{Reader: &FakeReader{id: uint64(id), size: fileSize}, FileSize: fileSize}
			if assignResult, err := operation.Assign(*b.server, 1, "", *b.collection, ""); err == nil {
				fp.Server, fp.Fid, fp.Collection = assignResult.PublicUrl, assignResult.Fid, *b.collection
				if _, ok := serverLimitChan[fp.Server]; !ok {
					serverLimitChan[fp.Server] = make(chan bool, 7)
				}
				serverLimitChan[fp.Server] <- true
				if _, err := fp.Upload(0, *b.server); 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++
				}
				writeStats.addSample(time.Now().Sub(start))
				<-serverLimitChan[fp.Server]
				if *cmdBenchmark.IsDebug {
					fmt.Printf("writing %d file %s\n", id, fp.Fid)
				}
			} else {
				s.failed++
				println("writing file error:", err.Error())
			}
		} else {
			break
		}
	}
	close(delayedDeleteChan)
	waitForDeletions.Wait()
	wait.Done()
}
Beispiel #3
0
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
	query := r.URL.Query()
	assignResult, ae := operation.Assign(fs.master, 1, query.Get("replication"), fs.collection)
	if ae != nil {
		glog.V(0).Infoln("failing to assign a file id", ae.Error())
		writeJsonError(w, r, ae)
		return
	}

	u, _ := url.Parse("http://" + assignResult.PublicUrl + "/" + assignResult.Fid)
	glog.V(4).Infoln("post to", u)
	request := &http.Request{
		Method:        r.Method,
		URL:           u,
		Proto:         r.Proto,
		ProtoMajor:    r.ProtoMajor,
		ProtoMinor:    r.ProtoMinor,
		Header:        r.Header,
		Body:          r.Body,
		Host:          r.Host,
		ContentLength: r.ContentLength,
	}
	resp, do_err := util.Do(request)
	if do_err != nil {
		glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
		writeJsonError(w, r, do_err)
		return
	}
	defer resp.Body.Close()
	resp_body, ra_err := ioutil.ReadAll(resp.Body)
	if ra_err != nil {
		glog.V(0).Infoln("failing to upload to volume server", ra_err.Error())
		writeJsonError(w, r, ra_err)
		return
	}
	glog.V(4).Infoln("post result", string(resp_body))
	var ret operation.UploadResult
	unmarshal_err := json.Unmarshal(resp_body, &ret)
	if unmarshal_err != nil {
		glog.V(0).Infoln("failing to read upload resonse", string(resp_body))
		writeJsonError(w, r, unmarshal_err)
		return
	}
	if ret.Error != "" {
		glog.V(0).Infoln("failing to post to volume server", ret.Error)
		writeJsonError(w, r, errors.New(ret.Error))
		return
	}
	path := r.URL.Path
	if strings.HasSuffix(path, "/") {
		if ret.Name != "" {
			path += ret.Name
		} else {
			operation.DeleteFile(fs.master, assignResult.Fid) //clean up
			glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
			writeJsonError(w, r, errors.New("Can not to write to folder "+path+" without a file name"))
			return
		}
	}
	glog.V(4).Infoln("saving", path, "=>", assignResult.Fid)
	if db_err := fs.filer.CreateFile(path, assignResult.Fid); db_err != nil {
		operation.DeleteFile(fs.master, assignResult.Fid) //clean up
		glog.V(0).Infoln("failing to write to filer server", db_err.Error())
		writeJsonError(w, r, db_err)
		return
	}
	w.WriteHeader(http.StatusCreated)
}