Files
seaweedfs/weed/operation/lookup.go
T
Chris LuandGitHub 15e4da65f7 volume: avoid read-only replica write targets (#11195)
* master: carry replica read-only state in volume lookups

* volume: refresh writable replica targets

* volume: preserve read-only replicas for deletes

* master: propagate read-only delete capability

* volume: target delete-capable replicas

* volume: honor configured HTTPS for replica deletes

* volume: reject insecure delete authorization forwarding

* master: broadcast delete capability changes

* volume: align Rust replica routing

* http: protect credentialed replica redirects

* master: preserve digest compatibility for delete capability

* volume: propagate read-only state in short heartbeats

* volume: report changed short volume state

* http: guard TLS client redirects

* master: announce mounted volume read-only state

* volume: replace changed identity deltas

* master: replace incremental volume layouts in order

* master: keep moved volume lookup available

* volume: announce read-only mounts
2026-09-07 09:23:56 -07:00

159 lines
4.8 KiB
Go

package operation
import (
"context"
"errors"
"fmt"
"math/rand/v2"
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb"
"google.golang.org/grpc"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
)
type Location struct {
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
DataCenter string `json:"dataCenter,omitempty"`
GrpcPort int `json:"grpcPort,omitempty"`
DataInRemote bool `json:"dataInRemote,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
ReadOnlyCanDelete bool `json:"readOnlyCanDelete,omitempty"`
}
func (l *Location) ServerAddress() pb.ServerAddress {
return pb.NewServerAddressWithGrpcPort(l.Url, l.GrpcPort)
}
type LookupResult struct {
VolumeOrFileId string `json:"volumeOrFileId,omitempty"`
Locations []Location `json:"locations,omitempty"`
Jwt string `json:"jwt,omitempty"`
Error string `json:"error,omitempty"`
NotFound bool `json:"-"`
}
func (lr *LookupResult) String() string {
return fmt.Sprintf("VolumeOrFileId:%s, Locations:%v, Error:%s", lr.VolumeOrFileId, lr.Locations, lr.Error)
}
var (
vc VidCache // caching of volume locations, re-check if after 10 minutes
)
// LookupFileId resolves a "<vid>,<cookie>" file id to one HTTP read URL,
// preferring a volume server whose replica holds the data locally over one
// backed by remote-tier storage. If no local replica is known the function
// falls back to a random remote replica. The returned jwt is the read
// authorization the master stamped on the volume; pass it through to the
// volume server on the read request.
func LookupFileId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, fileId string) (fullUrl string, jwt string, err error) {
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return "", jwt, errors.New("Invalid fileId " + fileId)
}
lookup, lookupError := LookupVolumeId(masterFn, grpcDialOption, parts[0])
if lookupError != nil {
return "", jwt, lookupError
}
if len(lookup.Locations) == 0 {
return "", jwt, errors.New("File Not Found")
}
localUrls := make([]string, 0, len(lookup.Locations))
for _, loc := range lookup.Locations {
if !loc.DataInRemote {
localUrls = append(localUrls, loc.Url)
}
}
if len(localUrls) == 0 {
for _, loc := range lookup.Locations {
localUrls = append(localUrls, loc.Url)
}
}
return "http://" + localUrls[rand.IntN(len(localUrls))] + "/" + fileId, lookup.Jwt, nil
}
func LookupVolumeId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vid string) (*LookupResult, error) {
results, err := LookupVolumeIds(masterFn, grpcDialOption, []string{vid})
return results[vid], err
}
// InvalidateVolumeIdLocationCache drops cached locations for vid so the next lookup re-queries the master.
func InvalidateVolumeIdLocationCache(vid string) {
vc.Delete(vid)
}
// LookupVolumeIds find volume locations by cache and actual lookup
func LookupVolumeIds(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vids []string, useCache ...bool) (map[string]*LookupResult, error) {
ret := make(map[string]*LookupResult)
var unknown_vids []string
cacheLocations := len(useCache) == 0 || useCache[0]
//check vid cache first
for _, vid := range vids {
if cacheLocations {
locations, cacheErr := vc.Get(vid)
if cacheErr == nil {
ret[vid] = &LookupResult{VolumeOrFileId: vid, Locations: locations}
continue
}
}
unknown_vids = append(unknown_vids, vid)
}
//return success if all volume ids are known
if len(unknown_vids) == 0 {
return ret, nil
}
//only query unknown_vids
err := WithMasterServerClient(context.Background(), false, masterFn(context.Background()), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
req := &master_pb.LookupVolumeRequest{
VolumeOrFileIds: unknown_vids,
}
resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
if grpcErr != nil {
return grpcErr
}
//set newly checked vids to cache
for _, vidLocations := range resp.VolumeIdLocations {
var locations []Location
for _, loc := range vidLocations.Locations {
locations = append(locations, Location{
Url: loc.Url,
PublicUrl: loc.PublicUrl,
DataCenter: loc.DataCenter,
GrpcPort: int(loc.GrpcPort),
DataInRemote: loc.DataInRemote,
ReadOnly: loc.ReadOnly,
ReadOnlyCanDelete: loc.ReadOnlyCanDelete,
})
}
if cacheLocations && vidLocations.Error == "" {
vc.Set(vidLocations.VolumeOrFileId, locations, 10*time.Minute)
}
ret[vidLocations.VolumeOrFileId] = &LookupResult{
VolumeOrFileId: vidLocations.VolumeOrFileId,
Locations: locations,
Jwt: vidLocations.Auth,
Error: vidLocations.Error,
}
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}