impl.go 148.6 KB
Newer Older
1 2 3 4 5 6
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
7 8
// with the License. You may obtain a copy of the License at
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11 12 13 14 15
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
16

C
Cai Yudong 已提交
17
package proxy
18 19 20

import (
	"context"
21
	"errors"
22
	"fmt"
C
cai.zhang 已提交
23
	"os"
24 25
	"strconv"

26 27 28
	"github.com/golang/protobuf/proto"
	"github.com/milvus-io/milvus/api/commonpb"
	"github.com/milvus-io/milvus/api/milvuspb"
29
	"github.com/milvus-io/milvus/internal/common"
X
Xiangyu Wang 已提交
30
	"github.com/milvus-io/milvus/internal/log"
31
	"github.com/milvus-io/milvus/internal/metrics"
J
jaime 已提交
32
	"github.com/milvus-io/milvus/internal/mq/msgstream"
X
Xiangyu Wang 已提交
33 34 35 36
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/proto/proxypb"
	"github.com/milvus-io/milvus/internal/proto/querypb"
37
	"github.com/milvus-io/milvus/internal/util"
38
	"github.com/milvus-io/milvus/internal/util/crypto"
39
	"github.com/milvus-io/milvus/internal/util/errorutil"
40 41
	"github.com/milvus-io/milvus/internal/util/logutil"
	"github.com/milvus-io/milvus/internal/util/metricsinfo"
42
	"github.com/milvus-io/milvus/internal/util/timerecord"
43
	"github.com/milvus-io/milvus/internal/util/trace"
X
Xiangyu Wang 已提交
44
	"github.com/milvus-io/milvus/internal/util/typeutil"
45 46
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
47 48
)

49 50
const moduleName = "Proxy"

51
// UpdateStateCode updates the state code of Proxy.
C
Cai Yudong 已提交
52
func (node *Proxy) UpdateStateCode(code internalpb.StateCode) {
53
	node.stateCode.Store(code)
Z
zhenshan.cao 已提交
54 55
}

56
// GetComponentStates get state of Proxy.
C
Cai Yudong 已提交
57
func (node *Proxy) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
G
godchen 已提交
58 59 60 61 62 63 64 65 66 67 68 69
	stats := &internalpb.ComponentStates{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
		},
	}
	code, ok := node.stateCode.Load().(internalpb.StateCode)
	if !ok {
		errMsg := "unexpected error in type assertion"
		stats.Status = &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    errMsg,
		}
G
godchen 已提交
70
		return stats, nil
G
godchen 已提交
71
	}
72 73 74 75
	nodeID := common.NotRegisteredID
	if node.session != nil && node.session.Registered() {
		nodeID = node.session.ServerID
	}
G
godchen 已提交
76
	info := &internalpb.ComponentInfo{
77 78
		// NodeID:    Params.ProxyID, // will race with Proxy.Register()
		NodeID:    nodeID,
C
Cai Yudong 已提交
79
		Role:      typeutil.ProxyRole,
G
godchen 已提交
80 81 82 83 84 85
		StateCode: code,
	}
	stats.State = info
	return stats, nil
}

C
cxytz01 已提交
86
// GetStatisticsChannel gets statistics channel of Proxy.
C
Cai Yudong 已提交
87
func (node *Proxy) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
G
godchen 已提交
88 89 90 91 92 93 94 95 96
	return &milvuspb.StringResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
			Reason:    "",
		},
		Value: "",
	}, nil
}

97
// InvalidateCollectionMetaCache invalidate the meta cache of specific collection.
C
Cai Yudong 已提交
98
func (node *Proxy) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
99
	ctx = logutil.WithModule(ctx, moduleName)
100
	logutil.Logger(ctx).Info("received request to invalidate collection meta cache",
101
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
102
		zap.String("db", request.DbName),
103 104
		zap.String("collectionName", request.CollectionName),
		zap.Int64("collectionID", request.CollectionID))
D
dragondriver 已提交
105

106
	collectionName := request.CollectionName
107
	collectionID := request.CollectionID
N
neza2017 已提交
108
	if globalMetaCache != nil {
109 110 111 112 113 114
		if collectionName != "" {
			globalMetaCache.RemoveCollection(ctx, collectionName) // no need to return error, though collection may be not cached
		}
		if request.CollectionID != UniqueID(0) {
			globalMetaCache.RemoveCollectionsByID(ctx, collectionID)
		}
N
neza2017 已提交
115
	}
116
	logutil.Logger(ctx).Info("complete to invalidate collection meta cache",
117
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
118
		zap.String("db", request.DbName),
119 120
		zap.String("collection", collectionName),
		zap.Int64("collectionID", collectionID))
D
dragondriver 已提交
121

122
	return &commonpb.Status{
123
		ErrorCode: commonpb.ErrorCode_Success,
124 125
		Reason:    "",
	}, nil
126 127
}

128
// CreateCollection create a collection by the schema.
129
// TODO(dragondriver): add more detailed ut for ConsistencyLevel, should we support multiple consistency level in Proxy?
C
Cai Yudong 已提交
130
func (node *Proxy) CreateCollection(ctx context.Context, request *milvuspb.CreateCollectionRequest) (*commonpb.Status, error) {
131 132 133
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
134 135 136 137

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-CreateCollection")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
138 139 140
	method := "CreateCollection"
	tr := timerecord.NewTimeRecorder(method)

X
Xiaofan 已提交
141
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
142

143
	cct := &createCollectionTask{
S
sunby 已提交
144
		ctx:                     ctx,
145 146
		Condition:               NewTaskCondition(ctx),
		CreateCollectionRequest: request,
147
		rootCoord:               node.rootCoord,
148 149
	}

150 151 152
	// avoid data race
	lenOfSchema := len(request.Schema)

153 154
	log.Debug(
		rpcReceived(method),
155
		zap.String("traceID", traceID),
156
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
157 158
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
159
		zap.Int("len(schema)", lenOfSchema),
160 161
		zap.Int32("shards_num", request.ShardsNum),
		zap.String("consistency_level", request.ConsistencyLevel.String()))
162

163 164 165
	if err := node.sched.ddQueue.Enqueue(cct); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
166 167
			zap.Error(err),
			zap.String("traceID", traceID),
168
			zap.String("role", typeutil.ProxyRole),
169 170 171
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Int("len(schema)", lenOfSchema),
172 173
			zap.Int32("shards_num", request.ShardsNum),
			zap.String("consistency_level", request.ConsistencyLevel.String()))
174

X
Xiaofan 已提交
175
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
176
		return &commonpb.Status{
177
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
178 179 180 181
			Reason:    err.Error(),
		}, nil
	}

182 183
	log.Debug(
		rpcEnqueued(method),
184
		zap.String("traceID", traceID),
185
		zap.String("role", typeutil.ProxyRole),
186 187 188
		zap.Int64("MsgID", cct.ID()),
		zap.Uint64("BeginTs", cct.BeginTs()),
		zap.Uint64("EndTs", cct.EndTs()),
D
dragondriver 已提交
189 190
		zap.Uint64("timestamp", request.Base.Timestamp),
		zap.String("db", request.DbName),
191 192
		zap.String("collection", request.CollectionName),
		zap.Int("len(schema)", lenOfSchema),
193 194
		zap.Int32("shards_num", request.ShardsNum),
		zap.String("consistency_level", request.ConsistencyLevel.String()))
195

196 197 198
	if err := cct.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
199
			zap.Error(err),
200
			zap.String("traceID", traceID),
201
			zap.String("role", typeutil.ProxyRole),
202 203 204
			zap.Int64("MsgID", cct.ID()),
			zap.Uint64("BeginTs", cct.BeginTs()),
			zap.Uint64("EndTs", cct.EndTs()),
D
dragondriver 已提交
205 206
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
207
			zap.Int("len(schema)", lenOfSchema),
208 209
			zap.Int32("shards_num", request.ShardsNum),
			zap.String("consistency_level", request.ConsistencyLevel.String()))
D
dragondriver 已提交
210

X
Xiaofan 已提交
211
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
212
		return &commonpb.Status{
213
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
214 215 216 217
			Reason:    err.Error(),
		}, nil
	}

218 219
	log.Debug(
		rpcDone(method),
220
		zap.String("traceID", traceID),
221
		zap.String("role", typeutil.ProxyRole),
222 223 224 225 226 227
		zap.Int64("MsgID", cct.ID()),
		zap.Uint64("BeginTs", cct.BeginTs()),
		zap.Uint64("EndTs", cct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Int("len(schema)", lenOfSchema),
228 229
		zap.Int32("shards_num", request.ShardsNum),
		zap.String("consistency_level", request.ConsistencyLevel.String()))
230

X
Xiaofan 已提交
231 232
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
233 234 235
	return cct.result, nil
}

