Skip to content

Commit

Permalink
Merge pull request #687 from isucon/fix-validate-chairs
Browse files Browse the repository at this point in the history
total_distanceの反映が遅いデータがありますエラーのバグを修正
  • Loading branch information
wtks authored Dec 6, 2024
2 parents 106165e + 2c52875 commit 51bd04f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 12 additions & 0 deletions bench/benchmarker/world/chair_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,15 @@ func (r *ChairLocation) GetCoordByTime(t time.Time) Coordinate {
}
return r.Initial
}

func (r *ChairLocation) GetLocationEntryByTime(t time.Time) *LocationEntry {
r.mu.RLock()
defer r.mu.RUnlock()

for _, entry := range slices.Backward(r.history) {
if entry.ServerTime.Valid && !entry.ServerTime.Time.After(t) {
return entry
}
}
return nil
}
9 changes: 5 additions & 4 deletions bench/benchmarker/world/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ func (p *Owner) Tick(ctx *Context) error {
return WrapCodeError(ErrorCodeFailedToGetOwnerChairs, err)
}

baseTime := time.Now()
res, err := p.Client.GetOwnerChairs(ctx, &GetOwnerChairsRequest{})
if err != nil {
return WrapCodeError(ErrorCodeFailedToGetOwnerChairs, err)
}
if err := p.ValidateChairs(res); err != nil {
if err := p.ValidateChairs(res, baseTime); err != nil {
return WrapCodeError(ErrorCodeIncorrectOwnerChairsData, err)
}
} else if ctx.CurrentTime()%LengthOfHour == LengthOfHour-1 {
Expand Down Expand Up @@ -201,7 +202,7 @@ func (p *Owner) ValidateSales(until time.Time, serverSide *GetOwnerSalesResponse
return nil
}

func (p *Owner) ValidateChairs(serverSide *GetOwnerChairsResponse) error {
func (p *Owner) ValidateChairs(serverSide *GetOwnerChairsResponse, baseTime time.Time) error {
if p.ChairDB.Len() != len(serverSide.Chairs) {
return fmt.Errorf("オーナーの椅子の数が一致していません")
}
Expand All @@ -222,8 +223,8 @@ func (p *Owner) ValidateChairs(serverSide *GetOwnerChairsResponse) error {
// return fmt.Errorf("activeが一致しないデータがあります (id: %s, got: %v, want: %v)", chair.ServerID, data.Active, !data.Active)
//}
if data.TotalDistanceUpdatedAt.Valid {
lastMovedAt, ok := chair.Location.LastMovedAt()
if ok && data.TotalDistanceUpdatedAt.Time.Sub(lastMovedAt) > 3*time.Second {
lastMoved := chair.Location.GetLocationEntryByTime(baseTime)
if lastMoved != nil && lastMoved.ServerTime.Time.Sub(data.TotalDistanceUpdatedAt.Time) > 3*time.Second {
return fmt.Errorf("total_distanceの反映が遅いデータがあります (id: %s)", chair.ServerID)
}
want := chair.Location.TotalTravelDistanceUntil(data.TotalDistanceUpdatedAt.Time)
Expand Down

0 comments on commit 51bd04f

Please sign in to comment.