From 18567439c4c7bb99282be2fe6daf00a53b2110d7 Mon Sep 17 00:00:00 2001 From: yokowu <18836617@qq.com> Date: Tue, 21 Apr 2026 20:54:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(host):=20PickHost=20=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=8A=A0=E6=9D=83=E9=9A=8F=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/biz/host/usecase/publichost.go | 50 +++++-- backend/biz/host/usecase/publichost_test.go | 144 ++++++++++++++++++++ 2 files changed, 182 insertions(+), 12 deletions(-) create mode 100644 backend/biz/host/usecase/publichost_test.go diff --git a/backend/biz/host/usecase/publichost.go b/backend/biz/host/usecase/publichost.go index 07f7954d..2f8a967e 100644 --- a/backend/biz/host/usecase/publichost.go +++ b/backend/biz/host/usecase/publichost.go @@ -2,8 +2,9 @@ package usecase import ( "context" + "crypto/rand" + "encoding/binary" "fmt" - "sync/atomic" "github.com/samber/do" @@ -17,9 +18,10 @@ import ( type PublicHostUsecase struct { repo domain.PublicHostRepo taskflow taskflow.Clienter - rr uint64 } +var randUint64n = randomUint64n + func NewPublicHostUsecase(i *do.Injector) (domain.PublicHostUsecase, error) { return &PublicHostUsecase{ repo: do.MustInvoke[domain.PublicHostRepo](i), @@ -52,9 +54,18 @@ func (p *PublicHostUsecase) PickHost(ctx context.Context) (*domain.Host, error) return nil, errcode.ErrPublicHostNotFound.Wrap(fmt.Errorf("no online public hosts found")) } - weights := make([]uint64, len(onlines)) + selected, err := pickWeightedHost(onlines) + if err != nil { + return nil, err + } + + return cvt.From(selected, &domain.Host{}), nil +} + +func pickWeightedHost(hosts []*db.Host) (*db.Host, error) { + weights := make([]uint64, len(hosts)) var totalWeight uint64 - for i, h := range onlines { + for i, h := range hosts { w := h.Weight if w <= 0 { w = 1 @@ -66,19 +77,34 @@ func (p *PublicHostUsecase) PickHost(ctx context.Context) (*domain.Host, error) return nil, errcode.ErrPublicHostNotFound.Wrap(fmt.Errorf("no valid weights found")) } - idx := atomic.AddUint64(&p.rr, 1) - 1 - offset := idx % totalWeight - var selected *db.Host + offset, err := randUint64n(totalWeight) + if err != nil { + return nil, err + } for i, w := range weights { if offset < w { - selected = onlines[i] - break + return hosts[i], nil } offset -= w } - if selected == nil { - return nil, errcode.ErrPublicHostNotFound.Wrap(fmt.Errorf("failed to select public host")) + + return nil, errcode.ErrPublicHostNotFound.Wrap(fmt.Errorf("failed to select public host")) +} + +func randomUint64n(n uint64) (uint64, error) { + if n == 0 { + return 0, fmt.Errorf("random upper bound must be positive") } - return cvt.From(selected, &domain.Host{}), nil + limit := ^uint64(0) - (^uint64(0) % n) + var buf [8]byte + for { + if _, err := rand.Read(buf[:]); err != nil { + return 0, err + } + v := binary.BigEndian.Uint64(buf[:]) + if v < limit { + return v % n, nil + } + } } diff --git a/backend/biz/host/usecase/publichost_test.go b/backend/biz/host/usecase/publichost_test.go new file mode 100644 index 00000000..21bb07be --- /dev/null +++ b/backend/biz/host/usecase/publichost_test.go @@ -0,0 +1,144 @@ +package usecase + +import ( + "context" + "testing" + + "github.com/chaitin/MonkeyCode/backend/db" + "github.com/chaitin/MonkeyCode/backend/pkg/taskflow" +) + +func TestPickHostSelectsHostByRandomOffset(t *testing.T) { + repo := &publicHostRepoStub{ + hosts: []*db.Host{ + {ID: "host-a", Hostname: "a", Weight: 1}, + {ID: "host-b", Hostname: "b", Weight: 3}, + {ID: "host-c", Hostname: "c", Weight: 1}, + }, + } + hoster := &publicHosterStub{ + onlineMap: map[string]bool{ + "host-a": true, + "host-b": true, + "host-c": true, + }, + } + u := &PublicHostUsecase{ + repo: repo, + taskflow: &taskflowClientStub{hoster: hoster}, + } + + offsets := []uint64{0, 1, 3, 4} + limits := make([]uint64, 0, len(offsets)) + prevRandUint64n := randUint64n + randUint64n = func(n uint64) (uint64, error) { + limits = append(limits, n) + v := offsets[0] + offsets = offsets[1:] + return v, nil + } + t.Cleanup(func() { + randUint64n = prevRandUint64n + }) + + got := make([]string, 0, 4) + for range 4 { + host, err := u.PickHost(context.Background()) + if err != nil { + t.Fatalf("PickHost() error = %v", err) + } + got = append(got, host.ID) + } + + want := []string{"host-a", "host-b", "host-b", "host-c"} + for i := range want { + if got[i] != want[i] { + t.Fatalf("PickHost() at %d = %q, want %q", i, got[i], want[i]) + } + } + + for _, limit := range limits { + if limit != 5 { + t.Fatalf("rand limit = %d, want 5", limit) + } + } +} + +func TestPickHostTreatsNonPositiveWeightsAsOne(t *testing.T) { + u := &PublicHostUsecase{ + repo: &publicHostRepoStub{ + hosts: []*db.Host{ + {ID: "host-a", Hostname: "a", Weight: 0}, + {ID: "host-b", Hostname: "b", Weight: -2}, + }, + }, + taskflow: &taskflowClientStub{ + hoster: &publicHosterStub{ + onlineMap: map[string]bool{ + "host-a": true, + "host-b": true, + }, + }, + }, + } + + prevRandUint64n := randUint64n + randUint64n = func(n uint64) (uint64, error) { + if n != 2 { + t.Fatalf("rand limit = %d, want 2", n) + } + return 1, nil + } + t.Cleanup(func() { + randUint64n = prevRandUint64n + }) + + host, err := u.PickHost(context.Background()) + if err != nil { + t.Fatalf("PickHost() error = %v", err) + } + if host.ID != "host-b" { + t.Fatalf("PickHost() = %q, want %q", host.ID, "host-b") + } +} + +type publicHostRepoStub struct { + hosts []*db.Host + err error +} + +func (s *publicHostRepoStub) All(context.Context) ([]*db.Host, error) { + return s.hosts, s.err +} + +type publicHosterStub struct { + onlineMap map[string]bool + err error +} + +func (s *publicHosterStub) List(context.Context, string) (map[string]*taskflow.Host, error) { + return nil, nil +} + +func (s *publicHosterStub) IsOnline(context.Context, *taskflow.IsOnlineReq[string]) (*taskflow.IsOnlineResp, error) { + if s.err != nil { + return nil, s.err + } + return &taskflow.IsOnlineResp{OnlineMap: s.onlineMap}, nil +} + +type taskflowClientStub struct { + hoster taskflow.Hoster +} + +func (s *taskflowClientStub) VirtualMachiner() taskflow.VirtualMachiner { return nil } +func (s *taskflowClientStub) Host() taskflow.Hoster { return s.hoster } +func (s *taskflowClientStub) FileManager() taskflow.FileManager { return nil } +func (s *taskflowClientStub) TaskManager() taskflow.TaskManager { return nil } +func (s *taskflowClientStub) PortForwarder() taskflow.PortForwarder { return nil } +func (s *taskflowClientStub) Stats(context.Context) (*taskflow.Stats, error) { + return nil, nil +} +func (s *taskflowClientStub) TaskLive(context.Context, string, bool, func(*taskflow.TaskChunk) error) error { + return nil +}