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

⚡ jobflow-controller jobtemplate-controller enqueue should use struct not pointer #3134

Merged
merged 3 commits into from
Sep 25, 2023
Merged

⚡ jobflow-controller jobtemplate-controller enqueue should use struct not pointer #3134

merged 3 commits into from
Sep 25, 2023

Conversation

kingeasternsun
Copy link
Contributor

Why should not use pointer

The workqueue will deduplicate same item in queue.
According the internal struct of workqueue, when we add a item to workqueue firstly, workqueue will also put it in q.dirty which is `map[interface]struct{}``

// Add marks item as needing processing.
func (q *Type) Add(item interface{}) {
	
    ...

	q.dirty.insert(item)
	if q.processing.has(item) {
		return
	}

	q.queue = append(q.queue, item)
	q.cond.Signal()
}

before this item processed, if same item re added to the workqueue , this add will skiped

// Add marks item as needing processing.
func (q *Type) Add(item interface{}) {
	q.cond.L.Lock()
	defer q.cond.L.Unlock()
	if q.shuttingDown {
		return
	}
	if q.dirty.has(item) {
		return
	}
    ...
}

the current code use *apis.FlowRequest as item to add to the workqueue, even the different apis.FlowRequest have same values, different *apis.FlowRequest will be considerd as different item in workqueue, so the deduplication is not work.

We can try the example code blow,

type empty struct{}
type t interface{}
type set map[t]empty

func (s set) has(item t) bool {
	_, exists := s[item]
	return exists
}

func (s set) insert(item t) {
	s[item] = empty{}
}

func (s set) delete(item t) {
	delete(s, item)
}

func CompareMap() {
	a := &FlowRequest{Namespace: "a", JobFlowName: "n"}
	a1 := &FlowRequest{Namespace: "a", JobFlowName: "n"}
	b := &FlowRequest{Namespace: "b", JobFlowName: "n"}

	frMap := set{}
	frMap.insert(a)
	fmt.Println(frMap.has(a1))
	fmt.Println(frMap.has(b))
}
func CompareMapB() {
	a := FlowRequest{Namespace: "a", JobFlowName: "n"}
	a1 := FlowRequest{Namespace: "a", JobFlowName: "n"}
	b := FlowRequest{Namespace: "b", JobFlowName: "n"}

	frMap := set{}
	frMap.insert(a)
	fmt.Println(frMap.has(a1))
	fmt.Println(frMap.has(b))

}

the CompareMap will Print

false
false

the CompareMapB will Print

true
false

@volcano-sh-bot volcano-sh-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 21, 2023
@kingeasternsun
Copy link
Contributor Author

/assign @hwdef, could u review this please, thanks.

Copy link
Member

@hwdef hwdef left a comment

Choose a reason for hiding this comment

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

/lgtm

@volcano-sh-bot volcano-sh-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 23, 2023
Copy link
Member

@william-wang william-wang left a comment

Choose a reason for hiding this comment

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

/approve

@volcano-sh-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: william-wang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@volcano-sh-bot volcano-sh-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 25, 2023
@volcano-sh-bot volcano-sh-bot merged commit 00c5e7b into volcano-sh:master Sep 25, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants