query_node.go 13.2 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
	"fmt"
19
	"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
20
	"io"
21
	"log"
C
cai.zhang 已提交
22
	"sync/atomic"
23 24 25

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

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

35 36 37 38 39 40 41 42 43 44
type Node interface {
	typeutil.Component

	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)
}

X
xige-16 已提交
45
type QueryService = typeutil.QueryServiceInterface
46

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

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

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

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

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

65 66
	segManager *segmentManager

67 68 69
	//opentracing
	tracer opentracing.Tracer
	closer io.Closer
70 71

	// clients
72
	queryClient QueryServiceInterface
73 74
	indexClient IndexServiceInterface
	dataClient  DataServiceInterface
B
bigsheeper 已提交
75
}
76

77
func NewQueryNode(ctx context.Context, queryNodeID uint64) *QueryNode {
X
XuanYang-cn 已提交
78
	ctx1, cancel := context.WithCancel(ctx)
C
cai.zhang 已提交
79
	node := &QueryNode{
80 81 82 83 84 85 86 87
		queryNodeLoopCtx:    ctx1,
		queryNodeLoopCancel: cancel,
		QueryNodeID:         queryNodeID,

		dataSyncService: nil,
		metaService:     nil,
		searchService:   nil,
		statsService:    nil,
X
XuanYang-cn 已提交
88
		segManager:      nil,
89 90 91 92 93 94 95 96 97 98
	}

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

B
bigsheeper 已提交
105
	segmentsMap := make(map[int64]*Segment)
106
	collections := make([]*Collection, 0)
B
bigsheeper 已提交
107

108 109
	tSafe := newTSafe()

C
cai.zhang 已提交
110
	node.replica = &collectionReplicaImpl{
G
godchen 已提交
111 112
		collections: collections,
		segments:    segmentsMap,
113 114

		tSafe: tSafe,
G
godchen 已提交
115
	}
C
cai.zhang 已提交
116 117 118
	node.stateCode.Store(internalpb2.StateCode_INITIALIZING)
	return node
}
G
godchen 已提交
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
func NewQueryNodeWithoutID(ctx context.Context) *QueryNode {
	ctx1, cancel := context.WithCancel(ctx)
	node := &QueryNode{
		queryNodeLoopCtx:    ctx1,
		queryNodeLoopCancel: cancel,

		dataSyncService: nil,
		metaService:     nil,
		searchService:   nil,
		statsService:    nil,
		segManager:      nil,
	}

	var err error
	cfg := &config.Configuration{
		ServiceName: "query_node",
		Sampler: &config.SamplerConfig{
			Type:  "const",
			Param: 1,
		},
	}
	node.tracer, node.closer, err = cfg.NewTracer()
	if err != nil {
		panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
	}
	opentracing.SetGlobalTracer(node.tracer)

	segmentsMap := make(map[int64]*Segment)
	collections := make([]*Collection, 0)

	tSafe := newTSafe()

	node.replica = &collectionReplicaImpl{
		collections: collections,
		segments:    segmentsMap,

		tSafe: tSafe,
	}
	node.stateCode.Store(internalpb2.StateCode_INITIALIZING)
	return node
}

C
cai.zhang 已提交
162 163 164
// TODO: delete this and call node.Init()
func Init() {
	Params.Init()
B
bigsheeper 已提交
165 166
}

N
neza2017 已提交
167
func (node *QueryNode) Init() error {
168 169 170 171 172
	Params.Init()
	return nil
}

func (node *QueryNode) Start() error {
X
xige-16 已提交
173
	registerReq := &queryPb.RegisterNodeRequest{
C
cai.zhang 已提交
174 175 176 177 178
		Address: &commonpb.Address{
			Ip:   Params.QueryNodeIP,
			Port: Params.QueryNodePort,
		},
	}
179 180

	response, err := node.queryClient.RegisterNode(registerReq)
C
cai.zhang 已提交
181 182 183 184 185 186 187
	if err != nil {
		panic(err)
	}
	if response.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		panic(response.Status.Reason)
	}

188 189
	Params.QueryNodeID = response.InitParams.NodeID
	fmt.Println("QueryNodeID is", Params.QueryNodeID)
C
cai.zhang 已提交
190

191 192 193 194 195 196 197 198
	if node.indexClient == nil {
		log.Println("WARN: null index service detected")
	}

	if node.dataClient == nil {
		log.Println("WARN: null data service detected")
	}

X
XuanYang-cn 已提交
199
	// todo add connectMaster logic
X
XuanYang-cn 已提交
200
	// init services and manager
X
XuanYang-cn 已提交
201 202 203
	node.dataSyncService = newDataSyncService(node.queryNodeLoopCtx, node.replica)
	node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica)
	node.metaService = newMetaService(node.queryNodeLoopCtx, node.replica)
204 205
	node.loadIndexService = newLoadIndexService(node.queryNodeLoopCtx, node.replica)
	node.statsService = newStatsService(node.queryNodeLoopCtx, node.replica, node.loadIndexService.fieldStatsChan)
206
	node.segManager = newSegmentManager(node.queryNodeLoopCtx, node.dataClient, node.indexClient, node.replica, node.dataSyncService.dmStream, node.loadIndexService.loadIndexReqChan)
B
bigsheeper 已提交
207

X
XuanYang-cn 已提交
208
	// start services
209
	go node.dataSyncService.start()
N
neza2017 已提交
210
	go node.searchService.start()
B
bigsheeper 已提交
211
	go node.metaService.start()
212
	go node.loadIndexService.start()
X
XuanYang-cn 已提交
213
	go node.statsService.start()
