Skip to content
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

Ftr: Add config loader hooks for 1.5 #1372

Conversation

ChangedenCZD
Copy link
Contributor

@ChangedenCZD ChangedenCZD commented Aug 10, 2021

Hook触发流程图

image

  • 目前定义了9种类型的钩子,消费者4个,提供者4个,程序正常退出前1个
  • 开发者可在config.Load前创建并添加钩子AddLoaderHooks
  • 每种类型的钩子可创建和添加多个实例,在流程中对应类型的所有钩子都会被触发
  • 所有消费者实例被验证前会触发beforeConsumerConnectHook
  • 当消费者验证失败时,会触发consumerConnectFailHook
  • 当消费者验证通过时,会触发consumerConnectSuccessHook
  • 所有提供者实例被验证前会触发beforeProviderConnectHook
  • 当提供者注册失败时,会触发consumerProviderFailHook
  • 当提供者注册通过时,会触发consumerProviderSuccessHook
  • 当allConsumersConnectCompleteHook被触发时,表示所有消费者已完成验证(即可调用)
  • 当allProvidersConnectCompleteHook被触发时,表示所有提供者已完成注册(即可被调用)
  • 当beforeShutDownHook被触发时,表示程序将要退出(panic的不会触发该钩子)

What this PR does:

func TestLoadWithLoaderHooks(t *testing.T) {
	extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter {
		return &mockGracefulShutdownFilter{}
	})
	extension.SetFilter(constant.GracefulShutdownProviderFilterKey, func() filter.Filter {
		return &mockGracefulShutdownFilter{}
	})

	doInitConsumer()
	doInitProvider()

	ms := &MockService{}
	SetConsumerService(ms)
	SetProviderService(ms)

	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster(constant.ZONEAWARE_CLUSTER_NAME, cluster_impl.NewZoneAwareCluster)
	extension.SetProxyFactory("default", proxy_factory.NewDefaultProxyFactory)
	GetApplicationConfig().MetadataType = "mock"
	var mm *mockMetadataService
	extension.SetLocalMetadataService("mock", func() (metadataService service.MetadataService, err error) {
		if mm == nil {
			mm = &mockMetadataService{
				exportedServiceURLs: new(sync.Map),
				lock:                new(sync.RWMutex),
			}
		}
		return mm, nil
	})

	// create consumer loader hooks
	beforeConsumerConnectHook := NewBeforeConsumerConnectHook(func(info *ConsumerConnectInfo) {
		logger.Debug("BeforeConsumerConnectHook", info)
		assert.NotNil(t, info)
	})
	consumerConnectSuccessHook := NewConsumerConnectSuccessHook(func(info *ConsumerConnectInfo) {
		logger.Debug("ConsumerConnectSuccessHook", info)
		assert.NotNil(t, info)
	})
	consumerConnectFailHook := NewConsumerConnectFailHook(func(info *ConsumerConnectFailInfo) {
		logger.Debug("ConsumerConnectFailHook", info)
		assert.NotNil(t, info)
	})
	allConsumersConnectCompleteHook := NewAllConsumersConnectCompleteHook(func() {
		logger.Debug("AllConsumersConnectCompleteHook")
		RemoveLoaderHooks( // Remove some consumer loader hooks
			beforeConsumerConnectHook,
			consumerConnectSuccessHook,
			consumerConnectFailHook,
		)
		logger.Debug("LoaderHooks length = ", len(loaderHooks), " after AllConsumersConnectCompleteHook")
		// beforeConsumerConnectHook, consumerConnectSuccessHook 
                // and consumerConnectFailHook have been removed
		assert.Equal(t, len(loaderHooks), 6)
	})
	// create provider loader hooks
	beforeProviderConnectHook := NewBeforeProviderConnectHook(func(info *ProviderConnectInfo) {
		logger.Debug("BeforeProviderConnectHook", info)
		assert.NotNil(t, info)
	})
	providerConnectSuccessHook := NewProviderConnectSuccessHook(func(info *ProviderConnectInfo) {
		logger.Debug("ProviderConnectSuccessHook", info)
		assert.NotNil(t, info)
	})
	providerConnectFailHook := NewProviderConnectFailHook(func(info *ProviderConnectFailInfo) {
		logger.Debug("ProviderConnectFailHook", info)
		assert.NotNil(t, info)
	})
	allProvidersConnectCompleteHook := NewAllProvidersConnectCompleteHook(func() {
		logger.Debug("AllProvidersConnectCompleteHook")
		RemoveLoaderHooks( // Remove some provider loader hooks
			beforeProviderConnectHook,
			providerConnectSuccessHook,
			providerConnectFailHook,
		)
		logger.Debug("LoaderHooks length = ", len(loaderHooks), " after AllProvidersConnectCompleteHook")
		// beforeProviderConnectHook, providerConnectSuccessHook
                // and providerConnectFailHook have been removed
		assert.Equal(t, len(loaderHooks), 3)
	})

	beforeShutdownHook := NewBeforeShutdownHook(func() {
		logger.Debug("BeforeShutDownHook")
		RemoveLoaderHooks( // Remove some consumer and provider loader hooks
			// Consumer Hooks
			beforeConsumerConnectHook,
			consumerConnectSuccessHook,
			consumerConnectFailHook,
			allConsumersConnectCompleteHook,
			// Provider Hooks
			beforeProviderConnectHook,
			providerConnectSuccessHook,
			providerConnectFailHook,
			allProvidersConnectCompleteHook,
		)
		assert.Equal(t, len(loaderHooks), 1)
	})
	// add loader hooks before config.Load
	AddLoaderHooks(
		// Consumer Hooks
		beforeConsumerConnectHook,
		consumerConnectSuccessHook,
		consumerConnectFailHook,
		allConsumersConnectCompleteHook,
		// Provider Hooks
		beforeProviderConnectHook,
		providerConnectSuccessHook,
		providerConnectFailHook,
		allProvidersConnectCompleteHook,
		// Shut Down hook
		beforeShutdownHook,
	)
	// check loaderHooks length
	assert.Equal(t, len(loaderHooks), 9)

	Load()

	assert.Equal(t, ms, GetRPCService(ms.Reference()))
	ms2 := &struct {
		MockService
	}{}
	RPCService(ms2)
	assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

	conServices = map[string]common.RPCService{}
	proServices = map[string]common.RPCService{}
	err := common.ServiceMap.UnRegister("com.MockService", "mock",
		common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
	assert.Nil(t, err)
	consumerConfig = nil
	providerConfig = nil
}

