Use proto3 types for all CallerIDs

This commit is contained in:
Jeff (Zhefu) Jiang 2015-07-20 13:35:30 -07:00
Родитель bd35d9ad8f
Коммит f3201f6625
7 изменённых файлов: 60 добавлений и 100 удалений

Просмотреть файл

@ -16,12 +16,6 @@ import (
// The datatype for CallerID Context Keys
type callerIDKey int
// EffectiveCallerID reuses gRPC's CallerID
type EffectiveCallerID vtpb.CallerID
// ImmediateCallerID reuses gRPC's VTGateCallerID
type ImmediateCallerID qrpb.VTGateCallerID
var (
// internal Context key for immediate CallerID
immediateCallerIDKey callerIDKey = 0
@ -29,23 +23,23 @@ var (
effectiveCallerIDKey callerIDKey = 1
)
// NewImmediateCallerID creates a ImmediateCallerID initialized with username
func NewImmediateCallerID(username string) *ImmediateCallerID {
return &ImmediateCallerID{Username: username}
// NewImmediateCallerID creates a qrpb.VTGateCallerID initialized with username
func NewImmediateCallerID(username string) *qrpb.VTGateCallerID {
return &qrpb.VTGateCallerID{Username: username}
}
// GetUsername returns the immediate caller of VTGate
func (im *ImmediateCallerID) GetUsername() string {
func GetUsername(im *qrpb.VTGateCallerID) string {
if im == nil {
return ""
}
return im.Username
}
// NewEffectiveCallerID creates a new effective CallerID with principal, component and
// NewEffectiveCallerID creates a new vtpb.CallerID with principal, component and
// subComponent
func NewEffectiveCallerID(principal string, component string, subComponent string) *EffectiveCallerID {
return &EffectiveCallerID{Principal: principal, Component: component, Subcomponent: subComponent}
func NewEffectiveCallerID(principal string, component string, subComponent string) *vtpb.CallerID {
return &vtpb.CallerID{Principal: principal, Component: component, Subcomponent: subComponent}
}
// GetPrincipal returns the effective user identifier, which is usually filled in
@ -54,7 +48,7 @@ func NewEffectiveCallerID(principal string, component string, subComponent strin
// If the request comes directly from the Internet, or if the Vitess client
// takes action on its own accord, it is okay for this method to
// return empty string.
func (ef *EffectiveCallerID) GetPrincipal() string {
func GetPrincipal(ef *vtpb.CallerID) string {
if ef == nil {
return ""
}
@ -64,7 +58,7 @@ func (ef *EffectiveCallerID) GetPrincipal() string {
// GetComponent returns the running process of the effective caller.
// It can for instance return hostname:port of the servlet initiating the
// database call, or the container engine ID used by the servlet.
func (ef *EffectiveCallerID) GetComponent() string {
func GetComponent(ef *vtpb.CallerID) string {
if ef == nil {
return ""
}
@ -74,15 +68,16 @@ func (ef *EffectiveCallerID) GetComponent() string {
// GetSubcomponent returns a component inisde the process of effective caller,
// which is responsible for generating this request. Suggested values are a
// servlet name or an API endpoint name.
func (ef *EffectiveCallerID) GetSubcomponent() string {
func GetSubcomponent(ef *vtpb.CallerID) string {
if ef == nil {
return ""
}
return ef.Subcomponent
}
// NewContext adds the provided EffectiveCallerID and ImmediateCallerID into the Context
func NewContext(ctx context.Context, ef *EffectiveCallerID, im *ImmediateCallerID) context.Context {
// NewContext adds the provided EffectiveCallerID(vtpb.CallerID) and ImmediateCallerID(qrpb.VTGateCallerID)
// into the Context
func NewContext(ctx context.Context, ef *vtpb.CallerID, im *qrpb.VTGateCallerID) context.Context {
ctx = context.WithValue(
context.WithValue(ctx, effectiveCallerIDKey, ef),
immediateCallerIDKey,
@ -91,18 +86,20 @@ func NewContext(ctx context.Context, ef *EffectiveCallerID, im *ImmediateCallerI
return ctx
}
// EffectiveCallerIDFromContext returns the EffectiveCallerID stored in the Context, if any
func EffectiveCallerIDFromContext(ctx context.Context) *EffectiveCallerID {
ef, ok := ctx.Value(effectiveCallerIDKey).(*EffectiveCallerID)
// EffectiveCallerIDFromContext returns the EffectiveCallerID(vtpb.CallerID)
// stored in the Context, if any
func EffectiveCallerIDFromContext(ctx context.Context) *vtpb.CallerID {
ef, ok := ctx.Value(effectiveCallerIDKey).(*vtpb.CallerID)
if ok && ef != nil {
return ef
}
return nil
}
// ImmediateCallerIDFromContext returns the ImmediateCallerID stored in the Context, if any
func ImmediateCallerIDFromContext(ctx context.Context) *ImmediateCallerID {
im, ok := ctx.Value(immediateCallerIDKey).(*ImmediateCallerID)
// ImmediateCallerIDFromContext returns the ImmediateCallerID(qrpb.VTGateCallerID)
// stored in the Context, if any
func ImmediateCallerIDFromContext(ctx context.Context) *qrpb.VTGateCallerID {
im, ok := ctx.Value(immediateCallerIDKey).(*qrpb.VTGateCallerID)
if ok && im != nil {
return im
}

Просмотреть файл

@ -2,13 +2,16 @@ package callerid
import (
"testing"
qrpb "github.com/youtube/vitess/go/vt/proto/query"
vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
func TestFakeCallerID(t *testing.T) {
im := ImmediateCallerID{
im := qrpb.VTGateCallerID{
Username: FakeUsername,
}
ef := EffectiveCallerID{
ef := vtpb.CallerID{
Principal: FakePrincipal,
Component: FakeComponent,
Subcomponent: FakeSubcomponent,

Просмотреть файл

@ -1,12 +1,14 @@
package callerid
import (
qrpb "github.com/youtube/vitess/go/vt/proto/query"
vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/tabletserver/proto"
)
// GoRPCImmediateCallerID creates new ImmediateCallerID structure
// from GoRPC's VTGateCallerID
func GoRPCImmediateCallerID(v *proto.VTGateCallerID) *ImmediateCallerID {
func GoRPCImmediateCallerID(v *proto.VTGateCallerID) *qrpb.VTGateCallerID {
if v == nil {
return nil
}
@ -15,7 +17,7 @@ func GoRPCImmediateCallerID(v *proto.VTGateCallerID) *ImmediateCallerID {
// GoRPCEffectiveCallerID creates new EffectiveCallerID structure
// from GoRPC's CallerID
func GoRPCEffectiveCallerID(c *proto.CallerID) *EffectiveCallerID {
func GoRPCEffectiveCallerID(c *proto.CallerID) *vtpb.CallerID {
if c == nil {
return nil
}

Просмотреть файл

@ -1,18 +0,0 @@
package callerid
import (
"github.com/youtube/vitess/go/vt/proto/query"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// GRPCImmediateCallerID creates new ImmediateCallerID structure
// by type-conversion from GRPC's VTGateCallerID
func GRPCImmediateCallerID(v *query.VTGateCallerID) *ImmediateCallerID {
return (*ImmediateCallerID)(v)
}
// GRPCEffectiveCallerID creates new EffectiveCallerID structure
// from GRPC's CallerID by type-conversion from GRPC's CallerID
func GRPCEffectiveCallerID(c *vtrpc.CallerID) *EffectiveCallerID {
return (*EffectiveCallerID)(c)
}

Просмотреть файл

@ -1,27 +0,0 @@
package callerid
import (
"testing"
"github.com/youtube/vitess/go/vt/proto/query"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
)
func TestGRPCCallerID(t *testing.T) {
im := query.VTGateCallerID{
Username: FakeUsername,
}
ef := vtrpc.CallerID{
Principal: FakePrincipal,
Component: FakeComponent,
Subcomponent: FakeSubcomponent,
}
// Test nil cases
if n := GRPCImmediateCallerID(nil); n != nil {
t.Errorf("Expect nil from GRPCImmediateCallerID(nil), but got %v", n)
}
if n := GRPCEffectiveCallerID(nil); n != nil {
t.Errorf("Expect nil from GRPCEffectiveCallerID(nil), but got %v", n)
}
Tests(t, GRPCImmediateCallerID(&im), GRPCEffectiveCallerID(&ef))
}

Просмотреть файл

@ -5,6 +5,9 @@ import (
"testing"
"golang.org/x/net/context"
qrpb "github.com/youtube/vitess/go/vt/proto/query"
vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
const (
@ -19,7 +22,7 @@ const (
)
// Tests performs the necessary testsuite for CallerID operations
func Tests(t *testing.T, im *ImmediateCallerID, ef *EffectiveCallerID) {
func Tests(t *testing.T, im *qrpb.VTGateCallerID, ef *vtpb.CallerID) {
ctx := context.TODO()
ctxim := ImmediateCallerIDFromContext(ctx)
// For Contexts without ImmediateCallerID, ImmediateCallerIDFromContext should fail
@ -45,16 +48,16 @@ func Tests(t *testing.T, im *ImmediateCallerID, ef *EffectiveCallerID) {
}
// Test GetXxx on nil receivers, should get all empty strings
if u := ctxim.GetUsername(); u != "" {
if u := GetUsername(ctxim); u != "" {
t.Errorf("Expect empty string from (nil).GetUsername(), but got %v", u)
}
if p := ctxef.GetPrincipal(); p != "" {
if p := GetPrincipal(ctxef); p != "" {
t.Errorf("Expect empty string from (nil).GetPrincipal(), but got %v", p)
}
if c := ctxef.GetComponent(); c != "" {
if c := GetComponent(ctxef); c != "" {
t.Errorf("Expect empty string from (nil).GetComponent(), but got %v", c)
}
if s := ctxef.GetSubcomponent(); s != "" {
if s := GetSubcomponent(ctxef); s != "" {
t.Errorf("Expect empty string from (nil).GetSubcomponent(), but got %v", s)
}
@ -64,8 +67,8 @@ func Tests(t *testing.T, im *ImmediateCallerID, ef *EffectiveCallerID) {
if !reflect.DeepEqual(ctxim, im) {
t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
}
if im.Username != FakeUsername {
t.Errorf("Expect %v from im.Username(), but got %v", FakeUsername, im.Username)
if u := GetUsername(im); u != FakeUsername {
t.Errorf("Expect %v from im.Username(), but got %v", FakeUsername, u)
}
ctxef = EffectiveCallerIDFromContext(ctx)
@ -73,13 +76,13 @@ func Tests(t *testing.T, im *ImmediateCallerID, ef *EffectiveCallerID) {
if !reflect.DeepEqual(ctxef, ef) {
t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
}
if ef.GetPrincipal() != FakePrincipal {
t.Errorf("Expect %v from ef.Principal(), but got %v", FakePrincipal, ef.GetPrincipal())
if p := GetPrincipal(ef); p != FakePrincipal {
t.Errorf("Expect %v from ef.Principal(), but got %v", FakePrincipal, p)
}
if ef.GetComponent() != FakeComponent {
t.Errorf("Expect %v from ef.Component(), but got %v", FakeComponent, ef.GetComponent())
if c := GetComponent(ef); c != FakeComponent {
t.Errorf("Expect %v from ef.Component(), but got %v", FakeComponent, c)
}
if ef.GetSubcomponent() != FakeSubcomponent {
t.Errorf("Expect %v from ef.Subcomponent(), but got %v", FakeSubcomponent, ef.GetSubcomponent())
if s := GetSubcomponent(ef); s != FakeSubcomponent {
t.Errorf("Expect %v from ef.Subcomponent(), but got %v", FakeSubcomponent, s)
}
}

Просмотреть файл

@ -48,8 +48,8 @@ func (q *query) GetSessionId(ctx context.Context, request *pb.GetSessionIdReques
func (q *query) Execute(ctx context.Context, request *pb.ExecuteRequest) (response *pb.ExecuteResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
reply := new(mproto.QueryResult)
execErr := q.server.Execute(ctx, &proto.Query{
@ -72,8 +72,8 @@ func (q *query) Execute(ctx context.Context, request *pb.ExecuteRequest) (respon
func (q *query) ExecuteBatch(ctx context.Context, request *pb.ExecuteBatchRequest) (response *pb.ExecuteBatchResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
reply := new(proto.QueryResultList)
execErr := q.server.ExecuteBatch(ctx, &proto.QueryList{
@ -96,8 +96,8 @@ func (q *query) ExecuteBatch(ctx context.Context, request *pb.ExecuteBatchReques
func (q *query) StreamExecute(request *pb.StreamExecuteRequest, stream pbs.Query_StreamExecuteServer) (err error) {
defer q.server.HandlePanic(&err)
ctx := callerid.NewContext(callinfo.GRPCCallInfo(stream.Context()),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
seErr := q.server.StreamExecute(ctx, &proto.Query{
Sql: string(request.Query.Sql),
@ -123,8 +123,8 @@ func (q *query) StreamExecute(request *pb.StreamExecuteRequest, stream pbs.Query
func (q *query) Begin(ctx context.Context, request *pb.BeginRequest) (response *pb.BeginResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
txInfo := new(proto.TransactionInfo)
if beginErr := q.server.Begin(ctx, &proto.Session{
@ -144,8 +144,8 @@ func (q *query) Begin(ctx context.Context, request *pb.BeginRequest) (response *
func (q *query) Commit(ctx context.Context, request *pb.CommitRequest) (response *pb.CommitResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
commitErr := q.server.Commit(ctx, &proto.Session{
SessionId: request.SessionId,
@ -160,8 +160,8 @@ func (q *query) Commit(ctx context.Context, request *pb.CommitRequest) (response
func (q *query) Rollback(ctx context.Context, request *pb.RollbackRequest) (response *pb.RollbackResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
rollbackErr := q.server.Rollback(ctx, &proto.Session{
SessionId: request.SessionId,
@ -177,8 +177,8 @@ func (q *query) Rollback(ctx context.Context, request *pb.RollbackRequest) (resp
func (q *query) SplitQuery(ctx context.Context, request *pb.SplitQueryRequest) (response *pb.SplitQueryResponse, err error) {
defer q.server.HandlePanic(&err)
ctx = callerid.NewContext(callinfo.GRPCCallInfo(ctx),
callerid.GRPCEffectiveCallerID(request.GetEffectiveCallerId()),
callerid.GRPCImmediateCallerID(request.GetImmediateCallerId()),
request.GetEffectiveCallerId(),
request.GetImmediateCallerId(),
)
reply := &proto.SplitQueryResult{}
if sqErr := q.server.SplitQuery(ctx, &proto.SplitQueryRequest{