Make indexparamcheck thread-safe (#11916)

Signed-off-by: dragondriver <jiquan.long@zilliz.com>
This commit is contained in:
dragondriver 2021-11-21 00:39:13 +08:00 committed by GitHub
parent 2147507e89
commit bf391f2449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View File

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

View File

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