2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2015-01-08 16:19:32 +08:00
|
|
|
"fmt"
|
2013-12-02 17:37:36 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2019-02-14 16:08:20 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2019-04-19 12:43:36 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
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
|
|
|
|
}
|
2019-04-19 12:43:36 +08:00
|
|
|
volumeId, err := needle.NewVolumeId(vid)
|
2014-04-14 14:41:34 +08:00
|
|
|
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 {
|
2017-06-03 16:58:28 +08:00
|
|
|
volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: fmt.Sprintf("volumeId %s not found.", vid)}
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-03 16:58:28 +08:00
|
|
|
volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: fmt.Sprintf("Unknown volumeId format: %s", vid)}
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:08:20 +08:00
|
|
|
// If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
|
|
|
|
// If "volumeId" is provided, this only returns the volumeId location
|
2013-12-02 17:37:36 +08:00
|
|
|
func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vid := r.FormValue("volumeId")
|
2019-02-14 16:08:20 +08:00
|
|
|
if vid != "" {
|
|
|
|
// backward compatible
|
|
|
|
commaSep := strings.Index(vid, ",")
|
|
|
|
if commaSep > 0 {
|
|
|
|
vid = vid[0:commaSep]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileId := r.FormValue("fileId")
|
|
|
|
if fileId != "" {
|
|
|
|
commaSep := strings.Index(fileId, ",")
|
|
|
|
if commaSep > 0 {
|
|
|
|
vid = fileId[0:commaSep]
|
|
|
|
}
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
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]
|
2015-01-08 16:19:32 +08:00
|
|
|
httpStatus := http.StatusOK
|
2014-04-14 14:41:34 +08:00
|
|
|
if location.Error != "" {
|
2015-01-08 16:19:32 +08:00
|
|
|
httpStatus = http.StatusNotFound
|
2019-02-14 16:08:20 +08:00
|
|
|
} else {
|
2019-06-06 15:29:02 +08:00
|
|
|
forRead := r.FormValue("read")
|
|
|
|
isRead := forRead == "yes"
|
|
|
|
ms.maybeAddJwtAuthorization(w, fileId, !isRead)
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, httpStatus, location)
|
2014-04-14 14:41:34 +08:00
|
|
|
}
|
|
|
|
|
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()
|
2015-04-07 05:17:36 +08:00
|
|
|
requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
|
|
|
|
if e != nil || requestedCount == 0 {
|
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 {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusNotAcceptable, 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 {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left!"})
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
2015-03-10 15:20:31 +08:00
|
|
|
}
|
|
|
|
ms.vgLock.Lock()
|
|
|
|
defer ms.vgLock.Unlock()
|
|
|
|
if !ms.Topo.HasWritableVolume(option) {
|
2019-02-19 04:11:52 +08:00
|
|
|
if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOpiton, ms.Topo); err != nil {
|
2015-03-10 15:20:31 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError,
|
|
|
|
fmt.Errorf("Cannot grow volume group! %v", err))
|
|
|
|
return
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2019-06-06 15:29:02 +08:00
|
|
|
ms.maybeAddJwtAuthorization(w, fid, true)
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
|
2013-12-02 17:37:36 +08:00
|
|
|
} else {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
|
2019-06-06 15:29:02 +08:00
|
|
|
func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
|
|
|
|
var encodedJwt security.EncodedJwt
|
|
|
|
if isWrite {
|
|
|
|
encodedJwt = security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
|
|
|
|
} else {
|
|
|
|
encodedJwt = security.GenJwt(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
if encodedJwt == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-06-06 15:29:02 +08:00
|
|
|
|
2019-02-14 16:08:20 +08:00
|
|
|
w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
|
|
|
|
}
|