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

discovery+graph: track job set dependencies in vb #9241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Crypt-iQ
Copy link
Collaborator

@Crypt-iQ Crypt-iQ commented Nov 1, 2024

This PR changes the ValidationBarrier to track abstract job dependencies. This just means that every time a child job comes in (i.e. channel update or node announcement), we track the set of possible parent jobs that are related to it (channel announcement(s)) that have registered via InitJobDependencies. The goroutines containing the child jobs will then wait to be notified every time one of their parent jobs completes. From the child job's POV, this just works as ref-counting except that you're only counting the parent jobs you're interested in.

With this, we can now extend the ValidationBarrier to track any sort of abstract job that requires both concurrency and waiting for another job to finish. It also makes it possible in a future PR to very easily make node announcements depend on channel announcements. See the commit messages for more details.

TODO:

  • Introduce a test to simulate goroutines randomly calling into the ValidationBarrier and ensure that all child jobs finish after their related parent jobs.
  • If time permits, write a fuzz test for the above as well.
  • Continue testing on mainnet

Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Crypt-iQ Crypt-iQ force-pushed the fix_vb branch 3 times, most recently from 7acf321 to fc00572 Compare November 4, 2024 18:56
@Crypt-iQ Crypt-iQ changed the title [DRAFT] graph: fix validation barrier [DRAFT] discovery+graph: track job set dependencies in ValidationBarrier Nov 4, 2024
@Crypt-iQ Crypt-iQ changed the title [DRAFT] discovery+graph: track job set dependencies in ValidationBarrier [DRAFT] discovery+graph: track job set dependencies in vb Nov 4, 2024
@Crypt-iQ Crypt-iQ force-pushed the fix_vb branch 3 times, most recently from fc72083 to 7d95cd2 Compare November 6, 2024 18:19
@Crypt-iQ Crypt-iQ marked this pull request as ready for review November 6, 2024 18:46
@Crypt-iQ Crypt-iQ changed the title [DRAFT] discovery+graph: track job set dependencies in vb discovery+graph: track job set dependencies in vb Nov 6, 2024
This omits calls to InitJobDependencies, SignalDependants, and
WaitForDependants. A new method FetchJobSlot has been added to the
ValidationBarrier which only reserves a job slot and does not set
up any dependency mappings. These changes have been made here
because the router / builder code does not actually need job
dependency management. Calls to the builder code (i.e. AddNode,
AddEdge, UpdateEdge) are all blocking in the gossiper. This,
combined with the fact that child jobs are run after parent jobs
in the gossiper, means that the calls to the router will happen
in the proper dependency order. This means that usage of the
ValidationBarrier is therefore useless except for the job
slot reservation to prevent DoS.
This commit does two things:
- removes the concept of allow / deny. Having this in place was a
  minor optimization and removing it makes the solution simpler.
- changes the job dependency tracking to track sets of abstact
  parent jobs rather than individual parent jobs.

As a note, the purpose of the ValidationBarrier is that it allows us
to launch gossip validation jobs in goroutines while still ensuring
that the validation order of these goroutines is adhered to when it
comes to validating ChannelAnnouncement _before_ ChannelUpdate and
_before_ NodeAnnouncement.
@saubyk
Copy link
Collaborator

saubyk commented Nov 7, 2024

cc: @gijswijs for review

