Skip to content

Backport of paginator: fix tokenizer comparison of composite index and ID into release/1.10.x #25793

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

Open
wants to merge 1 commit into
base: release/1.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions .changelog/25792.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fixed pagination bug which could result in duplicate results
```
28 changes: 27 additions & 1 deletion nomad/state/paginator/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"cmp"
"fmt"
"strconv"
"strings"
)

// Tokenizer is the interface that must be implemented to provide pagination
Expand Down Expand Up @@ -45,7 +46,32 @@ func CreateIndexAndIDTokenizer[T idAndCreateIndexGetter](target string) Tokenize
index := item.GetCreateIndex()
id := item.GetID()
token := fmt.Sprintf("%d.%s", index, id)
return token, cmp.Compare(token, target)

// Split the target to extract the create index and the ID values.
targetParts := strings.SplitN(target, ".", 2)
// If the target wasn't composed of both parts, directly compare.
if len(targetParts) < 2 {
return token, cmp.Compare(token, target)
}

// Convert the create index to an integer for comparison. This
// prevents a lexigraphical comparison of the create index which
// can cause unexpected results when comparing index values like
// '12' and '102'. If the index cannot be converted to an integer,
// fall back to direct comparison.
targetIndex, err := strconv.Atoi(targetParts[0])
if err != nil {
return token, cmp.Compare(token, target)
}

indexCmp := cmp.Compare(index, uint64(targetIndex))
if indexCmp != 0 {
return token, indexCmp
}

// If the index values are equivalent use the ID values
// as the comparison.
return token, cmp.Compare(id, targetParts[1])
}
}

Expand Down
91 changes: 91 additions & 0 deletions nomad/state/paginator/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,94 @@ func TestTokenizer(t *testing.T) {
})
}
}

func TestCreateIndexAndIDTokenizer(t *testing.T) {
ci.Parallel(t)

cases := []struct {
name string
obj *mockCreateIndexObject
target string
expectedToken string
expectedCmp int
}{
{
name: "common index (less)",
obj: newMockCreateIndexObject(12, "aaa-bbb-ccc"),
target: "12.bbb-ccc-ddd",
expectedToken: "12.aaa-bbb-ccc",
expectedCmp: -1,
},
{
name: "common index (greater)",
obj: newMockCreateIndexObject(12, "bbb-ccc-ddd"),
target: "12.aaa-bbb-ccc",
expectedToken: "12.bbb-ccc-ddd",
expectedCmp: 1,
},
{
name: "common index (equal)",
obj: newMockCreateIndexObject(12, "bbb-ccc-ddd"),
target: "12.bbb-ccc-ddd",
expectedToken: "12.bbb-ccc-ddd",
expectedCmp: 0,
},
{
name: "less index",
obj: newMockCreateIndexObject(12, "aaa-bbb-ccc"),
target: "89.aaa-bbb-ccc",
expectedToken: "12.aaa-bbb-ccc",
expectedCmp: -1,
},
{
name: "greater index",
obj: newMockCreateIndexObject(89, "aaa-bbb-ccc"),
target: "12.aaa-bbb-ccc",
expectedToken: "89.aaa-bbb-ccc",
expectedCmp: 1,
},
{
name: "common index start (less)",
obj: newMockCreateIndexObject(12, "aaa-bbb-ccc"),
target: "102.aaa-bbb-ccc",
expectedToken: "12.aaa-bbb-ccc",
expectedCmp: -1,
},
{
name: "common index start (greater)",
obj: newMockCreateIndexObject(102, "aaa-bbb-ccc"),
target: "12.aaa-bbb-ccc",
expectedToken: "102.aaa-bbb-ccc",
expectedCmp: 1,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
fn := CreateIndexAndIDTokenizer[*mockCreateIndexObject](tc.target)
actualToken, actualCmp := fn(tc.obj)
must.Eq(t, tc.expectedToken, actualToken)
must.Eq(t, tc.expectedCmp, actualCmp)
})
}
}

func newMockCreateIndexObject(createIndex uint64, id string) *mockCreateIndexObject {
return &mockCreateIndexObject{
createIndex: createIndex,
id: id,
}
}

type mockCreateIndexObject struct {
createIndex uint64
id string
}

func (m *mockCreateIndexObject) GetCreateIndex() uint64 {
return m.createIndex
}

func (m *mockCreateIndexObject) GetID() string {
return m.id
}