236
// DropCollection drop a collection.
C
Cai Yudong 已提交
237
func (node *Proxy) DropCollection(ctx context.Context, request *milvuspb.DropCollectionRequest) (*commonpb.Status, error) {
238 239 240
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
241 242 243 244

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DropCollection")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
245 246
	method := "DropCollection"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
247
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
248

249
	dct := &dropCollectionTask{
S
sunby 已提交
250
		ctx:                   ctx,
251 252
		Condition:             NewTaskCondition(ctx),
		DropCollectionRequest: request,
253
		rootCoord:             node.rootCoord,
254
		chMgr:                 node.chMgr,
S
sunby 已提交
255
		chTicker:              node.chTicker,
256 257
	}

258 259
	log.Debug("DropCollection received",
		zap.String("traceID", traceID),
260
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
261 262
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
263 264 265 266 267

	if err := node.sched.ddQueue.Enqueue(dct); err != nil {
		log.Warn("DropCollection failed to enqueue",
			zap.Error(err),
			zap.String("traceID", traceID),
268
			zap.String("role", typeutil.ProxyRole),
269 270 271
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
272
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
273
		return &commonpb.Status{
274
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
275 276 277 278
			Reason:    err.Error(),
		}, nil
	}

279 280
	log.Debug("DropCollection enqueued",
		zap.String("traceID", traceID),
281
		zap.String("role", typeutil.ProxyRole),
282 283 284
		zap.Int64("MsgID", dct.ID()),
		zap.Uint64("BeginTs", dct.BeginTs()),
		zap.Uint64("EndTs", dct.EndTs()),
D
dragondriver 已提交
285 286
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
287 288 289

	if err := dct.WaitToFinish(); err != nil {
		log.Warn("DropCollection failed to WaitToFinish",
D
dragondriver 已提交
290
			zap.Error(err),
291
			zap.String("traceID", traceID),
292
			zap.String("role", typeutil.ProxyRole),
293 294 295
			zap.Int64("MsgID", dct.ID()),
			zap.Uint64("BeginTs", dct.BeginTs()),
			zap.Uint64("EndTs", dct.EndTs()),
D
dragondriver 已提交
296 297 298
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
299
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
300
		return &commonpb.Status{
301
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
302 303 304 305
			Reason:    err.Error(),
		}, nil
	}

306 307
	log.Debug("DropCollection done",
		zap.String("traceID", traceID),
308
		zap.String("role", typeutil.ProxyRole),
309 310 311 312 313 314
		zap.Int64("MsgID", dct.ID()),
		zap.Uint64("BeginTs", dct.BeginTs()),
		zap.Uint64("EndTs", dct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
315 316
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
317 318 319
	return dct.result, nil
}

320
// HasCollection check if the specific collection exists in Milvus.
C
Cai Yudong 已提交
321
func (node *Proxy) HasCollection(ctx context.Context, request *milvuspb.HasCollectionRequest) (*milvuspb.BoolResponse, error) {
322 323 324 325 326
	if !node.checkHealthy() {
		return &milvuspb.BoolResponse{
			Status: unhealthyStatus(),
		}, nil
	}
327 328 329 330

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-HasCollection")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
331 332
	method := "HasCollection"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
333
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
334
		metrics.TotalLabel).Inc()
335 336 337

	log.Debug("HasCollection received",
		zap.String("traceID", traceID),
338
		zap.String("role", typeutil.ProxyRole),
339 340 341
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

342
	hct := &hasCollectionTask{
S
sunby 已提交
343
		ctx:                  ctx,
344 345
		Condition:            NewTaskCondition(ctx),
		HasCollectionRequest: request,
346
		rootCoord:            node.rootCoord,
347 348
	}

349 350 351 352
	if err := node.sched.ddQueue.Enqueue(hct); err != nil {
		log.Warn("HasCollection failed to enqueue",
			zap.Error(err),
			zap.String("traceID", traceID),
353
			zap.String("role", typeutil.ProxyRole),
354 355 356
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
357
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
358
			metrics.AbandonLabel).Inc()
359 360
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
361
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
362 363 364 365 366
				Reason:    err.Error(),
			},
		}, nil
	}

367 368
	log.Debug("HasCollection enqueued",
		zap.String("traceID", traceID),
369
		zap.String("role", typeutil.ProxyRole),
370 371 372
		zap.Int64("MsgID", hct.ID()),
		zap.Uint64("BeginTS", hct.BeginTs()),
		zap.Uint64("EndTS", hct.EndTs()),
D
dragondriver 已提交
373 374
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
375 376 377

	if err := hct.WaitToFinish(); err != nil {
		log.Warn("HasCollection failed to WaitToFinish",
D
dragondriver 已提交
378
			zap.Error(err),
379
			zap.String("traceID", traceID),
380
			zap.String("role", typeutil.ProxyRole),
381 382 383
			zap.Int64("MsgID", hct.ID()),
			zap.Uint64("BeginTS", hct.BeginTs()),
			zap.Uint64("EndTS", hct.EndTs()),
D
dragondriver 已提交
384 385 386
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
387
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
388
			metrics.FailLabel).Inc()
389 390
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
391
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
392 393 394 395 396
				Reason:    err.Error(),
			},
		}, nil
	}

397 398
	log.Debug("HasCollection done",
		zap.String("traceID", traceID),
399
		zap.String("role", typeutil.ProxyRole),
400 401 402 403 404 405
		zap.Int64("MsgID", hct.ID()),
		zap.Uint64("BeginTS", hct.BeginTs()),
		zap.Uint64("EndTS", hct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
406
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
407
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
408
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
409 410 411
	return hct.result, nil
}

412
// LoadCollection load a collection into query nodes.
C
Cai Yudong 已提交
413
func (node *Proxy) LoadCollection(ctx context.Context, request *milvuspb.LoadCollectionRequest) (*commonpb.Status, error) {
414 415 416
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
417 418 419 420

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-LoadCollection")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
421 422
	method := "LoadCollection"
	tr := timerecord.NewTimeRecorder(method)
423

424
	lct := &loadCollectionTask{
S
sunby 已提交
425
		ctx:                   ctx,
426 427
		Condition:             NewTaskCondition(ctx),
		LoadCollectionRequest: request,
428
		queryCoord:            node.queryCoord,
429 430
	}

431 432
	log.Debug("LoadCollection received",
		zap.String("traceID", traceID),
433
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
434 435
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
436 437 438 439 440

	if err := node.sched.ddQueue.Enqueue(lct); err != nil {
		log.Warn("LoadCollection failed to enqueue",
			zap.Error(err),
			zap.String("traceID", traceID),
441
			zap.String("role", typeutil.ProxyRole),
442 443 444
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
445
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
446
			metrics.AbandonLabel).Inc()
447
		return &commonpb.Status{
448
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
449 450 451
			Reason:    err.Error(),
		}, nil
	}
D
dragondriver 已提交
452

453 454
	log.Debug("LoadCollection enqueued",
		zap.String("traceID", traceID),
455
		zap.String("role", typeutil.ProxyRole),
456 457 458
		zap.Int64("MsgID", lct.ID()),
		zap.Uint64("BeginTS", lct.BeginTs()),
		zap.Uint64("EndTS", lct.EndTs()),
D
dragondriver 已提交
459 460
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
461 462 463

	if err := lct.WaitToFinish(); err != nil {
		log.Warn("LoadCollection failed to WaitToFinish",
D
dragondriver 已提交
464
			zap.Error(err),
465
			zap.String("traceID", traceID),
466
			zap.String("role", typeutil.ProxyRole),
467 468 469
			zap.Int64("MsgID", lct.ID()),
			zap.Uint64("BeginTS", lct.BeginTs()),
			zap.Uint64("EndTS", lct.EndTs()),
D
dragondriver 已提交
470 471 472
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
473
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
474
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
475
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
476
			metrics.FailLabel).Inc()
477
		return &commonpb.Status{
478
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
479 480 481 482
			Reason:    err.Error(),
		}, nil
	}

483 484
	log.Debug("LoadCollection done",
		zap.String("traceID", traceID),
485
		zap.String("role", typeutil.ProxyRole),
486 487 488 489 490 491
		zap.Int64("MsgID", lct.ID()),
		zap.Uint64("BeginTS", lct.BeginTs()),
		zap.Uint64("EndTS", lct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
492
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
493
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
494
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
495
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
496
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
497
	return lct.result, nil
498 499
}

500
// ReleaseCollection remove the loaded collection from query nodes.
C
Cai Yudong 已提交
501
func (node *Proxy) ReleaseCollection(ctx context.Context, request *milvuspb.ReleaseCollectionRequest) (*commonpb.Status, error) {
502 503 504
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
505

506
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-ReleaseCollection")
507 508
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
509 510
	method := "ReleaseCollection"
	tr := timerecord.NewTimeRecorder(method)
511

512
	rct := &releaseCollectionTask{
S
sunby 已提交
513
		ctx:                      ctx,
514 515
		Condition:                NewTaskCondition(ctx),
		ReleaseCollectionRequest: request,
516
		queryCoord:               node.queryCoord,
517
		chMgr:                    node.chMgr,
518 519
	}

520 521
	log.Debug(
		rpcReceived(method),
522
		zap.String("traceID", traceID),
523
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
524 525
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
526 527

	if err := node.sched.ddQueue.Enqueue(rct); err != nil {
528 529
		log.Warn(
			rpcFailedToEnqueue(method),
530 531
			zap.Error(err),
			zap.String("traceID", traceID),
532
			zap.String("role", typeutil.ProxyRole),
533 534 535
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
536
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
537
			metrics.AbandonLabel).Inc()
538
		return &commonpb.Status{
539
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
540 541 542 543
			Reason:    err.Error(),
		}, nil
	}

544 545
	log.Debug(
		rpcEnqueued(method),
546
		zap.String("traceID", traceID),
547
		zap.String("role", typeutil.ProxyRole),
548 549 550
		zap.Int64("MsgID", rct.ID()),
		zap.Uint64("BeginTS", rct.BeginTs()),
		zap.Uint64("EndTS", rct.EndTs()),
D
dragondriver 已提交
551 552
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
553 554

	if err := rct.WaitToFinish(); err != nil {
555 556
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
557
			zap.Error(err),
558
			zap.String("traceID", traceID),
559
			zap.String("role", typeutil.ProxyRole),
560 561 562
			zap.Int64("MsgID", rct.ID()),
			zap.Uint64("BeginTS", rct.BeginTs()),
			zap.Uint64("EndTS", rct.EndTs()),
D
dragondriver 已提交
563 564 565
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
566
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
567
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
568
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
569
			metrics.FailLabel).Inc()
570
		return &commonpb.Status{
571
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
572 573 574 575
			Reason:    err.Error(),
		}, nil
	}

576 577
	log.Debug(
		rpcDone(method),
578
		zap.String("traceID", traceID),
579
		zap.String("role", typeutil.ProxyRole),
580 581 582 583 584 585
		zap.Int64("MsgID", rct.ID()),
		zap.Uint64("BeginTS", rct.BeginTs()),
		zap.Uint64("EndTS", rct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
586
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
587
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
588
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
589
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
590
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
591
	return rct.result, nil
592 593
}

594
// DescribeCollection get the meta information of specific collection, such as schema, created timestamp and etc.
C
Cai Yudong 已提交
595
func (node *Proxy) DescribeCollection(ctx context.Context, request *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) {
596 597 598 599 600
	if !node.checkHealthy() {
		return &milvuspb.DescribeCollectionResponse{
			Status: unhealthyStatus(),
		}, nil
	}
601

602
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DescribeCollection")
603 604
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
605 606
	method := "DescribeCollection"
	tr := timerecord.NewTimeRecorder(method)
607

608
	dct := &describeCollectionTask{
S
sunby 已提交
609
		ctx:                       ctx,
610 611
		Condition:                 NewTaskCondition(ctx),
		DescribeCollectionRequest: request,
612
		rootCoord:                 node.rootCoord,
613 614
	}

615 616
	log.Debug("DescribeCollection received",
		zap.String("traceID", traceID),
617
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
618 619
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
620 621 622 623 624

	if err := node.sched.ddQueue.Enqueue(dct); err != nil {
		log.Warn("DescribeCollection failed to enqueue",
			zap.Error(err),
			zap.String("traceID", traceID),
625
			zap.String("role", typeutil.ProxyRole),
626 627 628
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
629
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
630
			metrics.AbandonLabel).Inc()
631 632
		return &milvuspb.DescribeCollectionResponse{
			Status: &commonpb.Status{
633
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
634 635 636 637 638
				Reason:    err.Error(),
			},
		}, nil
	}

639 640
	log.Debug("DescribeCollection enqueued",
		zap.String("traceID", traceID),
641
		zap.String("role", typeutil.ProxyRole),
642 643 644
		zap.Int64("MsgID", dct.ID()),
		zap.Uint64("BeginTS", dct.BeginTs()),
		zap.Uint64("EndTS", dct.EndTs()),
D
dragondriver 已提交
645 646
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
647 648 649

	if err := dct.WaitToFinish(); err != nil {
		log.Warn("DescribeCollection failed to WaitToFinish",
D
dragondriver 已提交
650
			zap.Error(err),
651
			zap.String("traceID", traceID),
652
			zap.String("role", typeutil.ProxyRole),
653 654 655
			zap.Int64("MsgID", dct.ID()),
			zap.Uint64("BeginTS", dct.BeginTs()),
			zap.Uint64("EndTS", dct.EndTs()),
D
dragondriver 已提交
656 657 658
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
659
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
660
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
661
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
662
			metrics.FailLabel).Inc()
663

664 665
		return &milvuspb.DescribeCollectionResponse{
			Status: &commonpb.Status{
666
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
667 668 669 670 671
				Reason:    err.Error(),
			},
		}, nil
	}

672 673
	log.Debug("DescribeCollection done",
		zap.String("traceID", traceID),
674
		zap.String("role", typeutil.ProxyRole),
675 676 677 678 679 680
		zap.Int64("MsgID", dct.ID()),
		zap.Uint64("BeginTS", dct.BeginTs()),
		zap.Uint64("EndTS", dct.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
681
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
682
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
683
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
684
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
685
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
686 687 688
	return dct.result, nil
}

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
// GetStatistics get the statistics, such as `num_rows`.
// WARNING: It is an experimental API
func (node *Proxy) GetStatistics(ctx context.Context, request *milvuspb.GetStatisticsRequest) (*milvuspb.GetStatisticsResponse, error) {
	if !node.checkHealthy() {
		return &milvuspb.GetStatisticsResponse{
			Status: unhealthyStatus(),
		}, nil
	}

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-GetCollectionStatistics")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
	method := "GetStatistics"
	tr := timerecord.NewTimeRecorder(method)

	g := &getStatisticsTask{
		request:   request,
		Condition: NewTaskCondition(ctx),
		ctx:       ctx,
		tr:        tr,
		dc:        node.dataCoord,
		qc:        node.queryCoord,
		shardMgr:  node.shardMgr,
	}

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Strings("partitions", request.PartitionNames))

	if err := node.sched.ddQueue.Enqueue(g); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
			zap.String("role", typeutil.ProxyRole),
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Strings("partitions", request.PartitionNames))

		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.AbandonLabel).Inc()

		return &milvuspb.GetStatisticsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.Int64("msgID", g.ID()),
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Strings("partitions", request.PartitionNames))

	if err := g.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
			zap.Error(err),
			zap.String("traceID", traceID),
			zap.String("role", typeutil.ProxyRole),
			zap.Int64("MsgID", g.ID()),
			zap.Uint64("BeginTS", g.BeginTs()),
			zap.Uint64("EndTS", g.EndTs()),
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Strings("partitions", request.PartitionNames))

		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.TotalLabel).Inc()
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.FailLabel).Inc()

		return &milvuspb.GetStatisticsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.Int64("msgID", g.ID()),
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.TotalLabel).Inc()
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.SuccessLabel).Inc()
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
	return g.result, nil
}

798
// GetCollectionStatistics get the collection statistics, such as `num_rows`.
C
Cai Yudong 已提交
799
func (node *Proxy) GetCollectionStatistics(ctx context.Context, request *milvuspb.GetCollectionStatisticsRequest) (*milvuspb.GetCollectionStatisticsResponse, error) {
800 801 802 803 804
	if !node.checkHealthy() {
		return &milvuspb.GetCollectionStatisticsResponse{
			Status: unhealthyStatus(),
		}, nil
	}
805 806 807 808

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-GetCollectionStatistics")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
809 810
	method := "GetCollectionStatistics"
	tr := timerecord.NewTimeRecorder(method)
811

812
	g := &getCollectionStatisticsTask{
G
godchen 已提交
813 814 815
		ctx:                            ctx,
		Condition:                      NewTaskCondition(ctx),
		GetCollectionStatisticsRequest: request,
816
		dataCoord:                      node.dataCoord,
817 818
	}

819 820
	log.Debug(
		rpcReceived(method),
821
		zap.String("traceID", traceID),
822
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
823 824
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
825 826

	if err := node.sched.ddQueue.Enqueue(g); err != nil {
827 828
		log.Warn(
			rpcFailedToEnqueue(method),
829 830
			zap.Error(err),
			zap.String("traceID", traceID),
831
			zap.String("role", typeutil.ProxyRole),
832 833 834
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
835
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
836
			metrics.AbandonLabel).Inc()
837

G
godchen 已提交
838
		return &milvuspb.GetCollectionStatisticsResponse{
839
			Status: &commonpb.Status{
840
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
841 842 843 844 845
				Reason:    err.Error(),
			},
		}, nil
	}

846 847
	log.Debug(
		rpcEnqueued(method),
848
		zap.String("traceID", traceID),
849
		zap.String("role", typeutil.ProxyRole),
850
		zap.Int64("msgID", g.ID()),
851 852
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
D
dragondriver 已提交
853 854
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))
855 856

	if err := g.WaitToFinish(); err != nil {
857 858
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
859
			zap.Error(err),
860
			zap.String("traceID", traceID),
861
			zap.String("role", typeutil.ProxyRole),
862 863 864
			zap.Int64("MsgID", g.ID()),
			zap.Uint64("BeginTS", g.BeginTs()),
			zap.Uint64("EndTS", g.EndTs()),
D
dragondriver 已提交
865 866 867
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
868
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
869
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
870
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
871
			metrics.FailLabel).Inc()
872

G
godchen 已提交
873
		return &milvuspb.GetCollectionStatisticsResponse{
874
			Status: &commonpb.Status{
875
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
876 877 878 879 880
				Reason:    err.Error(),
			},
		}, nil
	}

881 882
	log.Debug(
		rpcDone(method),
883
		zap.String("traceID", traceID),
884
		zap.String("role", typeutil.ProxyRole),
885
		zap.Int64("msgID", g.ID()),
886 887 888 889 890
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
891
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
892
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
893
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
894
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
895
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
896
	return g.result, nil
897 898
}

899
// ShowCollections list all collections in Milvus.
C
Cai Yudong 已提交
900
func (node *Proxy) ShowCollections(ctx context.Context, request *milvuspb.ShowCollectionsRequest) (*milvuspb.ShowCollectionsResponse, error) {
901 902 903 904 905
	if !node.checkHealthy() {
		return &milvuspb.ShowCollectionsResponse{
			Status: unhealthyStatus(),
		}, nil
	}
906 907
	method := "ShowCollections"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
908
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
909

910
	sct := &showCollectionsTask{
G
godchen 已提交
911 912 913
		ctx:                    ctx,
		Condition:              NewTaskCondition(ctx),
		ShowCollectionsRequest: request,
914
		queryCoord:             node.queryCoord,
915
		rootCoord:              node.rootCoord,
916 917
	}

918
	log.Debug("ShowCollections received",
919
		zap.String("role", typeutil.ProxyRole),
920 921 922 923 924 925
		zap.String("DbName", request.DbName),
		zap.Uint64("TimeStamp", request.TimeStamp),
		zap.String("ShowType", request.Type.String()),
		zap.Any("CollectionNames", request.CollectionNames),
	)

926
	err := node.sched.ddQueue.Enqueue(sct)
927
	if err != nil {
928 929
		log.Warn("ShowCollections failed to enqueue",
			zap.Error(err),
930
			zap.String("role", typeutil.ProxyRole),
931 932 933 934 935 936
			zap.String("DbName", request.DbName),
			zap.Uint64("TimeStamp", request.TimeStamp),
			zap.String("ShowType", request.Type.String()),
			zap.Any("CollectionNames", request.CollectionNames),
		)

X
Xiaofan 已提交
937
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
G
godchen 已提交
938
		return &milvuspb.ShowCollectionsResponse{
939
			Status: &commonpb.Status{
940
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
941 942 943 944 945
				Reason:    err.Error(),
			},
		}, nil
	}

946
	log.Debug("ShowCollections enqueued",
947
		zap.String("role", typeutil.ProxyRole),
948
		zap.Int64("MsgID", sct.ID()),
949
		zap.String("DbName", sct.ShowCollectionsRequest.DbName),
950
		zap.Uint64("TimeStamp", request.TimeStamp),
951 952 953
		zap.String("ShowType", sct.ShowCollectionsRequest.Type.String()),
		zap.Any("CollectionNames", sct.ShowCollectionsRequest.CollectionNames),
	)
D
dragondriver 已提交
954

955 956
	err = sct.WaitToFinish()
	if err != nil {
957 958
		log.Warn("ShowCollections failed to WaitToFinish",
			zap.Error(err),
959
			zap.String("role", typeutil.ProxyRole),
960 961 962 963 964 965 966
			zap.Int64("MsgID", sct.ID()),
			zap.String("DbName", request.DbName),
			zap.Uint64("TimeStamp", request.TimeStamp),
			zap.String("ShowType", request.Type.String()),
			zap.Any("CollectionNames", request.CollectionNames),
		)

X
Xiaofan 已提交
967
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
968

G
godchen 已提交
969
		return &milvuspb.ShowCollectionsResponse{
970
			Status: &commonpb.Status{
971
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
972 973 974 975 976
				Reason:    err.Error(),
			},
		}, nil
	}

977
	log.Debug("ShowCollections Done",
978
		zap.String("role", typeutil.ProxyRole),
979 980 981 982
		zap.Int64("MsgID", sct.ID()),
		zap.String("DbName", request.DbName),
		zap.Uint64("TimeStamp", request.TimeStamp),
		zap.String("ShowType", request.Type.String()),
983 984
		zap.Int("len(CollectionNames)", len(request.CollectionNames)),
		zap.Int("num_collections", len(sct.result.CollectionNames)))
985

X
Xiaofan 已提交
986 987
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
988 989 990
	return sct.result, nil
}

991
// CreatePartition create a partition in specific collection.
C
Cai Yudong 已提交
992
func (node *Proxy) CreatePartition(ctx context.Context, request *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) {
993 994 995
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
996

997
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-CreatePartition")
998 999
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
1000 1001
	method := "CreatePartition"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
1002
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
1003

1004
	cpt := &createPartitionTask{
S
sunby 已提交
1005
		ctx:                    ctx,
1006 1007
		Condition:              NewTaskCondition(ctx),
		CreatePartitionRequest: request,
1008
		rootCoord:              node.rootCoord,
1009 1010 1011
		result:                 nil,
	}

1012 1013 1014
	log.Debug(
		rpcReceived("CreatePartition"),
		zap.String("traceID", traceID),
1015
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1016 1017 1018
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1019 1020 1021 1022 1023 1024

	if err := node.sched.ddQueue.Enqueue(cpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue("CreatePartition"),
			zap.Error(err),
			zap.String("traceID", traceID),
1025
			zap.String("role", typeutil.ProxyRole),
1026 1027 1028 1029
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1030
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
1031

1032
		return &commonpb.Status{
1033
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1034 1035 1036
			Reason:    err.Error(),
		}, nil
	}
D
dragondriver 已提交
1037

1038 1039 1040
	log.Debug(
		rpcEnqueued("CreatePartition"),
		zap.String("traceID", traceID),
1041
		zap.String("role", typeutil.ProxyRole),
1042 1043 1044
		zap.Int64("MsgID", cpt.ID()),
		zap.Uint64("BeginTS", cpt.BeginTs()),
		zap.Uint64("EndTS", cpt.EndTs()),
D
dragondriver 已提交
1045 1046 1047
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1048 1049 1050 1051

	if err := cpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish("CreatePartition"),
D
dragondriver 已提交
1052
			zap.Error(err),
1053
			zap.String("traceID", traceID),
1054
			zap.String("role", typeutil.ProxyRole),
1055 1056 1057
			zap.Int64("MsgID", cpt.ID()),
			zap.Uint64("BeginTS", cpt.BeginTs()),
			zap.Uint64("EndTS", cpt.EndTs()),
D
dragondriver 已提交
1058 1059 1060 1061
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1062
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
1063

1064
		return &commonpb.Status{
1065
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1066 1067 1068
			Reason:    err.Error(),
		}, nil
	}
1069 1070 1071 1072

	log.Debug(
		rpcDone("CreatePartition"),
		zap.String("traceID", traceID),
1073
		zap.String("role", typeutil.ProxyRole),
1074 1075 1076 1077 1078 1079 1080
		zap.Int64("MsgID", cpt.ID()),
		zap.Uint64("BeginTS", cpt.BeginTs()),
		zap.Uint64("EndTS", cpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1081 1082
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1083 1084 1085
	return cpt.result, nil
}

1086
// DropPartition drop a partition in specific collection.
C
Cai Yudong 已提交
1087
func (node *Proxy) DropPartition(ctx context.Context, request *milvuspb.DropPartitionRequest) (*commonpb.Status, error) {
1088 1089 1090
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
1091

1092
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DropPartition")
1093 1094
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
1095 1096
	method := "DropPartition"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
1097
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
1098

1099
	dpt := &dropPartitionTask{
S
sunby 已提交
1100
		ctx:                  ctx,
1101 1102
		Condition:            NewTaskCondition(ctx),
		DropPartitionRequest: request,
1103
		rootCoord:            node.rootCoord,
1104 1105 1106
		result:               nil,
	}

1107 1108 1109
	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1110
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1111 1112 1113
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1114 1115 1116 1117 1118 1119

	if err := node.sched.ddQueue.Enqueue(dpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1120
			zap.String("role", typeutil.ProxyRole),
1121 1122 1123 1124
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1125
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
1126

1127
		return &commonpb.Status{
1128
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1129 1130 1131
			Reason:    err.Error(),
		}, nil
	}
D
dragondriver 已提交
1132

1133 1134 1135
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1136
		zap.String("role", typeutil.ProxyRole),
1137 1138 1139
		zap.Int64("MsgID", dpt.ID()),
		zap.Uint64("BeginTS", dpt.BeginTs()),
		zap.Uint64("EndTS", dpt.EndTs()),
D
dragondriver 已提交
1140 1141 1142
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1143 1144 1145 1146

	if err := dpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1147
			zap.Error(err),
1148
			zap.String("traceID", traceID),
1149
			zap.String("role", typeutil.ProxyRole),
1150 1151 1152
			zap.Int64("MsgID", dpt.ID()),
			zap.Uint64("BeginTS", dpt.BeginTs()),
			zap.Uint64("EndTS", dpt.EndTs()),
D
dragondriver 已提交
1153 1154 1155 1156
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1157
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
1158

1159
		return &commonpb.Status{
1160
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1161 1162 1163
			Reason:    err.Error(),
		}, nil
	}
1164 1165 1166 1167

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1168
		zap.String("role", typeutil.ProxyRole),
1169 1170 1171 1172 1173 1174 1175
		zap.Int64("MsgID", dpt.ID()),
		zap.Uint64("BeginTS", dpt.BeginTs()),
		zap.Uint64("EndTS", dpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1176 1177
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1178 1179 1180
	return dpt.result, nil
}

1181
// HasPartition check if partition exist.
C
Cai Yudong 已提交
1182
func (node *Proxy) HasPartition(ctx context.Context, request *milvuspb.HasPartitionRequest) (*milvuspb.BoolResponse, error) {
1183 1184 1185 1186 1187
	if !node.checkHealthy() {
		return &milvuspb.BoolResponse{
			Status: unhealthyStatus(),
		}, nil
	}
D
dragondriver 已提交
1188

D
dragondriver 已提交
1189
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-HasPartition")
D
dragondriver 已提交
1190 1191
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
1192 1193 1194
	method := "HasPartition"
	tr := timerecord.NewTimeRecorder(method)
	//TODO: use collectionID instead of collectionName
X
Xiaofan 已提交
1195
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1196
		metrics.TotalLabel).Inc()
D
dragondriver 已提交
1197

1198
	hpt := &hasPartitionTask{
S
sunby 已提交
1199
		ctx:                 ctx,
1200 1201
		Condition:           NewTaskCondition(ctx),
		HasPartitionRequest: request,
1202
		rootCoord:           node.rootCoord,
1203 1204 1205
		result:              nil,
	}

D
dragondriver 已提交
1206 1207 1208
	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1209
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1210 1211 1212
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
D
dragondriver 已提交
1213 1214 1215 1216 1217 1218

	if err := node.sched.ddQueue.Enqueue(hpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1219
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1220 1221 1222 1223
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1224
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1225
			metrics.AbandonLabel).Inc()
1226

1227 1228
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
1229
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
1230 1231 1232 1233 1234
				Reason:    err.Error(),
			},
			Value: false,
		}, nil
	}
D
dragondriver 已提交
1235

D
dragondriver 已提交
1236 1237 1238
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1239
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1240 1241 1242
		zap.Int64("MsgID", hpt.ID()),
		zap.Uint64("BeginTS", hpt.BeginTs()),
		zap.Uint64("EndTS", hpt.EndTs()),
D
dragondriver 已提交
1243 1244 1245
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
D
dragondriver 已提交
1246 1247 1248 1249

	if err := hpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1250
			zap.Error(err),
D
dragondriver 已提交
1251
			zap.String("traceID", traceID),
1252
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1253 1254 1255
			zap.Int64("MsgID", hpt.ID()),
			zap.Uint64("BeginTS", hpt.BeginTs()),
			zap.Uint64("EndTS", hpt.EndTs()),
D
dragondriver 已提交
1256 1257 1258 1259
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1260
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1261
			metrics.FailLabel).Inc()
1262

1263 1264
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
1265
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
1266 1267 1268 1269 1270
				Reason:    err.Error(),
			},
			Value: false,
		}, nil
	}
D
dragondriver 已提交
1271 1272 1273 1274

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1275
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1276 1277 1278 1279 1280 1281 1282
		zap.Int64("MsgID", hpt.ID()),
		zap.Uint64("BeginTS", hpt.BeginTs()),
		zap.Uint64("EndTS", hpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1283
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1284
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1285
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1286 1287 1288
	return hpt.result, nil
}

1289
// LoadPartitions load specific partitions into query nodes.
C
Cai Yudong 已提交
1290
func (node *Proxy) LoadPartitions(ctx context.Context, request *milvuspb.LoadPartitionsRequest) (*commonpb.Status, error) {
1291 1292 1293
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
1294

D
dragondriver 已提交
1295
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-LoadPartitions")
1296 1297
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
1298 1299
	method := "LoadPartitions"
	tr := timerecord.NewTimeRecorder(method)
1300

1301
	lpt := &loadPartitionsTask{
G
godchen 已提交
1302 1303 1304
		ctx:                   ctx,
		Condition:             NewTaskCondition(ctx),
		LoadPartitionsRequest: request,
1305
		queryCoord:            node.queryCoord,
1306 1307
	}

1308 1309 1310
	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1311
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1312 1313 1314
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))
1315 1316 1317 1318 1319 1320

	if err := node.sched.ddQueue.Enqueue(lpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1321
			zap.String("role", typeutil.ProxyRole),
1322 1323 1324 1325
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1326
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1327
			metrics.AbandonLabel).Inc()
1328

1329
		return &commonpb.Status{
1330
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1331 1332 1333 1334
			Reason:    err.Error(),
		}, nil
	}

1335 1336 1337
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1338
		zap.String("role", typeutil.ProxyRole),
1339 1340 1341
		zap.Int64("MsgID", lpt.ID()),
		zap.Uint64("BeginTS", lpt.BeginTs()),
		zap.Uint64("EndTS", lpt.EndTs()),
D
dragondriver 已提交
1342 1343 1344
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))
1345 1346 1347 1348

	if err := lpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1349
			zap.Error(err),
