1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-06-30 06:12:15 +02:00

tweaking data types

This commit is contained in:
Chris Lu 2019-09-14 01:21:51 -07:00
parent b0e4771135
commit ad3efbb197
2 changed files with 19 additions and 17 deletions

View file

@ -3,7 +3,6 @@ package wdclient
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -12,6 +11,10 @@ import (
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
) )
const (
maxCursorIndex = 4096
)
type Location struct { type Location struct {
Url string `json:"url,omitempty"` Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"` PublicUrl string `json:"publicUrl,omitempty"`
@ -21,7 +24,7 @@ type vidMap struct {
sync.RWMutex sync.RWMutex
vid2Locations map[uint32][]Location vid2Locations map[uint32][]Location
cursor int64 cursor int32
} }
func newVidMap() vidMap { func newVidMap() vidMap {
@ -31,14 +34,14 @@ func newVidMap() vidMap {
} }
} }
func (vc *vidMap) getLocationIndex(length int64) (int64, error) { func (vc *vidMap) getLocationIndex(length int) (int, error) {
if length <= 0 { if length <= 0 {
return 0, fmt.Errorf("invalid length: %d", length) return 0, fmt.Errorf("invalid length: %d", length)
} }
if atomic.LoadInt64(&vc.cursor) == math.MaxInt64 { if atomic.LoadInt32(&vc.cursor) == maxCursorIndex {
atomic.CompareAndSwapInt64(&vc.cursor, math.MaxInt64, -1) atomic.CompareAndSwapInt32(&vc.cursor, maxCursorIndex, -1)
} }
return atomic.AddInt64(&vc.cursor, 1) % length, nil return int(atomic.AddInt32(&vc.cursor, 1)) % length, nil
} }
func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrl string, err error) { func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrl string, err error) {
@ -105,9 +108,9 @@ func (vc *vidMap) GetRandomLocation(vid uint32) (serverUrl string, err error) {
return "", fmt.Errorf("volume %d not found", vid) return "", fmt.Errorf("volume %d not found", vid)
} }
index, err := vc.getLocationIndex(int64(len(locations))) index, err := vc.getLocationIndex(len(locations))
if err != nil { if err != nil {
return "", fmt.Errorf("volume %d. %v", vid, err) return "", fmt.Errorf("volume %d: %v", vid, err)
} }
return locations[index].Url, nil return locations[index].Url, nil

View file

@ -2,14 +2,13 @@ package wdclient
import ( import (
"fmt" "fmt"
"math"
"testing" "testing"
) )
func TestLocationIndex(t *testing.T) { func TestLocationIndex(t *testing.T) {
vm := vidMap{} vm := vidMap{}
// test must be failed // test must be failed
mustFailed := func(length int64) { mustFailed := func(length int) {
_, err := vm.getLocationIndex(length) _, err := vm.getLocationIndex(length)
if err == nil { if err == nil {
t.Errorf("length %d must be failed", length) t.Errorf("length %d must be failed", length)
@ -22,11 +21,11 @@ func TestLocationIndex(t *testing.T) {
mustFailed(-1) mustFailed(-1)
mustFailed(0) mustFailed(0)
mustOk := func(length, cursor, expect int64) { mustOk := func(length, cursor, expect int) {
if length <= 0 { if length <= 0 {
t.Fatal("please don't do this") t.Fatal("please don't do this")
} }
vm.cursor = cursor vm.cursor = int32(cursor)
got, err := vm.getLocationIndex(length) got, err := vm.getLocationIndex(length)
if err != nil { if err != nil {
t.Errorf("length: %d, why? %v\n", length, err) t.Errorf("length: %d, why? %v\n", length, err)
@ -38,17 +37,17 @@ func TestLocationIndex(t *testing.T) {
} }
} }
for i := int64(-1); i < 100; i++ { for i := -1; i < 100; i++ {
mustOk(7, i, (i+1)%7) mustOk(7, i, (i+1)%7)
} }
// when cursor reaches MaxInt64 // when cursor reaches MaxInt64
mustOk(7, math.MaxInt64, 0) mustOk(7, maxCursorIndex, 0)
// test with constructor // test with constructor
vm = newVidMap() vm = newVidMap()
length := int64(7) length := 7
for i := int64(0); i < 100; i++ { for i := 0; i < 100; i++ {
got, err := vm.getLocationIndex(length) got, err := vm.getLocationIndex(length)
if err != nil { if err != nil {
t.Errorf("length: %d, why? %v\n", length, err) t.Errorf("length: %d, why? %v\n", length, err)
@ -63,7 +62,7 @@ func TestLocationIndex(t *testing.T) {
func BenchmarkLocationIndex(b *testing.B) { func BenchmarkLocationIndex(b *testing.B) {
b.SetParallelism(8) b.SetParallelism(8)
vm := vidMap{ vm := vidMap{
cursor: math.MaxInt64 - 10000, cursor: maxCursorIndex - 4000,
} }
b.ResetTimer() b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {