query_node.go 12.9 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"
20
	"log"
C
cai.zhang 已提交
21
	"sync/atomic"
22 23 24

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

X
Xiangyu Wang 已提交
26
	"github.com/zilliztech/milvus-distributed/internal/msgstream/pulsarms"
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
	"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
B
bigsheeper 已提交
31 32
)

33 34 35 36 37 38 39 40 41 42
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 已提交
43
type QueryService = typeutil.QueryServiceInterface
44

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

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

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

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

56
	// internal services
57 58 59 60 61
	dataSyncService *dataSyncService
	metaService     *metaService
	searchService   *searchService
	loadService     *loadService
	statsService    *statsService
62

63 64 65
	//opentracing
	tracer opentracing.Tracer
	closer io.Closer
66 67

	// clients
B
bigsheeper 已提交
68 69 70 71
	masterClient MasterServiceInterface
	queryClient  QueryServiceInterface
	indexClient  IndexServiceInterface
	dataClient   DataServiceInterface
B
bigsheeper 已提交
72
}
73

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

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

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

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

104 105
	tSafe := newTSafe()

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

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

116 117 118 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
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,
	}

	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 已提交
157 158 159
// TODO: delete this and call node.Init()
func Init() {
	Params.Init()
B
bigsheeper 已提交
160 161
}

N
neza2017 已提交
162
func (node *QueryNode) Init() error {
163
	Params.Init()
X
xige-16 已提交
164
	registerReq := &queryPb.RegisterNodeRequest{
C
cai.zhang 已提交
165 166 167 168 169
		Address: &commonpb.Address{
			Ip:   Params.QueryNodeIP,
			Port: Params.QueryNodePort,
		},
	}
170 171

	response, err := node.queryClient.RegisterNode(registerReq)
C
cai.zhang 已提交
172 173 174 175 176 177 178
	if err != nil {
		panic(err)
	}
	if response.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		panic(response.Status.Reason)
	}

179 180
	Params.QueryNodeID = response.InitParams.NodeID
	fmt.Println("QueryNodeID is", Params.QueryNodeID)
C
cai.zhang 已提交
181

182 183 184 185
	if node.masterClient == nil {
		log.Println("WARN: null master service detected")
	}

186 187 188 189 190 191 192 193
	if node.indexClient == nil {
		log.Println("WARN: null index service detected")
	}

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

194 195 196 197
	return nil
}

func (node *QueryNode) Start() error {
X
XuanYang-cn 已提交
198
	// init services and manager
X
XuanYang-cn 已提交
199 200 201
	node.dataSyncService = newDataSyncService(node.queryNodeLoopCtx, node.replica)
	node.searchService = newSearchService(node.queryNodeLoopCtx, node.replica)
	node.metaService = newMetaService(node.queryNodeLoopCtx, node.replica)
202 203
	node.loadService = newLoadService(node.queryNodeLoopCtx, node.masterClient, node.dataClient, node.indexClient, node.replica, node.dataSyncService.dmStream)
	node.statsService = newStatsService(node.queryNodeLoopCtx, node.replica, node.loadService.fieldStatsChan)
B
bigsheeper 已提交
204

X
XuanYang-cn 已提交
205
	// start services
206
	go node.dataSyncService.start()
N
neza2017 已提交
207
	go node.searchService.start()
B
bigsheeper 已提交
208
	go node.metaService.start()
209
	go node.loadService.start()
X
XuanYang-cn 已提交
210
	go node.statsService.start()
211

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

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

B
bigsheeper 已提交
221
	// free collectionReplica
X
XuanYang-cn 已提交
222
	node.replica.freeAll()
B
bigsheeper 已提交
223 224 225

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

B
bigsheeper 已提交
243 244 245 246 247 248 249 250
func (node *QueryNode) SetMasterService(master MasterServiceInterface) error {
	if master == nil {
		return errors.New("null master service interface")
	}
	node.masterClient = master
	return nil
}

251 252
func (node *QueryNode) SetQueryService(query QueryServiceInterface) error {
	if query == nil {
B
bigsheeper 已提交
253
		return errors.New("null query service interface")
254 255 256 257 258
	}
	node.queryClient = query
	return nil
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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 已提交
275 276 277 278 279 280 281
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 已提交
282
		Role:      typeutil.QueryNodeRole,
C
cai.zhang 已提交
283 284 285 286 287 288 289 290 291
		StateCode: code,
	}
	stats := &internalpb2.ComponentStates{
		State: info,
	}
	return stats, nil
}

func (node *QueryNode) GetTimeTickChannel() (string, error) {
N
neza2017 已提交
292
	return Params.QueryTimeTickChannelName, nil
C
cai.zhang 已提交
293 294 295 296 297 298
}

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

X
XuanYang-cn 已提交
299 300 301 302 303 304 305 306 307 308 309
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 已提交
310
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
311 312 313 314 315 316 317 318 319 320
	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 已提交
321
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
	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
	consumeChannels := []string{in.RequestChannelID}
	consumeSubName := Params.MsgChannelSubName
335
	searchStream.CreatePulsarConsumers(consumeChannels, consumeSubName)
X
XuanYang-cn 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

	// 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 已提交
358
	searchStream, ok := node.searchService.searchMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
359 360 361 362 363 364 365 366 367 368
	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 已提交
369
	resultStream, ok := node.searchService.searchResultMsgStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383
	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
	consumeChannels := []string{in.RequestChannelID}
	consumeSubName := Params.MsgChannelSubName
	// TODO: searchStream.RemovePulsarConsumers(producerChannels)
384
	searchStream.CreatePulsarConsumers(consumeChannels, consumeSubName)
X
XuanYang-cn 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

	// 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 已提交
408
	fgDMMsgStream, ok := node.dataSyncService.dmStream.(*pulsarms.PulsarMsgStream)
X
XuanYang-cn 已提交
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
	consumeChannels := in.ChannelIDs
	consumeSubName := Params.MsgChannelSubName
422
	fgDMMsgStream.CreatePulsarConsumers(consumeChannels, consumeSubName)
X
XuanYang-cn 已提交
423 424 425 426 427 428 429 430 431

	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 已提交
432
	collectionID := in.CollectionID
C
cai.zhang 已提交
433 434
	partitionID := in.PartitionID
	segmentIDs := in.SegmentIDs
X
XuanYang-cn 已提交
435
	fieldIDs := in.FieldIDs
436

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

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

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

func (node *QueryNode) ReleaseSegments(in *queryPb.ReleaseSegmentRequest) (*commonpb.Status, error) {
C
cai.zhang 已提交
475 476 477 478 479 480 481 482 483 484 485
	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 已提交
486 487
	// release all fields in the segments
	for _, id := range in.SegmentIDs {
488
		err := node.loadService.releaseSegment(id)
X
XuanYang-cn 已提交
489 490 491 492 493 494 495 496
		if err != nil {
			status := &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			}
			return status, err
		}
	}
497 498 499
	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
	}, nil
X
XuanYang-cn 已提交
500
}