Use more uints

Previously we accepted int values although only uint values made sense.
We adjusted this to accept uints where appropriate.
This commit is contained in:
sirkrypt0
2021-06-01 09:49:45 +02:00
committed by Tobias Kantusch
parent 1c4daa99a9
commit 630a006258
9 changed files with 32 additions and 31 deletions

View File

@ -57,7 +57,7 @@ func (m *NomadEnvironmentManager) CreateOrUpdate(
if err == nil { if err == nil {
if !exists { if !exists {
m.runnerManager.RegisterEnvironment( m.runnerManager.RegisterEnvironment(
runner.EnvironmentId(idInt), runner.NomadJobId(id), int(request.PrewarmingPoolSize)) runner.EnvironmentId(idInt), runner.NomadJobId(id), request.PrewarmingPoolSize)
} }
return !exists, nil return !exists, nil
} }

View File

@ -53,7 +53,7 @@ func (s *CreateOrUpdateTestSuite) mockEnvironmentExists(exists bool) {
func (s *CreateOrUpdateTestSuite) mockRegisterEnvironment() *mock.Call { func (s *CreateOrUpdateTestSuite) mockRegisterEnvironment() *mock.Call {
return s.runnerManagerMock.On("RegisterEnvironment", return s.runnerManagerMock.On("RegisterEnvironment",
mock.AnythingOfType("EnvironmentId"), mock.AnythingOfType("NomadJobId"), mock.AnythingOfType("int")). mock.AnythingOfType("EnvironmentId"), mock.AnythingOfType("NomadJobId"), mock.AnythingOfType("uint")).
Return() Return()
} }

View File

@ -16,10 +16,10 @@ type apiQuerier interface {
LoadJobList() (list []*nomadApi.JobListStub, err error) LoadJobList() (list []*nomadApi.JobListStub, err error)
// JobScale returns the scale of the passed job. // JobScale returns the scale of the passed job.
JobScale(jobId string) (jobScale int, err error) JobScale(jobId string) (jobScale uint, err error)
// SetJobScale sets the scaling count of the passed job to Nomad. // SetJobScale sets the scaling count of the passed job to Nomad.
SetJobScale(jobId string, count int, reason string) (err error) SetJobScale(jobId string, count uint, reason string) (err error)
// DeleteRunner deletes the runner with the given Id. // DeleteRunner deletes the runner with the given Id.
DeleteRunner(runnerId string) (err error) DeleteRunner(runnerId string) (err error)

View File

@ -78,14 +78,14 @@ func (_m *apiQuerierMock) ExecuteCommand(allocationID string, ctx context.Contex
} }
// JobScale provides a mock function with given fields: jobId // JobScale provides a mock function with given fields: jobId
func (_m *apiQuerierMock) JobScale(jobId string) (int, error) { func (_m *apiQuerierMock) JobScale(jobId string) (uint, error) {
ret := _m.Called(jobId) ret := _m.Called(jobId)
var r0 int var r0 uint
if rf, ok := ret.Get(0).(func(string) int); ok { if rf, ok := ret.Get(0).(func(string) uint); ok {
r0 = rf(jobId) r0 = rf(jobId)
} else { } else {
r0 = ret.Get(0).(int) r0 = ret.Get(0).(uint)
} }
var r1 error var r1 error
@ -143,11 +143,11 @@ func (_m *apiQuerierMock) RegisterNomadJob(job *api.Job) (string, error) {
} }
// SetJobScale provides a mock function with given fields: jobId, count, reason // SetJobScale provides a mock function with given fields: jobId, count, reason
func (_m *apiQuerierMock) SetJobScale(jobId string, count int, reason string) error { func (_m *apiQuerierMock) SetJobScale(jobId string, count uint, reason string) error {
ret := _m.Called(jobId, count, reason) ret := _m.Called(jobId, count, reason)
var r0 error var r0 error
if rf, ok := ret.Get(0).(func(string, int, string) error); ok { if rf, ok := ret.Get(0).(func(string, uint, string) error); ok {
r0 = rf(jobId, count, reason) r0 = rf(jobId, count, reason)
} else { } else {
r0 = ret.Error(0) r0 = ret.Error(0)

View File

@ -1,4 +1,4 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT. // Code generated by mockery v2.8.0. DO NOT EDIT.
package nomad package nomad
@ -78,14 +78,14 @@ func (_m *ExecutorApiMock) ExecuteCommand(allocationID string, ctx context.Conte
} }
// JobScale provides a mock function with given fields: jobId // JobScale provides a mock function with given fields: jobId
func (_m *ExecutorApiMock) JobScale(jobId string) (int, error) { func (_m *ExecutorApiMock) JobScale(jobId string) (uint, error) {
ret := _m.Called(jobId) ret := _m.Called(jobId)
var r0 int var r0 uint
if rf, ok := ret.Get(0).(func(string) int); ok { if rf, ok := ret.Get(0).(func(string) uint); ok {
r0 = rf(jobId) r0 = rf(jobId)
} else { } else {
r0 = ret.Get(0).(int) r0 = ret.Get(0).(uint)
} }
var r1 error var r1 error
@ -180,11 +180,11 @@ func (_m *ExecutorApiMock) RegisterNomadJob(job *api.Job) (string, error) {
} }
// SetJobScale provides a mock function with given fields: jobId, count, reason // SetJobScale provides a mock function with given fields: jobId, count, reason
func (_m *ExecutorApiMock) SetJobScale(jobId string, count int, reason string) error { func (_m *ExecutorApiMock) SetJobScale(jobId string, count uint, reason string) error {
ret := _m.Called(jobId, count, reason) ret := _m.Called(jobId, count, reason)
var r0 error var r0 error
if rf, ok := ret.Get(0).(func(string, int, string) error); ok { if rf, ok := ret.Get(0).(func(string, uint, string) error); ok {
r0 = rf(jobId, count, reason) r0 = rf(jobId, count, reason)
} else { } else {
r0 = ret.Error(0) r0 = ret.Error(0)

View File

@ -17,18 +17,19 @@ func (nc *nomadApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error
} }
// JobScale returns the scale of the passed job. // JobScale returns the scale of the passed job.
func (nc *nomadApiClient) JobScale(jobId string) (jobScale int, err error) { func (nc *nomadApiClient) JobScale(jobId string) (jobScale uint, err error) {
status, _, err := nc.client.Jobs().ScaleStatus(jobId, nil) status, _, err := nc.client.Jobs().ScaleStatus(jobId, nil)
if err != nil { if err != nil {
return return
} }
// ToDo: Consider counting also the placed and desired allocations // ToDo: Consider counting also the placed and desired allocations
jobScale = status.TaskGroups[fmt.Sprintf(TaskGroupNameFormat, jobId)].Running jobScale = uint(status.TaskGroups[fmt.Sprintf(TaskGroupNameFormat, jobId)].Running)
return return
} }
// SetJobScale sets the scaling count of the passed job to Nomad. // SetJobScale sets the scaling count of the passed job to Nomad.
func (nc *nomadApiClient) SetJobScale(jobId string, count int, reason string) (err error) { func (nc *nomadApiClient) SetJobScale(jobId string, count uint, reason string) (err error) {
_, _, err = nc.client.Jobs().Scale(jobId, fmt.Sprintf(TaskGroupNameFormat, jobId), &count, reason, false, nil, nil) intCount := int(count)
_, _, err = nc.client.Jobs().Scale(jobId, fmt.Sprintf(TaskGroupNameFormat, jobId), &intCount, reason, false, nil, nil)
return return
} }

View File

@ -25,7 +25,7 @@ type NomadJobId string
// Manager keeps track of the used and unused runners of all execution environments in order to provide unused runners to new clients and ensure no runner is used twice. // Manager keeps track of the used and unused runners of all execution environments in order to provide unused runners to new clients and ensure no runner is used twice.
type Manager interface { type Manager interface {
// RegisterEnvironment adds a new environment that should be managed. // RegisterEnvironment adds a new environment that should be managed.
RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount int) RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount uint)
// EnvironmentExists returns whether the environment with the given id exists. // EnvironmentExists returns whether the environment with the given id exists.
EnvironmentExists(id EnvironmentId) bool EnvironmentExists(id EnvironmentId) bool
@ -61,14 +61,14 @@ type NomadJob struct {
environmentId EnvironmentId environmentId EnvironmentId
jobId NomadJobId jobId NomadJobId
idleRunners Storage idleRunners Storage
desiredIdleRunnersCount int desiredIdleRunnersCount uint
} }
func (j *NomadJob) Id() EnvironmentId { func (j *NomadJob) Id() EnvironmentId {
return j.environmentId return j.environmentId
} }
func (m *NomadRunnerManager) RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount int) { func (m *NomadRunnerManager) RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount uint) {
m.jobs.Add(&NomadJob{ m.jobs.Add(&NomadJob{
environmentId, environmentId,
nomadJobId, nomadJobId,
@ -120,7 +120,7 @@ func (m *NomadRunnerManager) refreshEnvironment(id EnvironmentId) {
// this environment does not exist // this environment does not exist
return return
} }
lastJobScaling := -1 var lastJobScaling uint = 0
for { for {
runners, err := m.apiClient.LoadRunners(string(job.jobId)) runners, err := m.apiClient.LoadRunners(string(job.jobId))
if err != nil { if err != nil {
@ -138,7 +138,7 @@ func (m *NomadRunnerManager) refreshEnvironment(id EnvironmentId) {
log.WithError(err).Printf("Failed get allocation count") log.WithError(err).Printf("Failed get allocation count")
break break
} }
additionallyNeededRunners := job.desiredIdleRunnersCount - job.idleRunners.Length() + 1 additionallyNeededRunners := job.desiredIdleRunnersCount - uint(job.idleRunners.Length()) + 1
requiredRunnerCount := jobScale requiredRunnerCount := jobScale
if additionallyNeededRunners > 0 { if additionallyNeededRunners > 0 {
requiredRunnerCount += additionallyNeededRunners requiredRunnerCount += additionallyNeededRunners

View File

@ -1,4 +1,4 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT. // Code generated by mockery v2.8.0. DO NOT EDIT.
package runner package runner
@ -70,7 +70,7 @@ func (_m *ManagerMock) Get(runnerId string) (Runner, error) {
} }
// RegisterEnvironment provides a mock function with given fields: environmentId, nomadJobId, desiredIdleRunnersCount // RegisterEnvironment provides a mock function with given fields: environmentId, nomadJobId, desiredIdleRunnersCount
func (_m *ManagerMock) RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount int) { func (_m *ManagerMock) RegisterEnvironment(environmentId EnvironmentId, nomadJobId NomadJobId, desiredIdleRunnersCount uint) {
_m.Called(environmentId, nomadJobId, desiredIdleRunnersCount) _m.Called(environmentId, nomadJobId, desiredIdleRunnersCount)
} }

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
defaultDesiredRunnersCount = 5 defaultDesiredRunnersCount uint = 5
) )
func TestGetNextRunnerTestSuite(t *testing.T) { func TestGetNextRunnerTestSuite(t *testing.T) {
@ -38,8 +38,8 @@ func (s *ManagerTestSuite) mockRunnerQueries(returnedRunnerIds []string) {
// reset expected calls to allow new mocked return values // reset expected calls to allow new mocked return values
s.apiMock.ExpectedCalls = []*mock.Call{} s.apiMock.ExpectedCalls = []*mock.Call{}
s.apiMock.On("LoadRunners", tests.DefaultJobId).Return(returnedRunnerIds, nil) s.apiMock.On("LoadRunners", tests.DefaultJobId).Return(returnedRunnerIds, nil)
s.apiMock.On("JobScale", tests.DefaultJobId).Return(len(returnedRunnerIds), nil) s.apiMock.On("JobScale", tests.DefaultJobId).Return(uint(len(returnedRunnerIds)), nil)
s.apiMock.On("SetJobScale", tests.DefaultJobId, mock.AnythingOfType("int"), "Runner Requested").Return(nil) s.apiMock.On("SetJobScale", tests.DefaultJobId, mock.AnythingOfType("uint"), "Runner Requested").Return(nil)
} }
func (s *ManagerTestSuite) registerDefaultEnvironment() { func (s *ManagerTestSuite) registerDefaultEnvironment() {