2020-11-05 18:01:33 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-11 09:54:01 +08:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2020-11-05 18:01:33 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/masterpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/servicepb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniqueID = typeutil.UniqueID
|
|
|
|
type Timestamp = typeutil.Timestamp
|
|
|
|
|
|
|
|
type Proxy struct {
|
|
|
|
proxyLoopCtx context.Context
|
|
|
|
proxyLoopCancel func()
|
|
|
|
proxyLoopWg sync.WaitGroup
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
masterConn *grpc.ClientConn
|
|
|
|
masterClient masterpb.MasterClient
|
2020-11-21 16:31:38 +08:00
|
|
|
sched *TaskScheduler
|
2020-11-11 09:54:01 +08:00
|
|
|
tick *timeTick
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
idAllocator *allocator.IDAllocator
|
2020-11-11 09:54:01 +08:00
|
|
|
tsoAllocator *allocator.TimestampAllocator
|
2020-11-19 21:02:31 +08:00
|
|
|
segAssigner *allocator.SegIDAssigner
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
manipulationMsgStream *msgstream.PulsarMsgStream
|
|
|
|
queryMsgStream *msgstream.PulsarMsgStream
|
2020-11-09 17:52:47 +08:00
|
|
|
|
|
|
|
// Add callback functions at different stages
|
|
|
|
startCallbacks []func()
|
|
|
|
closeCallbacks []func()
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
func Init() {
|
|
|
|
Params.InitParamTable()
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
func CreateProxy(ctx context.Context) (*Proxy, error) {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-11-11 09:54:01 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
p := &Proxy{
|
|
|
|
proxyLoopCtx: ctx1,
|
|
|
|
proxyLoopCancel: cancel,
|
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
|
|
|
|
// TODO: use config instead
|
2020-11-21 15:06:46 +08:00
|
|
|
pulsarAddress := Params.PulsarAddress()
|
2020-11-11 09:54:01 +08:00
|
|
|
bufSize := int64(1000)
|
2020-11-14 11:24:49 +08:00
|
|
|
manipulationChannels := []string{"manipulation"}
|
|
|
|
queryChannels := []string{"query"}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
p.manipulationMsgStream = msgstream.NewPulsarMsgStream(p.proxyLoopCtx, bufSize)
|
2020-11-18 19:46:18 +08:00
|
|
|
p.manipulationMsgStream.SetPulsarClient(pulsarAddress)
|
2020-11-14 11:24:49 +08:00
|
|
|
p.manipulationMsgStream.CreatePulsarProducers(manipulationChannels)
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
p.queryMsgStream = msgstream.NewPulsarMsgStream(p.proxyLoopCtx, bufSize)
|
2020-11-18 19:46:18 +08:00
|
|
|
p.queryMsgStream.SetPulsarClient(pulsarAddress)
|
2020-11-14 11:24:49 +08:00
|
|
|
p.queryMsgStream.CreatePulsarProducers(queryChannels)
|
|
|
|
|
2020-11-21 15:06:46 +08:00
|
|
|
masterAddr := Params.MasterAddress()
|
2020-11-16 21:10:43 +08:00
|
|
|
idAllocator, err := allocator.NewIDAllocator(p.proxyLoopCtx, masterAddr)
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.idAllocator = idAllocator
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
tsoAllocator, err := allocator.NewTimestampAllocator(p.proxyLoopCtx, masterAddr)
|
2020-11-11 09:54:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.tsoAllocator = tsoAllocator
|
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
segAssigner, err := allocator.NewSegIDAssigner(p.proxyLoopCtx, masterAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.segAssigner = segAssigner
|
|
|
|
|
2020-11-21 16:31:38 +08:00
|
|
|
p.sched, err = NewTaskScheduler(p.proxyLoopCtx, p.idAllocator, p.tsoAllocator)
|
2020-11-11 09:54:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
|
|
|
|
return p, nil
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-09 17:52:47 +08:00
|
|
|
// AddStartCallback adds a callback in the startServer phase.
|
2020-11-11 09:54:01 +08:00
|
|
|
func (p *Proxy) AddStartCallback(callbacks ...func()) {
|
|
|
|
p.startCallbacks = append(p.startCallbacks, callbacks...)
|
2020-11-09 17:52:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
func (p *Proxy) startProxy() error {
|
|
|
|
err := p.connectMaster()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-20 17:53:31 +08:00
|
|
|
initGlobalMetaCache(p.proxyLoopCtx, p.masterClient, p.idAllocator, p.tsoAllocator)
|
2020-11-11 09:54:01 +08:00
|
|
|
p.manipulationMsgStream.Start()
|
|
|
|
p.queryMsgStream.Start()
|
2020-11-21 16:31:38 +08:00
|
|
|
p.sched.Start()
|
2020-11-11 09:54:01 +08:00
|
|
|
p.idAllocator.Start()
|
|
|
|
p.tsoAllocator.Start()
|
2020-11-19 21:02:31 +08:00
|
|
|
p.segAssigner.Start()
|
2020-11-09 17:52:47 +08:00
|
|
|
|
2020-11-21 16:31:38 +08:00
|
|
|
// Start callbacks
|
2020-11-11 09:54:01 +08:00
|
|
|
for _, cb := range p.startCallbacks {
|
2020-11-09 17:52:47 +08:00
|
|
|
cb()
|
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-21 16:31:38 +08:00
|
|
|
p.proxyLoopWg.Add(1)
|
|
|
|
go p.grpcLoop()
|
|
|
|
|
2020-11-09 17:52:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCloseCallback adds a callback in the Close phase.
|
2020-11-12 12:04:12 +08:00
|
|
|
func (p *Proxy) AddCloseCallback(callbacks ...func()) {
|
|
|
|
p.closeCallbacks = append(p.closeCallbacks, callbacks...)
|
2020-11-09 17:52:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
func (p *Proxy) grpcLoop() {
|
|
|
|
defer p.proxyLoopWg.Done()
|
|
|
|
|
|
|
|
// TODO: use address in config instead
|
2020-11-14 11:24:49 +08:00
|
|
|
lis, err := net.Listen("tcp", ":5053")
|
2020-11-05 18:01:33 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Proxy grpc server fatal error=%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.grpcServer = grpc.NewServer()
|
|
|
|
servicepb.RegisterMilvusServiceServer(p.grpcServer, p)
|
|
|
|
if err = p.grpcServer.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("Proxy grpc server fatal error=%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Proxy) connectMaster() error {
|
2020-11-21 15:06:46 +08:00
|
|
|
masterAddr := Params.MasterAddress()
|
2020-11-14 11:24:49 +08:00
|
|
|
log.Printf("Proxy connected to master, master_addr=%s", masterAddr)
|
2020-11-18 15:44:17 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
2020-11-14 11:24:49 +08:00
|
|
|
conn, err := grpc.DialContext(ctx, masterAddr, grpc.WithInsecure(), grpc.WithBlock())
|
2020-11-05 18:01:33 +08:00
|
|
|
if err != nil {
|
2020-11-14 11:24:49 +08:00
|
|
|
log.Printf("Proxy connect to master failed, error= %v", err)
|
2020-11-05 18:01:33 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-11-14 11:24:49 +08:00
|
|
|
log.Printf("Proxy connected to master, master_addr=%s", masterAddr)
|
2020-11-05 18:01:33 +08:00
|
|
|
p.masterConn = conn
|
|
|
|
p.masterClient = masterpb.NewMasterClient(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-21 16:31:38 +08:00
|
|
|
func (p *Proxy) Start() error {
|
|
|
|
return p.startProxy()
|
2020-11-09 17:52:47 +08:00
|
|
|
}
|
2020-11-05 18:01:33 +08:00
|
|
|
|
2020-11-09 17:52:47 +08:00
|
|
|
func (p *Proxy) stopProxyLoop() {
|
2020-11-11 09:54:01 +08:00
|
|
|
p.proxyLoopCancel()
|
|
|
|
|
|
|
|
if p.grpcServer != nil {
|
2020-11-09 17:52:47 +08:00
|
|
|
p.grpcServer.GracefulStop()
|
|
|
|
}
|
2020-11-11 09:54:01 +08:00
|
|
|
p.tsoAllocator.Close()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
p.idAllocator.Close()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-19 21:02:31 +08:00
|
|
|
p.segAssigner.Close()
|
|
|
|
|
2020-11-21 16:31:38 +08:00
|
|
|
p.sched.Close()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
p.manipulationMsgStream.Close()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-11 09:54:01 +08:00
|
|
|
p.queryMsgStream.Close()
|
2020-11-14 11:24:49 +08:00
|
|
|
|
2020-11-05 18:01:33 +08:00
|
|
|
p.proxyLoopWg.Wait()
|
2020-11-09 17:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the server.
|
|
|
|
func (p *Proxy) Close() {
|
|
|
|
p.stopProxyLoop()
|
2020-11-11 09:54:01 +08:00
|
|
|
|
2020-11-09 17:52:47 +08:00
|
|
|
for _, cb := range p.closeCallbacks {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
log.Print("proxy closed.")
|
2020-11-05 18:01:33 +08:00
|
|
|
}
|