-
Notifications
You must be signed in to change notification settings - Fork 995
fix(extension): eliminate race-prone global registries in common/extension #3264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 12 commits
b3bc0ab
c975f31
ddefa6d
37db665
b648f5b
45667a5
3fbcad7
878966d
9eeded4
ff8a25b
532151a
bc3ca50
bc5c9e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) 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 extension | ||
|
|
||
| import ( | ||
| "container/list" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| ) | ||
|
|
||
| import ( | ||
| gxset "github.com/dubbogo/gost/container/set" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| import ( | ||
| "dubbo.apache.org/dubbo-go/v3/cluster/directory" | ||
| "dubbo.apache.org/dubbo-go/v3/common" | ||
| commonconfig "dubbo.apache.org/dubbo-go/v3/common/config" | ||
| "dubbo.apache.org/dubbo-go/v3/metadata/mapping" | ||
| "dubbo.apache.org/dubbo-go/v3/protocol/base" | ||
| "dubbo.apache.org/dubbo-go/v3/registry" | ||
| ) | ||
|
|
||
| type mockDir struct{} | ||
|
|
||
| func (m *mockDir) GetURL() *common.URL { return &common.URL{} } | ||
| func (m *mockDir) IsAvailable() bool { return true } | ||
| func (m *mockDir) Destroy() {} | ||
|
Check failure on line 47 in common/extension/concurrency_guards_test.go
|
||
| func (m *mockDir) List(invocation base.Invocation) []base.Invoker { return nil } | ||
| func (m *mockDir) Subscribe(url *common.URL) error { return nil } | ||
|
|
||
| type mockCustomizer struct{ priority int } | ||
|
|
||
| func (m mockCustomizer) GetPriority() int { return m.priority } | ||
| func (m mockCustomizer) Customize(instance registry.ServiceInstance) {} | ||
|
Check failure on line 54 in common/extension/concurrency_guards_test.go
|
||
|
|
||
| type mockServiceNameMapping struct{} | ||
|
|
||
| func (m *mockServiceNameMapping) Map(url *common.URL) error { return nil } | ||
| func (m *mockServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { | ||
| return nil, nil | ||
| } | ||
| func (m *mockServiceNameMapping) Remove(url *common.URL) error { return nil } | ||
|
|
||
| type mockPostProcessor struct{} | ||
|
|
||
| func (m mockPostProcessor) PostProcessReferenceConfig(url *common.URL) {} | ||
|
Check failure on line 66 in common/extension/concurrency_guards_test.go
|
||
| func (m mockPostProcessor) PostProcessServiceConfig(url *common.URL) {} | ||
|
Check failure on line 67 in common/extension/concurrency_guards_test.go
|
||
|
|
||
| func TestGetAllCustomShutdownCallbacksReturnsCopy(t *testing.T) { | ||
| customShutdownCallbacksLock.Lock() | ||
| original := customShutdownCallbacks | ||
| customShutdownCallbacks = list.New() | ||
| customShutdownCallbacksLock.Unlock() | ||
|
|
||
| t.Cleanup(func() { | ||
| customShutdownCallbacksLock.Lock() | ||
| customShutdownCallbacks = original | ||
| customShutdownCallbacksLock.Unlock() | ||
| }) | ||
|
|
||
| AddCustomShutdownCallback(func() {}) | ||
|
Check failure on line 81 in common/extension/concurrency_guards_test.go
|
||
| AddCustomShutdownCallback(func() {}) | ||
|
Check failure on line 82 in common/extension/concurrency_guards_test.go
|
||
|
|
||
| callbacks := GetAllCustomShutdownCallbacks() | ||
| assert.Len(t, asSlice(callbacks), 2) | ||
|
|
||
| callbacks.PushBack(func() {}) | ||
|
Check failure on line 87 in common/extension/concurrency_guards_test.go
|
||
| callbacksAgain := GetAllCustomShutdownCallbacks() | ||
| assert.Len(t, asSlice(callbacksAgain), 2) | ||
| } | ||
|
|
||
| func TestGetDirectoryInstanceUsesProtocolAndFallback(t *testing.T) { | ||
| originalDirectories := directories | ||
| directories = NewRegistry[registryDirectory]("registry directory test") | ||
| t.Cleanup(func() { | ||
| directories = originalDirectories | ||
| }) | ||
|
|
||
| if oldDefault := defaultDirectory.Load(); oldDefault != nil { | ||
| t.Cleanup(func() { defaultDirectory.Store(oldDefault.(registryDirectory)) }) | ||
| } else { | ||
| t.Cleanup(func() { defaultDirectory = atomic.Value{} }) | ||
| } | ||
|
|
||
| defaultHit := 0 | ||
| protocolHit := 0 | ||
|
|
||
| SetDefaultRegistryDirectory(func(url *common.URL, reg registry.Registry) (directory.Directory, error) { | ||
| defaultHit++ | ||
| return &mockDir{}, nil | ||
| }) | ||
|
|
||
| SetDirectory("polaris", func(url *common.URL, reg registry.Registry) (directory.Directory, error) { | ||
| protocolHit++ | ||
| return &mockDir{}, nil | ||
| }) | ||
|
|
||
| _, err := GetDirectoryInstance(&common.URL{Protocol: "polaris"}, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, protocolHit) | ||
| assert.Equal(t, 0, defaultHit) | ||
|
|
||
| _, err = GetDirectoryInstance(&common.URL{Protocol: ""}, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 1, defaultHit) | ||
|
|
||
| _, err = GetDirectoryInstance(&common.URL{Protocol: "unknown"}, nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 2, defaultHit) | ||
| } | ||
|
|
||
| func TestCustomizersAreSortedAndReturnedAsCopy(t *testing.T) { | ||
| customizersLock.Lock() | ||
| original := customizers | ||
| customizers = make([]registry.ServiceInstanceCustomizer, 0, 8) | ||
| customizersLock.Unlock() | ||
|
|
||
| t.Cleanup(func() { | ||
| customizersLock.Lock() | ||
| customizers = original | ||
| customizersLock.Unlock() | ||
| }) | ||
|
|
||
| AddCustomizers(mockCustomizer{priority: 20}) | ||
| AddCustomizers(mockCustomizer{priority: 10}) | ||
|
|
||
| got := GetCustomizers() | ||
| require.Len(t, got, 2) | ||
| assert.Equal(t, 10, got[0].GetPriority()) | ||
| assert.Equal(t, 20, got[1].GetPriority()) | ||
|
|
||
| _ = append(got, mockCustomizer{priority: 1}) | ||
| assert.Len(t, GetCustomizers(), 2) | ||
| } | ||
|
|
||
| func TestGlobalServiceNameMappingCreator(t *testing.T) { | ||
| if old := globalNameMappingCreator.Load(); old != nil { | ||
| t.Cleanup(func() { globalNameMappingCreator.Store(old.(ServiceNameMappingCreator)) }) | ||
| } | ||
|
|
||
| expected := &mockServiceNameMapping{} | ||
| SetGlobalServiceNameMapping(func() mapping.ServiceNameMapping { | ||
| return expected | ||
| }) | ||
|
|
||
| got := GetGlobalServiceNameMapping() | ||
| assert.Same(t, expected, got) | ||
| } | ||
|
|
||
| func TestConfigPostProcessorRegistrySnapshot(t *testing.T) { | ||
| originalProcessors := processors | ||
| processors = NewRegistry[commonconfig.ConfigPostProcessor]("config post processor test") | ||
| t.Cleanup(func() { | ||
| processors = originalProcessors | ||
| }) | ||
|
|
||
| SetConfigPostProcessor("p1", mockPostProcessor{}) | ||
| SetConfigPostProcessor("p2", mockPostProcessor{}) | ||
|
|
||
| assert.NotNil(t, GetConfigPostProcessor("p1")) | ||
| all := GetConfigPostProcessors() | ||
| assert.Len(t, all, 2) | ||
| } | ||
|
|
||
| func TestConcurrentCustomShutdownCallbacksAndCustomizers(t *testing.T) { | ||
| customShutdownCallbacksLock.Lock() | ||
| originalCallbacks := customShutdownCallbacks | ||
| customShutdownCallbacks = list.New() | ||
| customShutdownCallbacksLock.Unlock() | ||
|
|
||
| customizersLock.Lock() | ||
| originalCustomizers := customizers | ||
| customizers = make([]registry.ServiceInstanceCustomizer, 0, 8) | ||
| customizersLock.Unlock() | ||
|
|
||
| t.Cleanup(func() { | ||
| customShutdownCallbacksLock.Lock() | ||
| customShutdownCallbacks = originalCallbacks | ||
| customShutdownCallbacksLock.Unlock() | ||
|
|
||
| customizersLock.Lock() | ||
| customizers = originalCustomizers | ||
| customizersLock.Unlock() | ||
| }) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < 20; i++ { | ||
| wg.Add(1) | ||
| go func(p int) { | ||
| defer wg.Done() | ||
| AddCustomShutdownCallback(func() {}) | ||
|
Check failure on line 211 in common/extension/concurrency_guards_test.go
|
||
| AddCustomizers(mockCustomizer{priority: p}) | ||
| _ = GetAllCustomShutdownCallbacks() | ||
| _ = GetCustomizers() | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| assert.Len(t, asSlice(GetAllCustomShutdownCallbacks()), 20) | ||
| assert.Len(t, GetCustomizers(), 20) | ||
| } | ||
|
|
||
| func asSlice(l *list.List) []any { | ||
| ret := make([]any, 0, l.Len()) | ||
| for e := l.Front(); e != nil; e = e.Next() { | ||
| ret = append(ret, e.Value) | ||
| } | ||
| return ret | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.