http assign logic should be the same as grpc assign

similar logic as bebbc9fe44
This commit is contained in:
chrislu 2024-01-18 08:58:53 -08:00
parent 4e9ea1e628
commit 3002087541

View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/operation" "github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/security"
@ -119,34 +120,52 @@ func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request)
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType) vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(requestedCount, option, vl) var (
if shouldGrow && !vl.HasGrowRequest() { lastErr error
// if picked volume is almost full, trigger a volume-grow request maxTimeout = time.Second * 10
glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr) startTime = time.Now()
if ms.Topo.AvailableSpaceFor(option) <= 0 { )
writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
return
}
errCh := make(chan error, 1) for time.Now().Sub(startTime) < maxTimeout {
vl.AddGrowRequest() fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(requestedCount, option, vl)
ms.vgCh <- &topology.VolumeGrowRequest{ if shouldGrow && !vl.HasGrowRequest() {
Option: option, // if picked volume is almost full, trigger a volume-grow request
Count: writableVolumeCount, glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
ErrCh: errCh, if ms.Topo.AvailableSpaceFor(option) <= 0 {
writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
return
}
errCh := make(chan error, 1)
vl.AddGrowRequest()
ms.vgCh <- &topology.VolumeGrowRequest{
Option: option,
Count: writableVolumeCount,
ErrCh: errCh,
}
if err := <-errCh; err != nil {
writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("cannot grow volume group! %v", err))
return
}
} }
if err := <-errCh; err != nil { if err != nil {
writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("cannot grow volume group! %v", err)) // glog.Warningf("PickForWrite %+v: %v", req, err)
lastErr = err
time.Sleep(200 * time.Millisecond)
continue
} else {
ms.maybeAddJwtAuthorization(w, fid, true)
dn := dnList.Head()
writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
return return
} }
} }
if err == nil {
ms.maybeAddJwtAuthorization(w, fid, true)
dn := dnList.Head()
writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count}) if lastErr != nil {
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: lastErr.Error()})
} else { } else {
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()}) writeJsonQuiet(w, r, http.StatusRequestTimeout, operation.AssignResult{Error: "request timeout"})
} }
} }