2018-07-29 05:22:46 +08:00
|
|
|
package wdclient
|
|
|
|
|
|
|
|
import (
|
2018-07-29 05:51:36 +08:00
|
|
|
"errors"
|
2018-07-29 12:02:56 +08:00
|
|
|
"fmt"
|
2021-09-13 13:47:52 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2018-07-29 05:51:36 +08:00
|
|
|
"strconv"
|
2018-07-29 12:02:56 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-09-13 20:06:02 +08:00
|
|
|
"sync/atomic"
|
2018-07-29 12:02:56 +08:00
|
|
|
|
2018-07-29 05:51:36 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-29 05:22:46 +08:00
|
|
|
)
|
|
|
|
|
2019-09-14 16:21:51 +08:00
|
|
|
const (
|
|
|
|
maxCursorIndex = 4096
|
|
|
|
)
|
|
|
|
|
2021-01-06 20:21:34 +08:00
|
|
|
type HasLookupFileIdFunction interface {
|
|
|
|
GetLookupFileIdFunction() LookupFileIdFunctionType
|
|
|
|
}
|
|
|
|
|
|
|
|
type LookupFileIdFunctionType func(fileId string) (targetUrls []string, err error)
|
|
|
|
|
2018-07-29 05:22:46 +08:00
|
|
|
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"`
|
2021-09-13 13:47:52 +08:00
|
|
|
GrpcPort int `json:"grpcPort,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) ServerAddress() pb.ServerAddress {
|
|
|
|
return pb.NewServerAddressWithGrpcPort(l.Url, l.GrpcPort)
|
2018-07-29 05:22:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
type vidMap struct {
|
2018-07-29 05:22:46 +08:00
|
|
|
sync.RWMutex
|
|
|
|
vid2Locations map[uint32][]Location
|
2020-11-12 05:13:33 +08:00
|
|
|
DataCenter string
|
|
|
|
cursor int32
|
2018-07-29 05:22:46 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 05:13:33 +08:00
|
|
|
func newVidMap(dataCenter string) vidMap {
|
2018-07-29 09:40:31 +08:00
|
|
|
return vidMap{
|
|
|
|
vid2Locations: make(map[uint32][]Location),
|
2020-11-12 05:13:33 +08:00
|
|
|
DataCenter: dataCenter,
|
2019-09-13 20:06:02 +08:00
|
|
|
cursor: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 16:21:51 +08:00
|
|
|
func (vc *vidMap) getLocationIndex(length int) (int, error) {
|
2019-09-13 20:06:02 +08:00
|
|
|
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-13 20:06:02 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-10-08 13:49:04 +08:00
|
|
|
func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err error) {
|
2018-07-29 05:51:36 +08:00
|
|
|
id, err := strconv.Atoi(vid)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("Unknown volume id %s", vid)
|
2020-10-08 13:49:04 +08:00
|
|
|
return nil, err
|
2018-07-29 05:51:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-08 13:49:04 +08:00
|
|
|
locations, found := vc.GetLocations(uint32(id))
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("volume %d not found", id)
|
2018-07-29 05:51:36 +08:00
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
for _, loc := range locations {
|
2020-11-12 05:13:33 +08:00
|
|
|
if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter {
|
|
|
|
serverUrls = append(serverUrls, loc.Url)
|
|
|
|
} else {
|
|
|
|
serverUrls = append([]string{loc.Url}, serverUrls...)
|
|
|
|
}
|
2018-07-29 05:51:36 +08:00
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
return
|
2018-07-29 05:51:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-06 20:21:34 +08:00
|
|
|
func (vc *vidMap) GetLookupFileIdFunction() LookupFileIdFunctionType {
|
|
|
|
return vc.LookupFileId
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:49:04 +08:00
|
|
|
func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) {
|
2018-10-14 15:12:28 +08:00
|
|
|
parts := strings.Split(fileId, ",")
|
|
|
|
if len(parts) != 2 {
|
2020-10-08 13:49:04 +08:00
|
|
|
return nil, errors.New("Invalid fileId " + fileId)
|
2018-10-14 15:12:28 +08:00
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
serverUrls, lookupError := vc.LookupVolumeServerUrl(parts[0])
|
2018-10-14 15:12:28 +08:00
|
|
|
if lookupError != nil {
|
2020-10-08 13:49:04 +08:00
|
|
|
return nil, lookupError
|
2018-10-14 15:12:28 +08:00
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
for _, serverUrl := range serverUrls {
|
|
|
|
fullUrls = append(fullUrls, "http://"+serverUrl+"/"+fileId)
|
|
|
|
}
|
|
|
|
return
|
2018-10-14 15:12:28 +08:00
|
|
|
}
|
|
|
|
|
2019-07-28 18:58:13 +08:00
|
|
|
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)
|
2019-07-28 18:58:13 +08:00
|
|
|
return nil, fmt.Errorf("Unknown volume id %s", vid)
|
2018-11-21 12:56:28 +08:00
|
|
|
}
|
2019-07-28 18:58:13 +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
|
|
|
}
|
|
|
|
|
2019-07-28 18:58:13 +08:00
|
|
|
func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
|
2018-07-29 05:22:46 +08:00
|
|
|
vc.RLock()
|
|
|
|
defer vc.RUnlock()
|
2020-11-12 07:10:06 +08:00
|
|
|
|
2019-07-28 18:58:13 +08:00
|
|
|
locations, found = vc.vid2Locations[vid]
|
|
|
|
return
|
2018-07-29 05:22:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
func (vc *vidMap) addLocation(vid uint32, location Location) {
|
2018-07-29 05:22:46 +08:00
|
|
|
vc.Lock()
|
|
|
|
defer vc.Unlock()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
func (vc *vidMap) deleteLocation(vid uint32, location Location) {
|
2018-07-29 05:22:46 +08:00
|
|
|
vc.Lock()
|
|
|
|
defer vc.Unlock()
|
|
|
|
|
|
|
|
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
|
2018-07-29 05:22:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|