query_node.go 10.6 KB
Newer Older
1
package querynode
B
bigsheeper 已提交
2

3 4
/*

5
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
6

G
GuoRentong 已提交
7
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
8

F
FluorineDog 已提交
9 10
#include "segcore/collection_c.h"
#include "segcore/segment_c.h"
11 12

*/
B
bigsheeper 已提交
13
import "C"
14

B
bigsheeper 已提交
15
import (
16
	"context"
X
XuanYang-cn 已提交
17
	"errors"
18 19
	"fmt"
	"io"
C
cai.zhang 已提交
20
	"sync/atomic"
21 22 23

	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go/config"
C
cai.zhang 已提交
24

X
Xiangyu Wang 已提交
25 26
	"github.com/zilliztech/milvus-distributed/internal/msgstream/pulsarms"
	"github.com/zilliztech/milvus-distributed/internal/msgstream/util"
27
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
C
cai.zhang 已提交
28
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
29
	queryPb "github.com/zilliztech/milvus-distributed/internal/proto/querypb"
30 31
	queryserviceimpl "github.com/zilliztech/milvus-distributed/internal/queryservice"
	"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
B
bigsheeper 已提交
32 33
)

34
type Node interface {
C
cai.zhang 已提交
35 36 37
	GetComponentStates() (*internalpb2.ComponentStates, error)
	GetTimeTickChannel() (string, error)
	GetStatisticsChannel() (string, error)
38

39 40 41 42 43
	AddQueryChannel(in *queryPb.AddQueryChannelsRequest) (*commonpb.Status, error)
	RemoveQueryChannel(in *queryPb.RemoveQueryChannelsRequest) (*commonpb.Status, error)
	WatchDmChannels(in *queryPb.WatchDmChannelsRequest) (*commonpb.Status, error)
	LoadSegments(in *queryPb.LoadSegmentRequest) (*commonpb.Status, error)
	ReleaseSegments(in *queryPb.ReleaseSegmentRequest) (*commonpb.Status, error)
44 45
}

B
bigsheeper 已提交
46
type QueryNode struct {
47 48
	typeutil.Service

X
XuanYang-cn 已提交
49
	queryNodeLoopCtx    context.Context
50
	queryNodeLoopCancel context.CancelFunc
51

B
bigsheeper 已提交
52
	QueryNodeID uint64
C
cai.zhang 已提交
53
	stateCode   atomic.Value
B
bigsheeper 已提交
54

X
XuanYang-cn 已提交
55
	replica collectionReplica
B
bigsheeper 已提交
56

57
	// internal services
58 59 60 61 62
	dataSyncService  *dataSyncService
	metaService      *metaService
	searchService    *searchService
	loadIndexService *loadIndexService
	statsService     *statsService
63

64 65
	segManager *segmentManager

66 67 68
	//opentracing
	tracer opentracing.Tracer
	closer io.Closer
B
bigsheeper 已提交
69
}
70

71
func NewQueryNode(ctx context.Context, queryNodeID uint64) *QueryNode {
X
XuanYang-cn 已提交
72
	ctx1, cancel := context.WithCancel(ctx)
C
cai.zhang 已提交
73
	node := &QueryNode{
74 75 76 77 78 79 80 81
		queryNodeLoopCtx:    ctx1,
		queryNodeLoopCancel: cancel,
		QueryNodeID:         queryNodeID,

		dataSyncService: nil,
		metaService:     nil,
		searchService:   nil,
		statsService:    nil,
X
XuanYang-cn 已提交
82
		segManager:      nil,
83 84 85 86 87 88 89 90 91 92
	}

	var err error
	cfg := &config.Configuration{
		ServiceName: "query_node",
		Sampler: &config.SamplerConfig{
			Type:  "const",
			Param: 1,
		},
	}
C
cai.zhang 已提交
93
	node.tracer, node.closer, err = cfg.NewTracer()
94 95 96
	if err != nil {
		panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
	}
C
cai.zhang 已提交
97
	opentracing.SetGlobalTracer(node.tracer)
X
XuanYang-cn 已提交
98

B
bigsheeper 已提交
99
	segmentsMap := make(map[int64]*Segment)
100
	collections := make([]*Collection, 0)
B
bigsheeper 已提交
101

102 103
	tSafe := newTSafe()

C
cai.zhang 已提交
104
	node.replica = &collectionReplicaImpl{
G
godchen 已提交
105 106
		collections: collections,
		segments:    segmentsMap,
107 108

		tSafe: tSafe,
G
godchen 已提交
109
	}
C
cai.zhang 已提交
110 111 112
	node.stateCode.Store(internalpb2.StateCode_INITIALIZING)
	return node
}
G
godchen 已提交
113

