Skip to content

Commit

Permalink
Merge pull request #610 from isucon/add-validation
Browse files Browse the repository at this point in the history
appGetNearbyChairにライドの完了状態チェックを追加
  • Loading branch information
wtks authored Dec 4, 2024
2 parents e9cccee + f757f32 commit ed81bb8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions bench/benchmarker/scenario/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (s *Scenario) Score(final bool) int64 {
score := lo.SumBy(s.world.OwnerDB.ToSlice(), func(p *world.Owner) int64 { return p.SubScore.Load() }) / 100
if final {
score += lo.SumBy(s.world.RequestDB.ToSlice(), func(r *world.Request) int64 {
if r.Evaluated {
if r.Evaluated.Load() {
return 0
}
return int64(r.PartialScore())
Expand All @@ -295,7 +295,7 @@ func (s *Scenario) Score(final bool) int64 {

func (s *Scenario) TotalDiscount() int64 {
return lo.SumBy(s.world.RequestDB.ToSlice(), func(r *world.Request) int64 {
if r.Evaluated {
if r.Evaluated.Load() {
return int64(r.ActualDiscount())
} else {
return 0
Expand Down
8 changes: 4 additions & 4 deletions bench/benchmarker/world/chair.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log/slog"
"math/rand/v2"
"slices"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -45,7 +44,7 @@ type Chair struct {
// Request 進行中のリクエスト
Request *Request
// RequestHistory 引き受けたリクエストの履歴
RequestHistory []*Request
RequestHistory *concurrent.SimpleSlice[*Request]
// Client webappへのクライアント
Client ChairClient
// Rand 専用の乱数
Expand Down Expand Up @@ -132,10 +131,11 @@ func (c *Chair) Tick(ctx *Context) error {
c.Request.Statuses.Chair = RequestStatusDispatching
c.Request.StartPoint = null.ValueFrom(c.Location.Current())
c.Request.MatchedAt = ctx.CurrentTime()
c.Request.BenchMatchedAt = time.Now()

c.Request.Statuses.Unlock()

c.RequestHistory = append(c.RequestHistory, c.Request)
c.RequestHistory.Append(c.Request)
if !c.Request.User.Region.Contains(c.Location.Current()) {
ctx.ContestantLogger().Warn("ユーザーのリージョン外部に存在する椅子がマッチングされました", slog.Int("distance", c.Request.PickupPoint.DistanceTo(c.Location.Current())))
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func (c *Chair) HandleNotification(event NotificationEvent) error {
request := c.Request
if request == nil {
// 履歴を見て、過去扱っていたRequestに向けてのCOMPLETED通知であれば無視する
for _, r := range slices.Backward(c.RequestHistory) {
for _, r := range c.RequestHistory.BackwardIter() {
if r.ServerID == data.ServerRequestID && r.Statuses.Desired == RequestStatusCompleted {
r.Statuses.Chair = RequestStatusCompleted
return nil
Expand Down
5 changes: 4 additions & 1 deletion bench/benchmarker/world/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/guregu/null/v5"
Expand Down Expand Up @@ -81,9 +82,11 @@ type Request struct {
CompletedAt int64
// ServerCompletedAt サーバー側で記録されている完了時間
ServerCompletedAt time.Time
// BenchMatchedAt ベンチがAcceptのリクエストを送って成功した時間
BenchMatchedAt time.Time

// Evaluated リクエストの評価が完了しているかどうか
Evaluated bool
Evaluated atomic.Bool

Statuses RequestStatuses
}
Expand Down
4 changes: 2 additions & 2 deletions bench/benchmarker/world/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (u *User) Tick(ctx *Context) error {

case RequestStatusArrived:
// 送迎の評価及び支払いがまだの場合は行う
if !u.Request.Evaluated {
if !u.Request.Evaluated.Load() {
score := u.Request.CalculateEvaluation().Score()

u.Request.Statuses.Lock()
Expand All @@ -166,7 +166,7 @@ func (u *User) Tick(ctx *Context) error {
u.Request.CompletedAt = ctx.CurrentTime()
u.Request.ServerCompletedAt = res.CompletedAt
u.Request.Statuses.Desired = RequestStatusCompleted
u.Request.Evaluated = true
u.Request.Evaluated.Store(true)
if requests := len(u.RequestHistory); requests == 1 {
u.Region.TotalEvaluation.Add(int32(score))
} else {
Expand Down
12 changes: 12 additions & 0 deletions bench/benchmarker/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func (w *World) CreateChair(ctx *Context, args *CreateChairArgs) (*Chair, error)
RegisteredData: registeredData,
Client: res.Client,
Rand: random.CreateChildRand(args.Owner.Rand),
RequestHistory: concurrent.NewSimpleSlice[*Request](),
notificationQueue: make(chan NotificationEvent, 500),
}
result := w.ChairDB.Create(c)
Expand Down Expand Up @@ -312,6 +313,17 @@ func (w *World) checkNearbyChairsResponse(baseTime time.Time, current Coordinate
if len(entries) == 0 {
return fmt.Errorf("ID:%sの椅子はレスポンスの座標に過去存在したことがありません", chair.ID)
}
for _, req := range c.RequestHistory.BackwardIter() {
if req.BenchMatchedAt.After(baseTime) {
// nearbychairsのリクエストを送った後にマッチされていて、レスポンスを生成とマッチのどちらが先か分からないので許容する
break
}
if !req.Evaluated.Load() {
return fmt.Errorf("ID:%sの椅子は既にライド中です", chair.ID)
}
break
}

if !lo.SomeBy(entries, func(entry GetPeriodsByCoordResultEntry) bool {
if !entry.Until.Valid {
// untilが無い場合は今もその位置にいることになるので、最新
Expand Down
12 changes: 12 additions & 0 deletions bench/internal/concurrent/simple_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func (s *SimpleSlice[V]) Iter() iter.Seq2[int, V] {
}
}

func (s *SimpleSlice[V]) BackwardIter() iter.Seq2[int, V] {
return func(yield func(int, V) bool) {
s.lock.RLock()
defer s.lock.RUnlock()
for i, v := range slices.Backward(s.s) {
if !yield(i, v) {
break
}
}
}
}

func (s *SimpleSlice[V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
s.lock.RLock()
Expand Down

0 comments on commit ed81bb8

Please sign in to comment.