Move execution storage to new package

This commit is contained in:
sirkrypt0
2021-07-29 15:20:16 +02:00
parent 8197235f1c
commit bd14b1e181
11 changed files with 175 additions and 93 deletions

View File

@@ -1,46 +0,0 @@
package runner
import (
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
"sync"
)
// ExecutionStorage stores executions.
type ExecutionStorage interface {
// Add adds a runner to the storage.
// It overwrites the existing execution if an execution with the same id already exists.
Add(id ExecutionID, executionRequest *dto.ExecutionRequest)
// Pop deletes the execution with the given id from the storage and returns it.
// If no such execution exists, ok is false and true otherwise.
Pop(id ExecutionID) (request *dto.ExecutionRequest, ok bool)
}
// localExecutionStorage stores execution objects in the local application memory.
// ToDo: Create implementation that use some persistent storage like a database.
type localExecutionStorage struct {
sync.RWMutex
executions map[ExecutionID]*dto.ExecutionRequest
}
// NewLocalExecutionStorage responds with an ExecutionStorage implementation.
// This implementation stores the data thread-safe in the local application memory.
func NewLocalExecutionStorage() *localExecutionStorage {
return &localExecutionStorage{
executions: make(map[ExecutionID]*dto.ExecutionRequest),
}
}
func (s *localExecutionStorage) Add(id ExecutionID, executionRequest *dto.ExecutionRequest) {
s.Lock()
defer s.Unlock()
s.executions[id] = executionRequest
}
func (s *localExecutionStorage) Pop(id ExecutionID) (*dto.ExecutionRequest, bool) {
s.Lock()
defer s.Unlock()
request, ok := s.executions[id]
delete(s.executions, id)
return request, ok
}

View File

