From 0ddaca889cf8b3c00375183ebbfd952ec12e1bad Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Mon, 9 Dec 2024 12:46:21 +0800 Subject: [PATCH] refactor(blockbuilder): transport job completion reports success (#15313) --- pkg/blockbuilder/builder/builder.go | 16 +-- pkg/blockbuilder/builder/worker.go | 5 +- pkg/blockbuilder/scheduler/scheduler_test.go | 6 +- pkg/blockbuilder/types/grpc_transport.go | 6 +- pkg/blockbuilder/types/interfaces.go | 8 +- .../types/proto/blockbuilder.pb.go | 98 ++++++++++--------- .../types/proto/blockbuilder.proto | 2 +- 7 files changed, 76 insertions(+), 65 deletions(-) diff --git a/pkg/blockbuilder/builder/builder.go b/pkg/blockbuilder/builder/builder.go index aaeaad0d864fb..d8784bf568d99 100644 --- a/pkg/blockbuilder/builder/builder.go +++ b/pkg/blockbuilder/builder/builder.go @@ -260,17 +260,21 @@ func (i *BlockBuilder) runOne(ctx context.Context, workerID string) (bool, error i.metrics.inflightJobs.Set(float64(len(i.inflightJobs))) i.jobsMtx.Unlock() - lastConsumedOffset, err := i.processJob(ctx, job, logger) + completion := &types.CompleteJobRequest{ + BuilderID: workerID, + Job: job, + Success: true, + } + if _, err = i.processJob(ctx, job, logger); err != nil { + level.Error(i.logger).Log("msg", "failed to process job", "err", err) + completion.Success = false + } if _, err := withBackoff( ctx, i.cfg.Backoff, func() (res struct{}, err error) { - if err = i.SendCompleteJob(ctx, &types.CompleteJobRequest{ - BuilderID: workerID, - Job: job, - LastConsumedOffset: lastConsumedOffset, - }); err != nil { + if err = i.SendCompleteJob(ctx, completion); err != nil { level.Error(i.logger).Log("msg", "failed to mark the job as complete", "err", err) } return diff --git a/pkg/blockbuilder/builder/worker.go b/pkg/blockbuilder/builder/worker.go index 41dc0cb7563ec..4b5f36d6492b9 100644 --- a/pkg/blockbuilder/builder/worker.go +++ b/pkg/blockbuilder/builder/worker.go @@ -18,7 +18,7 @@ func (u unimplementedWorker) GetJob(_ context.Context) (*types.Job, bool, error) panic("unimplemented") } -func (u unimplementedWorker) CompleteJob(_ context.Context, _ *types.Job) error { +func (u unimplementedWorker) CompleteJob(_ context.Context, _ *types.Job, _ bool) error { panic("unimplemented") } @@ -51,10 +51,11 @@ func (w *Worker) GetJob(ctx context.Context) (*types.Job, bool, error) { return resp.Job, resp.OK, nil } -func (w *Worker) CompleteJob(ctx context.Context, job *types.Job) error { +func (w *Worker) CompleteJob(ctx context.Context, job *types.Job, success bool) error { return w.transport.SendCompleteJob(ctx, &types.CompleteJobRequest{ BuilderID: w.builderID, Job: job, + Success: success, }) } diff --git a/pkg/blockbuilder/scheduler/scheduler_test.go b/pkg/blockbuilder/scheduler/scheduler_test.go index 977dd1d556d1e..201b56eeb9786 100644 --- a/pkg/blockbuilder/scheduler/scheduler_test.go +++ b/pkg/blockbuilder/scheduler/scheduler_test.go @@ -57,7 +57,7 @@ func TestScheduleAndProcessJob(t *testing.T) { } // Builder completes job - err = env.builder.CompleteJob(ctx, receivedJob) + err = env.builder.CompleteJob(ctx, receivedJob, true) if err != nil { t.Fatalf("failed to complete job: %v", err) } @@ -130,12 +130,12 @@ func TestMultipleBuilders(t *testing.T) { } // Complete jobs - err = env1.builder.CompleteJob(ctx, receivedJob1) + err = env1.builder.CompleteJob(ctx, receivedJob1, true) if err != nil { t.Fatalf("builder1 failed to complete job: %v", err) } - err = builder2.CompleteJob(ctx, receivedJob2) + err = builder2.CompleteJob(ctx, receivedJob2, true) if err != nil { t.Fatalf("builder2 failed to complete job: %v", err) } diff --git a/pkg/blockbuilder/types/grpc_transport.go b/pkg/blockbuilder/types/grpc_transport.go index b083cd76bb37a..a53f06282e895 100644 --- a/pkg/blockbuilder/types/grpc_transport.go +++ b/pkg/blockbuilder/types/grpc_transport.go @@ -84,9 +84,9 @@ func (t *GRPCTransport) SendGetJobRequest(ctx context.Context, req *GetJobReques // SendCompleteJob implements Transport func (t *GRPCTransport) SendCompleteJob(ctx context.Context, req *CompleteJobRequest) error { protoReq := &proto.CompleteJobRequest{ - BuilderId: req.BuilderID, - Job: jobToProto(req.Job), - LastConsumedOffset: req.LastConsumedOffset, + BuilderId: req.BuilderID, + Job: jobToProto(req.Job), + Success: req.Success, } _, err := t.CompleteJob(ctx, protoReq) diff --git a/pkg/blockbuilder/types/interfaces.go b/pkg/blockbuilder/types/interfaces.go index e3e4a8476e3ca..f5746dea5fbc2 100644 --- a/pkg/blockbuilder/types/interfaces.go +++ b/pkg/blockbuilder/types/interfaces.go @@ -7,7 +7,7 @@ type Worker interface { // GetJob requests a new job from the scheduler GetJob(ctx context.Context) (*Job, bool, error) // CompleteJob marks a job as finished - CompleteJob(ctx context.Context, job *Job) error + CompleteJob(ctx context.Context, job *Job, success bool) error // SyncJob informs the scheduler about an in-progress job SyncJob(ctx context.Context, job *Job) error } @@ -52,9 +52,9 @@ type GetJobResponse struct { } type CompleteJobRequest struct { - BuilderID string - Job *Job - LastConsumedOffset int64 + BuilderID string + Job *Job + Success bool } type SyncJobRequest struct { diff --git a/pkg/blockbuilder/types/proto/blockbuilder.pb.go b/pkg/blockbuilder/types/proto/blockbuilder.pb.go index b6a743a4b571f..071812c2fa189 100644 --- a/pkg/blockbuilder/types/proto/blockbuilder.pb.go +++ b/pkg/blockbuilder/types/proto/blockbuilder.pb.go @@ -126,9 +126,9 @@ func (m *GetJobResponse) GetOk() bool { // CompleteJobRequest represents a job completion notification type CompleteJobRequest struct { - BuilderId string `protobuf:"bytes,1,opt,name=builder_id,json=builderId,proto3" json:"builder_id,omitempty"` - Job *Job `protobuf:"bytes,2,opt,name=job,proto3" json:"job,omitempty"` - LastConsumedOffset int64 `protobuf:"varint,3,opt,name=LastConsumedOffset,proto3" json:"LastConsumedOffset,omitempty"` + BuilderId string `protobuf:"bytes,1,opt,name=builder_id,json=builderId,proto3" json:"builder_id,omitempty"` + Job *Job `protobuf:"bytes,2,opt,name=job,proto3" json:"job,omitempty"` + Success bool `protobuf:"varint,3,opt,name=success,proto3" json:"success,omitempty"` } func (m *CompleteJobRequest) Reset() { *m = CompleteJobRequest{} } @@ -177,11 +177,11 @@ func (m *CompleteJobRequest) GetJob() *Job { return nil } -func (m *CompleteJobRequest) GetLastConsumedOffset() int64 { +func (m *CompleteJobRequest) GetSuccess() bool { if m != nil { - return m.LastConsumedOffset + return m.Success } - return 0 + return false } // CompleteJobResponse is an empty response for job completion @@ -436,36 +436,36 @@ func init() { } var fileDescriptor_04968622516f7b79 = []byte{ - // 464 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0x31, 0x8e, 0xd3, 0x40, - 0x14, 0x86, 0x3d, 0xb6, 0xd8, 0x90, 0x17, 0x11, 0x60, 0x56, 0x88, 0x28, 0xc0, 0x28, 0x0c, 0x12, - 0x84, 0x02, 0x5b, 0x0a, 0x70, 0x81, 0x6c, 0x81, 0x58, 0x90, 0x10, 0x5e, 0xaa, 0x6d, 0xc0, 0x8e, - 0x27, 0x61, 0xe2, 0xc4, 0x63, 0x3c, 0x13, 0xb4, 0xdb, 0x71, 0x02, 0xc4, 0x11, 0x28, 0x39, 0x0a, - 0x65, 0xca, 0x2d, 0x89, 0xd3, 0x50, 0xee, 0x11, 0x90, 0xc7, 0x76, 0xc0, 0x5a, 0x2b, 0xa4, 0xd9, - 0xca, 0xd6, 0x3f, 0x9f, 0xdf, 0xff, 0xfb, 0xbd, 0x37, 0xe0, 0xc4, 0xe1, 0xc4, 0xf1, 0x67, 0x62, - 0x14, 0xfa, 0x0b, 0x3e, 0x0b, 0x58, 0xe2, 0xa8, 0xd3, 0x98, 0x49, 0x27, 0x4e, 0x84, 0x12, 0x95, - 0x03, 0x5b, 0x4b, 0x18, 0x57, 0x34, 0x0d, 0x53, 0x1b, 0xae, 0xbd, 0x60, 0xea, 0x50, 0xf8, 0x2e, - 0xfb, 0xb4, 0x60, 0x52, 0xe1, 0x7b, 0x00, 0x05, 0xf1, 0x9e, 0x07, 0x1d, 0xd4, 0x43, 0xf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b, 0x3e, 0xf3, 0x11, 0xc3, 0x6f, 0x61, 0x2f, 0x5f, 0x0b, 0x7c, 0xbf, - 0xae, 0x4e, 0x65, 0xc5, 0xba, 0x74, 0x1b, 0x52, 0xf4, 0xc0, 0xc0, 0x1f, 0xa0, 0xf5, 0xcf, 0x2c, - 0xf0, 0xc3, 0xba, 0x8f, 0x2e, 0x2e, 0x4f, 0xf7, 0xd1, 0x7f, 0xb9, 0x8d, 0xc3, 0x3b, 0x68, 0x14, - 0xad, 0xc7, 0xb5, 0x91, 0xaa, 0x33, 0xef, 0x3e, 0xd8, 0xca, 0x94, 0x55, 0x87, 0xd3, 0xe5, 0x8a, - 0x18, 0x67, 0x2b, 0x62, 0x9c, 0xaf, 0x08, 0xfa, 0x92, 0x12, 0xf4, 0x23, 0x25, 0xe8, 0x67, 0x4a, - 0xd0, 0x32, 0x25, 0xe8, 0x57, 0x4a, 0xd0, 0xef, 0x94, 0x18, 0xe7, 0x29, 0x41, 0xdf, 0xd6, 0xc4, - 0x58, 0xae, 0x89, 0x71, 0xb6, 0x26, 0xc6, 0xf1, 0xb3, 0x09, 0x57, 0x1f, 0x17, 0xbe, 0x3d, 0x12, - 0x73, 0x67, 0x92, 0x78, 0x63, 0x2f, 0xf2, 0x9c, 0x99, 0x08, 0xf9, 0xd6, 0x4b, 0xee, 0xef, 0xe9, - 0xc7, 0xd3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x20, 0x0d, 0x30, 0x7c, 0x0b, 0x04, 0x00, 0x00, + // 453 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xc1, 0x8e, 0x93, 0x40, + 0x18, 0xc7, 0x19, 0x88, 0x5b, 0xfb, 0x35, 0x56, 0x9d, 0x8d, 0xb1, 0xa9, 0x3a, 0x59, 0xc7, 0x44, + 0xd7, 0x83, 0x90, 0x54, 0x7d, 0x81, 0x7a, 0x30,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13, 0xf4, 0x75, 0x43, 0x8c, 0xd5, 0x86, 0x18, 0x67, 0x1b, 0x62, 0x9c, 0x3c, 0x9b, 0x71, + 0xf5, 0x31, 0xf3, 0xed, 0x40, 0x2c, 0x9c, 0x59, 0xea, 0x4d, 0xbd, 0xd8, 0x73, 0x22, 0x11, 0xf2, + 0x9d, 0xa7, 0xd9, 0xdf, 0xd3, 0x97, 0xa7, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xbf, 0xda, 0xc9, + 0x32, 0xf4, 0x03, 0x00, 0x00, } func (this *GetJobRequest) Equal(that interface{}) bool { @@ -544,7 +544,7 @@ func (this *CompleteJobRequest) Equal(that interface{}) bool { if !this.Job.Equal(that1.Job) { return false } - if this.LastConsumedOffset != that1.LastConsumedOffset { + if this.Success != that1.Success { return false } return true @@ -708,7 +708,7 @@ func (this *CompleteJobRequest) GoString() string { if this.Job != nil { s = append(s, "Job: "+fmt.Sprintf("%#v", this.Job)+",\n") } - s = append(s, "LastConsumedOffset: "+fmt.Sprintf("%#v", this.LastConsumedOffset)+",\n") + s = append(s, "Success: "+fmt.Sprintf("%#v", this.Success)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -1030,8 +1030,13 @@ func (m *CompleteJobRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if m.LastConsumedOffset != 0 { - i = encodeVarintBlockbuilder(dAtA, i, uint64(m.LastConsumedOffset)) + if m.Success { + i-- + if m.Success { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } i-- dAtA[i] = 0x18 } @@ -1279,8 +1284,8 @@ func (m *CompleteJobRequest) Size() (n int) { l = m.Job.Size() n += 1 + l + sovBlockbuilder(uint64(l)) } - if m.LastConsumedOffset != 0 { - n += 1 + sovBlockbuilder(uint64(m.LastConsumedOffset)) + if m.Success { + n += 2 } return n } @@ -1389,7 +1394,7 @@ func (this *CompleteJobRequest) String() string { s := strings.Join([]string{`&CompleteJobRequest{`, `BuilderId:` + fmt.Sprintf("%v", this.BuilderId) + `,`, `Job:` + strings.Replace(this.Job.String(), "Job", "Job", 1) + `,`, - `LastConsumedOffset:` + fmt.Sprintf("%v", this.LastConsumedOffset) + `,`, + `Success:` + fmt.Sprintf("%v", this.Success) + `,`, `}`, }, "") return s @@ -1747,9 +1752,9 @@ func (m *CompleteJobRequest) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field LastConsumedOffset", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Success", wireType) } - m.LastConsumedOffset = 0 + var v int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowBlockbuilder @@ -1759,11 +1764,12 @@ func (m *CompleteJobRequest) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.LastConsumedOffset |= int64(b&0x7F) << shift + v |= int(b&0x7F) << shift if b < 0x80 { break } } + m.Success = bool(v != 0) default: iNdEx = preIndex skippy, err := skipBlockbuilder(dAtA[iNdEx:]) diff --git a/pkg/blockbuilder/types/proto/blockbuilder.proto b/pkg/blockbuilder/types/proto/blockbuilder.proto index 56160d336d336..a29b03dc6186f 100644 --- a/pkg/blockbuilder/types/proto/blockbuilder.proto +++ b/pkg/blockbuilder/types/proto/blockbuilder.proto @@ -29,7 +29,7 @@ message GetJobResponse { message CompleteJobRequest { string builder_id = 1; Job job = 2; - int64 LastConsumedOffset = 3; + bool success = 3; } // CompleteJobResponse is an empty response for job completion