2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2015-01-08 16:19:32 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2013-12-02 17:37:36 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/topology"
|
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-07-28 18:58:13 +08:00
|
|
|
volumeLocations[vid] = ms.findVolumeLocation(collection, 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
|
|
|
}
|
2020-05-13 18:46:38 +08:00
|
|
|
collection := r.FormValue("collection") // optional, but can be faster if too many collections
|
2019-07-28 18:58:13 +08:00
|
|
|
location := ms.findVolumeLocation(collection, vid)
|
2015-01-08 16:19:32 +08:00
|
|
|
httpStatus := http.StatusOK
|
2019-09-29 14:17:37 +08:00
|
|
|
if location.Error != "" || location.Locations == nil {
|
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
|
|
|
}
|
|
|
|
|
2019-07-28 18:58:13 +08:00
|
|
|
// findVolumeLocation finds the volume location from master topo if it is leader,
|
|
|
|
// or from master client if not leader
|
2019-09-29 14:17:37 +08:00
|
|
|
func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
|
2019-07-28 18:58:13 +08:00
|
|
|
var locations []operation.Location
|
|
|
|
var err error
|
|
|
|
if ms.Topo.IsLeader() {
|
|
|
|
volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
|
2019-10-30 15:49:58 +08:00
|
|
|
if newVolumeIdErr != nil {
|
|
|
|
err = fmt.Errorf("Unknown volume id %s", vid)
|
|
|
|
} else {
|
|
|
|
machines := ms.Topo.Lookup(collection, volumeId)
|
|
|
|
for _, loc := range machines {
|
2022-08-05 08:35:00 +08:00
|
|
|
locations = append(locations, operation.Location{
|
|
|
|
Url: loc.Url(), PublicUrl: loc.PublicUrl, DataCenter: loc.GetDataCenterId(),
|
|
|
|
})
|
2019-10-30 15:49:58 +08:00
|
|
|
}
|
2019-07-28 18:58:13 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
|
|
|
|
for _, loc := range machines {
|
2022-08-05 08:35:00 +08:00
|
|
|
locations = append(locations, operation.Location{
|
|
|
|
Url: loc.Url, PublicUrl: loc.PublicUrl, DataCenter: loc.DataCenter,
|
|
|
|
})
|
2019-07-28 18:58:13 +08:00
|
|
|
}
|
|
|
|
err = getVidLocationsErr
|
|
|
|
}
|
2020-05-13 18:46:38 +08:00
|
|
|
if len(locations) == 0 && err == nil {
|
|
|
|
err = fmt.Errorf("volume id %s not found", vid)
|
|
|
|
}
|
2019-07-28 18:58:13 +08:00
|
|
|
ret := operation.LookupResult{
|
2021-08-13 12:40:33 +08:00
|
|
|
VolumeOrFileId: vid,
|
|
|
|
Locations: locations,
|
2019-07-28 18:58:13 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ret.Error = err.Error()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-11-10 20:11:03 +08:00
|
|
|
writableVolumeCount, e := strconv.Atoi(r.FormValue("writableVolumeCount"))
|
|
|
|
if e != nil {
|
|
|
|
writableVolumeCount = 0
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:40:04 +08:00
|
|
|
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
|
|
|
|
|
2021-10-05 16:58:30 +08:00
|
|
|
if !vl.HasGrowRequest() && vl.ShouldGrowVolumes(option) {
|
2021-08-11 04:04:25 +08:00
|
|
|
glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
|
2020-12-14 11:44:57 +08:00
|
|
|
if ms.Topo.AvailableSpaceFor(option) <= 0 {
|
2021-02-19 11:10:20 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
2015-03-10 15:20:31 +08:00
|
|
|
}
|
2021-05-06 18:46:14 +08:00
|
|
|
errCh := make(chan error, 1)
|
2021-10-05 16:58:30 +08:00
|
|
|
vl.AddGrowRequest()
|
2021-05-06 18:46:14 +08:00
|
|
|
ms.vgCh <- &topology.VolumeGrowRequest{
|
|
|
|
Option: option,
|
|
|
|
Count: writableVolumeCount,
|
|
|
|
ErrCh: errCh,
|
|
|
|
}
|
2021-07-01 16:21:14 +08:00
|
|
|
if err := <-errCh; err != nil {
|
2021-05-06 18:46:14 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("cannot grow volume group! %v", err))
|
|
|
|
return
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-06 14:17:15 +08:00
|
|
|
fid, count, dnList, 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)
|
2021-09-06 14:17:15 +08:00
|
|
|
dn := dnList.Head()
|
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) {
|
2021-02-17 07:39:12 +08:00
|
|
|
if fileId == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-06-06 15:29:02 +08:00
|
|
|
var encodedJwt security.EncodedJwt
|
|
|
|
if isWrite {
|
2021-12-29 19:39:41 +08:00
|
|
|
encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
|
2019-06-06 15:29:02 +08:00
|
|
|
} else {
|
2021-12-29 19:39:41 +08:00
|
|
|
encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
|
2019-06-06 15:29:02 +08:00
|
|
|
}
|
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))
|
|
|
|
}
|