1350
			zap.String("traceID", traceID),
1351
			zap.String("role", typeutil.ProxyRole),
1352 1353 1354
			zap.Int64("MsgID", lpt.ID()),
			zap.Uint64("BeginTS", lpt.BeginTs()),
			zap.Uint64("EndTS", lpt.EndTs()),
D
dragondriver 已提交
1355 1356 1357 1358
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1359
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1360
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1361
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1362
			metrics.FailLabel).Inc()
1363

1364
		return &commonpb.Status{
1365
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1366 1367 1368 1369
			Reason:    err.Error(),
		}, nil
	}

1370 1371 1372
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1373
		zap.String("role", typeutil.ProxyRole),
1374 1375 1376 1377 1378 1379 1380
		zap.Int64("MsgID", lpt.ID()),
		zap.Uint64("BeginTS", lpt.BeginTs()),
		zap.Uint64("EndTS", lpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1381
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1382
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1383
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1384
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1385
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1386
	return lpt.result, nil
1387 1388
}

1389
// ReleasePartitions release specific partitions from query nodes.
C
Cai Yudong 已提交
1390
func (node *Proxy) ReleasePartitions(ctx context.Context, request *milvuspb.ReleasePartitionsRequest) (*commonpb.Status, error) {
1391 1392 1393
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
1394 1395 1396 1397 1398

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-ReleasePartitions")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

1399
	rpt := &releasePartitionsTask{
G
godchen 已提交
1400 1401 1402
		ctx:                      ctx,
		Condition:                NewTaskCondition(ctx),
		ReleasePartitionsRequest: request,
1403
		queryCoord:               node.queryCoord,
1404 1405
	}

1406
	method := "ReleasePartitions"
1407
	tr := timerecord.NewTimeRecorder(method)
1408 1409 1410 1411

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1412
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1413 1414 1415
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))
1416 1417 1418 1419 1420 1421

	if err := node.sched.ddQueue.Enqueue(rpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1422
			zap.String("role", typeutil.ProxyRole),
1423 1424 1425 1426
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1427
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1428
			metrics.AbandonLabel).Inc()
1429

1430
		return &commonpb.Status{
1431
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1432 1433 1434 1435
			Reason:    err.Error(),
		}, nil
	}

1436 1437 1438
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1439
		zap.String("role", typeutil.ProxyRole),
1440 1441 1442
		zap.Int64("msgID", rpt.Base.MsgID),
		zap.Uint64("BeginTS", rpt.BeginTs()),
		zap.Uint64("EndTS", rpt.EndTs()),
D
dragondriver 已提交
1443 1444 1445
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))
1446 1447 1448 1449

	if err := rpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1450
			zap.Error(err),
1451
			zap.String("traceID", traceID),
1452
			zap.String("role", typeutil.ProxyRole),
1453 1454 1455
			zap.Int64("msgID", rpt.Base.MsgID),
			zap.Uint64("BeginTS", rpt.BeginTs()),
			zap.Uint64("EndTS", rpt.EndTs()),
D
dragondriver 已提交
1456 1457 1458 1459
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1460
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1461
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1462
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1463
			metrics.FailLabel).Inc()
1464

1465
		return &commonpb.Status{
1466
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1467 1468 1469 1470
			Reason:    err.Error(),
		}, nil
	}

1471 1472 1473
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1474
		zap.String("role", typeutil.ProxyRole),
1475 1476 1477 1478 1479 1480 1481
		zap.Int64("msgID", rpt.Base.MsgID),
		zap.Uint64("BeginTS", rpt.BeginTs()),
		zap.Uint64("EndTS", rpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))

X
Xiaofan 已提交
1482
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1483
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1484
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1485
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1486
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1487
	return rpt.result, nil
1488 1489
}

1490
// GetPartitionStatistics get the statistics of partition, such as num_rows.
C
Cai Yudong 已提交
1491
func (node *Proxy) GetPartitionStatistics(ctx context.Context, request *milvuspb.GetPartitionStatisticsRequest) (*milvuspb.GetPartitionStatisticsResponse, error) {
1492 1493 1494 1495 1496
	if !node.checkHealthy() {
		return &milvuspb.GetPartitionStatisticsResponse{
			Status: unhealthyStatus(),
		}, nil
	}
1497 1498 1499 1500

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-GetPartitionStatistics")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
1501 1502
	method := "GetPartitionStatistics"
	tr := timerecord.NewTimeRecorder(method)
1503

1504
	g := &getPartitionStatisticsTask{
1505 1506 1507
		ctx:                           ctx,
		Condition:                     NewTaskCondition(ctx),
		GetPartitionStatisticsRequest: request,
1508
		dataCoord:                     node.dataCoord,
1509 1510
	}

1511 1512 1513
	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1514
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1515 1516 1517
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1518 1519 1520 1521 1522 1523

	if err := node.sched.ddQueue.Enqueue(g); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1524
			zap.String("role", typeutil.ProxyRole),
1525 1526 1527 1528
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1529
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1530
			metrics.AbandonLabel).Inc()
1531

1532 1533 1534 1535 1536 1537 1538 1539
		return &milvuspb.GetPartitionStatisticsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

1540 1541 1542
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1543
		zap.String("role", typeutil.ProxyRole),
1544 1545 1546
		zap.Int64("msgID", g.ID()),
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
1547 1548 1549
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))
1550 1551 1552 1553

	if err := g.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
1554
			zap.Error(err),
1555
			zap.String("traceID", traceID),
1556
			zap.String("role", typeutil.ProxyRole),
1557 1558 1559
			zap.Int64("msgID", g.ID()),
			zap.Uint64("BeginTS", g.BeginTs()),
			zap.Uint64("EndTS", g.EndTs()),
1560 1561 1562 1563
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1564
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1565
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1566
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1567
			metrics.FailLabel).Inc()
1568

1569 1570 1571 1572 1573 1574 1575 1576
		return &milvuspb.GetPartitionStatisticsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

1577 1578 1579
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1580
		zap.String("role", typeutil.ProxyRole),
1581 1582 1583 1584 1585 1586 1587
		zap.Int64("msgID", g.ID()),
		zap.Uint64("BeginTS", g.BeginTs()),
		zap.Uint64("EndTS", g.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName))

X
Xiaofan 已提交
1588
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1589
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1590
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1591
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1592
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1593
	return g.result, nil
1594 1595
}

1596
// ShowPartitions list all partitions in the specific collection.
C
Cai Yudong 已提交
1597
func (node *Proxy) ShowPartitions(ctx context.Context, request *milvuspb.ShowPartitionsRequest) (*milvuspb.ShowPartitionsResponse, error) {
1598 1599 1600 1601 1602
	if !node.checkHealthy() {
		return &milvuspb.ShowPartitionsResponse{
			Status: unhealthyStatus(),
		}, nil
	}
1603 1604 1605 1606 1607

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-ShowPartitions")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

1608
	spt := &showPartitionsTask{
G
godchen 已提交
1609 1610 1611
		ctx:                   ctx,
		Condition:             NewTaskCondition(ctx),
		ShowPartitionsRequest: request,
1612
		rootCoord:             node.rootCoord,
1613
		queryCoord:            node.queryCoord,
G
godchen 已提交
1614
		result:                nil,
1615 1616
	}

1617
	method := "ShowPartitions"
1618 1619
	tr := timerecord.NewTimeRecorder(method)
	//TODO: use collectionID instead of collectionName
X
Xiaofan 已提交
1620
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1621
		metrics.TotalLabel).Inc()
1622 1623 1624 1625

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1626
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1627
		zap.Any("request", request))
1628 1629 1630 1631 1632 1633

	if err := node.sched.ddQueue.Enqueue(spt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1634
			zap.String("role", typeutil.ProxyRole),
1635 1636
			zap.Any("request", request))

X
Xiaofan 已提交
1637
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1638
			metrics.AbandonLabel).Inc()
1639

G
godchen 已提交
1640
		return &milvuspb.ShowPartitionsResponse{
1641
			Status: &commonpb.Status{
1642
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
1643 1644 1645 1646 1647
				Reason:    err.Error(),
			},
		}, nil
	}

1648 1649 1650
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1651
		zap.String("role", typeutil.ProxyRole),
1652 1653 1654
		zap.Int64("msgID", spt.ID()),
		zap.Uint64("BeginTS", spt.BeginTs()),
		zap.Uint64("EndTS", spt.EndTs()),
1655 1656
		zap.String("db", spt.ShowPartitionsRequest.DbName),
		zap.String("collection", spt.ShowPartitionsRequest.CollectionName),
1657 1658 1659 1660 1661
		zap.Any("partitions", spt.ShowPartitionsRequest.PartitionNames))

	if err := spt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1662
			zap.Error(err),
1663
			zap.String("traceID", traceID),
1664
			zap.String("role", typeutil.ProxyRole),
1665 1666 1667 1668 1669 1670
			zap.Int64("msgID", spt.ID()),
			zap.Uint64("BeginTS", spt.BeginTs()),
			zap.Uint64("EndTS", spt.EndTs()),
			zap.String("db", spt.ShowPartitionsRequest.DbName),
			zap.String("collection", spt.ShowPartitionsRequest.CollectionName),
			zap.Any("partitions", spt.ShowPartitionsRequest.PartitionNames))
D
dragondriver 已提交
1671

X
Xiaofan 已提交
1672
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1673
			metrics.FailLabel).Inc()
1674

G
godchen 已提交
1675
		return &milvuspb.ShowPartitionsResponse{
1676
			Status: &commonpb.Status{
1677
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
1678 1679 1680 1681
				Reason:    err.Error(),
			},
		}, nil
	}
1682 1683 1684 1685

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1686
		zap.String("role", typeutil.ProxyRole),
1687 1688 1689 1690 1691 1692 1693
		zap.Int64("msgID", spt.ID()),
		zap.Uint64("BeginTS", spt.BeginTs()),
		zap.Uint64("EndTS", spt.EndTs()),
		zap.String("db", spt.ShowPartitionsRequest.DbName),
		zap.String("collection", spt.ShowPartitionsRequest.CollectionName),
		zap.Any("partitions", spt.ShowPartitionsRequest.PartitionNames))

X
Xiaofan 已提交
1694
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1695
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1696
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1697 1698 1699
	return spt.result, nil
}

1700
// CreateIndex create index for collection.
C
Cai Yudong 已提交
1701
func (node *Proxy) CreateIndex(ctx context.Context, request *milvuspb.CreateIndexRequest) (*commonpb.Status, error) {
1702 1703 1704
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
D
dragondriver 已提交
1705 1706 1707 1708 1709

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-ShowPartitions")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

1710
	cit := &createIndexTask{
S
sunby 已提交
1711
		ctx:                ctx,
1712 1713
		Condition:          NewTaskCondition(ctx),
		CreateIndexRequest: request,
1714
		rootCoord:          node.rootCoord,
1715
		indexCoord:         node.indexCoord,
1716 1717
	}

D
dragondriver 已提交
1718
	method := "CreateIndex"
1719
	tr := timerecord.NewTimeRecorder(method)
D
dragondriver 已提交
1720 1721 1722 1723

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1724
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1725 1726 1727 1728
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.Any("extra_params", request.ExtraParams))
D
dragondriver 已提交
1729 1730 1731 1732 1733 1734

	if err := node.sched.ddQueue.Enqueue(cit); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1735
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1736 1737 1738 1739 1740
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.Any("extra_params", request.ExtraParams))

X
Xiaofan 已提交
1741
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1742
			metrics.AbandonLabel).Inc()
1743

1744
		return &commonpb.Status{
1745
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1746 1747 1748 1749
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
1750 1751 1752
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1753
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1754 1755 1756
		zap.Int64("MsgID", cit.ID()),
		zap.Uint64("BeginTs", cit.BeginTs()),
		zap.Uint64("EndTs", cit.EndTs()),
D
dragondriver 已提交
1757 1758 1759 1760
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.Any("extra_params", request.ExtraParams))
D
dragondriver 已提交
1761 1762 1763 1764

	if err := cit.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1765
			zap.Error(err),
D
dragondriver 已提交
1766
			zap.String("traceID", traceID),
1767
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1768 1769 1770
			zap.Int64("MsgID", cit.ID()),
			zap.Uint64("BeginTs", cit.BeginTs()),
			zap.Uint64("EndTs", cit.EndTs()),
D
dragondriver 已提交
1771 1772 1773 1774 1775
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.Any("extra_params", request.ExtraParams))

X
Xiaofan 已提交
1776
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1777
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1778
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1779
			metrics.FailLabel).Inc()
1780

1781
		return &commonpb.Status{
1782
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
1783 1784 1785 1786
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
1787 1788 1789
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1790
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1791 1792 1793 1794 1795 1796 1797 1798
		zap.Int64("MsgID", cit.ID()),
		zap.Uint64("BeginTs", cit.BeginTs()),
		zap.Uint64("EndTs", cit.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.Any("extra_params", request.ExtraParams))

X
Xiaofan 已提交
1799
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1800
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1801
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1802
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1803
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1804 1805 1806
	return cit.result, nil
}

1807
// DescribeIndex get the meta information of index, such as index state, index id and etc.
C
Cai Yudong 已提交
1808
func (node *Proxy) DescribeIndex(ctx context.Context, request *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
1809 1810 1811 1812 1813
	if !node.checkHealthy() {
		return &milvuspb.DescribeIndexResponse{
			Status: unhealthyStatus(),
		}, nil
	}
1814 1815 1816 1817 1818

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DescribeIndex")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

1819
	dit := &describeIndexTask{
S
sunby 已提交
1820
		ctx:                  ctx,
1821 1822
		Condition:            NewTaskCondition(ctx),
		DescribeIndexRequest: request,
1823
		indexCoord:           node.indexCoord,
1824 1825
	}

1826 1827 1828
	method := "DescribeIndex"
	// avoid data race
	indexName := request.IndexName
1829
	tr := timerecord.NewTimeRecorder(method)
1830 1831 1832 1833

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1834
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1835 1836 1837
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
1838 1839 1840 1841 1842 1843 1844
		zap.String("index name", indexName))

	if err := node.sched.ddQueue.Enqueue(dit); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1845
			zap.String("role", typeutil.ProxyRole),
1846 1847 1848 1849 1850
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", indexName))

X
Xiaofan 已提交
1851
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1852
			metrics.AbandonLabel).Inc()
1853

1854 1855
		return &milvuspb.DescribeIndexResponse{
			Status: &commonpb.Status{
1856
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
1857 1858 1859 1860 1861
				Reason:    err.Error(),
			},
		}, nil
	}

1862 1863 1864
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1865
		zap.String("role", typeutil.ProxyRole),
1866 1867 1868
		zap.Int64("MsgID", dit.ID()),
		zap.Uint64("BeginTs", dit.BeginTs()),
		zap.Uint64("EndTs", dit.EndTs()),
D
dragondriver 已提交
1869 1870 1871
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
1872 1873 1874 1875 1876
		zap.String("index name", indexName))

	if err := dit.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1877
			zap.Error(err),
1878
			zap.String("traceID", traceID),
1879
			zap.String("role", typeutil.ProxyRole),
