-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(blockbuilder): grpc transport #15218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,147 @@ | ||||||
package types | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"flag" | ||||||
"io" | ||||||
|
||||||
"github.com/grafana/dskit/grpcclient" | ||||||
"github.com/grafana/dskit/instrument" | ||||||
"github.com/pkg/errors" | ||||||
"github.com/prometheus/client_golang/prometheus" | ||||||
"github.com/prometheus/client_golang/prometheus/promauto" | ||||||
"google.golang.org/grpc" | ||||||
"google.golang.org/grpc/health/grpc_health_v1" | ||||||
|
||||||
"github.com/grafana/loki/v3/pkg/blockbuilder/types/proto" | ||||||
"github.com/grafana/loki/v3/pkg/util/constants" | ||||||
) | ||||||
|
||||||
var _ Transport = &GRPCTransport{} | ||||||
|
||||||
type GRPCTransportConfig struct { | ||||||
Address string `yaml:"address,omitempty"` | ||||||
|
||||||
// GRPCClientConfig configures the gRPC connection between the Bloom Gateway client and the server. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
GRPCClientConfig grpcclient.Config `yaml:"grpc_client_config"` | ||||||
} | ||||||
|
||||||
func (cfg *GRPCTransportConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { | ||||||
f.StringVar(&cfg.Address, prefix+"address", "", "address in DNS Service Discovery format: https://grafana.com/docs/mimir/latest/configure/about-dns-service-discovery/#supported-discovery-modes") | ||||||
} | ||||||
|
||||||
type grpcTransportMetrics struct { | ||||||
requestLatency *prometheus.HistogramVec | ||||||
} | ||||||
|
||||||
func newGRPCTransportMetrics(registerer prometheus.Registerer) *grpcTransportMetrics { | ||||||
return &grpcTransportMetrics{ | ||||||
requestLatency: promauto.With(registerer).NewHistogramVec(prometheus.HistogramOpts{ | ||||||
Namespace: constants.Loki, | ||||||
Subsystem: "block_builder_grpc", | ||||||
Name: "request_duration_seconds", | ||||||
Help: "Time (in seconds) spent serving requests when using the block builder grpc transport", | ||||||
Buckets: instrument.DefBuckets, | ||||||
}, []string{"operation", "status_code"}), | ||||||
} | ||||||
} | ||||||
|
||||||
// GRPCTransport implements the Transport interface using gRPC | ||||||
type GRPCTransport struct { | ||||||
grpc_health_v1.HealthClient | ||||||
io.Closer | ||||||
proto.BlockBuilderServiceClient | ||||||
} | ||||||
|
||||||
// NewGRPCTransportFromAddress creates a new gRPC transport instance from an address and dial options | ||||||
func NewGRPCTransportFromAddress( | ||||||
metrics *grpcTransportMetrics, | ||||||
cfg GRPCTransportConfig, | ||||||
) (*GRPCTransport, error) { | ||||||
|
||||||
dialOpts, err := cfg.GRPCClientConfig.DialOption(grpcclient.Instrument(metrics.requestLatency)) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
// nolint:staticcheck // grpc.Dial() has been deprecated; we'll address it before upgrading to gRPC 2. | ||||||
conn, err := grpc.Dial(cfg.Address, dialOpts...) | ||||||
if err != nil { | ||||||
return nil, errors.Wrap(err, "new grpc pool dial") | ||||||
} | ||||||
|
||||||
return &GRPCTransport{ | ||||||
Closer: conn, | ||||||
HealthClient: grpc_health_v1.NewHealthClient(conn), | ||||||
BlockBuilderServiceClient: proto.NewBlockBuilderServiceClient(conn), | ||||||
}, nil | ||||||
} | ||||||
|
||||||
// SendGetJobRequest implements Transport | ||||||
func (t *GRPCTransport) SendGetJobRequest(ctx context.Context, req *GetJobRequest) (*GetJobResponse, error) { | ||||||
protoReq := &proto.GetJobRequest{ | ||||||
BuilderId: req.BuilderID, | ||||||
} | ||||||
|
||||||
resp, err := t.GetJob(ctx, protoReq) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
return &GetJobResponse{ | ||||||
Job: protoToJob(resp.GetJob()), | ||||||
OK: resp.GetOk(), | ||||||
}, nil | ||||||
} | ||||||
|
||||||
// SendCompleteJob implements Transport | ||||||
func (t *GRPCTransport) SendCompleteJob(ctx context.Context, req *CompleteJobRequest) error { | ||||||
protoReq := &proto.CompleteJobRequest{ | ||||||
BuilderId: req.BuilderID, | ||||||
Job: jobToProto(req.Job), | ||||||
} | ||||||
|
||||||
_, err := t.CompleteJob(ctx, protoReq) | ||||||
return err | ||||||
} | ||||||
|
||||||
// SendSyncJob implements Transport | ||||||
func (t *GRPCTransport) SendSyncJob(ctx context.Context, req *SyncJobRequest) error { | ||||||
protoReq := &proto.SyncJobRequest{ | ||||||
BuilderId: req.BuilderID, | ||||||
Job: jobToProto(req.Job), | ||||||
} | ||||||
|
||||||
_, err := t.SyncJob(ctx, protoReq) | ||||||
return err | ||||||
} | ||||||
|
||||||
// protoToJob converts a proto Job to a types.Job | ||||||
func protoToJob(p *proto.Job) *Job { | ||||||
if p == nil { | ||||||
return nil | ||||||
} | ||||||
return &Job{ | ||||||
ID: p.GetId(), | ||||||
Partition: int(p.GetPartition()), | ||||||
Offsets: Offsets{ | ||||||
Min: p.GetOffsets().GetMin(), | ||||||
Max: p.GetOffsets().GetMax(), | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
// jobToProto converts a types.Job to a proto Job | ||||||
func jobToProto(j *Job) *proto.Job { | ||||||
if j == nil { | ||||||
return nil | ||||||
} | ||||||
return &proto.Job{ | ||||||
Id: j.ID, | ||||||
Partition: int32(j.Partition), | ||||||
Offsets: &proto.Offsets{ | ||||||
Min: j.Offsets.Min, | ||||||
Max: j.Offsets.Max, | ||||||
}, | ||||||
} | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we embed this in builder config? ignore me if it is planned for a follow-up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I planned to leave this for when we're ready to wire it together.