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

Use token-based pagination for Jobs List operations #2810

Merged
merged 1 commit into from
Oct 23, 2023
Merged
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
12 changes: 6 additions & 6 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func TestImportingUsersGroupsSecretScopes(t *testing.T) {
},
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{},
},
{
Expand Down Expand Up @@ -588,7 +588,7 @@ func TestImportingNoResourcesError(t *testing.T) {
},
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{},
},
{
Expand Down Expand Up @@ -632,7 +632,7 @@ func TestImportingClusters(t *testing.T) {
},
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{},
},
{
Expand Down Expand Up @@ -816,7 +816,7 @@ func TestImportingJobs_JobList(t *testing.T) {
emptyRepos,
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{
Jobs: []jobs.Job{
{
Expand Down Expand Up @@ -1035,7 +1035,7 @@ func TestImportingJobs_JobListMultiTask(t *testing.T) {
emptyRepos,
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{
Jobs: []jobs.Job{
{
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func TestImportingSecrets(t *testing.T) {
},
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: jobs.JobListResponse{},
},
{
Expand Down
6 changes: 3 additions & 3 deletions jobs/data_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func commonFixtures(name string) []qa.HTTPFixture {
resource := "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0"
resource := "/api/2.1/jobs/list?expand_tasks=false&limit=25"
if name != "" {
resource = fmt.Sprintf("/api/2.1/jobs/list?expand_tasks=true&limit=25&name=%s&offset=0", name)
resource = fmt.Sprintf("/api/2.1/jobs/list?expand_tasks=true&limit=25&name=%s", name)
}
return []qa.HTTPFixture{
{
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestDataSourceQueryableJobNoMatchName(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=true&limit=25&name=Third&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=true&limit=25&name=Third",
Response: JobListResponse{
Jobs: []Job{},
},
Expand Down
2 changes: 1 addition & 1 deletion jobs/data_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestJobsData(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: JobListResponse{
Jobs: []Job{
{
Expand Down
14 changes: 9 additions & 5 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ func (js *JobSettings) sortWebhooksByID() {

// JobListResponse returns a list of all jobs
type JobListResponse struct {
Jobs []Job `json:"jobs"`
HasMore bool `json:"has_more,omitempty"`
Jobs []Job `json:"jobs"`
HasMore bool `json:"has_more,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
PrevPageToken string `json:"prev_page_token,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to confirm, you're adding PrevPageToken for completeness, even though we don't use it in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, only for completeness...

}

// Job contains the information when using a GET request from the Databricks Jobs api
Expand Down Expand Up @@ -416,12 +418,14 @@ func (a JobsAPI) ListByName(name string, expandTasks bool) ([]Job, error) {
if name != "" {
params["name"] = name
}
offset := 0

nextPageToken := ""
ctx := context.WithValue(a.context, common.Api, common.API_2_1)
for {
var resp JobListResponse
params["offset"] = offset
if nextPageToken != "" {
params["page_token"] = nextPageToken
}
err := a.client.Get(ctx, "/jobs/list", params, &resp)
if err != nil {
return nil, err
Expand All @@ -430,7 +434,7 @@ func (a JobsAPI) ListByName(name string, expandTasks bool) ([]Job, error) {
if !resp.HasMore {
break
}
offset += len(resp.Jobs)
nextPageToken = resp.NextPageToken
alexott marked this conversation as resolved.
Show resolved Hide resolved
}
return jobs, nil
}
Expand Down
12 changes: 6 additions & 6 deletions jobs/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ func TestJobsAPIList(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: JobListResponse{
Jobs: []Job{
{
Expand All @@ -2205,26 +2205,26 @@ func TestJobsAPIListMultiplePages(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Response: JobListResponse{
Jobs: []Job{
{
JobID: 1,
},
},
HasMore: true,
HasMore: true,
NextPageToken: "aaaa",
},
},
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=1",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&page_token=aaaa",
Response: JobListResponse{
Jobs: []Job{
{
JobID: 2,
},
},
HasMore: false,
alexott marked this conversation as resolved.
Show resolved Hide resolved
},
},
}, func(ctx context.Context, client *common.DatabricksClient) {
Expand All @@ -2239,7 +2239,7 @@ func TestJobsAPIListByName(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&name=test&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&name=test",
Response: JobListResponse{
Jobs: []Job{
{
Expand Down
Loading