Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions metadata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func GetMetadataFromMetadataReport(revision string, instance registry.ServiceIns
}

func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) {
return GetMetadataFromRpcWithContext(context.Background(), revision, instance)
Comment thread
XnLemon marked this conversation as resolved.
Resolved
}

// GetMetadataFromRpcWithContext fetches metadata through the metadata service
// while preserving the caller's context for the underlying RPC invocation.
func GetMetadataFromRpcWithContext(ctx context.Context, revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) {
if ctx == nil {
ctx = context.Background()
}
url, err := buildStandardMetadataServiceURL(instance)
if err != nil {
return nil, err
Expand All @@ -74,27 +83,28 @@ func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*in
defer func() {
invoker.Destroy()
}()
return remoteService.getMetadataInfo(context.Background(), revision)
return remoteService.getMetadataInfo(ctx, revision)
}

// remoteMetadataService is the internal interface for fetching MetadataInfo via RPC.
// The context parameter is accepted for future cancellation support but is not yet propagated.
type remoteMetadataService interface {
getMetadataInfo(_ context.Context, revision string) (*info.MetadataInfo, error)
getMetadataInfo(ctx context.Context, revision string) (*info.MetadataInfo, error)
}

type triMetadataServiceV2 struct {
invoker base.Invoker
}

// getMetadataInfo fetches metadata via RPC using the Triple protocol (Protobuf).
// TODO(context-propagation): ctx is not yet forwarded to the invoker; cancellation is not respected.
func (m *triMetadataServiceV2) getMetadataInfo(_ context.Context, revision string) (*info.MetadataInfo, error) {
func (m *triMetadataServiceV2) getMetadataInfo(ctx context.Context, revision string) (*info.MetadataInfo, error) {
const methodName = "GetMetadataInfo"
req := &tripleapi.MetadataRequest{Revision: revision}
metadataInfo := &tripleapi.MetadataInfoV2{}
inv, _ := generateInvocation(m.invoker.GetURL(), methodName, req, metadataInfo, constant.CallUnary)
res := m.invoker.Invoke(context.Background(), inv)
if rpcInv, ok := inv.(*invocation.RPCInvocation); ok {
rpcInv.SetContext(ctx)
}
res := m.invoker.Invoke(ctx, inv)
if res.Error() != nil {
logger.Errorf("[Metadata][RPC] could not get the metadata info from remote provider, err=%v", res.Error())
return nil, perrors.Wrapf(res.Error(), "remote metadata call failed")
Expand Down Expand Up @@ -160,15 +170,17 @@ type remoteMetadataServiceV1 struct {
}

// getMetadataInfo fetches metadata via RPC using the dubbo:// protocol (Hessian2 serialization).
// TODO(context-propagation): ctx is not yet forwarded to the invoker; cancellation is not respected.
Comment thread
Alanxtl marked this conversation as resolved.
Resolved
func (m *remoteMetadataServiceV1) getMetadataInfo(_ context.Context, revision string) (*info.MetadataInfo, error) {
func (m *remoteMetadataServiceV1) getMetadataInfo(ctx context.Context, revision string) (*info.MetadataInfo, error) {
const methodName = "getMetadataInfo"
// Use interface{} as reply parameter to accept any type (MetadataInfo or string)
// This avoids panic when Java returns String instead of MetadataInfo
var rawResult any
inv, _ := generateInvocation(m.invoker.GetURL(), methodName, revision, &rawResult, constant.CallUnary)
if rpcInv, ok := inv.(*invocation.RPCInvocation); ok {
rpcInv.SetContext(ctx)
}

res := m.invoker.Invoke(context.Background(), inv)
res := m.invoker.Invoke(ctx, inv)
Comment thread
XnLemon marked this conversation as resolved.
Resolved
if res.Error() != nil {
logger.Errorf("[Metadata][RPC] RPC call failed to %s, err=%v", m.invoker.GetURL().Location, res.Error())
return nil, perrors.Wrapf(res.Error(), "RPC call failed to %s", m.invoker.GetURL().Location)
Expand Down
41 changes: 41 additions & 0 deletions metadata/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
}
)

type metadataContextKey struct{}

func TestConvertMetadataInfoV2PreservesTag(t *testing.T) {
got := convertMetadataInfoV2(&tripleapi.MetadataInfoV2{
App: "dubbo-app",
Expand Down Expand Up @@ -186,6 +188,39 @@
})
}

func TestGetMetadataFromRpcWithContext(t *testing.T) {
mockInvoker := new(mockInvoker)
mockProtocol := new(mockProtocol)
extension.SetProtocol("dubbo", func() base.Protocol {
return mockProtocol
})

mockProtocol.On("Refer").Return(mockInvoker).Once()
mockInvoker.On("Invoke").Return(&result.RPCResult{
Attrs: map[string]any{},
Rest: metadataInfo,
}).Once()
mockInvoker.On("Destroy").Once()

ctx := context.WithValue(context.Background(), metadataContextKey{}, "request-value")
metadata, err := GetMetadataFromRpcWithContext(ctx, "111", ins)
require.NoError(t, err)
assert.Equal(t, metadataInfo, metadata)
assert.Same(t, ctx, mockInvoker.invokedContext)
}

func TestTriMetadataServiceWithContext(t *testing.T) {
mockInvoker := new(mockInvoker)
mockInvoker.url = common.NewURLWithOptions(common.WithProtocol(constant.TriProtocol))
mockInvoker.On("Invoke").Return(&result.RPCResult{Attrs: map[string]any{}}).Once()

ctx := context.WithValue(context.Background(), metadataContextKey{}, "request-value")
metadata, err := (&triMetadataServiceV2{invoker: mockInvoker}).getMetadataInfo(ctx, "111")
require.NoError(t, err)
require.NotNil(t, metadata)
assert.Same(t, ctx, mockInvoker.invokedContext)
}

func TestGetMetadataFromRpc_MissingURLParams(t *testing.T) {
t.Run("missing protocol", func(t *testing.T) {
insNoProto := &registry.DefaultServiceInstance{
Expand Down Expand Up @@ -377,9 +412,14 @@

type mockInvoker struct {
mock.Mock
invokedContext context.Context

Check warning on line 415 in metadata/client_test.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'context.Context' field and pass context as a parameter to methods that need it.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-yUvuzoGPlgbDGtsVG&open=AZ-yUvuzoGPlgbDGtsVG&pullRequest=3579
url *common.URL
}

func (m *mockInvoker) GetURL() *common.URL {
if m.url != nil {
return m.url
}
return common.NewURLWithOptions(common.WithProtocol(constant.DefaultProtocol))
}

Expand All @@ -392,6 +432,7 @@
}

func (m *mockInvoker) Invoke(ctx context.Context, inv base.Invocation) result.Result {
m.invokedContext = ctx
args := m.Called()
res := args.Get(0).(result.Result)

Expand Down
31 changes: 30 additions & 1 deletion protocol/invocation/rpcinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
arguments []any
reply any
callBack any
ctx context.Context

Check warning on line 50 in protocol/invocation/rpcinvocation.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'context.Context' field and pass context as a parameter to methods that need it.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-yUvuioGPlgbDGtsVF&open=AZ-yUvuioGPlgbDGtsVF&pullRequest=3579
attachments map[string]any
// Refer to dubbo 2.7.6. It is different from attachment. It is used in internal process.
attributes map[string]any
Expand Down Expand Up @@ -174,6 +175,27 @@
r.callBack = c
}

// Context returns the request context associated with this invocation.
func (r *RPCInvocation) Context() context.Context {
r.lock.RLock()
ctx := r.ctx
r.lock.RUnlock()
if ctx == nil {
return context.Background()
}
return ctx
}

// SetContext associates a request context with this invocation.
func (r *RPCInvocation) SetContext(ctx context.Context) {
if ctx == nil {
return
}
r.lock.Lock()
defer r.lock.Unlock()
r.ctx = ctx
}

func (r *RPCInvocation) ServiceKey() string {
return common.ServiceKey(strings.TrimPrefix(r.GetAttachmentWithDefaultValue(constant.PathKey, r.GetAttachmentWithDefaultValue(constant.InterfaceKey, "")), "/"),
r.GetAttachmentWithDefaultValue(constant.GroupKey, ""), r.GetAttachmentWithDefaultValue(constant.VersionKey, ""))
Expand Down Expand Up @@ -244,7 +266,7 @@
}

func (r *RPCInvocation) GetAttachmentAsContext() context.Context {
ctx := context.Background()
ctx := r.Context()
Comment thread
XnLemon marked this conversation as resolved.
Resolved
var header = http.Header{}
for k, v := range r.Attachments() {
if str, ok := v.(string); ok {
Expand Down Expand Up @@ -343,6 +365,13 @@
}
}

// WithContext creates an option with the request context.
func WithContext(ctx context.Context) option {
return func(invo *RPCInvocation) {
invo.SetContext(ctx)
}
}

// WithAttachments creates option with @attachments.
func WithAttachments(attachments map[string]any) option {
return func(invo *RPCInvocation) {
Expand Down
13 changes: 13 additions & 0 deletions protocol/invocation/rpcinvocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ func TestRPCInvocation_GetAttachmentAsContext(t *testing.T) {
assert.NotContains(t, header, "key3")
}

func TestRPCInvocation_GetAttachmentAsContextPreservesRequestContext(t *testing.T) {
type requestContextKey struct{}
requestCtx := context.WithValue(context.Background(), requestContextKey{}, "request-value")
invocation := NewRPCInvocationWithOptions(
WithContext(requestCtx),
WithAttachment("key", "value"),
)

ctx := invocation.GetAttachmentAsContext()
assert.Equal(t, "request-value", ctx.Value(requestContextKey{}))
assert.Equal(t, "value", triple_protocol.ExtractFromOutgoingContext(ctx).Get("key"))
}

func TestRPCInvocation_MergeAttachmentFromContext(t *testing.T) {
invocation := NewRPCInvocationWithOptions()

Expand Down
24 changes: 15 additions & 9 deletions protocol/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,7 @@ func (s *Server) handlePkg(conn net.Conn) {
return
}

ctx := context.Background()

spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
if err == nil {
ctx = context.WithValue(ctx, constant.TracingRemoteSpanCtx, spanCtx)
}
ctx := contextFromRequest(r)

if len(reqHeader["Timeout"]) > 0 {
timeout, err := time.ParseDuration(reqHeader["Timeout"])
Expand All @@ -194,6 +188,16 @@ func (s *Server) handlePkg(conn net.Conn) {
}
}

func contextFromRequest(r *http.Request) context.Context {
ctx := r.Context()
Comment thread
XnLemon marked this conversation as resolved.
Resolved
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
if err == nil {
ctx = context.WithValue(ctx, constant.TracingRemoteSpanCtx, spanCtx)
}
return ctx
}

func accept(listener net.Listener, fn func(net.Conn)) error {
var (
ok bool
Expand Down Expand Up @@ -352,10 +356,12 @@ func serveRequest(ctx context.Context, header map[string]string, body []byte, co
}
invoker := exporter.(*JsonrpcExporter).GetInvoker()
if invoker != nil {
result := invoker.Invoke(ctx, invocation.NewRPCInvocation(methodName, args, map[string]any{
rpcInvocation := invocation.NewRPCInvocation(methodName, args, map[string]any{
constant.PathKey: path,
constant.VersionKey: codec.req.Version,
}))
})
rpcInvocation.SetContext(ctx)
result := invoker.Invoke(ctx, rpcInvocation)
if err := result.Error(); err != nil {
rspStream, codecErr := codec.Write(err.Error(), invalidRequest)
if codecErr != nil {
Expand Down
13 changes: 13 additions & 0 deletions protocol/jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
)
Expand Down Expand Up @@ -71,6 +72,18 @@ func TestServeRequest_ServiceNotFound(t *testing.T) {
require.Contains(t, err.Error(), "service not found")
}

func TestContextFromRequestPreservesRequestContext(t *testing.T) {
type contextKey struct{}
requestCtx, cancel := context.WithCancel(context.WithValue(context.Background(), contextKey{}, "request-value"))
defer cancel()
request := httptest.NewRequestWithContext(requestCtx, http.MethodPost, "/test", nil)

ctx := contextFromRequest(request)
require.Equal(t, "request-value", ctx.Value(contextKey{}))
cancel()
require.Error(t, ctx.Err())
}

func TestHandlePkg_ContentType(t *testing.T) {
tests := []struct {
name string
Expand Down
13 changes: 11 additions & 2 deletions protocol/rest/server/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package server

import (
"context"
"errors"
"net/http"
"reflect"
Expand Down Expand Up @@ -110,7 +109,17 @@ func GetRouteFunc(invoker base.Invoker, methodConfig *rest_config.RestMethodConf
logger.Errorf("[Rest][Server] write error string failed, err=%v", err)
}
}
result := invoker.Invoke(context.Background(), invocation.NewRPCInvocation(methodConfig.MethodName, args, make(map[string]any)))
rawRequest := req.RawRequest()
Comment thread
XnLemon marked this conversation as resolved.
Resolved
if rawRequest == nil {
logger.Errorf("[Rest][Server] request adapter returned a nil raw request")
if writeErr := resp.WriteError(http.StatusInternalServerError, errors.New("raw HTTP request is nil")); writeErr != nil {
logger.Errorf("[Rest][Server] write error failed, err=%v", writeErr)
}
return
}
rpcInvocation := invocation.NewRPCInvocation(methodConfig.MethodName, args, make(map[string]any))
rpcInvocation.SetContext(rawRequest.Context())
result := invoker.Invoke(rawRequest.Context(), rpcInvocation)
if result.Error() != nil {
err = resp.WriteError(http.StatusInternalServerError, result.Error())
if err != nil {
Expand Down
Loading
Loading