-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Queues: Move Notification to use a Queue #9902
Closed
Closed
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d8a52c
Ensure things are json marshalable
zeripath 7138650
Notification: move to use a queue
zeripath d313a4e
Add some more comments
zeripath 7e99301
Improve pull_merge_test
zeripath 569c7d7
Sort func names
zeripath e090f8e
Ensure fork has unique name in pull_merge_test
zeripath a7ad2ab
Remove unnecessary load of assignee
zeripath 1132cb2
Merge remote-tracking branch 'origin/master' into queues-notification
zeripath c21e6e9
placate lint
zeripath d288f3b
Merge branch 'master' into queues-notification
zeripath 1db4c91
Merge branch 'master' into queues-notification
guillep2k 8b84b1c
Merge branch 'master' into queues-notification
lafriks d43d378
Merge branch 'master' into queues-notification
lafriks 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 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 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 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 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 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,117 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/notification" | ||
"code.gitea.io/gitea/modules/notification/base" | ||
"code.gitea.io/gitea/modules/queue" | ||
) | ||
|
||
var notifierListener *NotifierListener | ||
|
||
var once = sync.Once{} | ||
|
||
type NotifierListener struct { | ||
lock sync.RWMutex | ||
callbacks map[string][]*func(string, [][]byte) | ||
notifier base.Notifier | ||
} | ||
|
||
func NotifierListenerInit() { | ||
once.Do(func() { | ||
notifierListener = &NotifierListener{ | ||
callbacks: map[string][]*func(string, [][]byte){}, | ||
} | ||
notifierListener.notifier = base.NewQueueNotifierWithHandle("test-notifier", notifierListener.handle) | ||
notification.RegisterNotifier(notifierListener.notifier) | ||
}) | ||
} | ||
|
||
// Register will register a callback with the provided notifier function | ||
func (n *NotifierListener) Register(functionName string, callback *func(string, [][]byte)) { | ||
n.lock.Lock() | ||
n.callbacks[functionName] = append(n.callbacks[functionName], callback) | ||
n.lock.Unlock() | ||
} | ||
|
||
// Deregister will remove the provided callback from the provided notifier function | ||
func (n *NotifierListener) Deregister(functionName string, callback *func(string, [][]byte)) { | ||
n.lock.Lock() | ||
defer n.lock.Unlock() | ||
for i, callbackPtr := range n.callbacks[functionName] { | ||
if callbackPtr == callback { | ||
n.callbacks[functionName] = append(n.callbacks[functionName][0:i], n.callbacks[functionName][i+1:]...) | ||
return | ||
} | ||
} | ||
} | ||
|
||
// RegisterChannel will return a registered channel with function name and return a function to deregister it and close the channel at the end | ||
func (n *NotifierListener) RegisterChannel(name string, argNumber int, exemplar interface{}) (<-chan interface{}, func()) { | ||
t := reflect.TypeOf(exemplar) | ||
channel := make(chan interface{}, 10) | ||
callback := func(_ string, args [][]byte) { | ||
n := reflect.New(t).Elem() | ||
err := json.Unmarshal(args[argNumber], n.Addr().Interface()) | ||
if err != nil { | ||
log.Error("Wrong Argument passed to register channel: %v ", err) | ||
} | ||
channel <- n.Interface() | ||
} | ||
n.Register(name, &callback) | ||
|
||
return channel, func() { | ||
n.Deregister(name, &callback) | ||
close(channel) | ||
} | ||
} | ||
|
||
func (n *NotifierListener) handle(data ...queue.Data) { | ||
n.lock.RLock() | ||
defer n.lock.RUnlock() | ||
for _, datum := range data { | ||
call := datum.(*base.FunctionCall) | ||
callbacks, ok := n.callbacks[call.Name] | ||
if ok && len(callbacks) > 0 { | ||
for _, callback := range callbacks { | ||
(*callback)(call.Name, call.Args) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestNotifierListener(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
createPullNotified, deregister := notifierListener.RegisterChannel("NotifyNewPullRequest", 0, &models.PullRequest{}) | ||
|
||
bs, _ := json.Marshal(&models.PullRequest{}) | ||
notifierListener.handle(&base.FunctionCall{ | ||
Name: "NotifyNewPullRequest", | ||
Args: [][]byte{ | ||
bs, | ||
}, | ||
}) | ||
<-createPullNotified | ||
|
||
notifierListener.notifier.NotifyNewPullRequest(&models.PullRequest{}) | ||
<-createPullNotified | ||
|
||
notification.NotifyNewPullRequest(&models.PullRequest{}) | ||
<-createPullNotified | ||
|
||
deregister() | ||
|
||
notification.NotifyNewPullRequest(&models.PullRequest{}) | ||
// would panic if not deregistered | ||
} |
This file contains 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 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 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 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.
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.
I'm wondering whether attachments, comments, reactions, assignees, etc. should be skipped too. Because there are reference fields in those structs as well (isn't there an
Issue
property inComment
?). Sounds like recursions can cause some headaches. On the other hand, the serialized data is a snapshot of the object state that protects the notifier from other processes changing the data, so it makes some sense to keep it as is. Decisions, decisions... 🤔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.
yup. I decided to bridge the problem as and when we see it. The trouble is under the default testing case you might not see the issue as we don't necessarily try to jsonify these things. (Hmm maybe I should add a jsontestqueue that will always json.Marshal stuff.)
As we pass into notifier pull requests and issues which definitely have a cyclic reference and therefore MarshalJSON doesn't work - we need to break the cycle. Every PR has an issue. Not all issues have PRs - so breaking the cycle at issue seemed best.
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.
I'm not particularly worried about cyclic references; those should stand out soon enough. But I think the overhead of serializing the whole bunch might be excessive. What I'm thinking is to use the opposite strategy: pass on only what is not derived, unless there's a reason to.
On the other hand, passing data which is tightly-coupled with the running Gitea version poses a problem for upgrading if any queues contain data during the process (e.g. file-system backed queues retained across shutdowns). One possible solution to this problem is to prevent upgrading if any queues contain data, but that may require a migration step being aware of the queue configuration and it would be very difficult to implement. May be we could drop a record on the database when shutting down if all queues are empty and the shutdown was orderly? A migration step could refuse to continue if that record is missing.
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.
The trouble with not passing "derived" data is that the notification you may get may be different from what was done.
You have to pass and parse all the data that makes the notification because you can't be certain what's going to change in future.
If you migrate and you can't deserialize mostly the notification will be dropped or default values added.
As you say we need a flush mechanism. That may mean opening a non listening Gitea to run through its queues.