@@ -675,45 +675,20 @@ func (b *Builder) handleNetworkUpdate(vb *ValidationBarrier,
defer b.wg.Done()
defer vb.CompleteJob()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit title nit: this is not the router, it's the graph.Builder

Comment on lines +84 to +85
func (v *ValidationBarrier) FetchJobSlot() {
// We'll wait for either a new slot to become open, or for the quit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the builder code is called by the gossiper code which itself also uses a semaphore - why isnt that inheritance enough?

Comment on lines +88 to +90
// Empty returns true if s is empty.
func (s Set[T]) Empty() bool {
return len(s) == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we need a separate PR for this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also already have IsEmpty

func (v *ValidationBarrier) SignalDependants(job interface{}, allow bool) {
// SignalDependents signals to any child jobs that this parent job has
// finished.
func (v *ValidationBarrier) SignalDependents(job interface{}, id JobID) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha sneaky change from British spelling to American 😝

Comment on lines +364 to +369
err = v.removeParentJob(route.Vertex(msg.NodeID2), id)
if err != nil {
return err
}

delete(v.chanEdgeDependencies, msg.ShortChannelID)
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just return v.removeParentJob(route.Vertex(msg.NodeID2), id)

Comment on lines +1581 to +1582
"JobID=%v", spew.Sdump(nMsg.msg), jobID)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not handle the error similarly to how it is handled for WaitForParents above? (including returning after handling?)

// If there is no entry in the jobInfoMap, we don't have to wait on any
// parent jobs to finish.
info, ok := v.jobInfoMap[annID]
if ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit:

if !ok {
        return
  }

  // the rest here

Comment on lines +165 to +166
info.activeParentJobIDs.Add(annJobID)
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: return after info.activeParentJobIDs.Add(annJobID) and remove & unindent the else block

// should complete after another) for the (childJobID, annID) tuple. This must
// only be called from InitJobDependencies.
// NOTE: MUST be called with the mutex held.
func (v *ValidationBarrier) populateDependencies(childJobID JobID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: can we keep the argument order the same for updateOrCreateJobInfo and populateDependencies 🙏

signals, ok = v.chanEdgeDependencies[msg.ShortChannelID]
annID = msg.ShortChannelID

// TODO: If ok is false, we have serious issues.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw the error? (if it is really impossible then panic)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the many legitimate uses of panics, they have been rejected every time I have tried to use them (even for the provably impossible scenario). I believe that a critical log is the next best thing.

Copy link
Collaborator

@ProofOfKeags ProofOfKeags left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding the distinction between parent and child jobs here both confusing and unnecessary. What we have here is a dependency graph of undifferentiated jobIDs. once all of the dependencies have run we can run. once we run we want to signal all of our dependents. We should be able to accomplish this with a single removeJob that does this index cleanup and dependent signaling.

The main difficulty I'm noticing in this PR is that we have multiple IDs that we want to be able to map to JobIDs from disjoint domains. My recommendation here is to make the core algebra of this component undifferentiated and then have auxilliary mappings that help recover the relevant JobID from the other unique protocol identifiers.

Comment on lines +88 to +90
// Empty returns true if s is empty.
func (s Set[T]) Empty() bool {
return len(s) == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also already have IsEmpty

// length and entries therefore cannot hash to the same keys.
// NOTE: IF OTHER TYPES OF KEYS ARE STORED, CHECK THAT COLLISION WON'T
// OCCUR.
jobInfoMap map[any]*jobInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we ought to use an explicit closed union (via an interface) in the key here. any is a disaster waiting to happen.

Comment on lines +180 to +181
// should complete after another) for the (childJobID, annID) tuple. This must
// only be called from InitJobDependencies.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just define this as a local function inside that scope to enforce that it is only referenceable there?

Comment on lines +208 to +210
// Copy over the parent job IDs at this moment for this annID.
// This job must be processed AFTER these parent IDs.
parentJobs := info.activeParentJobIDs.Union(fn.NewSet[JobID]())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this reveals a need for a set copying method.

signals, ok = v.chanEdgeDependencies[msg.ShortChannelID]
annID = msg.ShortChannelID

// TODO: If ok is false, we have serious issues.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the many legitimate uses of panics, they have been rejected every time I have tried to use them (even for the provably impossible scenario). I believe that a critical log is the next best thing.

annID = msg.ShortChannelID

// TODO: If ok is false, we have serious issues.
parentJobIDs, ok = v.jobDependencies[childJobID]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this read not need a mutex lock?

Comment on lines +390 to +391
// notifies to annID's child jobs that it has finished validating. This must be
// called from SignalDependents.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These "must be called from X" notes suggest lack of proper decomposition. It inverts the dependency. Callers should depend on the function's promises and integrate the results according to the API contract.

// and cleans up its job dependency mappings. This MUST be called from
// SignalDependents.
// NOTE: MUST be called with the mutex held.
func (v *ValidationBarrier) removeChildJob(annID any, childJobID JobID) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

Comment on lines +417 to +421
// We don't want to block when sending out the signal.
select {
case notifyChan <- struct{}{}:
default:
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ok with swallowing the signal instead? Seems like this could case jobs to never be run, particularly if lastJob is true

case *lnwire.NodeAnnouncement:
delete(v.nodeAnnDependencies, route.Vertex(msg.NodeID))
// Remove child job info.
v.removeChildJob(route.Vertex(msg.NodeID), id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need two distinct removal functions for parent and child.

@lightninglabs-deploy
Copy link

@Crypt-iQ, remember to re-request review from reviewers when ready

@saubyk
Copy link
Collaborator

saubyk commented Nov 19, 2024

cc: @gijswijs for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

5 participants