diff --git a/.gitignore b/.gitignore index b2eaa3b..97ec8a1 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ vendor build/ configs/ +.DS_Store diff --git a/disneyland/client_test.go b/disneyland/client_test.go index b20eb2e..3848d59 100755 --- a/disneyland/client_test.go +++ b/disneyland/client_test.go @@ -82,7 +82,7 @@ func TestGRPCJobCRUD(t *testing.T) { c := NewDisneylandClient(conn) ctx := context.Background() - + //first createdJob, err := c.CreateJob(ctx, &Job{Status: Job_PENDING}) checkTestErr(err, t) @@ -94,8 +94,8 @@ func TestGRPCJobCRUD(t *testing.T) { } createdJob.Status = Job_PENDING - createdJob.Metadata = "meta_test" - createdJob.Output = "output_test" + createdJob.Metadata = "updated_test" + createdJob.Output = "updated_test" createdJob.Kind = "docker" updatedJob, err := c.ModifyJob(ctx, createdJob) @@ -104,25 +104,41 @@ func TestGRPCJobCRUD(t *testing.T) { if !checkJobsEqual(createdJob, updatedJob) { t.Fail() } + //second + createdJob, err = c.CreateJob(ctx, &Job{Kind: "docker"}) + checkTestErr(err, t) - createdJob, err = c.CreateJob(ctx, &Job{}) + allJobs, err := c.ListJobs(ctx, &ListJobsRequest{HowMany: 0}) checkTestErr(err, t) - allJobs, err := c.ListJobs(ctx, &ListJobsRequest{HowMany: 2}) + if len(allJobs.Jobs) != 2 { + t.Fail() + } + + allJobs, err = c.ListJobs(ctx, &ListJobsRequest{HowMany: 1}) checkTestErr(err, t) - if len(allJobs.Jobs) < 1 { + if len(allJobs.Jobs) != 1 { t.Fail() } - pulledJobs, err := c.PullPendingJobs(ctx, &ListJobsRequest{HowMany: 2}) + allJobs, err = c.ListJobs(ctx, &ListJobsRequest{Kind: "docker", HowMany:2}) checkTestErr(err, t) - if len(pulledJobs.Jobs) < 1 { + if len(allJobs.Jobs) != 2 { t.Fail() } - _, err = c.DeleteJob(ctx, &RequestWithId{Id: 2}) + pulledJobs, err := c.PullPendingJobs(ctx, &ListJobsRequest{HowMany: 1}) checkTestErr(err, t) + if len(pulledJobs.Jobs) != 1 { + t.Fail() + } + //third + createdJob, err = c.CreateJob(ctx, &Job{Kind: "remove"}) + checkTestErr(err, t) + + _, err = c.DeleteJob(ctx, &RequestWithId{Id: createdJob.Id}) + checkTestErr(err, t) } diff --git a/disneyland/disneyland.pb.go b/disneyland/disneyland.pb.go index e4f22e8..6f545d8 100644 --- a/disneyland/disneyland.pb.go +++ b/disneyland/disneyland.pb.go @@ -11,7 +11,6 @@ It has these top-level messages: Job ListOfJobs RequestWithId - ListJobStatusResponse ListJobsRequest */ package disneyland @@ -162,22 +161,6 @@ func (m *RequestWithId) GetId() uint64 { return 0 } -type ListJobStatusResponse struct { - Status []Job_Status `protobuf:"varint,1,rep,packed,name=status,enum=Job_Status" json:"status,omitempty"` -} - -func (m *ListJobStatusResponse) Reset() { *m = ListJobStatusResponse{} } -func (m *ListJobStatusResponse) String() string { return proto.CompactTextString(m) } -func (*ListJobStatusResponse) ProtoMessage() {} -func (*ListJobStatusResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } - -func (m *ListJobStatusResponse) GetStatus() []Job_Status { - if m != nil { - return m.Status - } - return nil -} - type ListJobsRequest struct { HowMany uint32 `protobuf:"varint,1,opt,name=how_many,json=howMany" json:"how_many,omitempty"` Project string `protobuf:"bytes,2,opt,name=project" json:"project,omitempty"` @@ -187,7 +170,7 @@ type ListJobsRequest struct { func (m *ListJobsRequest) Reset() { *m = ListJobsRequest{} } func (m *ListJobsRequest) String() string { return proto.CompactTextString(m) } func (*ListJobsRequest) ProtoMessage() {} -func (*ListJobsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } +func (*ListJobsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } func (m *ListJobsRequest) GetHowMany() uint32 { if m != nil { @@ -214,7 +197,6 @@ func init() { proto.RegisterType((*Job)(nil), "Job") proto.RegisterType((*ListOfJobs)(nil), "ListOfJobs") proto.RegisterType((*RequestWithId)(nil), "RequestWithId") - proto.RegisterType((*ListJobStatusResponse)(nil), "ListJobStatusResponse") proto.RegisterType((*ListJobsRequest)(nil), "ListJobsRequest") proto.RegisterEnum("Job_Status", Job_Status_name, Job_Status_value) } @@ -459,33 +441,31 @@ var _Disneyland_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("disneyland.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 436 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0x4f, 0x6b, 0xdb, 0x30, - 0x14, 0x8f, 0x1c, 0xd7, 0x49, 0x5e, 0x48, 0x6a, 0x1e, 0xdb, 0x50, 0x73, 0x99, 0x71, 0x61, 0x04, - 0x06, 0x3e, 0x64, 0xd7, 0x5d, 0x46, 0x9d, 0x95, 0x84, 0x24, 0x35, 0xde, 0xca, 0x60, 0x97, 0x61, - 0x4f, 0xea, 0xa2, 0x2e, 0x95, 0xb2, 0x48, 0xa6, 0xe4, 0x2b, 0xef, 0x3b, 0x0c, 0x86, 0x14, 0x27, - 0x6b, 0xb7, 0x0c, 0x7a, 0x31, 0x7e, 0xef, 0x27, 0xbd, 0xf7, 0xfb, 0x23, 0x08, 0x99, 0xd0, 0x92, - 0x6f, 0x57, 0x85, 0x64, 0xc9, 0x7a, 0xa3, 0x8c, 0x8a, 0x7f, 0x11, 0x68, 0x4e, 0x55, 0x89, 0x14, - 0x5a, 0xeb, 0x8d, 0xba, 0xe5, 0x5f, 0x0d, 0x25, 0x11, 0x19, 0x76, 0xf2, 0x7d, 0x89, 0x7d, 0xf0, - 0x04, 0xa3, 0x5e, 0x44, 0x86, 0x7e, 0xee, 0x09, 0x86, 0x08, 0xfe, 0x77, 0x21, 0x19, 0x6d, 0xba, - 0x63, 0xee, 0x1f, 0xcf, 0x21, 0xd0, 0xa6, 0x30, 0x95, 0xa6, 0x7e, 0x44, 0x86, 0xfd, 0x51, 0x37, - 0x99, 0xaa, 0x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21, 0xb7, 0x0f, 0xc3, 0xf2, 0x1e, 0x87, 0x75, 0x24, - 0x9c, 0xd1, 0x4f, 0x02, 0x90, 0x1e, 0x72, 0xc7, 0x33, 0xe8, 0x5c, 0x6c, 0x78, 0x61, 0xb8, 0x8d, - 0xdd, 0x49, 0x1c, 0xb8, 0x6f, 0xdc, 0xc0, 0x08, 0x82, 0x4b, 0x6e, 0x49, 0x60, 0x3f, 0x79, 0xa4, - 0xf6, 0x70, 0xe2, 0x35, 0xb4, 0xf7, 0x3c, 0x31, 0x4c, 0xfe, 0xa2, 0x3c, 0xe8, 0x26, 0x7f, 0xbc, - 0x8c, 0x1b, 0x76, 0xd3, 0x5c, 0x31, 0x71, 0xb3, 0xfd, 0x77, 0xd3, 0x08, 0x4e, 0xb3, 0x6a, 0xb5, - 0xca, 0xb8, 0x64, 0x42, 0x7e, 0x7b, 0xda, 0xb8, 0x73, 0xe8, 0xa4, 0x7c, 0xc5, 0x77, 0xc4, 0xff, - 0x43, 0xb0, 0x0c, 0xdc, 0xb3, 0x7e, 0xf3, 0x3b, 0x00, 0x00, 0xff, 0xff, 0xd6, 0x96, 0xd3, 0x83, - 0xea, 0x02, 0x00, 0x00, + // 415 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0x5d, 0x6b, 0xdb, 0x30, + 0x14, 0x8d, 0x1c, 0xd7, 0x49, 0x6e, 0x48, 0x6a, 0x2e, 0x63, 0xa8, 0x79, 0x99, 0x71, 0x61, 0x04, + 0x06, 0x7e, 0xc8, 0x7e, 0xc1, 0xa8, 0xb3, 0x92, 0x90, 0xa4, 0xc6, 0x5b, 0x19, 0xec, 0x65, 0xc8, + 0x93, 0xba, 0xa8, 0x4b, 0xa5, 0x2c, 0x96, 0x29, 0xfe, 0xcb, 0xfb, 0x0f, 0x83, 0x21, 0xc5, 0xc9, + 0x96, 0x7d, 0x40, 0x5f, 0x8c,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, 0x28, 0x29, 0x89, 0xda, 0xe3, + 0xfe, 0xc4, 0xb7, 0x7f, 0x91, 0x3b, 0x24, 0x7e, 0x01, 0x83, 0x5c, 0x7c, 0xab, 0x44, 0x69, 0x3e, + 0x48, 0xb3, 0x9e, 0xf1, 0xa6, 0x16, 0x72, 0xa8, 0x25, 0xfe, 0x08, 0xe7, 0xd6, 0xc8, 0xda, 0x34, + 0x42, 0xbc, 0x80, 0xee, 0x5a, 0x3f, 0x7e, 0x7a, 0x60, 0xaa, 0x76, 0xc2, 0x41, 0xde, 0x59, 0xeb, + 0xc7, 0x25, 0x53, 0xf5, 0xef, 0x75, 0x7b, 0xa7, 0x75, 0xff, 0xa3, 0xde, 0xc9, 0x77, 0x02, 0x90, + 0x1e, 0x37, 0x87, 0x17, 0xd0, 0xbb, 0xda, 0x09, 0x66, 0x84, 0x5d, 0x9c, 0x0b, 0x39, 0x72, 0xcf, + 0xb8, 0x85, 0x11, 0x04, 0xd7, 0xc2, 0x86, 0xc0, 0x61, 0x72, 0x92, 0xf7, 0xa8, 0x78, 0x05, 0xdd, + 0x43, 0x4e, 0x0c, 0x93, 0x3f, 0x22, 0x8f, 0xfa, 0xc9, 0xaf, 0x36, 0xe2, 0x96, 0x3d, 0x69, 0xa9, + 0xb9, 0xbc, 0xab, 0xff, 0x3e, 0x69, 0x02, 0xe7, 0x59, 0xb5, 0xd9, 0x64, 0x42, 0x71, 0xa9, 0xbe, + 0x3c, 0xcd, 0xee, 0x12, 0x7a, 0xa9, 0xd8, 0x88, 0x7d, 0xf0, 0xff, 0x04, 0x2c, 0x02, 0x77, 0x31, + 0x5f, 0xff, 0x0c, 0x00, 0x00, 0xff, 0xff, 0xcb, 0xb6, 0xdf, 0x04, 0xac, 0x02, 0x00, 0x00, } diff --git a/disneyland/server.go b/disneyland/server.go index 2f7d797..d40327e 100755 --- a/disneyland/server.go +++ b/disneyland/server.go @@ -95,6 +95,7 @@ func (s *Server) PullPendingJobs(ctx context.Context, in *ListJobsRequest) (*Lis if user.IsUser() { in.Project = user.ProjectAccess } + pts, err := s.Storage.PullJobs(in.HowMany, in.Project, in.Kind) if err != nil { diff --git a/disneyland/storage.go b/disneyland/storage.go index 0d28ced..5b13759 100755 --- a/disneyland/storage.go +++ b/disneyland/storage.go @@ -3,8 +3,27 @@ package disneyland import ( "database/sql" _ "github.com/lib/pq" + "strconv" ) +const PULLINGSTRQ_1 = `WITH updatedPts AS ( + WITH pulledPts AS ( + SELECT id, project, kind + FROM jobs + WHERE status=$1` +const PULLINGSTRQ_2 = ` FOR UPDATE SKIP LOCKED) + UPDATE jobs pts + SET status=$2 + FROM pulledPts + WHERE pulledPts.id=pts.id AND pulledPts.project=pts.project AND pulledPts.kind=pts.kind + RETURNING pts.id, pts.project, pts.status, pts.metadata, pts.input, pts.output, pts.kind) + SELECT * + FROM updatedPts + ORDER BY id ASC;` +const LISTSTRQ_1 = `SELECT id, project, status, metadata, input, output, kind + FROM jobs + WHERE` + type DisneylandStorageConfig struct { DatabaseURI string `json:"db_uri"` } @@ -92,6 +111,7 @@ func (storage *DisneylandStorage) GetJob(id uint64) (*Job, error) { } return job, err } + func queryJobs(rows *sql.Rows) (*ListOfJobs, error) { ret := &ListOfJobs{Jobs: []*Job{}} var err error @@ -118,12 +138,56 @@ func queryJobs(rows *sql.Rows) (*ListOfJobs, error) { } func (storage *DisneylandStorage) ListJobs(howmany uint32, project string, kind string) (*ListOfJobs, error) { - strQuery := `SELECT id, project, status, metadata, input, output, kind - FROM jobs - WHERE project LIKE '%' || $1 || '%' AND kind LIKE '%' || $2 || '%' - LIMIT $3;` + tx, err := storage.db.Begin() + if err != nil { + return nil, err + } + var rows *sql.Rows + projectFlag := false + kindFlag := false + limitFlag := false + + strQuery := LISTSTRQ_1 + inc := 1 + if project != "" { + strQuery += " project=$" + strQuery += strconv.Itoa(inc) + inc++ + projectFlag = true + } + if kind != "" { + if projectFlag { + strQuery += " AND kind=$" + } else { + strQuery += " kind=$" + } + strQuery += strconv.Itoa(inc) + inc++ + kindFlag = true + } + if howmany != 0 { + strQuery += " LIMIT $" + strQuery += strconv.Itoa(inc) + inc++ + limitFlag = true + } + strQuery += `;` - rows, err := storage.db.Query(strQuery, project, kind, howmany) + if projectFlag { + if kindFlag { + if limitFlag { + rows, err = tx.Query(strQuery, project, kind, howmany) + } else { + rows, err = tx.Query(strQuery, project, kind) + } + } else if limitFlag { + rows, err = tx.Query(strQuery, project, howmany) + } else { + rows, err = tx.Query(strQuery, project) + } + } else if limitFlag { + rows, err = tx.Query(strQuery, kind, howmany) + } if err != nil { return nil, err @@ -180,32 +244,58 @@ func (storage *DisneylandStorage) UpdateJob(job *Job) (*Job, error) { return resultJob, err } -func (storage *DisneylandStorage) PullJobs(how_many uint32, project string, kind string) (*ListOfJobs, error) { +func (storage *DisneylandStorage) PullJobs(howmany uint32, project string, kind string) (*ListOfJobs, error) { tx, err := storage.db.Begin() if err != nil { return nil, err } - strQuery := `WITH updatedPts AS ( - WITH pulledPts AS ( - SELECT id, project, kind - FROM jobs - WHERE status=$1 AND project LIKE '%' || $2 || '%' AND kind LIKE '%' || $3 || '%' - LIMIT $4 - FOR UPDATE SKIP LOCKED) - UPDATE jobs pts - SET status=$5 - FROM pulledPts - WHERE pulledPts.id=pts.id AND pulledPts.project=pts.project AND pulledPts.kind=pts.kind - RETURNING pts.id, pts.project, pts.status, pts.metadata, pts.input, pts.output, pts.kind) - SELECT * - FROM updatedPts - ORDER BY id ASC;` + var rows *sql.Rows + projectFlag := false + kindFlag := false + limitFlag := false + + strQuery := PULLINGSTRQ_1 + inc := 3 + if project != "" { + strQuery += " AND project=$" + strQuery += strconv.Itoa(inc) + inc++ + projectFlag = true + } + if kind != "" { + strQuery += " AND kind=$" + strQuery += strconv.Itoa(inc) + inc++ + kindFlag = true + } + if howmany != 0 { + strQuery += " LIMIT $" + strQuery += strconv.Itoa(inc) + inc++ + limitFlag = true + } + strQuery += PULLINGSTRQ_2 - rows, err := tx.Query(strQuery, Job_PENDING, project, kind, how_many, Job_PULLED) + if projectFlag { + if kindFlag { + if limitFlag { + rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, kind, howmany) + } else { + rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, kind) + } + } else if limitFlag { + rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, howmany) + } else { + rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project) + } + } else if limitFlag { + rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, kind, howmany) + } if err != nil { return nil, err } + ret, err := queryJobs(rows) if err != nil { return nil, err diff --git a/disneyland/user.go b/disneyland/user.go index 7a0bc88..1cf7e51 100644 --- a/disneyland/user.go +++ b/disneyland/user.go @@ -8,7 +8,7 @@ type User struct { func (u *User) IsUser() bool { // if user - if u.ProjectAccess != "ANY" && u.KindAccess == "ANY" { + if u.ProjectAccess != "ANY" { return true } return false