-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: Use pool to limit kafka cgo thread number (#37744)
Related to #37665 Thread number went rocket high when there is lots of kafka consumers on datanode. Since the internal implementation is CGO, using which directly will make cgo thread leaked. This PR add a worker pool for kafka API utilzing CGO calls to limit thread number. Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
- Loading branch information
Showing
3 changed files
with
113 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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. | ||
|
||
package kafka | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
|
||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
|
||
"github.com/milvus-io/milvus/pkg/log" | ||
"github.com/milvus-io/milvus/pkg/util/conc" | ||
"github.com/milvus-io/milvus/pkg/util/hardware" | ||
) | ||
|
||
var ( | ||
kafkaCPool atomic.Pointer[conc.Pool[any]] | ||
initOnce sync.Once | ||
) | ||
|
||
func initPool() { | ||
pool := conc.NewPool[any]( | ||
hardware.GetCPUNum(), | ||
conc.WithPreAlloc(false), | ||
conc.WithDisablePurge(false), | ||
conc.WithPreHandler(runtime.LockOSThread), // lock os thread for cgo thread disposal | ||
) | ||
|
||
kafkaCPool.Store(pool) | ||
log.Info("init dynamicPool done", zap.Int("size", hardware.GetCPUNum())) | ||
} | ||
|
||
// GetSQPool returns the singleton pool instance for search/query operations. | ||
func getPool() *conc.Pool[any] { | ||
initPool() | ||
return kafkaCPool.Load() | ||
} |