-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: unify and enhance the lifecycle of an instance #1440
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
Open
Saramanda9988
wants to merge
13
commits into
apache:develop
Choose a base branch
from
Saramanda9988:lifesycle
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f408893
feat: add lifecycle state management and color coding for instance st…
Saramanda9988 540ba4c
feat: enhance instance lifecycle management with state derivation and…
Saramanda9988 c4cfa68
feat: improve resource handling in informer with enhanced error repor…
Saramanda9988 df13cc2
feat: enhance instance lifecycle logging with detailed merge and dele…
Saramanda9988 99a057e
feat: implement ResourceKeyProvider interface for consistent key gene…
Saramanda9988 9a198c1
refactor: refactor instance resource handling and key function resolu…
Saramanda9988 7b5ffd0
refactor: remove unused deployState and registerState columns from in…
Saramanda9988 970d4be
feat: enhance runtime instance retrieval with improved matching logic…
Saramanda9988 0a845fa
feat: improve runtime instance identification with enhanced error log…
Saramanda9988 0a5f243
feat: add pod watch selector and RPC port identifiers for improved se…
Saramanda9988 7c679c7
feat: add refresh button and localization support for improved user i…
Saramanda9988 5ed41d2
Merge branch 'develop' into lifesycle
Saramanda9988 8653515
fix: fix lint
Saramanda9988 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package subscriber | |
| import ( | ||
| "reflect" | ||
|
|
||
| "github.com/duke-git/lancet/v2/slice" | ||
| "github.com/duke-git/lancet/v2/strutil" | ||
| "k8s.io/client-go/tools/cache" | ||
|
|
||
|
|
@@ -110,7 +111,12 @@ func (s *RPCInstanceEventSubscriber) processUpsert(rpcInstanceRes *meshresource. | |
| // We should merge the rpc info into it | ||
| if instanceRes != nil { | ||
| meshresource.MergeRPCInstanceIntoInstance(rpcInstanceRes, instanceRes) | ||
| return s.instanceStore.Update(instanceRes) | ||
| if err := s.instanceStore.Update(instanceRes); err != nil { | ||
| return err | ||
| } | ||
| logger.Infof("instance lifecycle merged rpc source, instance: %s, rpc: %s, registerState: registered, deployState: %s", | ||
| instanceRes.ResourceKey(), rpcInstanceRes.ResourceKey(), instanceRes.Spec.DeployState) | ||
| return nil | ||
| } | ||
| // Otherwise we can create a new instance resource by rpc instance | ||
| instanceRes = meshresource.FromRPCInstance(rpcInstanceRes) | ||
|
|
@@ -119,6 +125,8 @@ func (s *RPCInstanceEventSubscriber) processUpsert(rpcInstanceRes *meshresource. | |
| logger.Errorf("add instance resource failed, instance: %s, err: %s", instanceRes.ResourceKey(), err.Error()) | ||
| return err | ||
| } | ||
| logger.Infof("instance lifecycle created rpc-only instance, instance: %s, rpc: %s, registerState: registered", | ||
| instanceRes.ResourceKey(), rpcInstanceRes.ResourceKey()) | ||
|
|
||
| instanceAddEvent := events.NewResourceChangedEvent(cache.Added, nil, instanceRes) | ||
| s.eventEmitter.Send(instanceAddEvent) | ||
|
|
@@ -135,10 +143,26 @@ func (s *RPCInstanceEventSubscriber) processDelete(rpcInstanceRes *meshresource. | |
| logger.Warnf("cannot find instance resource for rpc instance %s, skipped deleting instance", rpcInstanceRes.Name) | ||
| return nil | ||
| } | ||
| meshresource.ClearRPCInstanceFromInstance(instanceRes) | ||
| if meshresource.HasRuntimeInstanceSource(instanceRes) { | ||
| if err := s.instanceStore.Update(instanceRes); err != nil { | ||
| logger.Errorf("update instance resource failed after rpc delete, instance: %s, err: %s", | ||
| instanceRes.ResourceKey(), err.Error()) | ||
| return err | ||
| } | ||
| logger.Infof("instance lifecycle rpc source removed, keep instance by runtime source, instance: %s, rpc: %s, deployState: %s", | ||
| instanceRes.ResourceKey(), rpcInstanceRes.ResourceKey(), instanceRes.Spec.DeployState) | ||
| instanceUpdateEvent := events.NewResourceChangedEvent(cache.Updated, instanceRes, instanceRes) | ||
| s.eventEmitter.Send(instanceUpdateEvent) | ||
| logger.Debugf("rpc instance delete trigger instance update event, event: %s", instanceUpdateEvent.String()) | ||
| return nil | ||
| } | ||
| if err := s.instanceStore.Delete(instanceRes); err != nil { | ||
| logger.Errorf("delete instance resource failed, instance: %s, err: %s", instanceRes.ResourceKey(), err.Error()) | ||
| return err | ||
| } | ||
| logger.Infof("instance lifecycle rpc source removed and no runtime source remains, deleted instance: %s, rpc: %s", | ||
| instanceRes.ResourceKey(), rpcInstanceRes.ResourceKey()) | ||
| instanceDeleteEvent := events.NewResourceChangedEvent(cache.Deleted, instanceRes, nil) | ||
| s.eventEmitter.Send(instanceDeleteEvent) | ||
| logger.Debugf("rpc instance delete trigger instance delete event, event: %s", instanceDeleteEvent.String()) | ||
|
|
@@ -173,7 +197,7 @@ func (s *RPCInstanceEventSubscriber) getRelatedInstanceRes( | |
| func (s *RPCInstanceEventSubscriber) findRelatedRuntimeInstanceAndMerge(instanceRes *meshresource.InstanceResource) { | ||
| switch s.engineCfg.Type { | ||
| case enginecfg.Kubernetes: | ||
| rtInstance := s.getRuntimeInstanceByIp(instanceRes.Spec.Ip) | ||
| rtInstance := s.getRuntimeInstanceForInstance(instanceRes) | ||
| if rtInstance == nil { | ||
| logger.Warnf("cannot find runtime instance for instace %s, skipping merging", instanceRes.ResourceKey()) | ||
| return | ||
|
|
@@ -184,7 +208,8 @@ func (s *RPCInstanceEventSubscriber) findRelatedRuntimeInstanceAndMerge(instance | |
| } | ||
| } | ||
|
|
||
| func (s *RPCInstanceEventSubscriber) getRuntimeInstanceByIp(ip string) *meshresource.RuntimeInstanceResource { | ||
| func (s *RPCInstanceEventSubscriber) getRuntimeInstanceForInstance(instanceRes *meshresource.InstanceResource) *meshresource.RuntimeInstanceResource { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion: 这里用ip查出来之后,再用mesh,appName,rpcPort来做一个过滤选择,但可能都会被过滤掉,因为mesh,appName,rpcPort这些属性是需要用户在k8s pod部署时手动打上的。在之前的代码逻辑里面,确定engine type是k8s后用ip来查询,这是因为k8s中ip是唯一的,ip能够将pod和注册中心上的rpc instance一一关联起来,是没有问题的。所以这里建议是加一个兜底返回,或者还是用之前的逻辑 |
||
| ip := instanceRes.Spec.Ip | ||
| resources, err := s.rtInstanceStore.ListByIndexes(map[string]string{ | ||
| index.ByRuntimeInstanceIPIndex: ip, | ||
| }) | ||
|
|
@@ -195,9 +220,29 @@ func (s *RPCInstanceEventSubscriber) getRuntimeInstanceByIp(ip string) *meshreso | |
| if len(resources) == 0 { | ||
| return nil | ||
| } | ||
| runtimeInstanceRes, ok := resources[0].(*meshresource.RuntimeInstanceResource) | ||
| if !ok { | ||
| return nil | ||
| candidates := make([]*meshresource.RuntimeInstanceResource, 0, len(resources)) | ||
| for _, item := range resources { | ||
| res, ok := item.(*meshresource.RuntimeInstanceResource) | ||
| if !ok { | ||
| continue | ||
| } | ||
| candidates = append(candidates, res) | ||
| } | ||
| // Prefer exact runtime identity match by app + rpcPort + mesh. | ||
| for _, candidate := range candidates { | ||
| if candidate.Spec == nil { | ||
| continue | ||
| } | ||
| if candidate.Mesh == instanceRes.Mesh && | ||
| candidate.Spec.AppName == instanceRes.Spec.AppName && | ||
| candidate.Spec.RpcPort == instanceRes.Spec.RpcPort { | ||
| return candidate | ||
| } | ||
| } | ||
| return runtimeInstanceRes | ||
| keys := slice.Map(candidates, func(_ int, item *meshresource.RuntimeInstanceResource) string { | ||
| return item.ResourceKey() | ||
| }) | ||
| logger.Warnf("cannot find exact runtime instance match by identity, skip runtime merge for instance %s, ip: %s, runtime candidates: %v", | ||
| instanceRes.ResourceKey(), ip, keys) | ||
| return nil | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: 看能不能把DeployState,RegisterState,LifecycleState统统定义成枚举/自定义类型,定义在一个地方,再写上对应的注释,就能清楚每个state的具体含义