1880 1881 1882
			zap.Int64("MsgID", dit.ID()),
			zap.Uint64("BeginTs", dit.BeginTs()),
			zap.Uint64("EndTs", dit.EndTs()),
D
dragondriver 已提交
1883 1884 1885
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
1886
			zap.String("index name", indexName))
D
dragondriver 已提交
1887

Z
zhenshan.cao 已提交
1888 1889 1890 1891
		errCode := commonpb.ErrorCode_UnexpectedError
		if dit.result != nil {
			errCode = dit.result.Status.GetErrorCode()
		}
X
Xiaofan 已提交
1892
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1893
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1894
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1895
			metrics.FailLabel).Inc()
1896

1897 1898
		return &milvuspb.DescribeIndexResponse{
			Status: &commonpb.Status{
Z
zhenshan.cao 已提交
1899
				ErrorCode: errCode,
1900 1901 1902 1903 1904
				Reason:    err.Error(),
			},
		}, nil
	}

1905 1906 1907
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
1908
		zap.String("role", typeutil.ProxyRole),
1909 1910 1911 1912 1913 1914 1915 1916
		zap.Int64("MsgID", dit.ID()),
		zap.Uint64("BeginTs", dit.BeginTs()),
		zap.Uint64("EndTs", dit.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", indexName))

X
Xiaofan 已提交
1917
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1918
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
1919
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1920
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
1921
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
1922 1923 1924
	return dit.result, nil
}

1925
// DropIndex drop the index of collection.
C
Cai Yudong 已提交
1926
func (node *Proxy) DropIndex(ctx context.Context, request *milvuspb.DropIndexRequest) (*commonpb.Status, error) {
1927 1928 1929
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
D
dragondriver 已提交
1930 1931 1932 1933 1934

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DropIndex")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

1935
	dit := &dropIndexTask{
S
sunby 已提交
1936
		ctx:              ctx,
B
BossZou 已提交
1937 1938
		Condition:        NewTaskCondition(ctx),
		DropIndexRequest: request,
1939
		indexCoord:       node.indexCoord,
B
BossZou 已提交
1940
	}
G
godchen 已提交
1941

D
dragondriver 已提交
1942
	method := "DropIndex"
1943
	tr := timerecord.NewTimeRecorder(method)
D
dragondriver 已提交
1944 1945 1946 1947

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
1948
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
1949 1950 1951 1952 1953
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))

D
dragondriver 已提交
1954 1955 1956 1957 1958
	if err := node.sched.ddQueue.Enqueue(dit); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
1959
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1960 1961 1962 1963
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))
X
Xiaofan 已提交
1964
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
1965
			metrics.AbandonLabel).Inc()
D
dragondriver 已提交
1966

B
BossZou 已提交
1967
		return &commonpb.Status{
1968
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
B
BossZou 已提交
1969 1970 1971
			Reason:    err.Error(),
		}, nil
	}
D
dragondriver 已提交
1972

D
dragondriver 已提交
1973 1974 1975
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
1976
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1977 1978 1979
		zap.Int64("MsgID", dit.ID()),
		zap.Uint64("BeginTs", dit.BeginTs()),
		zap.Uint64("EndTs", dit.EndTs()),
D
dragondriver 已提交
1980 1981 1982 1983
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))
D
dragondriver 已提交
1984 1985 1986 1987

	if err := dit.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
1988
			zap.Error(err),
D
dragondriver 已提交
1989
			zap.String("traceID", traceID),
1990
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
1991 1992 1993
			zap.Int64("MsgID", dit.ID()),
			zap.Uint64("BeginTs", dit.BeginTs()),
			zap.Uint64("EndTs", dit.EndTs()),
D
dragondriver 已提交
1994 1995 1996 1997 1998
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))

X
Xiaofan 已提交
1999
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2000
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2001
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2002
			metrics.FailLabel).Inc()
2003

B
BossZou 已提交
2004
		return &commonpb.Status{
2005
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
B
BossZou 已提交
2006 2007 2008
			Reason:    err.Error(),
		}, nil
	}
D
dragondriver 已提交
2009 2010 2011 2012

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
2013
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2014 2015 2016 2017 2018 2019 2020 2021
		zap.Int64("MsgID", dit.ID()),
		zap.Uint64("BeginTs", dit.BeginTs()),
		zap.Uint64("EndTs", dit.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))

X
Xiaofan 已提交
2022
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2023
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2024
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2025
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
2026
	metrics.ProxyDMLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
B
BossZou 已提交
2027 2028 2029
	return dit.result, nil
}

2030 2031
// GetIndexBuildProgress gets index build progress with filed_name and index_name.
// IndexRows is the num of indexed rows. And TotalRows is the total number of segment rows.
2032
// Deprecated: use DescribeIndex instead
C
Cai Yudong 已提交
2033
func (node *Proxy) GetIndexBuildProgress(ctx context.Context, request *milvuspb.GetIndexBuildProgressRequest) (*milvuspb.GetIndexBuildProgressResponse, error) {
2034 2035 2036 2037 2038
	if !node.checkHealthy() {
		return &milvuspb.GetIndexBuildProgressResponse{
			Status: unhealthyStatus(),
		}, nil
	}
2039 2040 2041 2042 2043

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-GetIndexBuildProgress")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

2044
	gibpt := &getIndexBuildProgressTask{
2045 2046 2047
		ctx:                          ctx,
		Condition:                    NewTaskCondition(ctx),
		GetIndexBuildProgressRequest: request,
2048 2049
		indexCoord:                   node.indexCoord,
		rootCoord:                    node.rootCoord,
2050
		dataCoord:                    node.dataCoord,
2051 2052
	}

2053
	method := "GetIndexBuildProgress"
2054
	tr := timerecord.NewTimeRecorder(method)
2055 2056 2057 2058

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
2059
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
2060 2061 2062 2063
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))
2064 2065 2066 2067 2068 2069

	if err := node.sched.ddQueue.Enqueue(gibpt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
2070
			zap.String("role", typeutil.ProxyRole),
2071 2072 2073 2074
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))
X
Xiaofan 已提交
2075
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2076
			metrics.AbandonLabel).Inc()
2077

2078 2079 2080 2081 2082 2083 2084 2085
		return &milvuspb.GetIndexBuildProgressResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

2086 2087 2088
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
2089
		zap.String("role", typeutil.ProxyRole),
2090 2091 2092
		zap.Int64("MsgID", gibpt.ID()),
		zap.Uint64("BeginTs", gibpt.BeginTs()),
		zap.Uint64("EndTs", gibpt.EndTs()),
2093 2094 2095 2096
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))
2097 2098 2099 2100

	if err := gibpt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
2101
			zap.Error(err),
2102
			zap.String("traceID", traceID),
2103
			zap.String("role", typeutil.ProxyRole),
2104 2105 2106
			zap.Int64("MsgID", gibpt.ID()),
			zap.Uint64("BeginTs", gibpt.BeginTs()),
			zap.Uint64("EndTs", gibpt.EndTs()),
2107 2108 2109 2110
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))
X
Xiaofan 已提交
2111
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2112
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2113
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2114
			metrics.FailLabel).Inc()
2115 2116 2117 2118 2119 2120 2121 2122

		return &milvuspb.GetIndexBuildProgressResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}
2123 2124 2125 2126

	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
2127
		zap.String("role", typeutil.ProxyRole),
2128 2129 2130 2131 2132 2133 2134 2135
		zap.Int64("MsgID", gibpt.ID()),
		zap.Uint64("BeginTs", gibpt.BeginTs()),
		zap.Uint64("EndTs", gibpt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName),
		zap.Any("result", gibpt.result))
2136

X
Xiaofan 已提交
2137
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2138
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2139
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2140
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
2141
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
2142
	return gibpt.result, nil
2143 2144
}

2145
// GetIndexState get the build-state of index.
2146
// Deprecated: use DescribeIndex instead
C
Cai Yudong 已提交
2147
func (node *Proxy) GetIndexState(ctx context.Context, request *milvuspb.GetIndexStateRequest) (*milvuspb.GetIndexStateResponse, error) {
2148 2149 2150 2151 2152
	if !node.checkHealthy() {
		return &milvuspb.GetIndexStateResponse{
			Status: unhealthyStatus(),
		}, nil
	}
2153 2154 2155 2156 2157

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Insert")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

2158
	dipt := &getIndexStateTask{
G
godchen 已提交
2159 2160 2161
		ctx:                  ctx,
		Condition:            NewTaskCondition(ctx),
		GetIndexStateRequest: request,
2162 2163
		indexCoord:           node.indexCoord,
		rootCoord:            node.rootCoord,
2164 2165
	}

2166
	method := "GetIndexState"
2167
	tr := timerecord.NewTimeRecorder(method)
2168 2169 2170 2171

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
2172
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
2173 2174 2175 2176
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))
2177 2178 2179 2180 2181 2182

	if err := node.sched.ddQueue.Enqueue(dipt); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
2183
			zap.String("role", typeutil.ProxyRole),
2184 2185 2186 2187 2188
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))

X
Xiaofan 已提交
2189
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2190
			metrics.AbandonLabel).Inc()
2191

G
godchen 已提交
2192
		return &milvuspb.GetIndexStateResponse{
2193
			Status: &commonpb.Status{
2194
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
2195 2196 2197 2198 2199
				Reason:    err.Error(),
			},
		}, nil
	}

2200 2201 2202
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
2203
		zap.String("role", typeutil.ProxyRole),
2204 2205 2206
		zap.Int64("MsgID", dipt.ID()),
		zap.Uint64("BeginTs", dipt.BeginTs()),
		zap.Uint64("EndTs", dipt.EndTs()),
D
dragondriver 已提交
2207 2208 2209 2210
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))
2211 2212 2213 2214

	if err := dipt.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
2215
			zap.Error(err),
2216
			zap.String("traceID", traceID),
2217
			zap.String("role", typeutil.ProxyRole),
2218 2219 2220
			zap.Int64("MsgID", dipt.ID()),
			zap.Uint64("BeginTs", dipt.BeginTs()),
			zap.Uint64("EndTs", dipt.EndTs()),
D
dragondriver 已提交
2221 2222 2223 2224 2225
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.String("field", request.FieldName),
			zap.String("index name", request.IndexName))

X
Xiaofan 已提交
2226
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2227
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2228
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2229
			metrics.FailLabel).Inc()
2230

G
godchen 已提交
2231
		return &milvuspb.GetIndexStateResponse{
2232
			Status: &commonpb.Status{
2233
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
2234 2235 2236 2237 2238
				Reason:    err.Error(),
			},
		}, nil
	}

2239 2240 2241
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
2242
		zap.String("role", typeutil.ProxyRole),
2243 2244 2245 2246 2247 2248 2249 2250
		zap.Int64("MsgID", dipt.ID()),
		zap.Uint64("BeginTs", dipt.BeginTs()),
		zap.Uint64("EndTs", dipt.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("field", request.FieldName),
		zap.String("index name", request.IndexName))

X
Xiaofan 已提交
2251
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2252
		metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2253
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2254
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
2255
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
2256 2257 2258
	return dipt.result, nil
}

2259
// Insert insert records into collection.
C
Cai Yudong 已提交
2260
func (node *Proxy) Insert(ctx context.Context, request *milvuspb.InsertRequest) (*milvuspb.MutationResult, error) {
X
Xiangyu Wang 已提交
2261 2262 2263 2264 2265 2266
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Insert")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
	log.Info("Start processing insert request in Proxy", zap.String("traceID", traceID))
	defer log.Info("Finish processing insert request in Proxy", zap.String("traceID", traceID))

2267 2268 2269 2270 2271
	if !node.checkHealthy() {
		return &milvuspb.MutationResult{
			Status: unhealthyStatus(),
		}, nil
	}
2272 2273
	method := "Insert"
	tr := timerecord.NewTimeRecorder(method)
2274
	receiveSize := proto.Size(request)
2275 2276
	rateCol.Add(internalpb.RateType_DMLInsert.String(), float64(receiveSize))
	metrics.ProxyReceiveBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.InsertLabel).Add(float64(receiveSize))
D
dragondriver 已提交
2277

2278 2279 2280 2281 2282
	defer func() {
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.TotalLabel).Inc()
	}()

2283
	it := &insertTask{
2284 2285
		ctx:       ctx,
		Condition: NewTaskCondition(ctx),
X
xige-16 已提交
2286
		// req:       request,
2287 2288 2289 2290
		BaseInsertTask: BaseInsertTask{
			BaseMsg: msgstream.BaseMsg{
				HashValues: request.HashKeys,
			},
G
godchen 已提交
2291
			InsertRequest: internalpb.InsertRequest{
2292
				Base: &commonpb.MsgBase{
X
xige-16 已提交
2293 2294
					MsgType:  commonpb.MsgType_Insert,
					MsgID:    0,
X
Xiaofan 已提交
2295
					SourceID: Params.ProxyCfg.GetNodeID(),
2296 2297 2298
				},
				CollectionName: request.CollectionName,
				PartitionName:  request.PartitionName,
X
xige-16 已提交
2299 2300 2301
				FieldsData:     request.FieldsData,
				NumRows:        uint64(request.NumRows),
				Version:        internalpb.InsertDataVersion_ColumnBased,
2302
				// RowData: transfer column based request to this
2303 2304
			},
		},
2305 2306 2307 2308
		idAllocator:   node.idAllocator,
		segIDAssigner: node.segAssigner,
		chMgr:         node.chMgr,
		chTicker:      node.chTicker,
2309
	}
2310 2311

	if len(it.PartitionName) <= 0 {
2312
		it.PartitionName = Params.CommonCfg.DefaultPartitionName
2313 2314
	}

X
Xiangyu Wang 已提交
2315
	constructFailedResponse := func(err error) *milvuspb.MutationResult {
X
xige-16 已提交
2316
		numRows := request.NumRows
2317 2318 2319 2320
		errIndex := make([]uint32, numRows)
		for i := uint32(0); i < numRows; i++ {
			errIndex[i] = i
		}
2321

X
Xiangyu Wang 已提交
2322 2323 2324 2325 2326 2327 2328
		return &milvuspb.MutationResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
			ErrIndex: errIndex,
		}
2329 2330
	}

X
Xiangyu Wang 已提交
2331
	log.Debug("Enqueue insert request in Proxy",
2332
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2333 2334 2335 2336 2337
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName),
		zap.Int("len(FieldsData)", len(request.FieldsData)),
		zap.Int("len(HashKeys)", len(request.HashKeys)),
2338 2339
		zap.Uint32("NumRows", request.NumRows),
		zap.String("traceID", traceID))
D
dragondriver 已提交
2340

X
Xiangyu Wang 已提交
2341 2342
	if err := node.sched.dmQueue.Enqueue(it); err != nil {
		log.Debug("Failed to enqueue insert task: " + err.Error())
2343 2344
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.AbandonLabel).Inc()
X
Xiangyu Wang 已提交
2345
		return constructFailedResponse(err), nil
2346
	}
D
dragondriver 已提交
2347

X
Xiangyu Wang 已提交
2348
	log.Debug("Detail of insert request in Proxy",
2349
		zap.String("role", typeutil.ProxyRole),
X
Xiangyu Wang 已提交
2350
		zap.Int64("msgID", it.Base.MsgID),
D
dragondriver 已提交
2351 2352 2353 2354 2355
		zap.Uint64("BeginTS", it.BeginTs()),
		zap.Uint64("EndTS", it.EndTs()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName),
X
Xiangyu Wang 已提交
2356 2357 2358 2359 2360
		zap.Uint32("NumRows", request.NumRows),
		zap.String("traceID", traceID))

	if err := it.WaitToFinish(); err != nil {
		log.Debug("Failed to execute insert task in task scheduler: "+err.Error(), zap.String("traceID", traceID))
2361
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2362
			metrics.FailLabel).Inc()
X
Xiangyu Wang 已提交
2363 2364 2365 2366 2367
		return constructFailedResponse(err), nil
	}

	if it.result.Status.ErrorCode != commonpb.ErrorCode_Success {
		setErrorIndex := func() {
X
xige-16 已提交
2368
			numRows := request.NumRows
X
Xiangyu Wang 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
			errIndex := make([]uint32, numRows)
			for i := uint32(0); i < numRows; i++ {
				errIndex[i] = i
			}
			it.result.ErrIndex = errIndex
		}

		setErrorIndex()
	}

	// InsertCnt always equals to the number of entities in the request
X
xige-16 已提交
2380
	it.result.InsertCnt = int64(request.NumRows)
D
dragondriver 已提交
2381

2382
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2383
		metrics.SuccessLabel).Inc()
2384 2385
	successCnt := it.result.InsertCnt - int64(len(it.result.ErrIndex))
	metrics.ProxyInsertVectors.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10)).Add(float64(successCnt))
2386
	metrics.ProxyMutationLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.InsertLabel).Observe(float64(tr.ElapseSpan().Milliseconds()))
2387 2388 2389
	return it.result, nil
}

2390
// Delete delete records from collection, then these records cannot be searched.
G
groot 已提交
2391
func (node *Proxy) Delete(ctx context.Context, request *milvuspb.DeleteRequest) (*milvuspb.MutationResult, error) {
2392 2393 2394
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Delete")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)
2395 2396
	log.Info("Start processing delete request in Proxy", zap.String("traceID", traceID))
	defer log.Info("Finish processing delete request in Proxy", zap.String("traceID", traceID))
2397

2398
	receiveSize := proto.Size(request)
2399 2400
	rateCol.Add(internalpb.RateType_DMLDelete.String(), float64(receiveSize))
	metrics.ProxyReceiveBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.DeleteLabel).Add(float64(receiveSize))
2401

G
groot 已提交
2402 2403 2404 2405 2406 2407
	if !node.checkHealthy() {
		return &milvuspb.MutationResult{
			Status: unhealthyStatus(),
		}, nil
	}

2408 2409 2410
	method := "Delete"
	tr := timerecord.NewTimeRecorder(method)

2411 2412
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.TotalLabel).Inc()
2413
	dt := &deleteTask{
X
xige-16 已提交
2414 2415 2416
		ctx:        ctx,
		Condition:  NewTaskCondition(ctx),
		deleteExpr: request.Expr,
G
godchen 已提交
2417
		BaseDeleteTask: BaseDeleteTask{
G
godchen 已提交
2418 2419 2420
			BaseMsg: msgstream.BaseMsg{
				HashValues: request.HashKeys,
			},
G
godchen 已提交
2421 2422 2423 2424 2425
			DeleteRequest: internalpb.DeleteRequest{
				Base: &commonpb.MsgBase{
					MsgType: commonpb.MsgType_Delete,
					MsgID:   0,
				},
X
xige-16 已提交
2426
				DbName:         request.DbName,
G
godchen 已提交
2427 2428 2429
				CollectionName: request.CollectionName,
				PartitionName:  request.PartitionName,
				// RowData: transfer column based request to this
C
Cai Yudong 已提交
2430 2431 2432 2433
			},
		},
		chMgr:    node.chMgr,
		chTicker: node.chTicker,
G
groot 已提交
2434 2435
	}

