diff options
Diffstat (limited to 'weed/wdclient/vid_map.go')
| -rw-r--r-- | weed/wdclient/vid_map.go | 91 |
1 files changed, 78 insertions, 13 deletions
diff --git a/weed/wdclient/vid_map.go b/weed/wdclient/vid_map.go index 271baa132..754c77051 100644 --- a/weed/wdclient/vid_map.go +++ b/weed/wdclient/vid_map.go @@ -3,6 +3,8 @@ package wdclient import ( "errors" "fmt" + "github.com/chrislusf/seaweedfs/weed/pb" + "math/rand" "strconv" "strings" "sync" @@ -25,20 +27,27 @@ type Location struct { 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) } type vidMap struct { sync.RWMutex - vid2Locations map[uint32][]Location - DataCenter string - cursor int32 + vid2Locations map[uint32][]Location + ecVid2Locations map[uint32][]Location + DataCenter string + cursor int32 } func newVidMap(dataCenter string) vidMap { return vidMap{ - vid2Locations: make(map[uint32][]Location), - DataCenter: dataCenter, - cursor: -1, + vid2Locations: make(map[uint32][]Location), + ecVid2Locations: make(map[uint32][]Location), + DataCenter: dataCenter, + cursor: -1, } } @@ -63,20 +72,24 @@ func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err er if !found { return nil, fmt.Errorf("volume %d not found", id) } + var sameDcServers, otherDcServers []string for _, loc := range locations { if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter { - serverUrls = append(serverUrls, loc.Url) + otherDcServers = append(otherDcServers, loc.Url) } else { - serverUrls = append([]string{loc.Url}, serverUrls...) + sameDcServers = append(sameDcServers, loc.Url) } } + 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] + }) + serverUrls = append(sameDcServers, otherDcServers...) return } -func (vc *vidMap) GetLookupFileIdFunction() LookupFileIdFunctionType { - return vc.LookupFileId -} - func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) { parts := strings.Split(fileId, ",") if len(parts) != 2 { @@ -109,14 +122,22 @@ func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) { vc.RLock() defer vc.RUnlock() + glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations) + locations, found = vc.vid2Locations[vid] - return + if found && len(locations) > 0 { + return + } + locations, found = vc.ecVid2Locations[vid] + return locations, found && len(locations) > 0 } 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} @@ -133,10 +154,34 @@ func (vc *vidMap) addLocation(vid uint32, location 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) + +} + func (vc *vidMap) deleteLocation(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 { return @@ -150,3 +195,23 @@ func (vc *vidMap) deleteLocation(vid uint32, location Location) { } } + +func (vc *vidMap) deleteEcLocation(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 { + return + } + + for i, loc := range locations { + if loc.Url == location.Url { + vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...) + break + } + } + +} |
