Skip to content

Adjust resourcetopo directref relation update logic - #106

Open
AnnaYue wants to merge 7 commits into
mainfrom
adjust-resourcetopo-directref-relation-update-logic
Open

Adjust resourcetopo directref relation update logic#106
AnnaYue wants to merge 7 commits into
mainfrom
adjust-resourcetopo-directref-relation-update-logic

Conversation

@AnnaYue

@AnnaYue AnnaYue commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

  • N
  • Y

2. What is the scope of this PR (e.g. component or file name):

3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Other

4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

  • N
  • Y

5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

  • Unit test
  • Integration test
  • Benchmark (add benchmark stats below)
  • Manual test (add detailed scripts or steps below)
  • Other

6. Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

AnnaYue and others added 7 commits July 24, 2026 18:19
need to split to atomic reference pair to do sort and compare
This commit addresses potential deadlock issues and adds timeout protection
for event handlers.

Lock Hierarchy Fixes:
- Add doc.go with comprehensive lock hierarchy documentation (7 levels)
- Fix checkGC(): acquire lock before metaLock, release between
- Fix checkLabelUpdateForPostNode(): copy data under lock, release before processing
- Fix updateNodeMeta(): release metaLock before acquiring lock
- Fix deleteAllRelation(): restructure to follow preNode->postNode lock order
- Add storagesLock RWMutex to protect storages map from concurrent access

Event Handler Timeout:
- Add EventHandlerTimeout config option (default 1s)
- Implement timeout in handleNodeEvent() with context cancellation
- Implement timeout in handleRelationEvent() with context cancellation
- Handlers run sequentially; timeout skips remaining handlers after warning

Other Changes:
- Change event queue log level from Infof to V(6).Infof
- Add threading model documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Change map get operations to use the two-value lookup pattern (val, ok := map[key])
to explicitly check for key existence. This prevents returning nil values that could
cause NPE when callers try to use them.

Changes:
- manager.go: AddNodeHandler, GetTopoNodeStorage, GetNode now check ok before using value
- node_storage.go: getNode and deleteNode check ok for cluster, namespace, and name lookups
- GetClusterNode: return nil early if node not found

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract duplicate timeout logic into two helper methods:
- executeHandlers(): for NodeHandler with timeout support
- executeRelationHandlers(): for RelationHandler with timeout support

This reduces code duplication and makes the main event handling logic cleaner.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…, and timeout metrics

- Add context.Context to NodeHandler and RelationHandler for cooperative
  cancellation. When EventHandlerTimeout expires, ctx is cancelled so
  handlers can select on ctx.Done() and return cleanly (no goroutine leak).

- Add defer/recover() to executeHandlers and executeRelationHandlers to
  catch handler panics and prevent process crashes. Panics are logged
  and the done channel is always closed.

- Add HandlerTimeoutMetrics() returning atomic counters for timed-out
  node and relation handler callbacks.

- Fix checkGC() to use metaLock.RLock() instead of Lock() for a read-only
  boolean check, matching isObjectExisted().

- Revert GetClusterNode nil-return behavior change to preserve the
  original API contract for deleted-but-referenced nodes.

- Update lock hierarchy docs: swap Level 4 (relationsLock) and Level 5
  (lock) to match actual code ordering.

- Update doc.go callback threading docs to reflect goroutine + timeout
  execution model.

- Add tests: cooperative handler no-leak, panic recovery, goroutine
  leak detection, DirectRef delta updates, lock ordering stress.

Co-Authored-By: Claude <noreply@anthropic.com>
…ct{} for map set

- Replace executeHandlers/executeRelationHandlers (40-line copy-paste)
  with a single generic executeHandlers[H any]() function.
- Change map[nodeMeta]interface{} to map[nodeMeta]struct{} for
  idiomatic Go set semantics (zero-value sentinel, zero allocation).

Co-Authored-By: Claude <noreply@anthropic.com>
@AnnaYue
AnnaYue force-pushed the adjust-resourcetopo-directref-relation-update-logic branch from 13dcc94 to 078044d Compare July 25, 2026 11:27
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.

1 participant