-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blockbuilder): grpc transport (#15218)
- Loading branch information
Showing
7 changed files
with
2,547 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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. | ||
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 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 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.