Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to handle REJECTED_TIMEDOUT #224

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/app/coroutines/completePromise.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ func CompletePromise(metadata *metadata.Metadata, req *t_api.Request, res func(*
} else {
status := t_api.ForbiddenStatus(p.State)
strict := req.CompletePromise.Strict && p.State != req.CompletePromise.State
timeout := !req.CompletePromise.Strict && p.State == promise.Timedout

if !strict && p.IdempotencyKeyForComplete.Match(req.CompletePromise.IdempotencyKey) {
if (!strict && p.IdempotencyKeyForComplete.Match(req.CompletePromise.IdempotencyKey)) || timeout {
status = t_api.StatusOK
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/subsystems/aio/store/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const (
UPDATE
promises
SET
state = 8, completed_on = timeout
state = 16, completed_on = timeout
WHERE
state = 1 AND timeout <= $1`

Expand Down
2 changes: 1 addition & 1 deletion internal/app/subsystems/aio/store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const (
UPDATE
promises
SET
state = 8, completed_on = timeout
state = 16, completed_on = timeout
WHERE
state = 1 AND timeout <= ?`

Expand Down
18 changes: 9 additions & 9 deletions internal/app/subsystems/aio/store/test/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ var TestCases = []*testCase{
Kind: t_aio.UpdatePromise,
UpdatePromise: &t_aio.UpdatePromiseCommand{
Id: "qux",
State: 8,
State: promise.Timedout,
Value: promise.Value{
Headers: map[string]string{},
Data: []byte{},
Expand All @@ -1799,7 +1799,7 @@ var TestCases = []*testCase{
Kind: t_aio.UpdatePromise,
UpdatePromise: &t_aio.UpdatePromiseCommand{
Id: "quy",
State: 16,
State: promise.Canceled,
Value: promise.Value{
Headers: map[string]string{},
Data: []byte{},
Expand Down Expand Up @@ -1978,7 +1978,7 @@ var TestCases = []*testCase{
Records: []*promise.PromiseRecord{
{
Id: "quy",
State: 16,
State: 8,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
ValueHeaders: []byte("{}"),
Expand All @@ -1991,7 +1991,7 @@ var TestCases = []*testCase{
},
{
Id: "qux",
State: 8,
State: 16,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
ValueHeaders: []byte("{}"),
Expand Down Expand Up @@ -2026,7 +2026,7 @@ var TestCases = []*testCase{
Records: []*promise.PromiseRecord{
{
Id: "quy",
State: 16,
State: 8,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
ValueHeaders: []byte("{}"),
Expand All @@ -2039,7 +2039,7 @@ var TestCases = []*testCase{
},
{
Id: "qux",
State: 8,
State: 16,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
ValueHeaders: []byte("{}"),
Expand Down Expand Up @@ -3449,7 +3449,7 @@ var TestCases = []*testCase{
Records: []*promise.PromiseRecord{
{
Id: "baz",
State: 8,
State: 16,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
Expand All @@ -3460,7 +3460,7 @@ var TestCases = []*testCase{
},
{
Id: "bar",
State: 8,
State: 16,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
Expand All @@ -3471,7 +3471,7 @@ var TestCases = []*testCase{
},
{
Id: "foo",
State: 8,
State: 16,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
Expand Down
10 changes: 5 additions & 5 deletions pkg/promise/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
Pending State = 1 << iota
Resolved
Rejected
Timedout
Canceled
Timedout
)

func (s State) String() string {
Expand All @@ -54,10 +54,10 @@
return "RESOLVED"
case Rejected:
return "REJECTED"
case Timedout:
return "REJECTED_TIMEDOUT"
case Canceled:
return "REJECTED_CANCELED"
case Timedout:
return "REJECTED_TIMEDOUT"
default:
panic("invalid state")
}
Expand All @@ -80,10 +80,10 @@
*s = Resolved
case "REJECTED":
*s = Rejected
case "REJECTED_TIMEDOUT":
*s = Timedout
case "REJECTED_CANCELED":
*s = Canceled
case "REJECTED_TIMEDOUT":
*s = Timedout

Check warning on line 86 in pkg/promise/promise.go

View check run for this annotation

Codecov / codecov/patch

pkg/promise/promise.go#L85-L86

Added lines #L85 - L86 were not covered by tests
default:
return fmt.Errorf("invalid state '%s'", state)
}
Expand Down
2 changes: 1 addition & 1 deletion test/dst/dst.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *DST) Run(r *rand.Rand, api api.API, aio aio.AIO, system *system.System,
generator.AddRequest(generator.GenerateCreatePromise)
model.AddResponse(t_api.CreatePromise, model.ValidatCreatePromise)
case t_api.CompletePromise:
generator.AddRequest(generator.GenerateCancelPromise)
generator.AddRequest(generator.GenerateCompletePromise)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

model.AddResponse(t_api.CompletePromise, model.ValidateCompletePromise)

// SCHEDULES
Expand Down
50 changes: 4 additions & 46 deletions test/dst/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dst

import (
"fmt"
"math"
"math/rand" // nosemgrep
"strconv"

Expand Down Expand Up @@ -193,62 +194,19 @@ func (g *Generator) GenerateCreatePromise(r *rand.Rand, t int64) *t_api.Request
}
}

func (g *Generator) GenerateCancelPromise(r *rand.Rand, t int64) *t_api.Request {
func (g *Generator) GenerateCompletePromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0
state := promise.State(math.Exp2(float64(r.Intn(3) + 1)))

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Canceled,
Value: promise.Value{
Headers: headers,
Data: data,
},
IdempotencyKey: idempotencyKey,
Strict: strict,
},
}
}

func (g *Generator) GenerateResolvePromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Resolved,
Value: promise.Value{
Headers: headers,
Data: data,
},
IdempotencyKey: idempotencyKey,
Strict: strict,
},
}
}

func (g *Generator) GenerateRejectPromise(r *rand.Rand, t int64) *t_api.Request {
id := g.idSet[r.Intn(len(g.idSet))]
idempotencyKey := g.idemotencyKeySet[r.Intn(len(g.idemotencyKeySet))]
data := g.dataSet[r.Intn(len(g.dataSet))]
headers := g.headersSet[r.Intn(len(g.headersSet))]
strict := r.Intn(2) == 0

return &t_api.Request{
Kind: t_api.CompletePromise,
CompletePromise: &t_api.CompletePromiseRequest{
Id: id,
State: promise.Rejected,
State: state,
Value: promise.Value{
Headers: headers,
Data: data,
Expand Down
13 changes: 10 additions & 3 deletions test/dst/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@
switch res.CompletePromise.Status {
case t_api.StatusOK:
if pm.completed() {
if !pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise) {
if !(!req.CompletePromise.Strict && pm.promise.State == promise.Timedout) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding this if statement a little hard to follow, so I plugged it into a program that simplifies boolean expressions. To me the result is a little easier to understand, let me know what you think.

!(!req.CompletePromise.Strict && pm.promise.State == promise.Timedout)
  && !pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise)

a = req.CompletePromise.Strict
b = pm.promise.State == promise.Timedout
c = pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise)

!(!a && b) && !c
!c && !(!a && b)  put c first (I find this a little easier to read)

~c ∧ ~(~a ∧ b)    convert to boolean algebra
~c ∧ (a ∨ ~b)     an equivalent expression

!c && (a || !b)   convert back to go code, and finally put back in substitutions

!pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise)
  && (req.CompletePromise.Strict || pm.promise.State != promise.Timedout)

Here is the equivalency proof, and here are the corresponding truth tables for both expressions:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Yes, that looks much better.

Thanks for the detailed explanation.

!pm.idempotencyKeyForCompleteMatch(res.CompletePromise.Promise) {
return fmt.Errorf("ikey mismatch (%s, %s)", pm.promise.IdempotencyKeyForComplete, res.CompletePromise.Promise.IdempotencyKeyForComplete)
} else if req.CompletePromise.Strict && pm.promise.State != promise.Canceled {
} else if req.CompletePromise.Strict && pm.promise.State != req.CompletePromise.State {
return fmt.Errorf("unexpected state %s when strict true", pm.promise.State)
}
}
Expand All @@ -268,7 +269,13 @@
pm.promise = res.CompletePromise.Promise
return nil
case t_api.StatusCreated:
if res.CompletePromise.Promise.State != promise.Canceled {
if req.CompletePromise.State.In(promise.Resolved) && res.CompletePromise.Promise.State != promise.Resolved {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Resolved) && res.CompletePromise.Promise.State != promise.Resolved {
if req.CompletePromise.State == promise.Resolved && res.CompletePromise.Promise.State != promise.Resolved {

return fmt.Errorf("unexpected state %s after resolve promise", res.CompletePromise.Promise.State)
}

Check warning on line 274 in test/dst/model.go

View check run for this annotation

Codecov / codecov/patch

test/dst/model.go#L273-L274

Added lines #L273 - L274 were not covered by tests
if req.CompletePromise.State.In(promise.Rejected) && res.CompletePromise.Promise.State != promise.Rejected {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Rejected) && res.CompletePromise.Promise.State != promise.Rejected {
if req.CompletePromise.State == promise.Rejected && res.CompletePromise.Promise.State != promise.Rejected {

return fmt.Errorf("unexpected state %s after reject promise", res.CompletePromise.Promise.State)
}

Check warning on line 277 in test/dst/model.go

View check run for this annotation

Codecov / codecov/patch

test/dst/model.go#L276-L277

Added lines #L276 - L277 were not covered by tests
if req.CompletePromise.State.In(promise.Canceled) && res.CompletePromise.Promise.State != promise.Canceled {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if req.CompletePromise.State.In(promise.Canceled) && res.CompletePromise.Promise.State != promise.Canceled {
if req.CompletePromise.State == promise.Canceled && res.CompletePromise.Promise.State != promise.Canceled {

return fmt.Errorf("unexpected state %s after cancel promise", res.CompletePromise.Promise.State)
}
if pm.completed() {
Expand Down
Loading