2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/weed-fs/go/operation"
|
|
|
|
"github.com/chrislusf/weed-fs/go/stats"
|
|
|
|
"github.com/chrislusf/weed-fs/go/storage"
|
2013-12-02 17:37:36 +08:00
|
|
|
)
|
|
|
|
|
2014-04-14 14:56:15 +08:00
|
|
|
func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
|
|
|
|
volumeLocations = make(map[string]operation.LookupResult)
|
2014-04-14 14:41:34 +08:00
|
|
|
for _, vid := range vids {
|
|
|
|
commaSep := strings.Index(vid, ",")
|
|
|
|
if commaSep > 0 {
|
|
|
|
vid = vid[0:commaSep]
|
|
|
|
}
|
|
|
|
if _, ok := volumeLocations[vid]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
volumeId, err := storage.NewVolumeId(vid)
|
|
|
|
if err == nil {
|
|
|
|
machines := ms.Topo.Lookup(collection, volumeId)
|
|
|
|
if machines != nil {
|
2014-04-14 14:56:15 +08:00
|
|
|
var ret []operation.Location
|
2014-04-14 14:41:34 +08:00
|
|
|
for _, dn := range machines {
|
2014-04-14 14:56:15 +08:00
|
|
|
ret = append(ret, operation.Location{Url: dn.Url(), PublicUrl: dn.PublicUrl})
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
2014-04-14 14:56:15 +08:00
|
|
|
volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Locations: ret}
|
2014-04-14 14:41:34 +08:00
|
|
|
} else {
|
2014-04-14 14:56:15 +08:00
|
|
|
volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "volumeId not found."}
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-14 14:56:15 +08:00
|
|
|
volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "Unknown volumeId format."}
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes one volumeId only, can not do batch lookup
|
2013-12-02 17:37:36 +08:00
|
|
|
func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vid := r.FormValue("volumeId")
|
|
|
|
commaSep := strings.Index(vid, ",")
|
|
|
|
if commaSep > 0 {
|
|
|
|
vid = vid[0:commaSep]
|
|
|
|
}
|
2014-04-14 14:41:34 +08:00
|
|
|
vids := []string{vid}
|
|
|
|
collection := r.FormValue("collection") //optional, but can be faster if too many collections
|
|
|
|
volumeLocations := ms.lookupVolumeId(vids, collection)
|
|
|
|
location := volumeLocations[vid]
|
|
|
|
if location.Error != "" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, location)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This can take batched volumeIds, &volumeId=x&volumeId=y&volumeId=z
|
|
|
|
func (ms *MasterServer) volumeLookupHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
vids := r.Form["volumeId"]
|
|
|
|
collection := r.FormValue("collection") //optional, but can be faster if too many collections
|
|
|
|
volumeLocations := ms.lookupVolumeId(vids, collection)
|
2014-04-16 00:09:40 +08:00
|
|
|
writeJsonQuiet(w, r, volumeLocations)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.AssignRequest()
|
2014-04-13 16:29:52 +08:00
|
|
|
requestedCount, e := strconv.Atoi(r.FormValue("count"))
|
2013-12-02 17:37:36 +08:00
|
|
|
if e != nil {
|
2014-04-13 16:29:52 +08:00
|
|
|
requestedCount = 1
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
2014-04-13 16:29:52 +08:00
|
|
|
|
|
|
|
option, err := ms.getVolumeGrowOption(r)
|
2013-12-02 17:37:36 +08:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2014-04-16 00:30:08 +08:00
|
|
|
writeJsonQuiet(w, r, operation.AssignResult{Error: err.Error()})
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-26 14:45:31 +08:00
|
|
|
if !ms.Topo.HasWritableVolume(option) {
|
2014-03-16 14:03:49 +08:00
|
|
|
if ms.Topo.FreeSpace() <= 0 {
|
2013-12-02 17:37:36 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2014-04-16 00:30:08 +08:00
|
|
|
writeJsonQuiet(w, r, operation.AssignResult{Error: "No free volumes left!"})
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ms.vgLock.Lock()
|
|
|
|
defer ms.vgLock.Unlock()
|
2014-10-26 14:45:31 +08:00
|
|
|
if !ms.Topo.HasWritableVolume(option) {
|
2014-04-13 16:29:52 +08:00
|
|
|
if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
|
2014-04-16 00:30:08 +08:00
|
|
|
writeJsonQuiet(w, r, operation.AssignResult{Error: "Cannot grow volume group! " + err.Error()})
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-13 16:29:52 +08:00
|
|
|
fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
|
2013-12-02 17:37:36 +08:00
|
|
|
if err == nil {
|
2014-04-16 00:30:08 +08:00
|
|
|
writeJsonQuiet(w, r, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
|
2013-12-02 17:37:36 +08:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
2014-04-16 00:30:08 +08:00
|
|
|
writeJsonQuiet(w, r, operation.AssignResult{Error: err.Error()})
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|