From bb757c7375368035e5d5614224854b713538ad91 Mon Sep 17 00:00:00 2001 From: Jan-Eric Hellenberg Date: Wed, 5 May 2021 12:29:10 +0200 Subject: [PATCH] Only load available runners --- environment/execution_environment.go | 2 +- environment/execution_environment_test.go | 4 ++-- mocks/ExecutorApi.go | 2 +- nomad/nomad.go | 20 +++++++++++++------- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/environment/execution_environment.go b/environment/execution_environment.go index 937047a..68c4081 100644 --- a/environment/execution_environment.go +++ b/environment/execution_environment.go @@ -66,7 +66,7 @@ func (environment *NomadExecutionEnvironment) NextRunner() (r runner.Runner, err // Refresh Big ToDo: Improve this function!! State out that it also rescales the job; Provide context to be terminable... func (environment *NomadExecutionEnvironment) Refresh() { for { - runners, err := environment.nomadApiClient.LoadRunners(environment.jobId) + runners, err := environment.nomadApiClient.LoadAvailableRunners(environment.jobId) if err != nil { log.WithError(err).Printf("Failed fetching runners") break diff --git a/environment/execution_environment_test.go b/environment/execution_environment_test.go index 43ba142..313beed 100644 --- a/environment/execution_environment_test.go +++ b/environment/execution_environment_test.go @@ -66,7 +66,7 @@ func TestRefreshFetchRunners(t *testing.T) { // ToDo: Terminate Refresh when test finished (also in other tests) go environment.Refresh() _, _ = environment.NextRunner() - apiMock.AssertCalled(t, "LoadRunners", jobId) + apiMock.AssertCalled(t, "LoadAvailableRunners", jobId) } func TestRefreshFetchesRunnersIntoChannel(t *testing.T) { @@ -96,7 +96,7 @@ func TestRefreshAddsRunnerToPool(t *testing.T) { func newRefreshMock(returnedRunnerIds []string, allRunners RunnerPool) (apiClient *mocks.ExecutorApi, environment *NomadExecutionEnvironment) { apiClient = &mocks.ExecutorApi{} - apiClient.On("LoadRunners", jobId).Return(returnedRunnerIds, nil) + apiClient.On("LoadAvailableRunners", jobId).Return(returnedRunnerIds, nil) apiClient.On("GetJobScale", jobId).Return(len(returnedRunnerIds), nil) apiClient.On("SetJobScaling", jobId, mock.AnythingOfType("int"), "Runner Requested").Return(nil) environment = &NomadExecutionEnvironment{ diff --git a/mocks/ExecutorApi.go b/mocks/ExecutorApi.go index f30dceb..5bc6dcd 100644 --- a/mocks/ExecutorApi.go +++ b/mocks/ExecutorApi.go @@ -71,7 +71,7 @@ func (_m *ExecutorApi) LoadJobList() ([]*api.JobListStub, error) { } // LoadRunners provides a mock function with given fields: jobId -func (_m *ExecutorApi) LoadRunners(jobId string) ([]string, error) { +func (_m *ExecutorApi) LoadAvailableRunners(jobId string) ([]string, error) { ret := _m.Called(jobId) var r0 []string diff --git a/nomad/nomad.go b/nomad/nomad.go index 27d6aec..71ad98c 100644 --- a/nomad/nomad.go +++ b/nomad/nomad.go @@ -7,11 +7,18 @@ import ( // ExecutorApi provides access to an container orchestration solution type ExecutorApi interface { + // LoadJobList loads the list of jobs from the Nomad api. LoadJobList() (list []*nomadApi.JobListStub, err error) + // GetJobScale returns the scale of the passed job. GetJobScale(jobId string) (jobScale int, err error) + // SetJobScaling sets the scaling count of the passed job to Nomad. SetJobScaling(jobId string, count int, reason string) (err error) - LoadRunners(jobId string) (runnerIds []string, err error) + + // DeleteRunner deletes the runner with the given Id. DeleteRunner(runnerId string) (err error) + + // LoadAvailableRunners loads all allocations of the specified job which are running and not about to get stopped. + LoadAvailableRunners(jobId string) (runnerIds []string, err error) } // ApiClient provides access to the Nomad functionality @@ -36,13 +43,11 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) { return err } -// LoadJobList loads the list of jobs from the Nomad api. func (apiClient *ApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error) { list, _, err = apiClient.client.Jobs().List(nil) return } -// GetJobScale returns the scale of the passed job. func (apiClient *ApiClient) GetJobScale(jobId string) (jobScale int, err error) { status, _, err := apiClient.client.Jobs().ScaleStatus(jobId, nil) if err != nil { @@ -53,20 +58,21 @@ func (apiClient *ApiClient) GetJobScale(jobId string) (jobScale int, err error) return } -// SetJobScaling sets the scaling count of the passed job to Nomad. func (apiClient *ApiClient) SetJobScaling(jobId string, count int, reason string) (err error) { _, _, err = apiClient.client.Jobs().Scale(jobId, jobId, &count, reason, false, nil, nil) return } -// LoadRunners loads the allocations of the specified job. -func (apiClient *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) { +func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) { list, _, err := apiClient.client.Jobs().Allocations(jobId, true, nil) if err != nil { return nil, err } for _, stub := range list { - runnerIds = append(runnerIds, stub.ID) + // only add allocations which are running and not about to be stopped + if stub.ClientStatus == nomadApi.AllocClientStatusRunning && stub.DesiredStatus == nomadApi.AllocDesiredStatusRun { + runnerIds = append(runnerIds, stub.ID) + } } return }