milvus/internal/registry/in_mem_resolver.go
congqixia bcbe98aba1
Add querynode client wrapper and avoid grpc in standalone mode (#27781)
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
2023-10-19 11:10:07 +08:00

50 lines
1.1 KiB
Go

package registry
import (
"context"
"sync"
"go.uber.org/atomic"
qnClient "github.com/milvus-io/milvus/internal/distributed/querynode/client"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/wrappers"
"github.com/milvus-io/milvus/pkg/util/typeutil"
)
var (
once sync.Once
resolver atomic.Pointer[InMemResolver]
)
func GetInMemoryResolver() *InMemResolver {
r := resolver.Load()
if r == nil {
once.Do(func() {
newResolver := &InMemResolver{
queryNodes: typeutil.NewConcurrentMap[int64, types.QueryNode](),
}
resolver.Store(newResolver)
})
r = resolver.Load()
}
return r
}
type InMemResolver struct {
queryNodes *typeutil.ConcurrentMap[int64, types.QueryNode]
}
func (r *InMemResolver) RegisterQueryNode(id int64, qn types.QueryNode) {
r.queryNodes.Insert(id, qn)
}
func (r *InMemResolver) ResolveQueryNode(ctx context.Context, addr string, nodeID int64) (types.QueryNodeClient, error) {
qn, ok := r.queryNodes.Get(nodeID)
if !ok {
return qnClient.NewClient(ctx, addr, nodeID)
}
return wrappers.WrapQueryNodeServerAsClient(qn), nil
}