seaweedfs/weed/wdclient/vid_map.go

156 lines
3.3 KiB
Go
Raw Permalink Normal View History

package wdclient
import (
"errors"
2018-07-29 04:02:56 +00:00
"fmt"
"strconv"
2018-07-29 04:02:56 +00:00
"strings"
"sync"
"sync/atomic"
2018-07-29 04:02:56 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
)
2019-09-14 08:21:51 +00:00
const (
maxCursorIndex = 4096
)
type Location struct {
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
}
2018-07-29 01:40:31 +00:00
type vidMap struct {
sync.RWMutex
vid2Locations map[uint32][]Location
2019-09-14 08:21:51 +00:00
cursor int32
}
2018-07-29 01:40:31 +00:00
func newVidMap() vidMap {
return vidMap{
vid2Locations: make(map[uint32][]Location),
cursor: -1,
}
}
2019-09-14 08:21:51 +00:00
func (vc *vidMap) getLocationIndex(length int) (int, error) {
if length <= 0 {
return 0, fmt.Errorf("invalid length: %d", length)
2018-07-29 01:40:31 +00:00
}
2019-09-14 08:21:51 +00:00
if atomic.LoadInt32(&vc.cursor) == maxCursorIndex {
atomic.CompareAndSwapInt32(&vc.cursor, maxCursorIndex, -1)
}
2019-09-14 08:21:51 +00:00
return int(atomic.AddInt32(&vc.cursor, 1)) % length, nil
2018-07-29 01:40:31 +00:00
}
func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrl string, err error) {
id, err := strconv.Atoi(vid)
if err != nil {
glog.V(1).Infof("Unknown volume id %s", vid)
return "", err
}
return vc.GetRandomLocation(uint32(id))
}
2018-07-29 01:40:31 +00:00
func (vc *vidMap) LookupFileId(fileId string) (fullUrl string, err error) {
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return "", errors.New("Invalid fileId " + fileId)
}
2018-07-29 01:34:15 +00:00
serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
if lookupError != nil {
return "", lookupError
}
return "http://" + serverUrl + "/" + fileId, nil
}
func (vc *vidMap) LookupVolumeServer(fileId string) (volumeServer string, err error) {
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return "", errors.New("Invalid fileId " + fileId)
}
serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
if lookupError != nil {
return "", lookupError
}
return serverUrl, nil
}
func (vc *vidMap) GetVidLocations(vid string) (locations []Location, err error) {
2018-11-21 04:56:28 +00: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 04:56:28 +00: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 04:56:28 +00:00
}
func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
vc.RLock()
defer vc.RUnlock()
locations, found = vc.vid2Locations[vid]
return
}
func (vc *vidMap) GetRandomLocation(vid uint32) (serverUrl string, err error) {
vc.RLock()
defer vc.RUnlock()
locations := vc.vid2Locations[vid]
if len(locations) == 0 {
2019-06-28 18:19:08 +00:00
return "", fmt.Errorf("volume %d not found", vid)
}
2019-09-14 08:21:51 +00:00
index, err := vc.getLocationIndex(len(locations))
if err != nil {
2019-09-14 08:21:51 +00:00
return "", fmt.Errorf("volume %d: %v", vid, err)
}
return locations[index].Url, nil
}
2018-07-29 01:40:31 +00:00
func (vc *vidMap) addLocation(vid uint32, location Location) {
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 01:40:31 +00:00
func (vc *vidMap) deleteLocation(vid uint32, location Location) {
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-21 20:33:32 +00:00
break
}
}
}