214

C
cai.zhang 已提交
215
	node.stateCode.Store(internalpb2.StateCode_HEALTHY)
216
	<-node.queryNodeLoopCtx.Done()
N
neza2017 已提交
217
	return nil
B
bigsheeper 已提交
218
}
B
bigsheeper 已提交
219

N
neza2017 已提交
220
func (node *QueryNode) Stop() error {
C
cai.zhang 已提交
221
	node.stateCode.Store(internalpb2.StateCode_ABNORMAL)
X
XuanYang-cn 已提交
222 223
	node.queryNodeLoopCancel()

B
bigsheeper 已提交
224
	// free collectionReplica
X
XuanYang-cn 已提交
225
	node.replica.freeAll()
B
bigsheeper 已提交
226 227 228

	// close services
	if node.dataSyncService != nil {
X
XuanYang-cn 已提交
229
		node.dataSyncService.close()
B
bigsheeper 已提交
230 231
	}
	if node.searchService != nil {
X
XuanYang-cn 已提交
232
		node.searchService.close()
B
bigsheeper 已提交
233
	}
B
bigsheeper 已提交
234 235 236
	if node.loadIndexService != nil {
		node.loadIndexService.close()
	}
B
bigsheeper 已提交
237
	if node.statsService != nil {
X
XuanYang-cn 已提交
238
		node.statsService.close()
B
bigsheeper 已提交
239
	}
240 241 242
	if node.closer != nil {
		node.closer.Close()
	}
N
neza2017 已提交
243
	return nil
X
XuanYang-cn 已提交
244 245
}

246 247 248 249 250 251 252 253
func (node *QueryNode) SetQueryService(query QueryServiceInterface) error {
	if query == nil {
		return errors.New("query index service interface")
	}
	node.queryClient = query
	return nil
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
func (node *QueryNode) SetIndexService(index IndexServiceInterface) error {
	if index == nil {
		return errors.New("null index service interface")
	}
	node.indexClient = index
	return nil
}

func (node *QueryNode) SetDataService(data DataServiceInterface) error {
	if data == nil {
		return errors.New("null data service interface")
	}
	node.dataClient = data
	return nil
}

C
cai.zhang 已提交
270 271 272 273 274 275 276
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,
X
XuanYang-cn 已提交
277
		Role:      typeutil.QueryNodeRole,
C
cai.zhang 已提交
278 279 280 281 282 283 284 285 286
		StateCode: code,
	}
	stats := &internalpb2.ComponentStates{
		State: info,
	}
	return stats, nil
}

func (node *QueryNode) GetTimeTickChannel() (string, error) {
N
neza2017 已提交
287
	return Params.QueryTimeTickChannelName, nil
C
cai.zhang 已提交
288 289 290 291 292 293
}

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

X
XuanYang-cn 已提交
294 295 296 297 298 299 300 301 302 303 304
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 已提交
305
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
306 307 308 309 310 311 312 313 314 315
	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 已提交
316
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330
	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 已提交
331
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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 已提交
355
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
356 357 358 359 360 361 362 363 364 365
	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 已提交
366
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380
	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 已提交
381
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	// 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 已提交
407
	fgDMMsgStream, ok := node.dataSyncService.dmStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421
	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 已提交
422
	unmarshalDispatcher := util.NewUnmarshalDispatcher()
X
XuanYang-cn 已提交
423 424 425 426 427 428 429 430 431 432
	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 已提交
433
	collectionID := in.CollectionID
C
cai.zhang 已提交
434 435
	partitionID := in.PartitionID
	segmentIDs := in.SegmentIDs
X
XuanYang-cn 已提交
436
	fieldIDs := in.FieldIDs
437

C
cai.zhang 已提交
438 439 440 441 442 443 444 445 446
	err := node.replica.enablePartitionDM(collectionID, partitionID)
	if err != nil {
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}
		return status, err
	}

447 448 449
	// segments are ordered before LoadSegments calling
	if in.LastSegmentState.State == datapb.SegmentState_SegmentGrowing {
		segmentNum := len(segmentIDs)
C
cai.zhang 已提交
450 451 452 453 454 455 456 457 458
		positions := in.LastSegmentState.StartPositions
		err = node.segManager.seekSegment(positions)
		if err != nil {
			status := &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			}
			return status, err
		}
459 460 461
		segmentIDs = segmentIDs[:segmentNum-1]
	}

C
cai.zhang 已提交
462
	err = node.segManager.loadSegment(collectionID, partitionID, segmentIDs, fieldIDs)
C
cai.zhang 已提交
463 464 465 466
	if err != nil {
		status := &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
Z
zhenshan.cao 已提交
467
		}
C
cai.zhang 已提交
468
		return status, err
Z
zhenshan.cao 已提交
469
	}
C
cai.zhang 已提交
470 471 472 473
	return nil, nil
}

func (node *QueryNode) ReleaseSegments(in *queryPb.ReleaseSegmentRequest) (*commonpb.Status, error) {
C
cai.zhang 已提交
474 475 476 477 478 479 480 481 482 483 484
	for _, id := range in.PartitionIDs {
		err := node.replica.enablePartitionDM(in.CollectionID, id)
		if err != nil {
			status := &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			}
			return status, err
		}
	}

C
cai.zhang 已提交
485 486 487
	// release all fields in the segments
	for _, id := range in.SegmentIDs {
		err := node.segManager.releaseSegment(id)
X
XuanYang-cn 已提交
488 489 490 491 492 493 494 495 496 497
		if err != nil {
			status := &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			}
			return status, err
		}
	}
	return nil, nil
}