From 034e344671cd60668e4bb5ee424daa52f7b6e49f Mon Sep 17 00:00:00 2001 From: Alexander Rakoczy Date: Thu, 24 Sep 2020 14:01:35 -0400 Subject: [PATCH] cmd/relui: publish task start message to pubsub This change adds a new handler for starting a task by sending a message to pubsub. A test pubsub server is used in unit testing. The exact message used to start a task, as well as updating a task to mark it as started, is still under design and likely to change dramatically. This change is intended to unblock worker development. For golang/go#40279 Co-authored-by: Carlos Amedee Change-Id: I59bb5c5261a9a5d17e52597c1835a2a980cf91f8 Reviewed-on: https://go-review.googlesource.com/c/build/+/257239 Trust: Alexander Rakoczy Run-TryBot: Alexander Rakoczy TryBot-Result: Go Bot Reviewed-by: Dmitri Shuralyov --- cmd/relui/main.go | 1 + cmd/relui/protos/relui.pb.go | 128 +++++++++++++++++++++++-------- cmd/relui/protos/relui.proto | 12 +++ cmd/relui/store.go | 28 ++++++- cmd/relui/web.go | 22 ++++++ cmd/relui/web_test.go | 143 +++++++++++++++++++++++++++++++++++ 6 files changed, 300 insertions(+), 34 deletions(-) diff --git a/cmd/relui/main.go b/cmd/relui/main.go index 420b9f38f6..4e6e03c2cb 100644 --- a/cmd/relui/main.go +++ b/cmd/relui/main.go @@ -41,6 +41,7 @@ func main() { } http.Handle("/workflows/create", http.HandlerFunc(s.createWorkflowHandler)) http.Handle("/workflows/new", http.HandlerFunc(s.newWorkflowHandler)) + http.Handle("/tasks/start", http.HandlerFunc(s.startTaskHandler)) http.Handle("/", fileServerHandler(relativeFile("./static"), http.HandlerFunc(s.homeHandler))) port := os.Getenv("PORT") if port == "" { diff --git a/cmd/relui/protos/relui.pb.go b/cmd/relui/protos/relui.pb.go index 27d41e5b20..e85ff437d5 100644 --- a/cmd/relui/protos/relui.pb.go +++ b/cmd/relui/protos/relui.pb.go @@ -56,7 +56,7 @@ type Workflow struct { BuildableTasks []*BuildableTask `protobuf:"bytes,2,rep,name=buildable_tasks,json=buildableTasks,proto3" json:"buildable_tasks,omitempty"` // params are parameters provided when creating a workflow. Params map[string]string `protobuf:"bytes,3,rep,name=params,proto3" json:"params,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` - // id is a unique identifier generated by relui when a workflow is created. Do not set. + // id is a unique identifier generated by relui when a workflow is created. Id string `protobuf:"bytes,4,opt,name=id,proto3" json:"id,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -132,7 +132,7 @@ type BuildableTask struct { // task_type is a unique type for a task, such as FetchGerritSource. Types are used by task runners to identify // how to execute a task. TaskType string `protobuf:"bytes,6,opt,name=task_type,json=taskType,proto3" json:"task_type,omitempty"` - // id is a unique identifier generated by relui when a buildable task is created. Do not set. + // id is a unique identifier generated by relui when a buildable task is created. Id string `protobuf:"bytes,7,opt,name=id,proto3" json:"id,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -213,6 +213,65 @@ func (m *BuildableTask) GetId() string { return "" } +// StartBuildableTaskRequest is a message sent to workers to start working on a BuildableTask for a Workflow. +type StartBuildableTaskRequest struct { + // workflow_id is the workflow to which the BuildableTask belongs. + WorkflowId string `protobuf:"bytes,1,opt,name=workflow_id,json=workflowId,proto3" json:"workflow_id,omitempty"` + // buildable_task_id is the id of the BuildableTask to be started. + BuildableTaskId string `protobuf:"bytes,2,opt,name=buildable_task_id,json=buildableTaskId,proto3" json:"buildable_task_id,omitempty"` + // buildable_task_type is the type of the BuildableTask to be started. + BuildableTaskType string `protobuf:"bytes,3,opt,name=buildable_task_type,json=buildableTaskType,proto3" json:"buildable_task_type,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *StartBuildableTaskRequest) Reset() { *m = StartBuildableTaskRequest{} } +func (m *StartBuildableTaskRequest) String() string { return proto.CompactTextString(m) } +func (*StartBuildableTaskRequest) ProtoMessage() {} +func (*StartBuildableTaskRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_6de8859f82adce0a, []int{2} +} + +func (m *StartBuildableTaskRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_StartBuildableTaskRequest.Unmarshal(m, b) +} +func (m *StartBuildableTaskRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_StartBuildableTaskRequest.Marshal(b, m, deterministic) +} +func (m *StartBuildableTaskRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_StartBuildableTaskRequest.Merge(m, src) +} +func (m *StartBuildableTaskRequest) XXX_Size() int { + return xxx_messageInfo_StartBuildableTaskRequest.Size(m) +} +func (m *StartBuildableTaskRequest) XXX_DiscardUnknown() { + xxx_messageInfo_StartBuildableTaskRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_StartBuildableTaskRequest proto.InternalMessageInfo + +func (m *StartBuildableTaskRequest) GetWorkflowId() string { + if m != nil { + return m.WorkflowId + } + return "" +} + +func (m *StartBuildableTaskRequest) GetBuildableTaskId() string { + if m != nil { + return m.BuildableTaskId + } + return "" +} + +func (m *StartBuildableTaskRequest) GetBuildableTaskType() string { + if m != nil { + return m.BuildableTaskType + } + return "" +} + // LocalStorage is the persisted data of relui. It is used in development mode for saving application state. type LocalStorage struct { // workflows are a list of user-created workflows. @@ -226,7 +285,7 @@ func (m *LocalStorage) Reset() { *m = LocalStorage{} } func (m *LocalStorage) String() string { return proto.CompactTextString(m) } func (*LocalStorage) ProtoMessage() {} func (*LocalStorage) Descriptor() ([]byte, []int) { - return fileDescriptor_6de8859f82adce0a, []int{2} + return fileDescriptor_6de8859f82adce0a, []int{3} } func (m *LocalStorage) XXX_Unmarshal(b []byte) error { @@ -266,7 +325,7 @@ func (m *GitSource) Reset() { *m = GitSource{} } func (m *GitSource) String() string { return proto.CompactTextString(m) } func (*GitSource) ProtoMessage() {} func (*GitSource) Descriptor() ([]byte, []int) { - return fileDescriptor_6de8859f82adce0a, []int{3} + return fileDescriptor_6de8859f82adce0a, []int{4} } func (m *GitSource) XXX_Unmarshal(b []byte) error { @@ -306,6 +365,7 @@ func init() { proto.RegisterType((*Workflow)(nil), "protos.Workflow") proto.RegisterMapType((map[string]string)(nil), "protos.Workflow.ParamsEntry") proto.RegisterType((*BuildableTask)(nil), "protos.BuildableTask") + proto.RegisterType((*StartBuildableTaskRequest)(nil), "protos.StartBuildableTaskRequest") proto.RegisterType((*LocalStorage)(nil), "protos.LocalStorage") proto.RegisterType((*GitSource)(nil), "protos.GitSource") } @@ -313,32 +373,36 @@ func init() { func init() { proto.RegisterFile("relui.proto", fileDescriptor_6de8859f82adce0a) } var fileDescriptor_6de8859f82adce0a = []byte{ - // 426 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x52, 0x4d, 0x8f, 0xd3, 0x30, - 0x10, 0xc5, 0xe9, 0x6e, 0xd9, 0x4c, 0x96, 0x52, 0x0c, 0x08, 0x8b, 0x0f, 0xa9, 0xf4, 0x54, 0xed, - 0xa1, 0xa0, 0xc2, 0x01, 0x38, 0xac, 0x54, 0xa0, 0xe2, 0xb0, 0xa8, 0x8b, 0x9c, 0x54, 0x3d, 0x46, - 0x6e, 0xe3, 0x56, 0x51, 0xbd, 0x71, 0x64, 0x3b, 0xac, 0xf2, 0x6b, 0xf9, 0x0b, 0xfc, 0x04, 0x64, - 0xd7, 0x66, 0x4b, 0xc5, 0x29, 0xe3, 0xf7, 0xc6, 0x2f, 0xf3, 0xde, 0x18, 0x12, 0xc5, 0x45, 0x53, - 0x8e, 0x6b, 0x25, 0x8d, 0xc4, 0x5d, 0xf7, 0xd1, 0xc3, 0x5f, 0x08, 0xce, 0x96, 0x52, 0xed, 0x36, - 0x42, 0xde, 0x62, 0x0c, 0x27, 0x15, 0xbb, 0xe1, 0x04, 0x0d, 0xd0, 0x28, 0xa6, 0xae, 0xc6, 0x97, - 0xf0, 0x70, 0xd5, 0x94, 0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0x05, 0xc4, 0x36, 0xfc, 0xdc, 0xb4, 0x35, - 0x27, 0x5d, 0xa7, 0x78, 0x66, 0x81, 0xac, 0xad, 0xb9, 0x4f, 0xeb, 0x7e, 0x48, 0x6b, 0x78, 0x09, - 0xe7, 0xdf, 0xe5, 0x9a, 0x89, 0xd4, 0x48, 0xc5, 0xb6, 0x1c, 0x8f, 0x21, 0xbe, 0xf5, 0x69, 0x6b, - 0x82, 0xdc, 0x1a, 0xfa, 0xc7, 0x6b, 0xa0, 0x77, 0x2d, 0xc3, 0x37, 0x10, 0xff, 0x1d, 0xc2, 0x66, - 0x6d, 0x5d, 0xf8, 0xac, 0x1b, 0x25, 0x2c, 0xa2, 0xf8, 0xc6, 0x87, 0x64, 0xcb, 0x8b, 0x25, 0xc0, - 0x5d, 0x10, 0xf8, 0x19, 0x3c, 0xce, 0xa6, 0xe9, 0x55, 0x9e, 0x66, 0xd3, 0x6c, 0x91, 0xe6, 0x8b, - 0xf9, 0xd5, 0xfc, 0x7a, 0x39, 0xef, 0xdf, 0x3b, 0x26, 0xbe, 0xd0, 0xd9, 0x34, 0x9b, 0x7d, 0xed, - 0xa3, 0x63, 0x22, 0xcd, 0xa6, 0xd4, 0x12, 0xd1, 0x6a, 0xff, 0x4c, 0xdf, 0xfd, 0x09, 0x00, 0x00, - 0xff, 0xff, 0xa2, 0xed, 0x2b, 0x74, 0xbc, 0x02, 0x00, 0x00, + // 486 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x53, 0xdd, 0x6e, 0xd3, 0x30, + 0x14, 0xc6, 0xe9, 0x56, 0x96, 0x93, 0xd1, 0xb5, 0x1e, 0x88, 0xf0, 0x27, 0x4a, 0xae, 0xaa, 0x5e, + 0x04, 0x54, 0xb8, 0x00, 0x2e, 0x26, 0x15, 0xa8, 0xd0, 0x34, 0xd4, 0xa1, 0x24, 0x55, 0x2f, 0x23, + 0xb7,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x35, 0x50, 0x06, 0x9b, 0x35, 0xa8, + 0x39, 0x53, 0x19, 0x4e, 0x57, 0x46, 0xac, 0x0a, 0x87, 0x73, 0x80, 0xbb, 0x19, 0xe1, 0xc7, 0x70, + 0x9e, 0x8c, 0xe3, 0xab, 0x34, 0x4e, 0xc6, 0xc9, 0x2c, 0x4e, 0x67, 0xd3, 0xab, 0xe9, 0xf5, 0x7c, + 0xda, 0xbd, 0x77, 0x08, 0x7c, 0x8e, 0x26, 0xe3, 0x64, 0xf2, 0xa5, 0x8b, 0x0e, 0x81, 0x38, 0x19, + 0x47, 0x0a, 0x70, 0x16, 0xbb, 0x3f, 0xe8, 0xed, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd3, 0xc2, + 0xf2, 0x91, 0x57, 0x03, 0x00, 0x00, } diff --git a/cmd/relui/protos/relui.proto b/cmd/relui/protos/relui.proto index 7bad5db8fc..49f3df6791 100644 --- a/cmd/relui/protos/relui.proto +++ b/cmd/relui/protos/relui.proto @@ -47,6 +47,18 @@ message BuildableTask { string id = 7; } +// StartBuildableTaskRequest is a message sent to workers to start working on a BuildableTask for a Workflow. +message StartBuildableTaskRequest { + // workflow_id is the workflow to which the BuildableTask belongs. + string workflow_id = 1; + + // buildable_task_id is the id of the BuildableTask to be started. + string buildable_task_id = 2; + + // buildable_task_type is the type of the BuildableTask to be started. + string buildable_task_type = 3; +} + // LocalStorage is the persisted data of relui. It is used in development mode for saving application state. message LocalStorage { // workflows are a list of user-created workflows. diff --git a/cmd/relui/store.go b/cmd/relui/store.go index acb2a4c43a..8dc838c07b 100644 --- a/cmd/relui/store.go +++ b/cmd/relui/store.go @@ -17,8 +17,10 @@ import ( // store is a persistence adapter for saving data. type store interface { - Workflows() []*reluipb.Workflow AddWorkflow(workflow *reluipb.Workflow) error + BuildableTask(workflowId, id string) *reluipb.BuildableTask + Workflow(id string) *reluipb.Workflow + Workflows() []*reluipb.Workflow } var _ store = (*fileStore)(nil) @@ -57,11 +59,33 @@ func (f *fileStore) AddWorkflow(w *reluipb.Workflow) error { return nil } -// Workflows returns all workflows stored. +// Workflows returns all reluipb.Workflows stored. func (f *fileStore) Workflows() []*reluipb.Workflow { return f.localStorage().GetWorkflows() } +// Workflow returns a single reluipb.Workflow found by its id. If it is not found, it returns nil. +func (f *fileStore) Workflow(id string) *reluipb.Workflow { + for _, w := range f.Workflows() { + if w.GetId() == id { + return w + } + } + return nil +} + +// BuildableTask returns a single reluipb.BuildableTask found by the reluipb.Workflow id and its id. +// If it is not found, it returns nil. +func (f *fileStore) BuildableTask(workflowId, id string) *reluipb.BuildableTask { + wf := f.Workflow(workflowId) + for _, t := range wf.GetBuildableTasks() { + if t.GetId() == id { + return t + } + } + return nil +} + // localStorage returns a deep copy of data stored in fileStore. func (f *fileStore) localStorage() *reluipb.LocalStorage { f.mu.Lock() diff --git a/cmd/relui/web.go b/cmd/relui/web.go index 4915253f18..3a9b7fd9b7 100644 --- a/cmd/relui/web.go +++ b/cmd/relui/web.go @@ -122,6 +122,28 @@ func (s *server) createWorkflowHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusSeeOther) } +func (s *server) startTaskHandler(w http.ResponseWriter, r *http.Request) { + wf := s.store.Workflow(r.PostFormValue("workflow.id")) + bt := s.store.BuildableTask(r.PostFormValue("workflow.id"), r.PostFormValue("task.id")) + if bt == nil { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + res := s.topic.Publish(r.Context(), &pubsub.Message{ + Data: []byte((&reluipb.StartBuildableTaskRequest{ + WorkflowId: wf.GetId(), + BuildableTaskId: bt.GetId(), + BuildableTaskType: bt.GetTaskType(), + }).String()), + }) + if _, err := res.Get(r.Context()); err != nil { + log.Printf("Error publishing task start: %v", err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/", http.StatusSeeOther) +} + // relativeFile returns the path to the provided file or directory, // conditionally prepending a relative path depending on the environment. // diff --git a/cmd/relui/web_test.go b/cmd/relui/web_test.go index 7cab6e962d..54991d5cfe 100644 --- a/cmd/relui/web_test.go +++ b/cmd/relui/web_test.go @@ -5,6 +5,7 @@ package main import ( + "context" "io/ioutil" "net/http" "net/http/httptest" @@ -12,8 +13,12 @@ import ( "strings" "testing" + "cloud.google.com/go/pubsub" + "cloud.google.com/go/pubsub/pstest" "github.com/google/go-cmp/cmp" reluipb "golang.org/x/build/cmd/relui/protos" + "google.golang.org/api/option" + "google.golang.org/grpc" ) func TestFileServerHandler(t *testing.T) { @@ -171,3 +176,141 @@ func TestServerCreateWorkflowHandler(t *testing.T) { }) } } + +// newPSTest creates a new pstest.Server and returns a pubsub.Client connected to it and the server. +// +// cleanup will close the client, connection, and test server. +func newPSTest(ctx context.Context, t *testing.T) (c *pubsub.Client, s *pstest.Server, cleanup func()) { + t.Helper() + s = pstest.NewServer() + conn, err := grpc.DialContext(ctx, s.Addr, grpc.WithInsecure()) + if err != nil { + s.Close() + t.Fatalf("grpc.DialContext(_, %q, %v) = _, %v, wanted no error", s.Addr, grpc.WithInsecure(), err) + } + c, err = pubsub.NewClient(ctx, "relui-test", option.WithGRPCConn(conn)) + if err != nil { + s.Close() + conn.Close() + t.Fatalf("pubsub.NewClient(_, %q, %v) = _, %v, wanted no error", "relui-test", option.WithGRPCConn(conn), err) + } + return c, s, func() { + c.Close() + conn.Close() + s.Close() + } +} + +func TestServerStartTaskHandler(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + client, pssrv, cleanup := newPSTest(ctx, t) + defer cleanup() + topic, err := client.CreateTopic(ctx, "relui-test-topic") + if err != nil { + t.Fatalf("client.CreateTopic(_, %q) = _, %v", "relui-test-topic", err) + } + + s := server{store: newFileStore(""), topic: topic} + wf := &reluipb.Workflow{ + Id: "someworkflow", + Name: "test_workflow", + BuildableTasks: []*reluipb.BuildableTask{{ + Name: "test_task", + TaskType: "TestTask", + Id: "sometask", + }}, + Params: map[string]string{"GitObject": "master"}, + } + if s.store.AddWorkflow(wf) != nil { + t.Fatalf("store.AddWorkflow(%v) = %v, wanted no error", wf, err) + } + want := &reluipb.StartBuildableTaskRequest{ + WorkflowId: "someworkflow", + BuildableTaskId: "sometask", + BuildableTaskType: "TestTask", + } + params := url.Values{"workflow.id": []string{"someworkflow"}, "task.id": []string{"sometask"}} + req := httptest.NewRequest(http.MethodPost, "/tasks/start", strings.NewReader(params.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + w := httptest.NewRecorder() + s.startTaskHandler(w, req) + resp := w.Result() + + if resp.StatusCode != http.StatusSeeOther { + t.Errorf("resp.StatusCode = %d, wanted %d", resp.StatusCode, http.StatusSeeOther) + } + if resp.Header.Get("Location") != "/" { + t.Errorf("resp.Header.Get(%q) = %q, wanted %q", "Location", resp.Header.Get("Location"), "/") + } + if len(pssrv.Messages()) != 1 { + t.Fatalf("len(pssrv.Messages()) = %d, wanted %d", len(pssrv.Messages()), 1) + } + msg := pssrv.Messages()[0] + if string(msg.Data) != want.String() { + t.Errorf("msg.Data = %q, wanted %q", string(msg.Data), "hello world") + } +} + +func TestStartTaskHandlerErrors(t *testing.T) { + wf := &reluipb.Workflow{ + Id: "someworkflow", + Name: "test_workflow", + BuildableTasks: []*reluipb.BuildableTask{{ + Name: "test_task", + TaskType: "TestTask", + Id: "sometask", + }}, + Params: map[string]string{"GitObject": "master"}, + } + + cases := []struct { + desc string + params url.Values + wantCode int + }{ + { + desc: "task not found", + params: url.Values{"workflow.id": []string{"someworkflow"}, "task.id": []string{"notexist"}}, + wantCode: http.StatusNotFound, + }, + { + desc: "pubsub publish failure", + params: url.Values{"workflow.id": []string{"someworkflow"}, "task.id": []string{"sometask"}}, + wantCode: http.StatusInternalServerError, + }, + } + for _, c := range cases { + t.Run(c.desc, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + client, pssrv, cleanup := newPSTest(ctx, t) + defer cleanup() + topic, err := client.CreateTopic(ctx, "relui-test-topic") + if err != nil { + t.Fatalf("client.CreateTopic(_, %q) = _, %v", "relui-test-topic", err) + } + // Simulate pubsub failure by stopping publishing. + topic.Stop() + + s := server{store: newFileStore(""), topic: topic} + if s.store.AddWorkflow(wf) != nil { + t.Fatalf("store.AddWorkflow(%v) = %v, wanted no error", wf, err) + } + req := httptest.NewRequest(http.MethodPost, "/tasks/start", strings.NewReader(c.params.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + w := httptest.NewRecorder() + s.startTaskHandler(w, req) + resp := w.Result() + + if resp.StatusCode != c.wantCode { + t.Errorf("resp.StatusCode = %d, wanted %d", resp.StatusCode, c.wantCode) + } + if len(pssrv.Messages()) != 0 { + t.Fatalf("len(pssrv.Messages()) = %d, wanted %d", len(pssrv.Messages()), 0) + } + }) + } +}