Which issue(s) this PR fixes:

Fixes #1364

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

None

Changeden added 2 commits August 10, 2021 14:53
Add:
- BeforeShutdownHook
- BeforeConsumerConnectHook
- ConsumerConnectSuccessHook
- ConsumerConnectFailHook
- AllConsumerConnectCompleteHook
- BeforeProviderConnectHook
- ProviderConnectSuccessHook
- ProviderConnectFailHook
- AllProviderConnectCompleteHook

Tst:
- config_loader_test.TestLoadWithLoaderHooks
@ChangedenCZD ChangedenCZD changed the title Ftr: Add config loader hooks for 1.5. Ftr: Add config loader hooks for 1.5 Aug 17, 2021
@ChangedenCZD
Copy link
Contributor Author

使用事件分发机制实现 #1405

@ChangedenCZD
Copy link
Contributor Author

@wangxw666 1.5版本的已改为 #1405 这个解决方案,3.0的建议废除原有的分发机制,改为Hook

config/config_loader.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Aug 25, 2021

Codecov Report

Merging #1372 (b5ab3be) into 1.5 (467c867) will decrease coverage by 2.34%.
The diff coverage is 68.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##              1.5    #1372      +/-   ##
==========================================
- Coverage   58.07%   55.72%   -2.35%     
==========================================
  Files         275      277       +2     
  Lines       13381    16119    +2738     
==========================================
+ Hits         7771     8983    +1212     
- Misses       4650     6197    +1547     
+ Partials      960      939      -21     
Impacted Files Coverage Δ
config/graceful_shutdown.go 57.30% <0.00%> (+3.01%) ⬆️
config/provider_config.go 62.06% <0.00%> (+7.52%) ⬆️
config/reference_config.go 80.00% <0.00%> (-1.67%) ⬇️
config_center/nacos/facade.go 62.50% <0.00%> (-27.16%) ⬇️
registry/consul/registry.go 51.64% <0.00%> (+2.31%) ⬆️
...gistry/event/protocol_ports_metadata_customizer.go 65.78% <ø> (+6.41%) ⬆️
registry/kubernetes/registry.go 64.15% <0.00%> (+13.03%) ⬆️
registry/zookeeper/listener.go 26.58% <0.00%> (-42.65%) ⬇️
remoting/etcdv3/client.go 55.69% <0.00%> (+2.69%) ⬆️
remoting/etcdv3/facade.go 0.00% <0.00%> (ø)
... and 311 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 81c2e23...b5ab3be. Read the comment docs.

@AlexStocks AlexStocks closed this Aug 27, 2021
@ChangedenCZD ChangedenCZD deleted the feature/config-load-with-loader-hook-for-1.5 branch September 9, 2021 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants