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"
|
|
|
|
"math/rand"
|
2018-07-29 05:51:36 +08:00
|
|
|
"strconv"
|
2018-07-29 12:02:56 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-03-20 13:20:14 +08:00
|
|
|
"time"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-20 13:20:14 +08:00
|
|
|
r *rand.Rand
|
2018-07-29 05:22:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
func newVidMap() vidMap {
|
|
|
|
return vidMap{
|
|
|
|
vid2Locations: make(map[uint32][]Location),
|
2019-03-20 13:20:14 +08:00
|
|
|
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
2018-07-29 09:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrl 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)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
locations := vc.GetLocations(uint32(id))
|
|
|
|
if len(locations) == 0 {
|
|
|
|
return "", fmt.Errorf("volume %d not found", id)
|
|
|
|
}
|
|
|
|
|
2019-03-20 13:20:14 +08:00
|
|
|
return locations[vc.r.Intn(len(locations))].Url, nil
|
2018-07-29 05:51:36 +08:00
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
func (vc *vidMap) LookupFileId(fileId string) (fullUrl string, err error) {
|
2018-07-29 05:51:36 +08:00
|
|
|
parts := strings.Split(fileId, ",")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", errors.New("Invalid fileId " + fileId)
|
|
|
|
}
|
2018-07-29 09:34:15 +08:00
|
|
|
serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
|
2018-07-29 05:51:36 +08:00
|
|
|
if lookupError != nil {
|
|
|
|
return "", lookupError
|
|
|
|
}
|
|
|
|
return "http://" + serverUrl + "/" + fileId, nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 15:12:28 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:56:28 +08:00
|
|
|
func (vc *vidMap) GetVidLocations(vid string) (locations []Location) {
|
|
|
|
id, err := strconv.Atoi(vid)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("Unknown volume id %s", vid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vc.GetLocations(uint32(id))
|
|
|
|
}
|
|
|
|
|
2018-07-29 09:40:31 +08:00
|
|
|
func (vc *vidMap) GetLocations(vid uint32) (locations []Location) {
|
2018-07-29 05:22:46 +08:00
|
|
|
vc.RLock()
|
|
|
|
defer vc.RUnlock()
|
|
|
|
|
|
|
|
return vc.vid2Locations[vid]
|
|
|
|
}
|
|
|
|
|
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:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|