2436
	log.Debug("Enqueue delete request in Proxy",
2437
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
2438 2439 2440 2441
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName),
		zap.String("expr", request.Expr))
2442 2443 2444 2445

	// MsgID will be set by Enqueue()
	if err := node.sched.dmQueue.Enqueue(dt); err != nil {
		log.Error("Failed to enqueue delete task: "+err.Error(), zap.String("traceID", traceID))
X
Xiaofan 已提交
2446
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2447
			metrics.FailLabel).Inc()
2448

G
groot 已提交
2449 2450 2451 2452 2453 2454 2455 2456
		return &milvuspb.MutationResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

2457
	log.Debug("Detail of delete request in Proxy",
2458
		zap.String("role", typeutil.ProxyRole),
G
groot 已提交
2459 2460 2461 2462 2463
		zap.Int64("msgID", dt.Base.MsgID),
		zap.Uint64("timestamp", dt.Base.Timestamp),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.String("partition", request.PartitionName),
2464 2465
		zap.String("expr", request.Expr),
		zap.String("traceID", traceID))
G
groot 已提交
2466

2467 2468
	if err := dt.WaitToFinish(); err != nil {
		log.Error("Failed to execute delete task in task scheduler: "+err.Error(), zap.String("traceID", traceID))
X
Xiaofan 已提交
2469
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2470
			metrics.TotalLabel).Inc()
X
Xiaofan 已提交
2471
		metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2472
			metrics.FailLabel).Inc()
G
groot 已提交
2473 2474 2475 2476 2477 2478 2479 2480
		return &milvuspb.MutationResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

X
Xiaofan 已提交
2481
	metrics.ProxyDMLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
2482
		metrics.SuccessLabel).Inc()
2483
	metrics.ProxyMutationLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.DeleteLabel).Observe(float64(tr.ElapseSpan().Milliseconds()))
G
groot 已提交
2484 2485 2486
	return dt.result, nil
}

2487
// Search search the most similar records of requests.
C
Cai Yudong 已提交
2488
func (node *Proxy) Search(ctx context.Context, request *milvuspb.SearchRequest) (*milvuspb.SearchResults, error) {
2489 2490 2491 2492 2493
	receiveSize := proto.Size(request)
	metrics.ProxyReceiveBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.SearchLabel).Add(float64(receiveSize))

	rateCol.Add(internalpb.RateType_DQLSearch.String(), float64(request.GetNq()))

2494 2495 2496 2497 2498
	if !node.checkHealthy() {
		return &milvuspb.SearchResults{
			Status: unhealthyStatus(),
		}, nil
	}
2499 2500
	method := "Search"
	tr := timerecord.NewTimeRecorder(method)
2501 2502
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.TotalLabel).Inc()
D
dragondriver 已提交
2503

C
cai.zhang 已提交
2504 2505
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Search")
	defer sp.Finish()
D
dragondriver 已提交
2506

2507
	qt := &searchTask{
S
sunby 已提交
2508
		ctx:       ctx,
2509
		Condition: NewTaskCondition(ctx),
G
godchen 已提交
2510
		SearchRequest: &internalpb.SearchRequest{
2511
			Base: &commonpb.MsgBase{
2512
				MsgType:  commonpb.MsgType_Search,
X
Xiaofan 已提交
2513
				SourceID: Params.ProxyCfg.GetNodeID(),
2514
			},
2515
			ReqID: Params.ProxyCfg.GetNodeID(),
2516
		},
2517 2518 2519 2520
		request:  request,
		qc:       node.queryCoord,
		tr:       timerecord.NewTimeRecorder("search"),
		shardMgr: node.shardMgr,
2521 2522
	}

2523 2524 2525
	travelTs := request.TravelTimestamp
	guaranteeTs := request.GuaranteeTimestamp

Z
Zach 已提交
2526
	log.Ctx(ctx).Info(
2527
		rpcReceived(method),
2528
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
2529 2530 2531 2532 2533
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames),
		zap.Any("dsl", request.Dsl),
		zap.Any("len(PlaceholderGroup)", len(request.PlaceholderGroup)),
2534 2535 2536 2537
		zap.Any("OutputFields", request.OutputFields),
		zap.Any("search_params", request.SearchParams),
		zap.Uint64("travel_timestamp", travelTs),
		zap.Uint64("guarantee_timestamp", guaranteeTs))
D
dragondriver 已提交
2538

2539
	if err := node.sched.dqQueue.Enqueue(qt); err != nil {
Z
Zach 已提交
2540
		log.Ctx(ctx).Warn(
2541
			rpcFailedToEnqueue(method),
D
dragondriver 已提交
2542
			zap.Error(err),
2543
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2544 2545 2546 2547 2548 2549
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames),
			zap.Any("dsl", request.Dsl),
			zap.Any("len(PlaceholderGroup)", len(request.PlaceholderGroup)),
			zap.Any("OutputFields", request.OutputFields),
2550 2551 2552
			zap.Any("search_params", request.SearchParams),
			zap.Uint64("travel_timestamp", travelTs),
			zap.Uint64("guarantee_timestamp", guaranteeTs))
D
dragondriver 已提交
2553

2554 2555
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.AbandonLabel).Inc()
2556

2557 2558
		return &milvuspb.SearchResults{
			Status: &commonpb.Status{
2559
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
2560 2561 2562 2563
				Reason:    err.Error(),
			},
		}, nil
	}
Z
Zach 已提交
2564
	tr.CtxRecord(ctx, "search request enqueue")
2565

Z
Zach 已提交
2566
	log.Ctx(ctx).Debug(
2567
		rpcEnqueued(method),
2568
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2569
		zap.Int64("msgID", qt.ID()),
D
dragondriver 已提交
2570 2571 2572 2573 2574
		zap.Uint64("timestamp", qt.Base.Timestamp),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames),
		zap.Any("dsl", request.Dsl),
2575
		zap.Any("len(PlaceholderGroup)", len(request.PlaceholderGroup)),
2576 2577 2578 2579
		zap.Any("OutputFields", request.OutputFields),
		zap.Any("search_params", request.SearchParams),
		zap.Uint64("travel_timestamp", travelTs),
		zap.Uint64("guarantee_timestamp", guaranteeTs))
D
dragondriver 已提交
2580

2581
	if err := qt.WaitToFinish(); err != nil {
Z
Zach 已提交
2582
		log.Ctx(ctx).Warn(
2583
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
2584
			zap.Error(err),
2585
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2586
			zap.Int64("msgID", qt.ID()),
D
dragondriver 已提交
2587 2588 2589 2590
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames),
			zap.Any("dsl", request.Dsl),
2591
			zap.Any("len(PlaceholderGroup)", len(request.PlaceholderGroup)),
2592 2593 2594 2595
			zap.Any("OutputFields", request.OutputFields),
			zap.Any("search_params", request.SearchParams),
			zap.Uint64("travel_timestamp", travelTs),
			zap.Uint64("guarantee_timestamp", guaranteeTs))
2596

2597 2598
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.FailLabel).Inc()
2599

2600 2601
		return &milvuspb.SearchResults{
			Status: &commonpb.Status{
2602
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
2603 2604 2605 2606 2607
				Reason:    err.Error(),
			},
		}, nil
	}

Z
Zach 已提交
2608
	span := tr.CtxRecord(ctx, "wait search result")
2609 2610
	metrics.ProxyWaitForSearchResultLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10),
		metrics.SearchLabel).Observe(float64(span.Milliseconds()))
Z
Zach 已提交
2611
	log.Ctx(ctx).Debug(
2612
		rpcDone(method),
2613
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2614 2615 2616 2617 2618 2619
		zap.Int64("msgID", qt.ID()),
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames),
		zap.Any("dsl", request.Dsl),
		zap.Any("len(PlaceholderGroup)", len(request.PlaceholderGroup)),
2620 2621 2622 2623
		zap.Any("OutputFields", request.OutputFields),
		zap.Any("search_params", request.SearchParams),
		zap.Uint64("travel_timestamp", travelTs),
		zap.Uint64("guarantee_timestamp", guaranteeTs))
D
dragondriver 已提交
2624

2625 2626 2627
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.SuccessLabel).Inc()
	metrics.ProxySearchVectors.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10)).Add(float64(qt.result.GetResults().GetNumQueries()))
C
cai.zhang 已提交
2628
	searchDur := tr.ElapseSpan().Milliseconds()
X
Xiaofan 已提交
2629
	metrics.ProxySearchLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10),
2630
		metrics.SearchLabel).Observe(float64(searchDur))
2631 2632 2633 2634 2635

	if qt.result != nil {
		sentSize := proto.Size(qt.result)
		metrics.ProxyReadReqSendBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10)).Add(float64(sentSize))
	}
2636 2637 2638
	return qt.result, nil
}

2639
// Flush notify data nodes to persist the data of collection.
2640 2641 2642 2643 2644 2645 2646
func (node *Proxy) Flush(ctx context.Context, request *milvuspb.FlushRequest) (*milvuspb.FlushResponse, error) {
	resp := &milvuspb.FlushResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    "",
		},
	}
2647
	if !node.checkHealthy() {
2648 2649
		resp.Status.Reason = "proxy is not healthy"
		return resp, nil
2650
	}
D
dragondriver 已提交
2651 2652 2653 2654 2655

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Flush")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

2656
	ft := &flushTask{
T
ThreadDao 已提交
2657 2658 2659
		ctx:          ctx,
		Condition:    NewTaskCondition(ctx),
		FlushRequest: request,
2660
		dataCoord:    node.dataCoord,
2661 2662
	}

D
dragondriver 已提交
2663
	method := "Flush"
2664
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
2665
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
D
dragondriver 已提交
2666 2667 2668 2669

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
2670
		zap.String("role", typeutil.ProxyRole),
G
godchen 已提交
2671 2672
		zap.String("db", request.DbName),
		zap.Any("collections", request.CollectionNames))
D
dragondriver 已提交
2673 2674 2675 2676 2677 2678

	if err := node.sched.ddQueue.Enqueue(ft); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
2679
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2680 2681 2682
			zap.String("db", request.DbName),
			zap.Any("collections", request.CollectionNames))

X
Xiaofan 已提交
2683
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
2684

2685 2686
		resp.Status.Reason = err.Error()
		return resp, nil
2687 2688
	}

D
dragondriver 已提交
2689 2690 2691
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
2692
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2693 2694 2695
		zap.Int64("MsgID", ft.ID()),
		zap.Uint64("BeginTs", ft.BeginTs()),
		zap.Uint64("EndTs", ft.EndTs()),
D
dragondriver 已提交
2696 2697
		zap.String("db", request.DbName),
		zap.Any("collections", request.CollectionNames))
D
dragondriver 已提交
2698 2699 2700 2701

	if err := ft.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
D
dragondriver 已提交
2702
			zap.Error(err),
D
dragondriver 已提交
2703
			zap.String("traceID", traceID),
2704
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2705 2706 2707
			zap.Int64("MsgID", ft.ID()),
			zap.Uint64("BeginTs", ft.BeginTs()),
			zap.Uint64("EndTs", ft.EndTs()),
D
dragondriver 已提交
2708 2709 2710
			zap.String("db", request.DbName),
			zap.Any("collections", request.CollectionNames))

X
Xiaofan 已提交
2711
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
2712

D
dragondriver 已提交
2713
		resp.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
2714 2715
		resp.Status.Reason = err.Error()
		return resp, nil
2716 2717
	}

D
dragondriver 已提交
2718 2719 2720
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
2721
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2722 2723 2724 2725 2726 2727
		zap.Int64("MsgID", ft.ID()),
		zap.Uint64("BeginTs", ft.BeginTs()),
		zap.Uint64("EndTs", ft.EndTs()),
		zap.String("db", request.DbName),
		zap.Any("collections", request.CollectionNames))

X
Xiaofan 已提交
2728 2729
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
2730
	return ft.result, nil
2731 2732
}

2733
// Query get the records by primary keys.
C
Cai Yudong 已提交
2734
func (node *Proxy) Query(ctx context.Context, request *milvuspb.QueryRequest) (*milvuspb.QueryResults, error) {
2735 2736 2737 2738 2739
	receiveSize := proto.Size(request)
	metrics.ProxyReceiveBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), metrics.QueryLabel).Add(float64(receiveSize))

	rateCol.Add(internalpb.RateType_DQLQuery.String(), 1)

2740 2741 2742 2743 2744
	if !node.checkHealthy() {
		return &milvuspb.QueryResults{
			Status: unhealthyStatus(),
		}, nil
	}
2745

D
dragondriver 已提交
2746 2747
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-Query")
	defer sp.Finish()
2748
	tr := timerecord.NewTimeRecorder("Query")
D
dragondriver 已提交
2749

2750
	qt := &queryTask{
2751 2752 2753 2754 2755
		ctx:       ctx,
		Condition: NewTaskCondition(ctx),
		RetrieveRequest: &internalpb.RetrieveRequest{
			Base: &commonpb.MsgBase{
				MsgType:  commonpb.MsgType_Retrieve,
X
Xiaofan 已提交
2756
				SourceID: Params.ProxyCfg.GetNodeID(),
2757
			},
2758
			ReqID: Params.ProxyCfg.GetNodeID(),
2759
		},
2760 2761
		request:          request,
		qc:               node.queryCoord,
2762
		queryShardPolicy: mergeRoundRobinPolicy,
2763
		shardMgr:         node.shardMgr,
2764 2765
	}

D
dragondriver 已提交
2766 2767
	method := "Query"

2768 2769 2770
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.TotalLabel).Inc()

Z
Zach 已提交
2771
	log.Ctx(ctx).Info(
D
dragondriver 已提交
2772
		rpcReceived(method),
2773
		zap.String("role", typeutil.ProxyRole),
2774 2775
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
2776 2777 2778 2779 2780
		zap.Strings("partitions", request.PartitionNames),
		zap.String("expr", request.Expr),
		zap.Strings("OutputFields", request.OutputFields),
		zap.Uint64("travel_timestamp", request.TravelTimestamp),
		zap.Uint64("guarantee_timestamp", request.GuaranteeTimestamp))
G
godchen 已提交
2781

D
dragondriver 已提交
2782
	if err := node.sched.dqQueue.Enqueue(qt); err != nil {
Z
Zach 已提交
2783
		log.Ctx(ctx).Warn(
D
dragondriver 已提交
2784 2785 2786
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("role", typeutil.ProxyRole),
2787 2788 2789
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))
D
dragondriver 已提交
2790

2791 2792 2793
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.FailLabel).Inc()

2794 2795 2796 2797 2798 2799
		return &milvuspb.QueryResults{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
2800
	}
Z
Zach 已提交
2801
	tr.CtxRecord(ctx, "query request enqueue")
2802

Z
Zach 已提交
2803
	log.Ctx(ctx).Debug(
D
dragondriver 已提交
2804
		rpcEnqueued(method),
2805
		zap.String("role", typeutil.ProxyRole),
2806
		zap.Int64("msgID", qt.ID()),
2807 2808
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
2809
		zap.Strings("partitions", request.PartitionNames))
D
dragondriver 已提交
2810 2811

	if err := qt.WaitToFinish(); err != nil {
Z
Zach 已提交
2812
		log.Ctx(ctx).Warn(
D
dragondriver 已提交
2813 2814
			rpcFailedToWaitToFinish(method),
			zap.Error(err),
2815
			zap.String("role", typeutil.ProxyRole),
2816
			zap.Int64("msgID", qt.ID()),
2817 2818 2819
			zap.String("db", request.DbName),
			zap.String("collection", request.CollectionName),
			zap.Any("partitions", request.PartitionNames))
2820

2821 2822
		metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
			metrics.FailLabel).Inc()
2823

2824 2825 2826 2827 2828 2829 2830
		return &milvuspb.QueryResults{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}
Z
Zach 已提交
2831
	span := tr.CtxRecord(ctx, "wait query result")
2832 2833
	metrics.ProxyWaitForSearchResultLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10),
		metrics.QueryLabel).Observe(float64(span.Milliseconds()))
Z
Zach 已提交
2834
	log.Ctx(ctx).Debug(
D
dragondriver 已提交
2835 2836
		rpcDone(method),
		zap.String("role", typeutil.ProxyRole),
2837
		zap.Int64("msgID", qt.ID()),
2838 2839 2840
		zap.String("db", request.DbName),
		zap.String("collection", request.CollectionName),
		zap.Any("partitions", request.PartitionNames))
D
dragondriver 已提交
2841

2842 2843 2844 2845
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
		metrics.SuccessLabel).Inc()

	metrics.ProxySearchLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10),
2846
		metrics.QueryLabel).Observe(float64(tr.ElapseSpan().Milliseconds()))
2847 2848

	ret := &milvuspb.QueryResults{
2849 2850
		Status:     qt.result.Status,
		FieldsData: qt.result.FieldsData,
2851 2852 2853 2854
	}
	sentSize := proto.Size(qt.result)
	metrics.ProxyReadReqSendBytes.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10)).Add(float64(sentSize))
	return ret, nil
2855
}
2856

2857
// CreateAlias create alias for collection, then you can search the collection with alias.
Y
Yusup 已提交
2858 2859 2860 2861
func (node *Proxy) CreateAlias(ctx context.Context, request *milvuspb.CreateAliasRequest) (*commonpb.Status, error) {
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
D
dragondriver 已提交
2862 2863 2864 2865 2866

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-CreateAlias")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

Y
Yusup 已提交
2867 2868 2869 2870 2871 2872 2873
	cat := &CreateAliasTask{
		ctx:                ctx,
		Condition:          NewTaskCondition(ctx),
		CreateAliasRequest: request,
		rootCoord:          node.rootCoord,
	}

D
dragondriver 已提交
2874
	method := "CreateAlias"
2875
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
2876
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
D
dragondriver 已提交
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))

	if err := node.sched.ddQueue.Enqueue(cat); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
			zap.String("role", typeutil.ProxyRole),
			zap.String("db", request.DbName),
			zap.String("alias", request.Alias),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
2896
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
2897

Y
Yusup 已提交
2898 2899 2900 2901 2902 2903
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
2904 2905 2906
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
2907
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2908 2909 2910 2911
		zap.Int64("MsgID", cat.ID()),
		zap.Uint64("BeginTs", cat.BeginTs()),
		zap.Uint64("EndTs", cat.EndTs()),
		zap.String("db", request.DbName),
Y
Yusup 已提交
2912 2913
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))
D
dragondriver 已提交
2914 2915 2916 2917

	if err := cat.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
