-
Notifications
You must be signed in to change notification settings - Fork 20
Replication: authority delegation (cell-based elected syncer) #227
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
Segfaultd
wants to merge
3
commits into
develop
Choose a base branch
from
feature/authority-delegation
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
3 commits
Select commit
Hold shift + click to select a range
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
87 changes: 87 additions & 0 deletions
87
code/framework/src/networking/replication/delegation_policy.h
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * MafiaHub OSS license | ||
| * Copyright (c) 2021-2023, MafiaHub. All rights reserved. | ||
| * | ||
| * This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
| * See LICENSE file in the source repository for information regarding licensing. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mafianet/types.h> | ||
|
|
||
| #include <glm/glm.hpp> | ||
|
|
||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| namespace Framework::Networking::Replication { | ||
| // Tunables for proximity syncer election. Distances are ground-plane world units. acquireRange < | ||
| // dropRange gives hysteresis: a candidate must reach acquireRange to be elected, but the current | ||
| // owner is held until it drifts past dropRange, so a boundary owner doesn't thrash. | ||
| struct DelegationParams { | ||
| float acquireRange = 80.0f; | ||
| float dropRange = 130.0f; | ||
| uint32_t electionIntervalMs = 500; | ||
| int loadSoftCap = 0; // max delegated entities newly granted per client (0 = off) | ||
| }; | ||
|
|
||
| struct DelegationCandidate { | ||
| MafiaNet::PeerGuid guid = MafiaNet::UNASSIGNED_PEER_GUID; | ||
| glm::vec3 position {0.0f}; | ||
| int ownedLoad = 0; | ||
| bool eligible = true; // caller-applied dimension + game veto | ||
| }; | ||
|
|
||
| // Pure, networking-free election. Keeps the current owner within dropRange, else elects the | ||
| // least-loaded eligible candidate within acquireRange (nearest breaking ties), else UNASSIGNED. | ||
| // groundXY picks the second ground axis (Y for Z-up, else Z). | ||
| inline MafiaNet::PeerGuid ElectOwner(const glm::vec3 &entityPos, MafiaNet::PeerGuid currentOwner, const std::vector<DelegationCandidate> &candidates, const DelegationParams ¶ms, bool groundXY) { | ||
| const auto ground = [groundXY](const glm::vec3 &p) { | ||
| return groundXY ? p.y : p.z; | ||
| }; | ||
| const auto dist2 = [&](const glm::vec3 &p) { | ||
| const float dx = p.x - entityPos.x; | ||
| const float dv = ground(p) - ground(entityPos); | ||
| return dx * dx + dv * dv; | ||
| }; | ||
|
|
||
| // Hysteresis: hold the current owner while eligible and within dropRange (soft cap ignored). | ||
| if (currentOwner != MafiaNet::UNASSIGNED_PEER_GUID) { | ||
| const float dropSq = params.dropRange * params.dropRange; | ||
| for (const auto &candidate : candidates) { | ||
| if (candidate.guid != currentOwner) { | ||
| continue; | ||
| } | ||
| if (candidate.eligible && dist2(candidate.position) <= dropSq) { | ||
| return currentOwner; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const float acquireSq = params.acquireRange * params.acquireRange; | ||
| MafiaNet::PeerGuid best = MafiaNet::UNASSIGNED_PEER_GUID; | ||
| int bestLoad = 0; | ||
| float bestDist = 0.0f; | ||
| for (const auto &candidate : candidates) { | ||
| if (!candidate.eligible || candidate.guid == MafiaNet::UNASSIGNED_PEER_GUID) { | ||
| continue; | ||
| } | ||
| const float d = dist2(candidate.position); | ||
| if (d > acquireSq) { | ||
| continue; | ||
| } | ||
| if (params.loadSoftCap > 0 && candidate.ownedLoad >= params.loadSoftCap) { | ||
| continue; | ||
| } | ||
| const bool better = best == MafiaNet::UNASSIGNED_PEER_GUID || candidate.ownedLoad < bestLoad || (candidate.ownedLoad == bestLoad && d < bestDist); | ||
| if (better) { | ||
| best = candidate.guid; | ||
| bestLoad = candidate.ownedLoad; | ||
| bestDist = d; | ||
| } | ||
| } | ||
| return best; | ||
| } | ||
| } // namespace Framework::Networking::Replication |
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
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.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: MafiaHub/Framework
Length of output: 30953
🏁 Script executed:
Repository: MafiaHub/Framework
Length of output: 18815
Clear or validate
pinnedOwnerwhen the pinned peer disconnectscode/framework/src/networking/replication/replication_manager.cpp:241-243pinnedOwneris never cleared on disconnect, soRunDelegation()keeps reassigning the entity to a disconnected peer and bypasses normal election/orphan handling. Gate the re-pin on a live viewer, or cleardelegation.pinnedOwnerin the disconnect path.🤖 Prompt for AI Agents