未验证 提交 bf391f24 编写于 作者: D dragondriver 提交者: GitHub

Make indexparamcheck thread-safe (#11916)

Signed-off-by: Ndragondriver <jiquan.long@zilliz.com>
上级 2147507e
......@@ -24,15 +24,13 @@ type ConfAdapterMgr interface {
// ConfAdapterMgrImpl implements ConfAdapter.
type ConfAdapterMgrImpl struct {
init bool
adapters map[IndexType]ConfAdapter
once sync.Once
}
// GetAdapter gets the conf adapter by the index type.
func (mgr *ConfAdapterMgrImpl) GetAdapter(indexType string) (ConfAdapter, error) {
if !mgr.init {
mgr.registerConfAdapter()
}
mgr.once.Do(mgr.registerConfAdapter)
adapter, ok := mgr.adapters[indexType]
if ok {
......@@ -42,8 +40,6 @@ func (mgr *ConfAdapterMgrImpl) GetAdapter(indexType string) (ConfAdapter, error)
}
func (mgr *ConfAdapterMgrImpl) registerConfAdapter() {
mgr.init = true
mgr.adapters[IndexFaissIDMap] = newBaseConfAdapter()
mgr.adapters[IndexFaissIvfFlat] = newIVFConfAdapter()
mgr.adapters[IndexFaissIvfPQ] = newIVFPQConfAdapter()
......@@ -63,7 +59,6 @@ func (mgr *ConfAdapterMgrImpl) registerConfAdapter() {
func newConfAdapterMgrImpl() *ConfAdapterMgrImpl {
return &ConfAdapterMgrImpl{
init: false,
adapters: make(map[IndexType]ConfAdapter),
}
}
......
......@@ -12,6 +12,7 @@
package indexparamcheck
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
......@@ -220,3 +221,19 @@ func TestConfAdapterMgrImpl_GetAdapter(t *testing.T) {
_, ok = adapter.(*NGTONNGConfAdapter)
assert.Equal(t, true, ok)
}
func TestConfAdapterMgrImpl_GetAdapter_multiple_threads(t *testing.T) {
num := 4
mgr := newConfAdapterMgrImpl()
var wg sync.WaitGroup
for i := 0; i < num; i++ {
wg.Add(1)
go func() {
defer wg.Done()
adapter, err := mgr.GetAdapter(IndexHNSW)
assert.NoError(t, err)
assert.NotNil(t, adapter)
}()
}
wg.Wait()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册