Y
Yusup 已提交
2918
			zap.Error(err),
D
dragondriver 已提交
2919
			zap.String("traceID", traceID),
2920
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2921 2922 2923 2924
			zap.Int64("MsgID", cat.ID()),
			zap.Uint64("BeginTs", cat.BeginTs()),
			zap.Uint64("EndTs", cat.EndTs()),
			zap.String("db", request.DbName),
Y
Yusup 已提交
2925 2926
			zap.String("alias", request.Alias),
			zap.String("collection", request.CollectionName))
X
Xiaofan 已提交
2927
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
Y
Yusup 已提交
2928 2929 2930 2931 2932 2933 2934

		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.Int64("MsgID", cat.ID()),
		zap.Uint64("BeginTs", cat.BeginTs()),
		zap.Uint64("EndTs", cat.EndTs()),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
2946 2947
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
Y
Yusup 已提交
2948 2949 2950
	return cat.result, nil
}

2951
// DropAlias alter the alias of collection.
Y
Yusup 已提交
2952 2953 2954 2955
func (node *Proxy) DropAlias(ctx context.Context, request *milvuspb.DropAliasRequest) (*commonpb.Status, error) {
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
D
dragondriver 已提交
2956 2957 2958 2959 2960

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-DropAlias")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

Y
Yusup 已提交
2961 2962 2963 2964 2965 2966 2967
	dat := &DropAliasTask{
		ctx:              ctx,
		Condition:        NewTaskCondition(ctx),
		DropAliasRequest: request,
		rootCoord:        node.rootCoord,
	}

D
dragondriver 已提交
2968
	method := "DropAlias"
2969
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
2970
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
D
dragondriver 已提交
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias))

	if err := node.sched.ddQueue.Enqueue(dat); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
			zap.String("role", typeutil.ProxyRole),
			zap.String("db", request.DbName),
			zap.String("alias", request.Alias))
X
Xiaofan 已提交
2987
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
D
dragondriver 已提交
2988

Y
Yusup 已提交
2989 2990 2991 2992 2993 2994
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
2995 2996 2997
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
2998
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
2999 3000 3001 3002
		zap.Int64("MsgID", dat.ID()),
		zap.Uint64("BeginTs", dat.BeginTs()),
		zap.Uint64("EndTs", dat.EndTs()),
		zap.String("db", request.DbName),
Y
Yusup 已提交
3003
		zap.String("alias", request.Alias))
D
dragondriver 已提交
3004 3005 3006 3007

	if err := dat.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
Y
Yusup 已提交
3008
			zap.Error(err),
D
dragondriver 已提交
3009
			zap.String("traceID", traceID),
3010
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
3011 3012 3013 3014
			zap.Int64("MsgID", dat.ID()),
			zap.Uint64("BeginTs", dat.BeginTs()),
			zap.Uint64("EndTs", dat.EndTs()),
			zap.String("db", request.DbName),
Y
Yusup 已提交
3015 3016
			zap.String("alias", request.Alias))

X
Xiaofan 已提交
3017
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
3018

Y
Yusup 已提交
3019 3020 3021 3022 3023 3024
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.Int64("MsgID", dat.ID()),
		zap.Uint64("BeginTs", dat.BeginTs()),
		zap.Uint64("EndTs", dat.EndTs()),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias))

X
Xiaofan 已提交
3035 3036
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
Y
Yusup 已提交
3037 3038 3039
	return dat.result, nil
}

3040
// AlterAlias alter alias of collection.
Y
Yusup 已提交
3041 3042 3043 3044
func (node *Proxy) AlterAlias(ctx context.Context, request *milvuspb.AlterAliasRequest) (*commonpb.Status, error) {
	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}
D
dragondriver 已提交
3045 3046 3047 3048 3049

	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-AlterAlias")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

Y
Yusup 已提交
3050 3051 3052 3053 3054 3055 3056
	aat := &AlterAliasTask{
		ctx:               ctx,
		Condition:         NewTaskCondition(ctx),
		AlterAliasRequest: request,
		rootCoord:         node.rootCoord,
	}

D
dragondriver 已提交
3057
	method := "AlterAlias"
3058
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
3059
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
D
dragondriver 已提交
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077

	log.Debug(
		rpcReceived(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))

	if err := node.sched.ddQueue.Enqueue(aat); err != nil {
		log.Warn(
			rpcFailedToEnqueue(method),
			zap.Error(err),
			zap.String("traceID", traceID),
			zap.String("role", typeutil.ProxyRole),
			zap.String("db", request.DbName),
			zap.String("alias", request.Alias),
			zap.String("collection", request.CollectionName))
X
Xiaofan 已提交
3078
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.AbandonLabel).Inc()
D
dragondriver 已提交
3079

Y
Yusup 已提交
3080 3081 3082 3083 3084 3085
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
3086 3087 3088
	log.Debug(
		rpcEnqueued(method),
		zap.String("traceID", traceID),
3089
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
3090 3091 3092 3093
		zap.Int64("MsgID", aat.ID()),
		zap.Uint64("BeginTs", aat.BeginTs()),
		zap.Uint64("EndTs", aat.EndTs()),
		zap.String("db", request.DbName),
Y
Yusup 已提交
3094 3095
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))
D
dragondriver 已提交
3096 3097 3098 3099

	if err := aat.WaitToFinish(); err != nil {
		log.Warn(
			rpcFailedToWaitToFinish(method),
Y
Yusup 已提交
3100
			zap.Error(err),
D
dragondriver 已提交
3101
			zap.String("traceID", traceID),
3102
			zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
3103 3104 3105 3106
			zap.Int64("MsgID", aat.ID()),
			zap.Uint64("BeginTs", aat.BeginTs()),
			zap.Uint64("EndTs", aat.EndTs()),
			zap.String("db", request.DbName),
Y
Yusup 已提交
3107 3108 3109
			zap.String("alias", request.Alias),
			zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
3110
		metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.FailLabel).Inc()
3111

Y
Yusup 已提交
3112 3113 3114 3115 3116 3117
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}

D
dragondriver 已提交
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128
	log.Debug(
		rpcDone(method),
		zap.String("traceID", traceID),
		zap.String("role", typeutil.ProxyRole),
		zap.Int64("MsgID", aat.ID()),
		zap.Uint64("BeginTs", aat.BeginTs()),
		zap.Uint64("EndTs", aat.EndTs()),
		zap.String("db", request.DbName),
		zap.String("alias", request.Alias),
		zap.String("collection", request.CollectionName))

X
Xiaofan 已提交
3129 3130
	metrics.ProxyDDLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method, metrics.SuccessLabel).Inc()
	metrics.ProxyDDLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
Y
Yusup 已提交
3131 3132 3133
	return aat.result, nil
}

3134
// CalcDistance calculates the distances between vectors.
3135
func (node *Proxy) CalcDistance(ctx context.Context, request *milvuspb.CalcDistanceRequest) (*milvuspb.CalcDistanceResults, error) {
3136 3137 3138 3139 3140
	if !node.checkHealthy() {
		return &milvuspb.CalcDistanceResults{
			Status: unhealthyStatus(),
		}, nil
	}
3141

3142 3143 3144 3145
	sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "Proxy-CalcDistance")
	defer sp.Finish()
	traceID, _, _ := trace.InfoFromSpan(sp)

3146 3147
	query := func(ids *milvuspb.VectorIDs) (*milvuspb.QueryResults, error) {
		outputFields := []string{ids.FieldName}
3148

3149 3150 3151 3152 3153
		queryRequest := &milvuspb.QueryRequest{
			DbName:         "",
			CollectionName: ids.CollectionName,
			PartitionNames: ids.PartitionNames,
			OutputFields:   outputFields,
3154 3155
		}

3156
		qt := &queryTask{
3157 3158 3159 3160 3161
			ctx:       ctx,
			Condition: NewTaskCondition(ctx),
			RetrieveRequest: &internalpb.RetrieveRequest{
				Base: &commonpb.MsgBase{
					MsgType:  commonpb.MsgType_Retrieve,
X
Xiaofan 已提交
3162
					SourceID: Params.ProxyCfg.GetNodeID(),
3163
				},
3164
				ReqID: Params.ProxyCfg.GetNodeID(),
3165
			},
3166 3167 3168 3169
			request: queryRequest,
			qc:      node.queryCoord,
			ids:     ids.IdArray,

3170
			queryShardPolicy: mergeRoundRobinPolicy,
3171
			shardMgr:         node.shardMgr,
3172 3173
		}

G
groot 已提交
3174 3175 3176 3177 3178 3179
		items := []zapcore.Field{
			zap.String("collection", queryRequest.CollectionName),
			zap.Any("partitions", queryRequest.PartitionNames),
			zap.Any("OutputFields", queryRequest.OutputFields),
		}

3180
		err := node.sched.dqQueue.Enqueue(qt)
3181
		if err != nil {
G
groot 已提交
3182
			log.Error("CalcDistance queryTask failed to enqueue", append(items, zap.Error(err))...)
3183

3184 3185 3186 3187 3188
			return &milvuspb.QueryResults{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UnexpectedError,
					Reason:    err.Error(),
				},
3189
			}, err
3190
		}
3191

G
groot 已提交
3192
		log.Debug("CalcDistance queryTask enqueued", items...)
3193 3194 3195

		err = qt.WaitToFinish()
		if err != nil {
G
groot 已提交
3196
			log.Error("CalcDistance queryTask failed to WaitToFinish", append(items, zap.Error(err))...)
3197 3198 3199 3200 3201 3202

			return &milvuspb.QueryResults{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UnexpectedError,
					Reason:    err.Error(),
				},
3203
			}, err
3204
		}
3205

G
groot 已提交
3206
		log.Debug("CalcDistance queryTask Done", items...)
3207 3208

		return &milvuspb.QueryResults{
3209 3210
			Status:     qt.result.Status,
			FieldsData: qt.result.FieldsData,
3211 3212 3213
		}, nil
	}

G
groot 已提交
3214 3215 3216 3217
	// calcDistanceTask is not a standard task, no need to enqueue
	task := &calcDistanceTask{
		traceID:   traceID,
		queryFunc: query,
3218 3219
	}

G
groot 已提交
3220
	return task.Execute(ctx, request)
3221 3222
}

3223
// GetDdChannel returns the used channel for dd operations.
C
Cai Yudong 已提交
3224
func (node *Proxy) GetDdChannel(ctx context.Context, request *internalpb.GetDdChannelRequest) (*milvuspb.StringResponse, error) {
3225 3226
	panic("implement me")
}
X
XuanYang-cn 已提交
3227

3228
// GetPersistentSegmentInfo get the information of sealed segment.
C
Cai Yudong 已提交
3229
func (node *Proxy) GetPersistentSegmentInfo(ctx context.Context, req *milvuspb.GetPersistentSegmentInfoRequest) (*milvuspb.GetPersistentSegmentInfoResponse, error) {
D
dragondriver 已提交
3230
	log.Debug("GetPersistentSegmentInfo",
3231
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
3232 3233 3234
		zap.String("db", req.DbName),
		zap.Any("collection", req.CollectionName))

G
godchen 已提交
3235
	resp := &milvuspb.GetPersistentSegmentInfoResponse{
X
XuanYang-cn 已提交
3236
		Status: &commonpb.Status{
3237
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
X
XuanYang-cn 已提交
3238 3239
		},
	}
3240 3241 3242 3243
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}
3244 3245
	method := "GetPersistentSegmentInfo"
	tr := timerecord.NewTimeRecorder(method)
X
Xiaofan 已提交
3246
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
3247
		metrics.TotalLabel).Inc()
G
godchen 已提交
3248
	segments, err := node.getSegmentsOfCollection(ctx, req.DbName, req.CollectionName)
X
XuanYang-cn 已提交
3249
	if err != nil {
3250
		resp.Status.Reason = fmt.Errorf("getSegmentsOfCollection, err:%w", err).Error()
X
XuanYang-cn 已提交
3251 3252
		return resp, nil
	}
3253
	infoResp, err := node.dataCoord.GetSegmentInfo(ctx, &datapb.GetSegmentInfoRequest{
X
XuanYang-cn 已提交
3254
		Base: &commonpb.MsgBase{
3255
			MsgType:   commonpb.MsgType_SegmentInfo,
X
XuanYang-cn 已提交
3256 3257
			MsgID:     0,
			Timestamp: 0,
X
Xiaofan 已提交
3258
			SourceID:  Params.ProxyCfg.GetNodeID(),
X
XuanYang-cn 已提交
3259 3260 3261 3262
		},
		SegmentIDs: segments,
	})
	if err != nil {
3263
		log.Debug("GetPersistentSegmentInfo fail", zap.Error(err))
3264
		resp.Status.Reason = fmt.Errorf("dataCoord:GetSegmentInfo, err:%w", err).Error()
X
XuanYang-cn 已提交
3265 3266
		return resp, nil
	}
3267
	log.Debug("GetPersistentSegmentInfo ", zap.Int("len(infos)", len(infoResp.Infos)), zap.Any("status", infoResp.Status))
3268
	if infoResp.Status.ErrorCode != commonpb.ErrorCode_Success {
X
XuanYang-cn 已提交
3269 3270 3271 3272 3273 3274
		resp.Status.Reason = infoResp.Status.Reason
		return resp, nil
	}
	persistentInfos := make([]*milvuspb.PersistentSegmentInfo, len(infoResp.Infos))
	for i, info := range infoResp.Infos {
		persistentInfos[i] = &milvuspb.PersistentSegmentInfo{
S
sunby 已提交
3275
			SegmentID:    info.ID,
X
XuanYang-cn 已提交
3276 3277
			CollectionID: info.CollectionID,
			PartitionID:  info.PartitionID,
S
sunby 已提交
3278
			NumRows:      info.NumOfRows,
X
XuanYang-cn 已提交
3279 3280 3281
			State:        info.State,
		}
	}
X
Xiaofan 已提交
3282
	metrics.ProxyDQLFunctionCall.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method,
3283
		metrics.SuccessLabel).Inc()
X
Xiaofan 已提交
3284
	metrics.ProxyDQLReqLatency.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10), method).Observe(float64(tr.ElapseSpan().Milliseconds()))
3285
	resp.Status.ErrorCode = commonpb.ErrorCode_Success
X
XuanYang-cn 已提交
3286 3287 3288 3289
	resp.Infos = persistentInfos
	return resp, nil
}

J
jingkl 已提交
3290
// GetQuerySegmentInfo gets segment information from QueryCoord.
C
Cai Yudong 已提交
3291
func (node *Proxy) GetQuerySegmentInfo(ctx context.Context, req *milvuspb.GetQuerySegmentInfoRequest) (*milvuspb.GetQuerySegmentInfoResponse, error) {
D
dragondriver 已提交
3292
	log.Debug("GetQuerySegmentInfo",
3293
		zap.String("role", typeutil.ProxyRole),
D
dragondriver 已提交
3294 3295 3296
		zap.String("db", req.DbName),
		zap.Any("collection", req.CollectionName))

G
godchen 已提交
3297
	resp := &milvuspb.GetQuerySegmentInfoResponse{
Z
zhenshan.cao 已提交
3298
		Status: &commonpb.Status{
3299
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
Z
zhenshan.cao 已提交
3300 3301
		},
	}
3302 3303 3304 3305
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}
3306

3307 3308 3309 3310 3311
	collID, err := globalMetaCache.GetCollectionID(ctx, req.CollectionName)
	if err != nil {
		resp.Status.Reason = err.Error()
		return resp, nil
	}
3312
	infoResp, err := node.queryCoord.GetSegmentInfo(ctx, &querypb.GetSegmentInfoRequest{
Z
zhenshan.cao 已提交
3313
		Base: &commonpb.MsgBase{
3314
			MsgType:   commonpb.MsgType_SegmentInfo,
Z
zhenshan.cao 已提交
3315 3316
			MsgID:     0,
			Timestamp: 0,
X
Xiaofan 已提交
3317
			SourceID:  Params.ProxyCfg.GetNodeID(),
Z
zhenshan.cao 已提交
3318
		},
3319
		CollectionID: collID,
Z
zhenshan.cao 已提交
3320 3321
	})
	if err != nil {
3322
		log.Error("Failed to get segment info from QueryCoord",
3323
			zap.Error(err))
Z
zhenshan.cao 已提交
3324 3325 3326
		resp.Status.Reason = err.Error()
		return resp, nil
	}
3327
	log.Debug("GetQuerySegmentInfo ", zap.Any("infos", infoResp.Infos), zap.Any("status", infoResp.Status))
3328
	if infoResp.Status.ErrorCode != commonpb.ErrorCode_Success {
3329
		log.Error("Failed to get segment info from QueryCoord", zap.String("errMsg", infoResp.Status.Reason))
Z
zhenshan.cao 已提交
3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
		resp.Status.Reason = infoResp.Status.Reason
		return resp, nil
	}
	queryInfos := make([]*milvuspb.QuerySegmentInfo, len(infoResp.Infos))
	for i, info := range infoResp.Infos {
		queryInfos[i] = &milvuspb.QuerySegmentInfo{
			SegmentID:    info.SegmentID,
			CollectionID: info.CollectionID,
			PartitionID:  info.PartitionID,
			NumRows:      info.NumRows,
			MemSize:      info.MemSize,
			IndexName:    info.IndexName,
			IndexID:      info.IndexID,
X
xige-16 已提交
3343
			State:        info.SegmentState,
3344
			NodeIds:      info.NodeIds,
Z
zhenshan.cao 已提交
3345 3346
		}
	}
3347
	resp.Status.ErrorCode = commonpb.ErrorCode_Success
Z
zhenshan.cao 已提交
3348 3349 3350 3351
	resp.Infos = queryInfos
	return resp, nil
}

