seaweedfs/weed/wdclient/vid_map.go

245 lines
5.6 KiB
Go
Raw Normal View History

package wdclient
import (
"errors"
2018-07-29 12:02:56 +08:00
"fmt"
"github.com/seaweedfs/seaweedfs/weed/pb"
"math/rand"
"strconv"
2018-07-29 12:02:56 +08:00
"strings"
"sync"
"sync/atomic"
2018-07-29 12:02:56 +08:00
"github.com/seaweedfs/seaweedfs/weed/glog"
)
2019-09-14 16:21:51 +08:00
const (
maxCursorIndex = 4096
)
type HasLookupFileIdFunction interface {
GetLookupFileIdFunction() LookupFileIdFunctionType
}
type LookupFileIdFunctionType func(fileId string) (targetUrls []string, err error)
type Location struct {
2020-11-11 18:03:47 +08:00
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
DataCenter string `json:"dataCenter,omitempty"`
GrpcPort int `json:"grpcPort,omitempty"`
}
func (l Location) ServerAddress() pb.ServerAddress {
return pb.NewServerAddressWithGrpcPort(l.Url, l.GrpcPort)
}
2018-07-29 09:40:31 +08:00
type vidMap struct {
sync.RWMutex
vid2Locations map[uint32][]Location
ecVid2Locations map[uint32][]Location
DataCenter string
cursor int32
cache *vidMap
}
func newVidMap(dataCenter string) *vidMap {
return &vidMap{
vid2Locations: make(map[uint32][]Location),
ecVid2Locations: make(map[uint32][]Location),
DataCenter: dataCenter,
cursor: -1,
}
}
2019-09-14 16:21:51 +08:00
func (vc *vidMap) getLocationIndex(length int) (int, error) {
if length <= 0 {
return 0, fmt.Errorf("invalid length: %d", length)
2018-07-29 09:40:31 +08:00
}
2019-09-14 16:21:51 +08:00
if atomic.LoadInt32(&vc.cursor) == maxCursorIndex {
atomic.CompareAndSwapInt32(&vc.cursor, maxCursorIndex, -1)
}
2019-09-14 16:21:51 +08:00
return int(atomic.AddInt32(&vc.cursor, 1)) % length, nil
2018-07-29 09:40:31 +08:00
}
func (vc *vidMap) isSameDataCenter(loc *Location) bool {
if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter {
return false
}
return true
}
func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err error) {
id, err := strconv.Atoi(vid)
if err != nil {
glog.V(1).Infof("Unknown volume id %s", vid)
return nil, err
}
locations, found := vc.GetLocations(uint32(id))
if !found {
return nil, fmt.Errorf("volume %d not found", id)
}
var sameDcServers, otherDcServers []string
for _, loc := range locations {
if vc.isSameDataCenter(&loc) {
2021-11-17 01:14:01 +08:00
sameDcServers = append(sameDcServers, loc.Url)
} else {
otherDcServers = append(otherDcServers, loc.Url)
2020-11-12 05:13:33 +08:00
}
}
rand.Shuffle(len(sameDcServers), func(i, j int) {
sameDcServers[i], sameDcServers[j] = sameDcServers[j], sameDcServers[i]
})
rand.Shuffle(len(otherDcServers), func(i, j int) {
otherDcServers[i], otherDcServers[j] = otherDcServers[j], otherDcServers[i]
})
// Prefer same data center
serverUrls = append(sameDcServers, otherDcServers...)
return
}
func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) {
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return nil, errors.New("Invalid fileId " + fileId)
}
serverUrls, lookupError := vc.LookupVolumeServerUrl(parts[0])
if lookupError != nil {
return nil, lookupError
}
for _, serverUrl := range serverUrls {
fullUrls = append(fullUrls, "http://"+serverUrl+"/"+fileId)
}
return
}
func (vc *vidMap) GetVidLocations(vid string) (locations []Location, err error) {
2018-11-21 12:56:28 +08:00
id, err := strconv.Atoi(vid)
if err != nil {
glog.V(1).Infof("Unknown volume id %s", vid)
return nil, fmt.Errorf("Unknown volume id %s", vid)
2018-11-21 12:56:28 +08:00
}
foundLocations, found := vc.GetLocations(uint32(id))
if found {
return foundLocations, nil
}
return nil, fmt.Errorf("volume id %s not found", vid)
2018-11-21 12:56:28 +08:00
}
func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
2022-08-15 16:05:35 +08:00
// glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations)
locations, found = vc.getLocations(vid)
if found && len(locations) > 0 {
return locations, found
}
if vc.cache != nil {
return vc.cache.GetLocations(vid)
}
return nil, false
}
func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) {
vc.RLock()
defer vc.RUnlock()
2020-11-12 07:10:06 +08:00
locations, found = vc.vid2Locations[vid]
if found && len(locations) > 0 {
return
}
locations, found = vc.ecVid2Locations[vid]
return
}
2018-07-29 09:40:31 +08:00
func (vc *vidMap) addLocation(vid uint32, location Location) {
vc.Lock()
defer vc.Unlock()
glog.V(4).Infof("+ volume id %d: %+v", vid, location)
locations, found := vc.vid2Locations[vid]
if !found {
vc.vid2Locations[vid] = []Location{location}
return
}
for _, loc := range locations {
if loc.Url == location.Url {
return
}
}
vc.vid2Locations[vid] = append(locations, location)
}
func (vc *vidMap) addEcLocation(vid uint32, location Location) {
vc.Lock()
defer vc.Unlock()
glog.V(4).Infof("+ ec volume id %d: %+v", vid, location)
locations, found := vc.ecVid2Locations[vid]
if !found {
vc.ecVid2Locations[vid] = []Location{location}
return
}
for _, loc := range locations {
if loc.Url == location.Url {
return
}
}
vc.ecVid2Locations[vid] = append(locations, location)
}
2018-07-29 09:40:31 +08:00
func (vc *vidMap) deleteLocation(vid uint32, location Location) {
if vc.cache != nil {
vc.cache.deleteLocation(vid, location)
}
vc.Lock()
defer vc.Unlock()
glog.V(4).Infof("- volume id %d: %+v", vid, location)
locations, found := vc.vid2Locations[vid]
if !found {
return
}
for i, loc := range locations {
if loc.Url == location.Url {
vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
2019-04-22 04:33:32 +08:00
break
}
}
}
func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
if vc.cache != nil {
vc.cache.deleteLocation(vid, location)
}
vc.Lock()
defer vc.Unlock()
glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
locations, found := vc.ecVid2Locations[vid]
if !found {
return
}
for i, loc := range locations {
if loc.Url == location.Url {
vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
break
}
}
}