From 71f98bb02f2ea72b782816aa8e1516646c8842bd Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:07:19 +0900 Subject: [PATCH 1/7] =?UTF-8?q?idk=E6=8C=87=E5=AE=9A=E6=99=82=E3=81=AE?= =?UTF-8?q?=EF=BC=92=E5=9B=9E=E7=9B=AE=E4=BB=A5=E9=99=8D=E3=81=AE=E3=83=AA?= =?UTF-8?q?=E3=82=AF=E3=82=A8=E3=82=B9=E3=83=88=E3=81=8C=E5=B8=B8=E3=81=AB?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E3=83=AC=E3=82=B9=E3=83=9D=E3=83=B3=E3=82=B9?= =?UTF-8?q?=E3=82=92=E8=BF=94=E3=81=95=E3=82=8C=E3=81=A6=E3=81=84=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench/payment/handler.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index f222140e..e4c489bb 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -61,8 +61,6 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusUnprocessableEntity, map[string]string{"message": "リクエストペイロードがサーバーに記録されているものと異なります"}) return } - writeResponse(w, p.Status) - return } else { p.Token = token p.Amount = req.Amount From 6c2dfc7baa798e4bba229d4d19fdf35b5d555aee Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:10:04 +0900 Subject: [PATCH 2/7] =?UTF-8?q?locked=E3=81=ABfalse=E3=82=92=E5=85=A5?= =?UTF-8?q?=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench/payment/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index e4c489bb..8444d23e 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -83,6 +83,7 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { if rand.IntN(100) > failurePercentage || failureCount >= 4 { // lock はここでしか触らない。lock が true の場合は idempotency key が同じリクエストが処理中の場合のみ if p.locked.CompareAndSwap(false, true) { + defer p.locked.Store(false) alreadyProcessed := false if !newPayment { for _, processed := range s.processedPayments.ToSlice() { @@ -99,7 +100,6 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { s.errChan <- p.Status.Err } s.failureCounts.Delete(token) - p.locked.Store(false) // idenpotency key が同じリクエストが来たときにエラーを返さないように } if rand.IntN(100) > failurePercentage || failureCount >= 4 { writeResponse(w, p.Status) From e37ed358da331d32e42997797f2db7cd6140c95f Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:12:00 +0900 Subject: [PATCH 3/7] fix? --- bench/payment/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index 8444d23e..34041b31 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -99,7 +99,7 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { if p.Status.Err != nil { s.errChan <- p.Status.Err } - s.failureCounts.Delete(token) + s.failureCounts.Set(token, 0) } if rand.IntN(100) > failurePercentage || failureCount >= 4 { writeResponse(w, p.Status) From 7e7f9760fd5b298d1c2c9a8ad81687d1aa20b7f2 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:17:14 +0900 Subject: [PATCH 4/7] fix --- bench/payment/handler.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index 34041b31..c04b9d2a 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -87,7 +87,7 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { alreadyProcessed := false if !newPayment { for _, processed := range s.processedPayments.ToSlice() { - if processed.payment == p { + if processed.payment.IdempotencyKey == p.IdempotencyKey { alreadyProcessed = true break } @@ -99,13 +99,13 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { if p.Status.Err != nil { s.errChan <- p.Status.Err } - s.failureCounts.Set(token, 0) } - if rand.IntN(100) > failurePercentage || failureCount >= 4 { + if rand.IntN(100) > failurePercentage || failureCount >= 4 || alreadyProcessed { writeResponse(w, p.Status) } else { writeRandomError(w) } + s.failureCounts.Set(token, 0) return } } else { From 99528c49bea55ab5cc94f4ba5ba4e6182bfbd318 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:27:09 +0900 Subject: [PATCH 5/7] fix --- bench/payment/handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index c04b9d2a..9fa65edb 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -100,12 +100,12 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { s.errChan <- p.Status.Err } } - if rand.IntN(100) > failurePercentage || failureCount >= 4 || alreadyProcessed { + if rand.IntN(100) > failurePercentage || failureCount >= 4 { + s.failureCounts.Set(token, 0) writeResponse(w, p.Status) } else { writeRandomError(w) } - s.failureCounts.Set(token, 0) return } } else { From 1228eaa7c01e6a6b0fb587808ca0e89e87721b46 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 21:31:57 +0900 Subject: [PATCH 6/7] fix --- bench/payment/handler.go | 11 +++++------ bench/payment/server.go | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/bench/payment/handler.go b/bench/payment/handler.go index 9fa65edb..d1d5f6af 100644 --- a/bench/payment/handler.go +++ b/bench/payment/handler.go @@ -79,8 +79,9 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { if failurePercentage > 50 { failurePercentage = 50 } - failureCount, _ := s.failureCounts.GetOrSetDefault(token, func() int { return 0 }) - if rand.IntN(100) > failurePercentage || failureCount >= 4 { + retryCount, _ := s.retryCounts.GetOrSetDefault(token, func() int { return -1 }) + s.retryCounts.Set(token, retryCount+1) + if rand.IntN(100) > failurePercentage || retryCount >= 4 { // lock はここでしか触らない。lock が true の場合は idempotency key が同じリクエストが処理中の場合のみ if p.locked.CompareAndSwap(false, true) { defer p.locked.Store(false) @@ -100,16 +101,14 @@ func (s *Server) PostPaymentsHandler(w http.ResponseWriter, r *http.Request) { s.errChan <- p.Status.Err } } - if rand.IntN(100) > failurePercentage || failureCount >= 4 { - s.failureCounts.Set(token, 0) + if rand.IntN(100) > failurePercentage || retryCount >= 4 { + s.retryCounts.Set(token, 0) writeResponse(w, p.Status) } else { writeRandomError(w) } return } - } else { - s.failureCounts.Set(token, failureCount+1) } // 不安定なエラーを再現 diff --git a/bench/payment/server.go b/bench/payment/server.go index c6a98e63..7fcada44 100644 --- a/bench/payment/server.go +++ b/bench/payment/server.go @@ -19,7 +19,7 @@ type processedPayment struct { type Server struct { mux *http.ServeMux knownKeys *concurrent.SimpleMap[string, *Payment] - failureCounts *concurrent.SimpleMap[string, int] + retryCounts *concurrent.SimpleMap[string, int] processedPayments *concurrent.SimpleSlice[*processedPayment] processTime time.Duration verifier Verifier @@ -31,7 +31,7 @@ func NewServer(verifier Verifier, processTime time.Duration, errChan chan error) s := &Server{ mux: http.NewServeMux(), knownKeys: concurrent.NewSimpleMap[string, *Payment](), - failureCounts: concurrent.NewSimpleMap[string, int](), + retryCounts: concurrent.NewSimpleMap[string, int](), processedPayments: concurrent.NewSimpleSlice[*processedPayment](), processTime: processTime, verifier: verifier, From 98cde438271265735cd63f9687f53694b96c06e1 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Sat, 7 Dec 2024 16:19:04 +0900 Subject: [PATCH 7/7] =?UTF-8?q?post=20/evaluation=20=E3=81=8C=E3=82=BF?= =?UTF-8?q?=E3=82=A4=E3=83=A0=E3=82=A2=E3=82=A6=E3=83=88=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=82=89=E5=A4=B1=E6=95=97=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench/benchmarker/world/errors.go | 4 ++++ bench/benchmarker/world/user.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/bench/benchmarker/world/errors.go b/bench/benchmarker/world/errors.go index 7335d542..79911086 100644 --- a/bench/benchmarker/world/errors.go +++ b/bench/benchmarker/world/errors.go @@ -25,6 +25,8 @@ const ( _ // ErrorCodeFailedToEvaluate ユーザーが送迎の評価をしようとしたが失敗した ErrorCodeFailedToEvaluate + // ErrorCodeEvaluateTimeout ユーザーが送迎の評価をしようとしたがタイムアウトした + ErrorCodeEvaluateTimeout // ErrorCodeFailedToCheckRequestHistory ユーザーがリクエスト履歴を確認しようとしたが失敗した ErrorCodeFailedToCheckRequestHistory // ErrorCodeFailedToCreateRequest ユーザーがリクエストを作成しようとしたが失敗した @@ -88,6 +90,7 @@ var CriticalErrorCodes = map[ErrorCode]bool{ ErrorCodeIncorrectAmountOfFareCharged: true, ErrorCodeUncontrollableRequestReceived: true, ErrorCodeMatchingTimeout: true, + ErrorCodeEvaluateTimeout: true, } var ErrorTexts = map[ErrorCode]string{ @@ -95,6 +98,7 @@ var ErrorTexts = map[ErrorCode]string{ ErrorCodeFailedToDepart: "椅子が出発できませんでした", ErrorCodeFailedToAcceptRequest: "椅子がライドを受理できませんでした", ErrorCodeFailedToEvaluate: "ユーザーのライド評価に失敗しました", + ErrorCodeEvaluateTimeout: "ユーザーのライド評価がタイムアウトしました", ErrorCodeFailedToCheckRequestHistory: "ユーザーがライド履歴の取得に失敗しました", ErrorCodeFailedToCreateRequest: "ユーザーが新しくライドを作成できませんでした", ErrorCodeUserNotRequestingButStatusChanged: "ユーザーが想定していない通知を受け取りました", diff --git a/bench/benchmarker/world/user.go b/bench/benchmarker/world/user.go index 25c035aa..b590e4df 100644 --- a/bench/benchmarker/world/user.go +++ b/bench/benchmarker/world/user.go @@ -1,6 +1,7 @@ package world import ( + "context" "errors" "fmt" "log/slog" @@ -175,6 +176,9 @@ func (u *User) Tick(ctx *Context) error { res, err := u.Client.SendEvaluation(ctx, u.Request, score) if err != nil { u.Request.Statuses.Unlock() + if errors.Is(err, context.DeadlineExceeded) { + return WrapCodeError(ErrorCodeEvaluateTimeout, err) + } return WrapCodeError(ErrorCodeFailedToEvaluate, err) }