C
Cai Yudong 已提交
3352
func (node *Proxy) getSegmentsOfCollection(ctx context.Context, dbName string, collectionName string) ([]UniqueID, error) {
3353
	describeCollectionResponse, err := node.rootCoord.DescribeCollection(ctx, &milvuspb.DescribeCollectionRequest{
X
XuanYang-cn 已提交
3354
		Base: &commonpb.MsgBase{
3355
			MsgType:   commonpb.MsgType_DescribeCollection,
X
XuanYang-cn 已提交
3356 3357
			MsgID:     0,
			Timestamp: 0,
X
Xiaofan 已提交
3358
			SourceID:  Params.ProxyCfg.GetNodeID(),
X
XuanYang-cn 已提交
3359 3360 3361 3362 3363 3364 3365
		},
		DbName:         dbName,
		CollectionName: collectionName,
	})
	if err != nil {
		return nil, err
	}
3366
	if describeCollectionResponse.Status.ErrorCode != commonpb.ErrorCode_Success {
X
XuanYang-cn 已提交
3367 3368 3369
		return nil, errors.New(describeCollectionResponse.Status.Reason)
	}
	collectionID := describeCollectionResponse.CollectionID
3370
	showPartitionsResp, err := node.rootCoord.ShowPartitions(ctx, &milvuspb.ShowPartitionsRequest{
X
XuanYang-cn 已提交
3371
		Base: &commonpb.MsgBase{
3372
			MsgType:   commonpb.MsgType_ShowPartitions,
X
XuanYang-cn 已提交
3373 3374
			MsgID:     0,
			Timestamp: 0,
X
Xiaofan 已提交
3375
			SourceID:  Params.ProxyCfg.GetNodeID(),
X
XuanYang-cn 已提交
3376 3377 3378 3379 3380 3381 3382 3383
		},
		DbName:         dbName,
		CollectionName: collectionName,
		CollectionID:   collectionID,
	})
	if err != nil {
		return nil, err
	}
3384
	if showPartitionsResp.Status.ErrorCode != commonpb.ErrorCode_Success {
X
XuanYang-cn 已提交
3385 3386 3387 3388 3389
		return nil, errors.New(showPartitionsResp.Status.Reason)
	}

	ret := make([]UniqueID, 0)
	for _, partitionID := range showPartitionsResp.PartitionIDs {
3390
		showSegmentResponse, err := node.rootCoord.ShowSegments(ctx, &milvuspb.ShowSegmentsRequest{
X
XuanYang-cn 已提交
3391
			Base: &commonpb.MsgBase{
3392
				MsgType:   commonpb.MsgType_ShowSegments,
X
XuanYang-cn 已提交
3393 3394
				MsgID:     0,
				Timestamp: 0,
X
Xiaofan 已提交
3395
				SourceID:  Params.ProxyCfg.GetNodeID(),
X
XuanYang-cn 已提交
3396 3397 3398 3399 3400 3401 3402
			},
			CollectionID: collectionID,
			PartitionID:  partitionID,
		})
		if err != nil {
			return nil, err
		}
3403
		if showSegmentResponse.Status.ErrorCode != commonpb.ErrorCode_Success {
X
XuanYang-cn 已提交
3404 3405 3406 3407 3408 3409
			return nil, errors.New(showSegmentResponse.Status.Reason)
		}
		ret = append(ret, showSegmentResponse.SegmentIDs...)
	}
	return ret, nil
}
3410

J
jingkl 已提交
3411
// Dummy handles dummy request
C
Cai Yudong 已提交
3412
func (node *Proxy) Dummy(ctx context.Context, req *milvuspb.DummyRequest) (*milvuspb.DummyResponse, error) {
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
	failedResponse := &milvuspb.DummyResponse{
		Response: `{"status": "fail"}`,
	}

	// TODO(wxyu): change name RequestType to Request
	drt, err := parseDummyRequestType(req.RequestType)
	if err != nil {
		log.Debug("Failed to parse dummy request type")
		return failedResponse, nil
	}

3424 3425
	if drt.RequestType == "query" {
		drr, err := parseDummyQueryRequest(req.RequestType)
3426
		if err != nil {
3427
			log.Debug("Failed to parse dummy query request")
3428 3429 3430
			return failedResponse, nil
		}

3431
		request := &milvuspb.QueryRequest{
3432 3433 3434
			DbName:         drr.DbName,
			CollectionName: drr.CollectionName,
			PartitionNames: drr.PartitionNames,
3435
			OutputFields:   drr.OutputFields,
X
Xiangyu Wang 已提交
3436 3437
		}

3438
		_, err = node.Query(ctx, request)
3439
		if err != nil {
3440
			log.Debug("Failed to execute dummy query")
3441 3442
			return failedResponse, err
		}
X
Xiangyu Wang 已提交
3443 3444 3445 3446 3447 3448

		return &milvuspb.DummyResponse{
			Response: `{"status": "success"}`,
		}, nil
	}

3449 3450
	log.Debug("cannot find specify dummy request type")
	return failedResponse, nil
X
Xiangyu Wang 已提交
3451 3452
}

J
jingkl 已提交
3453
// RegisterLink registers a link
C
Cai Yudong 已提交
3454
func (node *Proxy) RegisterLink(ctx context.Context, req *milvuspb.RegisterLinkRequest) (*milvuspb.RegisterLinkResponse, error) {
G
godchen 已提交
3455
	code := node.stateCode.Load().(internalpb.StateCode)
D
dragondriver 已提交
3456
	log.Debug("RegisterLink",
3457
		zap.String("role", typeutil.ProxyRole),
C
Cai Yudong 已提交
3458
		zap.Any("state code of proxy", code))
D
dragondriver 已提交
3459

G
godchen 已提交
3460
	if code != internalpb.StateCode_Healthy {
3461 3462 3463
		return &milvuspb.RegisterLinkResponse{
			Address: nil,
			Status: &commonpb.Status{
3464
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
C
Cai Yudong 已提交
3465
				Reason:    "proxy not healthy",
3466 3467 3468
			},
		}, nil
	}
X
Xiaofan 已提交
3469
	//metrics.ProxyLinkedSDKs.WithLabelValues(strconv.FormatInt(Params.ProxyCfg.GetNodeID(), 10)).Inc()
3470 3471 3472
	return &milvuspb.RegisterLinkResponse{
		Address: nil,
		Status: &commonpb.Status{
3473
			ErrorCode: commonpb.ErrorCode_Success,
3474
			Reason:    os.Getenv(metricsinfo.DeployModeEnvKey),
3475 3476 3477
		},
	}, nil
}
3478

3479
// GetMetrics gets the metrics of proxy
3480 3481 3482
// TODO(dragondriver): cache the Metrics and set a retention to the cache
func (node *Proxy) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
	log.Debug("Proxy.GetMetrics",
X
Xiaofan 已提交
3483
		zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
3484 3485 3486 3487
		zap.String("req", req.Request))

	if !node.checkHealthy() {
		log.Warn("Proxy.GetMetrics failed",
X
Xiaofan 已提交
3488
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
3489
			zap.String("req", req.Request),
X
Xiaofan 已提交
3490
			zap.Error(errProxyIsUnhealthy(Params.ProxyCfg.GetNodeID())))
3491 3492 3493 3494

		return &milvuspb.GetMetricsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
X
Xiaofan 已提交
3495
				Reason:    msgProxyIsUnhealthy(Params.ProxyCfg.GetNodeID()),
3496 3497 3498 3499 3500 3501 3502 3503
			},
			Response: "",
		}, nil
	}

	metricType, err := metricsinfo.ParseMetricType(req.Request)
	if err != nil {
		log.Warn("Proxy.GetMetrics failed to parse metric type",
X
Xiaofan 已提交
3504
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519
			zap.String("req", req.Request),
			zap.Error(err))

		return &milvuspb.GetMetricsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
			Response: "",
		}, nil
	}

	log.Debug("Proxy.GetMetrics",
		zap.String("metric_type", metricType))

D
dragondriver 已提交
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
	msgID := UniqueID(0)
	msgID, err = node.idAllocator.AllocOne()
	if err != nil {
		log.Warn("Proxy.GetMetrics failed to allocate id",
			zap.Error(err))
	}
	req.Base = &commonpb.MsgBase{
		MsgType:   commonpb.MsgType_SystemInfo,
		MsgID:     msgID,
		Timestamp: 0,
X
Xiaofan 已提交
3530
		SourceID:  Params.ProxyCfg.GetNodeID(),
D
dragondriver 已提交
3531 3532
	}

3533
	if metricType == metricsinfo.SystemInfoMetrics {
3534 3535 3536 3537 3538 3539 3540
		ret, err := node.metricsCacheManager.GetSystemInfoMetrics()
		if err == nil && ret != nil {
			return ret, nil
		}
		log.Debug("failed to get system info metrics from cache, recompute instead",
			zap.Error(err))

3541
		metrics, err := getSystemInfoMetrics(ctx, req, node)
3542 3543

		log.Debug("Proxy.GetMetrics",
X
Xiaofan 已提交
3544
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
3545 3546 3547 3548 3549
			zap.String("req", req.Request),
			zap.String("metric_type", metricType),
			zap.Any("metrics", metrics), // TODO(dragondriver): necessary? may be very large
			zap.Error(err))

3550 3551
		node.metricsCacheManager.UpdateSystemInfoMetrics(metrics)

G
godchen 已提交
3552
		return metrics, nil
3553 3554 3555
	}

	log.Debug("Proxy.GetMetrics failed, request metric type is not implemented yet",
X
Xiaofan 已提交
3556
		zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
		zap.String("req", req.Request),
		zap.String("metric_type", metricType))

	return &milvuspb.GetMetricsResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    metricsinfo.MsgUnimplementedMetric,
		},
		Response: "",
	}, nil
}

3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
// GetProxyMetrics gets the metrics of proxy, it's an internal interface which is different from GetMetrics interface,
// because it only obtains the metrics of Proxy, not including the topological metrics of Query cluster and Data cluster.
func (node *Proxy) GetProxyMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
	log.Debug("Proxy.GetProxyMetrics",
		zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
		zap.String("req", req.Request))

	if !node.checkHealthy() {
		log.Warn("Proxy.GetProxyMetrics failed",
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
			zap.String("req", req.Request),
			zap.Error(errProxyIsUnhealthy(Params.ProxyCfg.GetNodeID())))

		return &milvuspb.GetMetricsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    msgProxyIsUnhealthy(Params.ProxyCfg.GetNodeID()),
			},
		}, nil
	}

	metricType, err := metricsinfo.ParseMetricType(req.Request)
	if err != nil {
		log.Warn("Proxy.GetProxyMetrics failed to parse metric type",
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
			zap.String("req", req.Request),
			zap.Error(err))

		return &milvuspb.GetMetricsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}

	log.Debug("Proxy.GetProxyMetrics",
		zap.String("metric_type", metricType))

	msgID := UniqueID(0)
	msgID, err = node.idAllocator.AllocOne()
	if err != nil {
		log.Warn("Proxy.GetProxyMetrics failed to allocate id",
			zap.Error(err))
	}
	req.Base = &commonpb.MsgBase{
		MsgType:  commonpb.MsgType_SystemInfo,
		MsgID:    msgID,
		SourceID: Params.ProxyCfg.GetNodeID(),
	}

	if metricType == metricsinfo.SystemInfoMetrics {
		proxyMetrics, err := getProxyMetrics(ctx, req, node)
		if err != nil {
			log.Warn("Proxy.GetProxyMetrics failed to getProxyMetrics",
				zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
				zap.String("req", req.Request),
				zap.Error(err))

			return &milvuspb.GetMetricsResponse{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UnexpectedError,
					Reason:    err.Error(),
				},
			}, nil
		}

		log.Debug("Proxy.GetProxyMetrics",
			zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
			zap.String("req", req.Request),
			zap.String("metric_type", metricType),
			zap.Error(err))

		return proxyMetrics, nil
	}

	log.Debug("Proxy.GetProxyMetrics failed, request metric type is not implemented yet",
		zap.Int64("node_id", Params.ProxyCfg.GetNodeID()),
		zap.String("req", req.Request),
		zap.String("metric_type", metricType))

	return &milvuspb.GetMetricsResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    metricsinfo.MsgUnimplementedMetric,
		},
	}, nil
}

B
bigsheeper 已提交
3658 3659 3660
// LoadBalance would do a load balancing operation between query nodes
func (node *Proxy) LoadBalance(ctx context.Context, req *milvuspb.LoadBalanceRequest) (*commonpb.Status, error) {
	log.Debug("Proxy.LoadBalance",
X
Xiaofan 已提交
3661
		zap.Int64("proxy_id", Params.ProxyCfg.GetNodeID()),
B
bigsheeper 已提交
3662 3663 3664 3665 3666 3667 3668 3669 3670
		zap.Any("req", req))

	if !node.checkHealthy() {
		return unhealthyStatus(), nil
	}

	status := &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_UnexpectedError,
	}
3671 3672 3673 3674 3675 3676 3677

	collectionID, err := globalMetaCache.GetCollectionID(ctx, req.GetCollectionName())
	if err != nil {
		log.Error("failed to get collection id", zap.String("collection name", req.GetCollectionName()), zap.Error(err))
		status.Reason = err.Error()
		return status, nil
	}
B
bigsheeper 已提交
3678 3679 3680 3681 3682
	infoResp, err := node.queryCoord.LoadBalance(ctx, &querypb.LoadBalanceRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_LoadBalanceSegments,
			MsgID:     0,
			Timestamp: 0,
X
Xiaofan 已提交
3683
			SourceID:  Params.ProxyCfg.GetNodeID(),
B
bigsheeper 已提交
3684 3685 3686
		},
		SourceNodeIDs:    []int64{req.SrcNodeID},
		DstNodeIDs:       req.DstNodeIDs,
X
xige-16 已提交
3687
		BalanceReason:    querypb.TriggerCondition_GrpcRequest,
B
bigsheeper 已提交
3688
		SealedSegmentIDs: req.SealedSegmentIDs,
3689
		CollectionID:     collectionID,
B
bigsheeper 已提交
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
	})
	if err != nil {
		log.Error("Failed to LoadBalance from Query Coordinator",
			zap.Any("req", req), zap.Error(err))
		status.Reason = err.Error()
		return status, nil
	}
	if infoResp.ErrorCode != commonpb.ErrorCode_Success {
		log.Error("Failed to LoadBalance from Query Coordinator", zap.String("errMsg", infoResp.Reason))
		status.Reason = infoResp.Reason
		return status, nil
	}
	log.Debug("LoadBalance Done", zap.Any("req", req), zap.Any("status", infoResp))
	status.ErrorCode = commonpb.ErrorCode_Success
	return status, nil
}

J
jingkl 已提交
3707
//GetCompactionState gets the compaction state of multiple segments
3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
func (node *Proxy) GetCompactionState(ctx context.Context, req *milvuspb.GetCompactionStateRequest) (*milvuspb.GetCompactionStateResponse, error) {
	log.Info("received GetCompactionState request", zap.Int64("compactionID", req.GetCompactionID()))
	resp := &milvuspb.GetCompactionStateResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

	resp, err := node.dataCoord.GetCompactionState(ctx, req)
	log.Info("received GetCompactionState response", zap.Int64("compactionID", req.GetCompactionID()), zap.Any("resp", resp), zap.Error(err))
	return resp, err
}

3721
// ManualCompaction invokes compaction on specified collection
3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734
func (node *Proxy) ManualCompaction(ctx context.Context, req *milvuspb.ManualCompactionRequest) (*milvuspb.ManualCompactionResponse, error) {
	log.Info("received ManualCompaction request", zap.Int64("collectionID", req.GetCollectionID()))
	resp := &milvuspb.ManualCompactionResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

	resp, err := node.dataCoord.ManualCompaction(ctx, req)
	log.Info("received ManualCompaction response", zap.Int64("collectionID", req.GetCollectionID()), zap.Any("resp", resp), zap.Error(err))
	return resp, err
}

3735
// GetCompactionStateWithPlans returns the compactions states with the given plan ID
3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
func (node *Proxy) GetCompactionStateWithPlans(ctx context.Context, req *milvuspb.GetCompactionPlansRequest) (*milvuspb.GetCompactionPlansResponse, error) {
	log.Info("received GetCompactionStateWithPlans request", zap.Int64("compactionID", req.GetCompactionID()))
	resp := &milvuspb.GetCompactionPlansResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

	resp, err := node.dataCoord.GetCompactionStateWithPlans(ctx, req)
	log.Info("received GetCompactionStateWithPlans response", zap.Int64("compactionID", req.GetCompactionID()), zap.Any("resp", resp), zap.Error(err))
	return resp, err
}

B
Bingyi Sun 已提交
3749 3750 3751
// GetFlushState gets the flush state of multiple segments
func (node *Proxy) GetFlushState(ctx context.Context, req *milvuspb.GetFlushStateRequest) (*milvuspb.GetFlushStateResponse, error) {
	log.Info("received get flush state request", zap.Any("request", req))
3752
	var err error
B
Bingyi Sun 已提交
3753 3754 3755 3756 3757 3758 3759
	resp := &milvuspb.GetFlushStateResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		log.Info("unable to get flush state because of closed server")
		return resp, nil
	}

3760
	resp, err = node.dataCoord.GetFlushState(ctx, req)
X
Xiaofan 已提交
3761 3762 3763 3764
	if err != nil {
		log.Info("failed to get flush state response", zap.Error(err))
		return nil, err
	}
B
Bingyi Sun 已提交
3765 3766 3767 3768
	log.Info("received get flush state response", zap.Any("response", resp))
	return resp, err
}

C
Cai Yudong 已提交
3769 3770
// checkHealthy checks proxy state is Healthy
func (node *Proxy) checkHealthy() bool {
3771 3772 3773 3774
	code := node.stateCode.Load().(internalpb.StateCode)
	return code == internalpb.StateCode_Healthy
}

3775 3776 3777 3778 3779
func (node *Proxy) checkHealthyAndReturnCode() (internalpb.StateCode, bool) {
	code := node.stateCode.Load().(internalpb.StateCode)
	return code, code == internalpb.StateCode_Healthy
}

J
jingkl 已提交
3780
//unhealthyStatus returns the proxy not healthy status
3781 3782 3783
func unhealthyStatus() *commonpb.Status {
	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_UnexpectedError,
C
Cai Yudong 已提交
3784
		Reason:    "proxy not healthy",
3785 3786
	}
}
G
groot 已提交
3787 3788 3789

// Import data files(json, numpy, etc.) on MinIO/S3 storage, read and parse them into sealed segments
func (node *Proxy) Import(ctx context.Context, req *milvuspb.ImportRequest) (*milvuspb.ImportResponse, error) {
3790 3791 3792
	log.Info("received import request",
		zap.String("collection name", req.GetCollectionName()),
		zap.Bool("row-based", req.GetRowBased()))
3793 3794 3795 3796 3797 3798
	resp := &milvuspb.ImportResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
			Reason:    "",
		},
	}
G
groot 已提交
3799 3800 3801 3802
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}
3803
	// Call rootCoord to finish import.
3804 3805 3806 3807 3808 3809 3810 3811
	respFromRC, err := node.rootCoord.Import(ctx, req)
	if err != nil {
		log.Error("failed to execute bulk load request", zap.Error(err))
		resp.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
		resp.Status.Reason = err.Error()
		return resp, nil
	}
	return respFromRC, nil
G
groot 已提交
3812 3813
}

3814
// GetImportState checks import task state from RootCoord.
G
groot 已提交
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
func (node *Proxy) GetImportState(ctx context.Context, req *milvuspb.GetImportStateRequest) (*milvuspb.GetImportStateResponse, error) {
	log.Info("received get import state request", zap.Int64("taskID", req.GetTask()))
	resp := &milvuspb.GetImportStateResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

	resp, err := node.rootCoord.GetImportState(ctx, req)
	log.Info("received get import state response", zap.Int64("taskID", req.GetTask()), zap.Any("resp", resp), zap.Error(err))
	return resp, err
}

