Skip to content

Commit 42adff0

Browse files
authored
feat: new plug apis, fix container removal, better performance (#115)
1 parent ccb6cd6 commit 42adff0

32 files changed

+549
-312
lines changed

app/broker/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package broker
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
// ErrMissingPayloadKey means that payload was missing from request headers.
9+
ErrMissingPayloadKey = errors.New("missing payload")
10+
// ErrJSONParsingFailed is used to mark any JSON unmarshal error during payload parsing.
11+
ErrJSONParsingFailed = errors.New("json parsing error, expected an object")
12+
)

app/broker/server.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import (
1414
"go.opencensus.io/stats/view"
1515
census_trace "go.opencensus.io/trace"
1616
"google.golang.org/grpc"
17-
"google.golang.org/grpc/codes"
1817
"google.golang.org/grpc/metadata"
19-
"google.golang.org/grpc/status"
2018

2119
"github.com/Syncano/codebox/app/common"
2220
"github.com/Syncano/pkg-go/celery"
@@ -70,9 +68,6 @@ var DefaultOptions = &ServerOptions{
7068
}
7169

7270
var (
73-
// ErrInvalidArgument signals that there are no suitable workers at this moment.
74-
ErrInvalidArgument = status.Error(codes.InvalidArgument, "invalid argument")
75-
7671
initOnce sync.Once
7772
overheadDuration = stats.Float64(
7873
"codebox/overhead/duration/seconds",
@@ -181,7 +176,7 @@ func (s *Server) Run(stream brokerpb.ScriptRunner_RunServer) error {
181176
scriptMeta = v.ScriptMeta
182177
default:
183178
logger.Error("grpc:broker:Run error parsing input")
184-
return ErrInvalidArgument
179+
return common.ErrInvalidArgument
185180
}
186181

187182
if meta != nil && lbMeta != nil && scriptMeta != nil {
@@ -197,7 +192,7 @@ func (s *Server) Run(stream brokerpb.ScriptRunner_RunServer) error {
197192

198193
chunk := req.GetScriptChunk()
199194
if chunk == nil {
200-
return nil, ErrInvalidArgument
195+
return nil, common.ErrInvalidArgument
201196
}
202197

203198
return chunk, nil
@@ -211,7 +206,7 @@ func (s *Server) processRun(ctx context.Context, logger logrus.FieldLogger,
211206
stream StreamReponder) error {
212207
if meta == nil || lbMeta == nil || scriptMeta == nil {
213208
logger.Error("grpc:broker:Run error parsing input")
214-
return ErrInvalidArgument
209+
return common.ErrInvalidArgument
215210
}
216211

217212
start := time.Now()

app/broker/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"google.golang.org/grpc/grpclog"
2525

2626
"github.com/Syncano/codebox/app/broker/mocks"
27+
"github.com/Syncano/codebox/app/common"
2728
repomocks "github.com/Syncano/codebox/app/filerepo/mocks"
2829
lbmocks "github.com/Syncano/codebox/app/lb/mocks"
2930
"github.com/Syncano/pkg-go/celery"
@@ -82,7 +83,7 @@ func TestServerMethods(t *testing.T) {
8283
Convey("Run returns error on invalid request", func() {
8384
stream.On("Context").Return(context.Background())
8485
e := s.SimpleRun(new(brokerpb.SimpleRunRequest), stream)
85-
So(e, ShouldResemble, ErrInvalidArgument)
86+
So(e, ShouldResemble, common.ErrInvalidArgument)
8687
})
8788
Convey("Run processes on valid request", func() {
8889
sourceHash := "abc"

app/broker/uwsgi.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package broker
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io/ioutil"
98
"net/http"
@@ -22,11 +21,7 @@ import (
2221
)
2322

2423
var (
25-
// ErrMissingPayloadKey means that payload was missing from request headers.
26-
ErrMissingPayloadKey = errors.New("missing payload")
27-
// ErrJSONParsingFailed is used to mark any JSON unmarshal error during payload parsing.
28-
ErrJSONParsingFailed = errors.New("json parsing error, expected an object")
29-
statusToHTTPCode = map[string]int{
24+
statusToHTTPCode = map[string]int{
3025
failureStatus: http.StatusInternalServerError,
3126
blockedStatus: http.StatusTooManyRequests,
3227
timeoutStatus: http.StatusRequestTimeout,

app/common/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package common
2+
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
)
7+
8+
var (
9+
// ErrSourceNotAvailable signals that specified source hash was not found.
10+
ErrSourceNotAvailable = status.Error(codes.FailedPrecondition, "source not available")
11+
12+
ErrInvalidArgument = status.Error(codes.InvalidArgument, "invalid argument")
13+
)

app/docker/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package docker
2+
3+
import "errors"
4+
5+
// ErrReservedCPUTooHigh signals that too we are trying to reserve more cpu than we got.
6+
var ErrReservedCPUTooHigh = errors.New("value of reserved cpu is higher than available")

app/docker/manager.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package docker
22

33
import (
44
"context"
5-
"errors"
65
"io"
76
"io/ioutil"
87
"time"
@@ -45,9 +44,6 @@ var DefaultOptions = &Options{
4544
DNS: []string{"208.67.222.222", "208.67.220.220"},
4645
}
4746

48-
// ErrReservedCPUTooHigh signals that too we are trying to reserve more cpu than we got.
49-
var ErrReservedCPUTooHigh = errors.New("value of reserved cpu is higher than available")
50-
5147
// Constraints defines limitations for docker container.
5248
type Constraints struct {
5349
CPULimit int64

app/filerepo/errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package filerepo
2+
3+
import (
4+
"errors"
5+
6+
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/status"
8+
)
9+
10+
var (
11+
// ErrMissingMeta signals that there are upload format was invalid and was missing upload meta.
12+
ErrMissingMeta = status.Error(codes.FailedPrecondition, "missing upload meta")
13+
14+
// ErrResourceNotFound signals that resource key was not found and it was a must.
15+
ErrResourceNotFound = errors.New("resource not found")
16+
// ErrVolumeNotFound signals that volume key was not found and it was a must.
17+
ErrVolumeNotFound = errors.New("volume not found")
18+
// ErrNotEnoughDiskSpace signals that there is not enough disk space available on storage path.
19+
ErrNotEnoughDiskSpace = errors.New("not enough disk space")
20+
)

app/filerepo/repo.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package filerepo
22

33
import (
44
"bufio"
5-
"errors"
65
"io"
76
"os"
87
"path/filepath"
@@ -69,15 +68,6 @@ type Resource struct {
6968
Path string
7069
}
7170

72-
var (
73-
// ErrResourceNotFound signals that resource key was not found and it was a must.
74-
ErrResourceNotFound = errors.New("resource not found")
75-
// ErrVolumeNotFound signals that volume key was not found and it was a must.
76-
ErrVolumeNotFound = errors.New("volume not found")
77-
// ErrNotEnoughDiskSpace signals that there is not enough disk space available on storage path.
78-
ErrNotEnoughDiskSpace = errors.New("not enough disk space")
79-
)
80-
8171
const (
8272
fileStorageName = "files"
8373
volumeStorageName = "volumes"

app/filerepo/server.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ type Server struct {
2121
// Assert that Server is compatible with proto interface.
2222
var _ pb.RepoServer = (*Server)(nil)
2323

24-
var (
25-
// ErrMissingMeta signals that there are upload format was invalid and was missing upload meta.
26-
ErrMissingMeta = status.Error(codes.FailedPrecondition, "missing upload meta")
27-
)
28-
2924
// Exists checks if file was defined in file repo.
3025
func (s *Server) Exists(ctx context.Context, in *pb.ExistsRequest) (*pb.ExistsResponse, error) {
3126
ctx, reqID := util.AddDefaultRequestID(ctx)

0 commit comments

Comments
 (0)