node_info.go 3.1 KB
Newer Older
D
dragondriver 已提交
1 2 3
package proxyservice

import (
4 5
	"context"
	"log"
D
dragondriver 已提交
6
	"math/rand"
7
	"strconv"
D
dragondriver 已提交
8 9 10
	"sync"
	"time"

G
godchen 已提交
11 12
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"

13 14
	grpcproxynodeclient "github.com/zilliztech/milvus-distributed/internal/distributed/proxynode/client"

D
dragondriver 已提交
15 16 17 18 19 20 21 22 23 24 25
	"github.com/zilliztech/milvus-distributed/internal/errors"

	"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
)

type NodeInfo struct {
	ip   string
	port int64
}

type NodeClient interface {
26 27 28
	Init() error
	Start() error
	Stop() error
D
dragondriver 已提交
29

G
godchen 已提交
30
	InvalidateCollectionMetaCache(request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error)
D
dragondriver 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

type GlobalNodeInfoTable struct {
	mtx             sync.RWMutex
	nodeIDs         []UniqueID
	infos           map[UniqueID]*NodeInfo
	createClientMtx sync.RWMutex
	// lazy creating, so len(clients) <= len(infos)
	clients map[UniqueID]NodeClient
}

func (table *GlobalNodeInfoTable) randomPick() UniqueID {
	rand.Seed(time.Now().UnixNano())
	l := len(table.nodeIDs)
	choice := rand.Intn(l)
	return table.nodeIDs[choice]
}

func (table *GlobalNodeInfoTable) Pick() (*NodeInfo, error) {
	table.mtx.RLock()
	defer table.mtx.RUnlock()

	if len(table.nodeIDs) <= 0 || len(table.infos) <= 0 {
		return nil, errors.New("no available server node")
	}

	id := table.randomPick()
	info, ok := table.infos[id]
	if !ok {
		// though impossible
		return nil, errors.New("fix me, something wrong in pick algorithm")
	}

	return info, nil
}

func (table *GlobalNodeInfoTable) Register(id UniqueID, info *NodeInfo) error {
	table.mtx.Lock()
	defer table.mtx.Unlock()

	_, ok := table.infos[id]
	if !ok {
		table.infos[id] = info
	}

	if !SliceContain(table.nodeIDs, id) {
		table.nodeIDs = append(table.nodeIDs, id)
	}

	return nil
}

func (table *GlobalNodeInfoTable) createClients() error {
84 85
	log.Println("infos: ", table.infos)
	log.Println("clients: ", table.clients)
D
dragondriver 已提交
86 87 88 89 90 91 92
	if len(table.clients) == len(table.infos) {
		return nil
	}

	for nodeID, info := range table.infos {
		_, ok := table.clients[nodeID]
		if !ok {
93 94 95 96 97
			log.Println(info)
			table.clients[nodeID] = grpcproxynodeclient.NewClient(context.Background(), info.ip+":"+strconv.Itoa(int(info.port)))
			var err error
			err = table.clients[nodeID].Init()
			if err != nil {
98
				panic(err)
99 100 101
			}
			err = table.clients[nodeID].Start()
			if err != nil {
102
				panic(err)
103
			}
D
dragondriver 已提交
104 105 106 107 108 109
		}
	}

	return nil
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
func (table *GlobalNodeInfoTable) ReleaseAllClients() error {
	table.createClientMtx.Lock()
	log.Println("get write lock")
	defer func() {
		table.createClientMtx.Unlock()
		log.Println("release write lock")
	}()

	var err error
	for id, client := range table.clients {
		err = client.Stop()
		if err != nil {
			panic(err)
		}
		delete(table.clients, id)
	}

	return nil
}

D
dragondriver 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
func (table *GlobalNodeInfoTable) ObtainAllClients() (map[UniqueID]NodeClient, error) {
	table.mtx.RLock()
	defer table.mtx.RUnlock()

	table.createClientMtx.Lock()
	defer table.createClientMtx.Unlock()

	err := table.createClients()

	return table.clients, err
}

func NewGlobalNodeInfoTable() *GlobalNodeInfoTable {
	return &GlobalNodeInfoTable{
		nodeIDs: make([]UniqueID, 0),
		infos:   make(map[UniqueID]*NodeInfo),
		clients: make(map[UniqueID]NodeClient),
	}
}