// ListImportTasks get id array of all import tasks from rootcoord
func (node *Proxy) ListImportTasks(ctx context.Context, req *milvuspb.ListImportTasksRequest) (*milvuspb.ListImportTasksResponse, error) {
	log.Info("received list import tasks request")
	resp := &milvuspb.ListImportTasksResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

	resp, err := node.rootCoord.ListImportTasks(ctx, req)
	log.Info("received list import tasks response")
	return resp, err
}

X
XuanYang-cn 已提交
3842 3843 3844 3845 3846 3847 3848 3849 3850
// GetReplicas gets replica info
func (node *Proxy) GetReplicas(ctx context.Context, req *milvuspb.GetReplicasRequest) (*milvuspb.GetReplicasResponse, error) {
	log.Info("received get replicas request")
	resp := &milvuspb.GetReplicasResponse{}
	if !node.checkHealthy() {
		resp.Status = unhealthyStatus()
		return resp, nil
	}

3851 3852
	req.Base = &commonpb.MsgBase{
		MsgType:  commonpb.MsgType_GetReplicas,
X
Xiaofan 已提交
3853
		SourceID: Params.ProxyCfg.GetNodeID(),
3854 3855
	}

X
XuanYang-cn 已提交
3856 3857 3858 3859 3860
	resp, err := node.queryCoord.GetReplicas(ctx, req)
	log.Info("received get replicas response", zap.Any("resp", resp), zap.Error(err))
	return resp, err
}

3861 3862 3863 3864 3865 3866
// InvalidateCredentialCache invalidate the credential cache of specified username.
func (node *Proxy) InvalidateCredentialCache(ctx context.Context, request *proxypb.InvalidateCredCacheRequest) (*commonpb.Status, error) {
	ctx = logutil.WithModule(ctx, moduleName)
	logutil.Logger(ctx).Debug("received request to invalidate credential cache",
		zap.String("role", typeutil.ProxyRole),
		zap.String("username", request.Username))
3867
	if !node.checkHealthy() {
3868
		return unhealthyStatus(), nil
3869
	}
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890

	username := request.Username
	if globalMetaCache != nil {
		globalMetaCache.RemoveCredential(username) // no need to return error, though credential may be not cached
	}
	logutil.Logger(ctx).Debug("complete to invalidate credential cache",
		zap.String("role", typeutil.ProxyRole),
		zap.String("username", request.Username))

	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_Success,
		Reason:    "",
	}, nil
}

// UpdateCredentialCache update the credential cache of specified username.
func (node *Proxy) UpdateCredentialCache(ctx context.Context, request *proxypb.UpdateCredCacheRequest) (*commonpb.Status, error) {
	ctx = logutil.WithModule(ctx, moduleName)
	logutil.Logger(ctx).Debug("received request to update credential cache",
		zap.String("role", typeutil.ProxyRole),
		zap.String("username", request.Username))
3891
	if !node.checkHealthy() {
3892
		return unhealthyStatus(), nil
3893
	}
3894 3895

	credInfo := &internalpb.CredentialInfo{
3896 3897
		Username:       request.Username,
		Sha256Password: request.Password,
3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912
	}
	if globalMetaCache != nil {
		globalMetaCache.UpdateCredential(credInfo) // no need to return error, though credential may be not cached
	}
	logutil.Logger(ctx).Debug("complete to update credential cache",
		zap.String("role", typeutil.ProxyRole),
		zap.String("username", request.Username))

	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_Success,
		Reason:    "",
	}, nil
}

func (node *Proxy) CreateCredential(ctx context.Context, req *milvuspb.CreateCredentialRequest) (*commonpb.Status, error) {
3913 3914
	log.Debug("CreateCredential", zap.String("role", typeutil.ProxyRole), zap.String("username", req.Username))
	if !node.checkHealthy() {
3915
		return unhealthyStatus(), nil
3916
	}
3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
	// validate params
	username := req.Username
	if err := ValidateUsername(username); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
		}, nil
	}
	rawPassword, err := crypto.Base64Decode(req.Password)
	if err != nil {
		log.Error("decode password fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_CreateCredentialFailure,
			Reason:    "decode password fail key:" + req.Username,
		}, nil
	}
	if err = ValidatePassword(rawPassword); err != nil {
		log.Error("illegal password", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
		}, nil
	}
	encryptedPassword, err := crypto.PasswordEncrypt(rawPassword)
	if err != nil {
		log.Error("encrypt password fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_CreateCredentialFailure,
			Reason:    "encrypt password fail key:" + req.Username,
		}, nil
	}
3948

3949 3950 3951
	credInfo := &internalpb.CredentialInfo{
		Username:          req.Username,
		EncryptedPassword: encryptedPassword,
3952
		Sha256Password:    crypto.SHA256(rawPassword, req.Username),
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
	}
	result, err := node.rootCoord.CreateCredential(ctx, credInfo)
	if err != nil { // for error like conntext timeout etc.
		log.Error("create credential fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}
	return result, err
}

C
codeman 已提交
3965
func (node *Proxy) UpdateCredential(ctx context.Context, req *milvuspb.UpdateCredentialRequest) (*commonpb.Status, error) {
3966 3967
	log.Debug("UpdateCredential", zap.String("role", typeutil.ProxyRole), zap.String("username", req.Username))
	if !node.checkHealthy() {
3968
		return unhealthyStatus(), nil
3969
	}
C
codeman 已提交
3970 3971 3972 3973 3974 3975 3976 3977 3978
	rawOldPassword, err := crypto.Base64Decode(req.OldPassword)
	if err != nil {
		log.Error("decode old password fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UpdateCredentialFailure,
			Reason:    "decode old password fail when updating:" + req.Username,
		}, nil
	}
	rawNewPassword, err := crypto.Base64Decode(req.NewPassword)
3979 3980 3981 3982 3983 3984 3985
	if err != nil {
		log.Error("decode password fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UpdateCredentialFailure,
			Reason:    "decode password fail when updating:" + req.Username,
		}, nil
	}
C
codeman 已提交
3986 3987
	// valid new password
	if err = ValidatePassword(rawNewPassword); err != nil {
3988 3989 3990 3991 3992 3993
		log.Error("illegal password", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
		}, nil
	}
3994 3995

	if !passwordVerify(ctx, req.Username, rawOldPassword, globalMetaCache) {
C
codeman 已提交
3996 3997 3998 3999 4000 4001 4002
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UpdateCredentialFailure,
			Reason:    "old password is not correct:" + req.Username,
		}, nil
	}
	// update meta data
	encryptedPassword, err := crypto.PasswordEncrypt(rawNewPassword)
4003 4004 4005 4006 4007 4008 4009
	if err != nil {
		log.Error("encrypt password fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UpdateCredentialFailure,
			Reason:    "encrypt password fail when updating:" + req.Username,
		}, nil
	}
C
codeman 已提交
4010
	updateCredReq := &internalpb.CredentialInfo{
4011
		Username:          req.Username,
4012
		Sha256Password:    crypto.SHA256(rawNewPassword, req.Username),
4013 4014
		EncryptedPassword: encryptedPassword,
	}
C
codeman 已提交
4015
	result, err := node.rootCoord.UpdateCredential(ctx, updateCredReq)
4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
	if err != nil { // for error like conntext timeout etc.
		log.Error("update credential fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}
	return result, err
}

func (node *Proxy) DeleteCredential(ctx context.Context, req *milvuspb.DeleteCredentialRequest) (*commonpb.Status, error) {
4027 4028
	log.Debug("DeleteCredential", zap.String("role", typeutil.ProxyRole), zap.String("username", req.Username))
	if !node.checkHealthy() {
4029
		return unhealthyStatus(), nil
4030 4031
	}

4032 4033 4034 4035 4036 4037
	if req.Username == util.UserRoot {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_DeleteCredentialFailure,
			Reason:    "user root cannot be deleted",
		}, nil
	}
4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
	result, err := node.rootCoord.DeleteCredential(ctx, req)
	if err != nil { // for error like conntext timeout etc.
		log.Error("delete credential fail", zap.String("username", req.Username), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
		}, nil
	}
	return result, err
}

func (node *Proxy) ListCredUsers(ctx context.Context, req *milvuspb.ListCredUsersRequest) (*milvuspb.ListCredUsersResponse, error) {
4050 4051
	log.Debug("ListCredUsers", zap.String("role", typeutil.ProxyRole))
	if !node.checkHealthy() {
4052
		return &milvuspb.ListCredUsersResponse{Status: unhealthyStatus()}, nil
4053
	}
4054 4055 4056 4057 4058 4059
	rootCoordReq := &milvuspb.ListCredUsersRequest{
		Base: &commonpb.MsgBase{
			MsgType: commonpb.MsgType_ListCredUsernames,
		},
	}
	resp, err := node.rootCoord.ListCredUsers(ctx, rootCoordReq)
4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
	if err != nil {
		return &milvuspb.ListCredUsersResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
		}, nil
	}
	return &milvuspb.ListCredUsersResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_Success,
		},
4072
		Usernames: resp.Usernames,
4073 4074
	}, nil
}
4075

4076 4077 4078
func (node *Proxy) CreateRole(ctx context.Context, req *milvuspb.CreateRoleRequest) (*commonpb.Status, error) {
	logger.Debug("CreateRole", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4079
		return errorutil.UnhealthyStatus(code), nil
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
	}

	var roleName string
	if req.Entity != nil {
		roleName = req.Entity.Name
	}
	if err := ValidateRoleName(roleName); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
4090
		}, nil
4091 4092 4093 4094 4095 4096 4097 4098
	}

	result, err := node.rootCoord.CreateRole(ctx, req)
	if err != nil {
		logger.Error("fail to create role", zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
4099
		}, nil
4100 4101
	}
	return result, nil
4102 4103
}

4104 4105 4106
func (node *Proxy) DropRole(ctx context.Context, req *milvuspb.DropRoleRequest) (*commonpb.Status, error) {
	logger.Debug("DropRole", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4107
		return errorutil.UnhealthyStatus(code), nil
4108 4109 4110 4111 4112
	}
	if err := ValidateRoleName(req.RoleName); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
4113
		}, nil
4114
	}
4115 4116 4117 4118 4119
	if IsDefaultRole(req.RoleName) {
		errMsg := fmt.Sprintf("the role[%s] is a default role, which can't be droped", req.RoleName)
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    errMsg,
4120
		}, nil
4121
	}
4122 4123 4124 4125 4126 4127
	result, err := node.rootCoord.DropRole(ctx, req)
	if err != nil {
		logger.Error("fail to drop role", zap.String("role_name", req.RoleName), zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
4128
		}, nil
4129 4130
	}
	return result, nil
4131 4132
}

4133 4134 4135
func (node *Proxy) OperateUserRole(ctx context.Context, req *milvuspb.OperateUserRoleRequest) (*commonpb.Status, error) {
	logger.Debug("OperateUserRole", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4136
		return errorutil.UnhealthyStatus(code), nil
4137 4138 4139 4140 4141
	}
	if err := ValidateUsername(req.Username); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
4142
		}, nil
4143 4144 4145 4146 4147
	}
	if err := ValidateRoleName(req.RoleName); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
4148
		}, nil
4149 4150 4151 4152 4153 4154 4155 4156
	}

	result, err := node.rootCoord.OperateUserRole(ctx, req)
	if err != nil {
		logger.Error("fail to operate user role", zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
4157
		}, nil
4158 4159
	}
	return result, nil
4160 4161
}

4162 4163 4164
func (node *Proxy) SelectRole(ctx context.Context, req *milvuspb.SelectRoleRequest) (*milvuspb.SelectRoleResponse, error) {
	logger.Debug("SelectRole", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4165
		return &milvuspb.SelectRoleResponse{Status: errorutil.UnhealthyStatus(code)}, nil
4166 4167 4168 4169 4170 4171 4172 4173 4174
	}

	if req.Role != nil {
		if err := ValidateRoleName(req.Role.Name); err != nil {
			return &milvuspb.SelectRoleResponse{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_IllegalArgument,
					Reason:    err.Error(),
				},
4175
			}, nil
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
		}
	}

	result, err := node.rootCoord.SelectRole(ctx, req)
	if err != nil {
		logger.Error("fail to select role", zap.Error(err))
		return &milvuspb.SelectRoleResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
4187
		}, nil
4188 4189
	}
	return result, nil
4190 4191
}

4192 4193 4194
func (node *Proxy) SelectUser(ctx context.Context, req *milvuspb.SelectUserRequest) (*milvuspb.SelectUserResponse, error) {
	logger.Debug("SelectUser", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4195
		return &milvuspb.SelectUserResponse{Status: errorutil.UnhealthyStatus(code)}, nil
4196 4197 4198 4199 4200 4201 4202 4203 4204
	}

	if req.User != nil {
		if err := ValidateUsername(req.User.Name); err != nil {
			return &milvuspb.SelectUserResponse{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_IllegalArgument,
					Reason:    err.Error(),
				},
4205
			}, nil
4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216
		}
	}

	result, err := node.rootCoord.SelectUser(ctx, req)
	if err != nil {
		logger.Error("fail to select user", zap.Error(err))
		return &milvuspb.SelectUserResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
4217
		}, nil
4218 4219
	}
	return result, nil
4220 4221
}

4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251
func (node *Proxy) validPrivilegeParams(req *milvuspb.OperatePrivilegeRequest) error {
	if req.Entity == nil {
		return fmt.Errorf("the entity in the request is nil")
	}
	if req.Entity.Grantor == nil {
		return fmt.Errorf("the grantor entity in the grant entity is nil")
	}
	if req.Entity.Grantor.Privilege == nil {
		return fmt.Errorf("the privilege entity in the grantor entity is nil")
	}
	if err := ValidatePrivilege(req.Entity.Grantor.Privilege.Name); err != nil {
		return err
	}
	if req.Entity.Object == nil {
		return fmt.Errorf("the resource entity in the grant entity is nil")
	}
	if err := ValidateObjectType(req.Entity.Object.Name); err != nil {
		return err
	}
	if err := ValidateObjectName(req.Entity.ObjectName); err != nil {
		return err
	}
	if req.Entity.Role == nil {
		return fmt.Errorf("the object entity in the grant entity is nil")
	}
	if err := ValidateRoleName(req.Entity.Role.Name); err != nil {
		return err
	}

	return nil
4252 4253
}

4254 4255 4256
func (node *Proxy) OperatePrivilege(ctx context.Context, req *milvuspb.OperatePrivilegeRequest) (*commonpb.Status, error) {
	logger.Debug("OperatePrivilege", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4257
		return errorutil.UnhealthyStatus(code), nil
4258 4259 4260 4261 4262
	}
	if err := node.validPrivilegeParams(req); err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_IllegalArgument,
			Reason:    err.Error(),
4263
		}, nil
4264 4265 4266 4267 4268 4269
	}
	curUser, err := GetCurUserFromContext(ctx)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
4270
		}, nil
4271 4272 4273 4274 4275 4276 4277 4278
	}
	req.Entity.Grantor.User = &milvuspb.UserEntity{Name: curUser}
	result, err := node.rootCoord.OperatePrivilege(ctx, req)
	if err != nil {
		logger.Error("fail to operate privilege", zap.Error(err))
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UnexpectedError,
			Reason:    err.Error(),
4279
		}, nil
4280 4281
	}
	return result, nil
4282 4283
}

4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
func (node *Proxy) validGrantParams(req *milvuspb.SelectGrantRequest) error {
	if req.Entity == nil {
		return fmt.Errorf("the grant entity in the request is nil")
	}

	if req.Entity.Object != nil {
		if err := ValidateObjectType(req.Entity.Object.Name); err != nil {
			return err
		}

		if err := ValidateObjectName(req.Entity.ObjectName); err != nil {
			return err
		}
	}

	if req.Entity.Role == nil {
		return fmt.Errorf("the role entity in the grant entity is nil")
	}

	if err := ValidateRoleName(req.Entity.Role.Name); err != nil {
		return err
	}

	return nil
}

func (node *Proxy) SelectGrant(ctx context.Context, req *milvuspb.SelectGrantRequest) (*milvuspb.SelectGrantResponse, error) {
	logger.Debug("SelectGrant", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
4313
		return &milvuspb.SelectGrantResponse{Status: errorutil.UnhealthyStatus(code)}, nil
4314 4315 4316 4317 4318 4319 4320 4321
	}

	if err := node.validGrantParams(req); err != nil {
		return &milvuspb.SelectGrantResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_IllegalArgument,
				Reason:    err.Error(),
			},
4322
		}, nil
4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
	}

	result, err := node.rootCoord.SelectGrant(ctx, req)
	if err != nil {
		logger.Error("fail to select grant", zap.Error(err))
		return &milvuspb.SelectGrantResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UnexpectedError,
				Reason:    err.Error(),
			},
4333
		}, nil
4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361
	}
	return result, nil
}

func (node *Proxy) RefreshPolicyInfoCache(ctx context.Context, req *proxypb.RefreshPolicyInfoCacheRequest) (*commonpb.Status, error) {
	logger.Debug("RefreshPrivilegeInfoCache", zap.Any("req", req))
	if code, ok := node.checkHealthyAndReturnCode(); !ok {
		return errorutil.UnhealthyStatus(code), errorutil.UnhealthyError()
	}

	if globalMetaCache != nil {
		err := globalMetaCache.RefreshPolicyInfo(typeutil.CacheOp{
			OpType: typeutil.CacheOpType(req.OpType),
			OpKey:  req.OpKey,
		})
		if err != nil {
			log.Error("fail to refresh policy info", zap.Error(err))
			return &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_RefreshPolicyInfoCacheFailure,
				Reason:    err.Error(),
			}, err
		}
	}
	logger.Debug("RefreshPrivilegeInfoCache success")

	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_Success,
	}, nil
4362
}
4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383

// SetRates limits the rates of requests.
func (node *Proxy) SetRates(ctx context.Context, request *proxypb.SetRatesRequest) (*commonpb.Status, error) {
	log.Debug("SetRates", zap.String("role", typeutil.ProxyRole), zap.Any("rates", request.GetRates()))
	resp := &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_UnexpectedError,
	}
	if !node.checkHealthy() {
		resp = unhealthyStatus()
		return resp, nil
	}

	err := node.multiRateLimiter.globalRateLimiter.setRates(request.GetRates())
	// TODO: set multiple rate limiter rates
	if err != nil {
		resp.Reason = err.Error()
		return resp, nil
	}
	resp.ErrorCode = commonpb.ErrorCode_Success
	return resp, nil
}