C
cai.zhang 已提交
114 115 116
// TODO: delete this and call node.Init()
func Init() {
	Params.Init()
B
bigsheeper 已提交
117 118
}

N
neza2017 已提交
119
func (node *QueryNode) Init() error {
C
cai.zhang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	registerReq := queryPb.RegisterNodeRequest{
		Address: &commonpb.Address{
			Ip:   Params.QueryNodeIP,
			Port: Params.QueryNodePort,
		},
	}
	var client queryserviceimpl.Interface // TODO: init interface
	response, err := client.RegisterNode(registerReq)
	if err != nil {
		panic(err)
	}
	if response.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		panic(response.Status.Reason)
	}
	// TODO: use response.initParams

	Params.Init()
N
neza2017 已提交
137
	return nil
C
cai.zhang 已提交
138 139
}

N
neza2017 已提交
140
func (node *QueryNode) Start() error {
X
XuanYang-cn 已提交
141
	// todo add connectMaster logic
X
XuanYang-cn 已提交
142
	// init services and manager
X
XuanYang-cn 已提交
143 144 145
	node.dataSyncService = newDataSyncService(node.queryNodeLoopCtx, node.replica)
	node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica)
	node.metaService = newMetaService(node.queryNodeLoopCtx, node.replica)
146 147
	node.loadIndexService = newLoadIndexService(node.queryNodeLoopCtx, node.replica)
	node.statsService = newStatsService(node.queryNodeLoopCtx, node.replica, node.loadIndexService.fieldStatsChan)
X
XuanYang-cn 已提交
148
	node.segManager = newSegmentManager(node.queryNodeLoopCtx, node.replica, node.loadIndexService.loadIndexReqChan)
B
bigsheeper 已提交
149

X
XuanYang-cn 已提交
150
	// start services
151
	go node.dataSyncService.start()
N
neza2017 已提交
152
	go node.searchService.start()
B
bigsheeper 已提交
153
	go node.metaService.start()
154
	go node.loadIndexService.start()
X
XuanYang-cn 已提交
155
	go node.statsService.start()
156

C
cai.zhang 已提交
157
	node.stateCode.Store(internalpb2.StateCode_HEALTHY)
158
	<-node.queryNodeLoopCtx.Done()
N
neza2017 已提交
159
	return nil
B
bigsheeper 已提交
160
}
B
bigsheeper 已提交
161

N
neza2017 已提交
162
func (node *QueryNode) Stop() error {
C
cai.zhang 已提交
163
	node.stateCode.Store(internalpb2.StateCode_ABNORMAL)
X
XuanYang-cn 已提交
164 165
	node.queryNodeLoopCancel()

B
bigsheeper 已提交
166
	// free collectionReplica
X
XuanYang-cn 已提交
167
	node.replica.freeAll()
B
bigsheeper 已提交
168 169 170

	// close services
	if node.dataSyncService != nil {
X
XuanYang-cn 已提交
171
		node.dataSyncService.close()
B
bigsheeper 已提交
172 173
	}
	if node.searchService != nil {
X
XuanYang-cn 已提交
174
		node.searchService.close()
B
bigsheeper 已提交
175
	}
B
bigsheeper 已提交
176 177 178
	if node.loadIndexService != nil {
		node.loadIndexService.close()
	}
B
bigsheeper 已提交
179
	if node.statsService != nil {
X
XuanYang-cn 已提交
180
		node.statsService.close()
B
bigsheeper 已提交
181
	}
182 183 184
	if node.closer != nil {
		node.closer.Close()
	}
N
neza2017 已提交
185
	return nil
X
XuanYang-cn 已提交
186 187
}

C
cai.zhang 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
func (node *QueryNode) GetComponentStates() (*internalpb2.ComponentStates, error) {
	code, ok := node.stateCode.Load().(internalpb2.StateCode)
	if !ok {
		return nil, errors.New("unexpected error in type assertion")
	}
	info := &internalpb2.ComponentInfo{
		NodeID:    Params.QueryNodeID,
		Role:      "query-node",
		StateCode: code,
	}
	stats := &internalpb2.ComponentStates{
		State: info,
	}
	return stats, nil
}

func (node *QueryNode) GetTimeTickChannel() (string, error) {
N
neza2017 已提交
205
	return Params.QueryTimeTickChannelName, nil
C
cai.zhang 已提交
206 207 208 209 210 211
}

func (node *QueryNode) GetStatisticsChannel() (string, error) {
	return Params.StatsChannelName, nil
}