@@ -10,6 +10,7 @@ import (
nomadApi "github.com/hashicorp/nomad/api"
"gitlab.hpi.de/codeocean/codemoon/poseidon/internal/nomad"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/execution"
"io"
"strings"
"time"
@@ -18,9 +19,6 @@ import (
// ContextKey is the type for keys in a request context.
type ContextKey string
// ExecutionID is an id for an execution in a Runner.
type ExecutionID string
const (
// runnerContextKey is the key used to store runners in context.Context.
runnerContextKey ContextKey = "runner"
@@ -39,7 +37,7 @@ type Runner interface {
// MappedPorts returns the mapped ports of the runner.
MappedPorts() []*dto.MappedPort
ExecutionStorage
execution.Storage
InactivityTimer
// ExecuteInteractively runs the given execution request and forwards from and to the given reader and writers.
@@ -62,7 +60,7 @@ type Runner interface {
// NomadJob is an abstraction to communicate with Nomad environments.
type NomadJob struct {
ExecutionStorage
execution.Storage
InactivityTimer
id string
portMappings []nomadApi.PortMapping
@@ -75,11 +73,11 @@ func NewNomadJob(id string, portMappings []nomadApi.PortMapping,
apiClient nomad.ExecutorAPI, manager Manager,
) *NomadJob {
job := &NomadJob{
id: id,
portMappings: portMappings,
api: apiClient,
ExecutionStorage: NewLocalExecutionStorage(),
manager: manager,
id: id,
portMappings: portMappings,
api: apiClient,
Storage: execution.NewLocalStorage(),
manager: manager,
}
job.InactivityTimer = NewInactivityTimer(job, manager)
return job

View File

@@ -4,6 +4,7 @@ package runner
import (
context "context"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/execution"
io "io"
dto "gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
@@ -19,7 +20,7 @@ type RunnerMock struct {
}
// Add provides a mock function with given fields: id, executionRequest
func (_m *RunnerMock) Add(id ExecutionID, executionRequest *dto.ExecutionRequest) {
func (_m *RunnerMock) Add(id execution.ID, executionRequest *dto.ExecutionRequest) {
_m.Called(id, executionRequest)
}
@@ -93,11 +94,11 @@ func (_m *RunnerMock) MappedPorts() []*dto.MappedPort {
}
// Pop provides a mock function with given fields: id
func (_m *RunnerMock) Pop(id ExecutionID) (*dto.ExecutionRequest, bool) {
func (_m *RunnerMock) Pop(id execution.ID) (*dto.ExecutionRequest, bool) {
ret := _m.Called(id)
var r0 *dto.ExecutionRequest
if rf, ok := ret.Get(0).(func(ExecutionID) *dto.ExecutionRequest); ok {
if rf, ok := ret.Get(0).(func(execution.ID) *dto.ExecutionRequest); ok {
r0 = rf(id)
} else {
if ret.Get(0) != nil {
@@ -106,7 +107,7 @@ func (_m *RunnerMock) Pop(id ExecutionID) (*dto.ExecutionRequest, bool) {
}
var r1 bool
if rf, ok := ret.Get(1).(func(ExecutionID) bool); ok {
if rf, ok := ret.Get(1).(func(execution.ID) bool); ok {
r1 = rf(id)
} else {
r1 = ret.Get(1).(bool)

View File

@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/suite"
"gitlab.hpi.de/codeocean/codemoon/poseidon/internal/nomad"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/execution"
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/nullio"
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests"
"io"
@@ -48,7 +49,7 @@ func TestExecutionRequestIsStored(t *testing.T) {
TimeLimit: 10,
Environment: nil,
}
id := ExecutionID("test-execution")
id := execution.ID("test-execution")
runner.Add(id, executionRequest)
storedExecutionRunner, ok := runner.Pop(id)
@@ -118,11 +119,11 @@ func (s *ExecuteInteractivelyTestSuite) SetupTest() {
s.manager.On("Return", mock.Anything).Return(nil)
s.runner = &NomadJob{
ExecutionStorage: NewLocalExecutionStorage(),
InactivityTimer: s.timer,
id: tests.DefaultRunnerID,
api: s.apiMock,
manager: s.manager,
Storage: execution.NewLocalStorage(),
InactivityTimer: s.timer,
id: tests.DefaultRunnerID,
api: s.apiMock,
manager: s.manager,
}
}
@@ -141,8 +142,8 @@ func (s *ExecuteInteractivelyTestSuite) TestReturnsAfterTimeout() {
}).Return(0, nil)
timeLimit := 1
execution := &dto.ExecutionRequest{TimeLimit: timeLimit}
exit, _ := s.runner.ExecuteInteractively(execution, &nullio.ReadWriter{}, nil, nil)
executionRequest := &dto.ExecutionRequest{TimeLimit: timeLimit}
exit, _ := s.runner.ExecuteInteractively(executionRequest, &nullio.ReadWriter{}, nil, nil)
select {
case <-exit:
@@ -170,8 +171,8 @@ func (s *ExecuteInteractivelyTestSuite) TestSendsSignalAfterTimeout() {
close(quit)
}).Return(0, nil)
timeLimit := 1
execution := &dto.ExecutionRequest{TimeLimit: timeLimit}
_, _ = s.runner.ExecuteInteractively(execution, bytes.NewBuffer(make([]byte, 1)), nil, nil)
executionRequest := &dto.ExecutionRequest{TimeLimit: timeLimit}
_, _ = s.runner.ExecuteInteractively(executionRequest, bytes.NewBuffer(make([]byte, 1)), nil, nil)
select {
case <-time.After(2 * (time.Duration(timeLimit) * time.Second)):
s.FailNow("The execution should receive a SIGQUIT after the timeout")
@@ -184,22 +185,22 @@ func (s *ExecuteInteractivelyTestSuite) TestDestroysRunnerAfterTimeoutAndSignal(
select {}
})
timeLimit := 1
execution := &dto.ExecutionRequest{TimeLimit: timeLimit}
_, _ = s.runner.ExecuteInteractively(execution, bytes.NewBuffer(make([]byte, 1)), nil, nil)
executionRequest := &dto.ExecutionRequest{TimeLimit: timeLimit}
_, _ = s.runner.ExecuteInteractively(executionRequest, bytes.NewBuffer(make([]byte, 1)), nil, nil)
<-time.After(executionTimeoutGracePeriod + time.Duration(timeLimit)*time.Second + tests.ShortTimeout)
s.manager.AssertCalled(s.T(), "Return", s.runner)
}
func (s *ExecuteInteractivelyTestSuite) TestResetTimerGetsCalled() {
execution := &dto.ExecutionRequest{}
s.runner.ExecuteInteractively(execution, nil, nil, nil)
executionRequest := &dto.ExecutionRequest{}
s.runner.ExecuteInteractively(executionRequest, nil, nil, nil)
s.timer.AssertCalled(s.T(), "ResetTimeout")
}
func (s *ExecuteInteractivelyTestSuite) TestExitHasTimeoutErrorIfRunnerTimesOut() {
s.mockedTimeoutPassedCall.Return(true)
execution := &dto.ExecutionRequest{}
exitChannel, _ := s.runner.ExecuteInteractively(execution, &nullio.ReadWriter{}, nil, nil)
executionRequest := &dto.ExecutionRequest{}
exitChannel, _ := s.runner.ExecuteInteractively(executionRequest, &nullio.ReadWriter{}, nil, nil)
exit := <-exitChannel
s.Equal(ErrorRunnerInactivityTimeout, exit.Err)
}
@@ -224,10 +225,10 @@ func (s *UpdateFileSystemTestSuite) SetupTest() {
s.timer.On("ResetTimeout").Return()
s.timer.On("TimeoutPassed").Return(false)
s.runner = &NomadJob{
ExecutionStorage: NewLocalExecutionStorage(),
InactivityTimer: s.timer,
id: tests.DefaultRunnerID,
api: s.apiMock,
Storage: execution.NewLocalStorage(),
InactivityTimer: s.timer,
id: tests.DefaultRunnerID,
api: s.apiMock,
}
s.mockedExecuteCommandCall = s.apiMock.On("ExecuteCommand", tests.DefaultRunnerID, mock.Anything,
mock.Anything, false, mock.Anything, mock.Anything, mock.Anything).