mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-07-30 12:03:21 +00:00
* fix(master): let the growth initiator wait for the growth it triggered The growth-in-flight shed also fired on the request that initiated the growth: it sets the pending flag right before the shed check, so a cold-start assign enqueued growth and immediately failed itself with "volume growth in progress". With no concurrent assigns around to pick up the freshly grown volume, a single writer against an empty cluster never completes a write despite ample free space. Claim the pending flag with a compare-and-swap so exactly one request becomes the initiator, triggering growth at most once, and let it wait for that growth to land. Everyone else still sheds retryably instead of pinning a goroutine: followers behind an in-flight growth, an initiator whose growth concluded without yielding a writable volume, and an initiator whose growth outlives the 10s wait budget, which previously surfaced a non-retryable error (gRPC Unknown, HTTP 406) even though a retry would have succeeded moments later. * fix(master): stop assign waits when the request is cancelled The assign retry loops slept through client cancellation, keeping a goroutine spinning for the rest of the 10s budget after the caller had gone; StreamAssign also ran assigns on a background context detached from the stream. Wait on the request context and pass the stream context through. * topology: drop the unconditional grow-request setter Growth is only claimed through AddGrowRequestIfAbsent's compare-and-swap now; keeping the raw Store(true) around invites the check-then-set race back. * test: cover cold-start first write with a real cluster Boot a fresh master plus three empty volume servers and require the very first assign - HTTP and gRPC, each on a cold volume layout, no client retries - to complete a write. The assign that triggers volume growth must wait for it rather than answering "volume growth in progress"; unit tests stub the topology, so only a real cluster exercises the assign-grow-wait path end to end.
253 lines
7.7 KiB
Go
253 lines
7.7 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"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"
|
|
)
|
|
|
|
func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
|
|
volumeLocations = make(map[string]operation.LookupResult)
|
|
for _, vid := range vids {
|
|
commaSep := strings.Index(vid, ",")
|
|
if commaSep > 0 {
|
|
vid = vid[0:commaSep]
|
|
}
|
|
if _, ok := volumeLocations[vid]; ok {
|
|
continue
|
|
}
|
|
volumeLocations[vid] = ms.findVolumeLocation(collection, vid)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 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
|
|
func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
|
|
vid := r.FormValue("volumeId")
|
|
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]
|
|
}
|
|
}
|
|
collection := r.FormValue("collection") // optional, but can be faster if too many collections
|
|
location := ms.findVolumeLocation(collection, vid)
|
|
httpStatus := http.StatusOK
|
|
if location.Error != "" || location.Locations == nil {
|
|
if location.NotFound && ms.Topo.IsLeader() && ms.Topo.IsWarmingUp() {
|
|
httpStatus = http.StatusServiceUnavailable
|
|
remaining := ms.Topo.RemainingWarmupDuration()
|
|
if remaining < time.Second {
|
|
remaining = time.Second
|
|
}
|
|
w.Header().Set("Retry-After", fmt.Sprintf("%d", int(math.Ceil(remaining.Seconds()))))
|
|
location.Error = "service warming up, please retry"
|
|
} else {
|
|
httpStatus = http.StatusNotFound
|
|
}
|
|
} else {
|
|
forRead := r.FormValue("read")
|
|
isRead := forRead == "yes"
|
|
ms.maybeAddJwtAuthorization(w, fileId, !isRead)
|
|
}
|
|
writeJsonQuiet(w, r, httpStatus, location)
|
|
}
|
|
|
|
// findVolumeLocation finds the volume location from master topo if it is leader,
|
|
// or from master client if not leader
|
|
func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
|
|
var locations []operation.Location
|
|
var err error
|
|
if ms.Topo.IsLeader() {
|
|
volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
|
|
if newVolumeIdErr != nil {
|
|
err = fmt.Errorf("Unknown volume id %s", vid)
|
|
} else {
|
|
machines := ms.Topo.Lookup(collection, volumeId)
|
|
for _, loc := range machines {
|
|
locations = append(locations, operation.Location{
|
|
Url: loc.Url(),
|
|
PublicUrl: loc.PublicUrl,
|
|
DataCenter: loc.GetDataCenterId(),
|
|
GrpcPort: loc.GrpcPort,
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
|
|
for _, loc := range machines {
|
|
locations = append(locations, operation.Location{
|
|
Url: loc.Url,
|
|
PublicUrl: loc.PublicUrl,
|
|
DataCenter: loc.DataCenter,
|
|
GrpcPort: loc.GrpcPort,
|
|
})
|
|
}
|
|
err = getVidLocationsErr
|
|
}
|
|
notFound := false
|
|
if len(locations) == 0 && err == nil {
|
|
err = fmt.Errorf("volume id %s not found", vid)
|
|
notFound = true
|
|
}
|
|
ret := operation.LookupResult{
|
|
VolumeOrFileId: vid,
|
|
Locations: locations,
|
|
NotFound: notFound,
|
|
}
|
|
if err != nil {
|
|
ret.Error = err.Error()
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
|
if ms.Topo.IsLeader() && ms.Topo.IsWarmingUp() {
|
|
remaining := ms.Topo.RemainingWarmupDuration()
|
|
if remaining < time.Second {
|
|
remaining = time.Second
|
|
}
|
|
w.Header().Set("Retry-After", fmt.Sprintf("%d", int(math.Ceil(remaining.Seconds()))))
|
|
writeJsonQuiet(w, r, http.StatusServiceUnavailable, operation.AssignResult{
|
|
Error: "master is warming up, topology is still loading",
|
|
})
|
|
return
|
|
}
|
|
stats.AssignRequest()
|
|
requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
|
|
if e != nil || requestedCount == 0 {
|
|
requestedCount = 1
|
|
}
|
|
|
|
writableVolumeCount, e := strconv.ParseUint(r.FormValue("writableVolumeCount"), 10, 32)
|
|
if e != nil {
|
|
writableVolumeCount = 0
|
|
}
|
|
|
|
expectedDataSize, e := strconv.ParseUint(r.FormValue("dataSize"), 10, 64)
|
|
if e != nil {
|
|
expectedDataSize = 0
|
|
}
|
|
|
|
option, err := ms.getVolumeGrowOption(r)
|
|
if err != nil {
|
|
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
|
|
return
|
|
}
|
|
|
|
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
|
|
|
|
var (
|
|
lastErr error
|
|
maxTimeout = time.Second * 10
|
|
startTime = time.Now()
|
|
initiatedGrow bool
|
|
)
|
|
|
|
if !ms.Topo.DataCenterExists(option.DataCenter) {
|
|
writeJsonQuiet(w, r, http.StatusBadRequest, operation.AssignResult{
|
|
Error: fmt.Sprintf("data center %v not found in topology", option.DataCenter),
|
|
})
|
|
return
|
|
}
|
|
|
|
for time.Since(startTime) < maxTimeout {
|
|
fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(requestedCount, option, vl, expectedDataSize)
|
|
if shouldGrow && !initiatedGrow && !ms.option.VolumeGrowthDisabled && vl.AddGrowRequestIfAbsent() {
|
|
initiatedGrow = true
|
|
glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
|
|
if err != nil && ms.Topo.AvailableSpaceFor(option) <= 0 {
|
|
err = fmt.Errorf("%s and no free volumes left for %s", err.Error(), option.String())
|
|
}
|
|
ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
|
|
Option: option,
|
|
Count: uint32(writableVolumeCount),
|
|
Reason: "http assign",
|
|
}
|
|
}
|
|
if err != nil {
|
|
stats.MasterPickForWriteErrorCounter.Inc()
|
|
lastErr = err
|
|
if shouldGrow {
|
|
if ms.Topo.AvailableSpaceFor(option) <= 0 {
|
|
break // out of space: surface the real error (406 below)
|
|
}
|
|
// See Assign: only the initiator waits, and only while the
|
|
// growth it triggered is still pending.
|
|
if initiatedGrow != vl.HasGrowRequest() {
|
|
w.Header().Set("Retry-After", "1")
|
|
writeJsonQuiet(w, r, http.StatusServiceUnavailable, operation.AssignResult{
|
|
Error: fmt.Sprintf("no writable volumes for %s, volume growth in progress", option.String()),
|
|
})
|
|
return
|
|
}
|
|
}
|
|
select {
|
|
case <-r.Context().Done():
|
|
return // client gone
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
continue
|
|
} else {
|
|
ms.maybeAddJwtAuthorization(w, fid, true)
|
|
dn := dnList.Head()
|
|
if dn == nil {
|
|
continue
|
|
}
|
|
writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
|
|
return
|
|
}
|
|
}
|
|
|
|
// See Assign: initiator that timed out with growth still pending stays retryable.
|
|
if initiatedGrow && vl.HasGrowRequest() && ms.Topo.AvailableSpaceFor(option) > 0 {
|
|
w.Header().Set("Retry-After", "1")
|
|
writeJsonQuiet(w, r, http.StatusServiceUnavailable, operation.AssignResult{
|
|
Error: fmt.Sprintf("no writable volumes for %s, volume growth in progress", option.String()),
|
|
})
|
|
return
|
|
}
|
|
if lastErr != nil {
|
|
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: lastErr.Error()})
|
|
} else {
|
|
writeJsonQuiet(w, r, http.StatusRequestTimeout, operation.AssignResult{Error: "request timeout"})
|
|
}
|
|
}
|
|
|
|
func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
|
|
if fileId == "" {
|
|
return
|
|
}
|
|
var encodedJwt security.EncodedJwt
|
|
if isWrite {
|
|
encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey(), ms.guard.ExpiresAfterSec(), fileId)
|
|
} else {
|
|
encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey(), ms.guard.ReadExpiresAfterSec(), fileId)
|
|
}
|
|
if encodedJwt == "" {
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Authorization", security.BearerPrefix+string(encodedJwt))
|
|
}
|