X
XuanYang-cn 已提交
212 213 214 215 216 217 218 219 220 221 222
func (node *QueryNode) AddQueryChannel(in *queryPb.AddQueryChannelsRequest) (*commonpb.Status, error) {
	if node.searchService == nil || node.searchService.searchMsgStream == nil {
		errMsg := "null search service or null search message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

X
Xiangyu Wang 已提交
223
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
224 225 226 227 228 229 230 231 232 233
	if !ok {
		errMsg := "type assertion failed for search message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

X
Xiangyu Wang 已提交
234
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248
	if !ok {
		errMsg := "type assertion failed for search result message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

	// add request channel
	pulsarBufSize := Params.SearchPulsarBufSize
	consumeChannels := []string{in.RequestChannelID}
	consumeSubName := Params.MsgChannelSubName
X
Xiangyu Wang 已提交
249
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	searchStream.CreatePulsarConsumers(consumeChannels, consumeSubName, unmarshalDispatcher, pulsarBufSize)

	// add result channel
	producerChannels := []string{in.ResultChannelID}
	resultStream.CreatePulsarProducers(producerChannels)

	status := &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
	}
	return status, nil
}

func (node *QueryNode) RemoveQueryChannel(in *queryPb.RemoveQueryChannelsRequest) (*commonpb.Status, error) {
	if node.searchService == nil || node.searchService.searchMsgStream == nil {
		errMsg := "null search service or null search result message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

X
Xiangyu Wang 已提交
273
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
274 275 276 277 278 279 280 281 282 283
	if !ok {
		errMsg := "type assertion failed for search message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

X
Xiangyu Wang 已提交
284
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if !ok {
		errMsg := "type assertion failed for search result message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

	// remove request channel
	pulsarBufSize := Params.SearchPulsarBufSize
	consumeChannels := []string{in.RequestChannelID}
	consumeSubName := Params.MsgChannelSubName
X
Xiangyu Wang 已提交
299
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	// TODO: searchStream.RemovePulsarConsumers(producerChannels)
	searchStream.CreatePulsarConsumers(consumeChannels, consumeSubName, unmarshalDispatcher, pulsarBufSize)

	// remove result channel
	producerChannels := []string{in.ResultChannelID}
	// TODO: resultStream.RemovePulsarProducer(producerChannels)
	resultStream.CreatePulsarProducers(producerChannels)

	status := &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
	}
	return status, nil
}

func (node *QueryNode) WatchDmChannels(in *queryPb.WatchDmChannelsRequest) (*commonpb.Status, error) {
	if node.dataSyncService == nil || node.dataSyncService.dmStream == nil {
		errMsg := "null data sync service or null data manipulation stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

X
Xiangyu Wang 已提交
325
	fgDMMsgStream, ok := node.dataSyncService.dmStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339
	if !ok {
		errMsg := "type assertion failed for dm message stream"
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    errMsg,
		}

		return status, errors.New(errMsg)
	}

	// add request channel
	pulsarBufSize := Params.SearchPulsarBufSize
	consumeChannels := in.ChannelIDs
	consumeSubName := Params.MsgChannelSubName
X
Xiangyu Wang 已提交
340
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
341 342 343 344 345 346 347 348 349 350
	fgDMMsgStream.CreatePulsarConsumers(consumeChannels, consumeSubName, unmarshalDispatcher, pulsarBufSize)

	status := &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
	}
	return status, nil
}

func (node *QueryNode) LoadSegments(in *queryPb.LoadSegmentRequest) (*commonpb.Status, error) {
	// TODO: support db
Z
zhenshan.cao 已提交
351
	collectionID := in.CollectionID
C
cai.zhang 已提交
352 353
	partitionID := in.PartitionID
	segmentIDs := in.SegmentIDs
X
XuanYang-cn 已提交
354
	fieldIDs := in.FieldIDs
C
cai.zhang 已提交
355 356 357 358 359
	err := node.segManager.loadSegment(collectionID, partitionID, segmentIDs, fieldIDs)
	if err != nil {
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
Z
zhenshan.cao 已提交
360
		}
C
cai.zhang 已提交
361
		return status, err
Z
zhenshan.cao 已提交
362
	}
C
cai.zhang 已提交
363 364 365 366 367 368 369
	return nil, nil
}

func (node *QueryNode) ReleaseSegments(in *queryPb.ReleaseSegmentRequest) (*commonpb.Status, error) {
	// release all fields in the segments
	for _, id := range in.SegmentIDs {
		err := node.segManager.releaseSegment(id)
X
XuanYang-cn 已提交
370 371 372 373 374 375 376 377 378 379
		if err != nil {
			status := &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			}
			return status, err
		}
	}
	return nil, nil
}