forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussion.go
261 lines (245 loc) · 7.27 KB
/
discussion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"sort"
"strings"
"github.com/google/syzkaller/dashboard/dashapi"
"github.com/google/syzkaller/pkg/email"
db "google.golang.org/appengine/v2/datastore"
)
// saveDiscussionMessage is meant to be called after each received E-mail message,
// for which we know the BugID.
func saveDiscussionMessage(c context.Context, msg *email.Email,
msgSource dashapi.DiscussionSource, msgType dashapi.DiscussionType) error {
discUpdate := &dashapi.Discussion{
Source: msgSource,
Type: msgType,
BugIDs: msg.BugIDs,
}
var parent *Discussion
var oldThreadInfo *email.OldThreadInfo
if msg.InReplyTo != "" {
parent, _ = discussionByMessageID(c, msgSource, msg.InReplyTo)
if parent != nil {
oldThreadInfo = &email.OldThreadInfo{
ThreadType: dashapi.DiscussionType(parent.Type),
}
}
}
switch email.NewMessageAction(msg, msgType, oldThreadInfo) {
case email.ActionIgnore:
return nil
case email.ActionAppend:
discUpdate.ID = parent.ID
discUpdate.Type = oldThreadInfo.ThreadType
case email.ActionNewThread:
// Use the current message as the discussion's head.
discUpdate.ID = msg.MessageID
discUpdate.Subject = msg.Subject
}
discUpdate.Messages = append(discUpdate.Messages, dashapi.DiscussionMessage{
ID: msg.MessageID,
Time: msg.Date,
External: !msg.OwnEmail,
})
return mergeDiscussion(c, discUpdate)
}
// mergeDiscussion either creates a new discussion or updates the existing one.
// It is assumed that the input is valid.
func mergeDiscussion(c context.Context, update *dashapi.Discussion) error {
if len(update.Messages) == 0 {
return fmt.Errorf("no messages")
}
newBugKeys, err := getBugKeys(c, update.BugIDs)
if err != nil {
return nil
}
// First update the discussion itself.
d := new(Discussion)
var diff DiscussionSummary
tx := func(c context.Context) error {
err := db.Get(c, discussionKey(c, string(update.Source), update.ID), d)
if err != nil && err != db.ErrNoSuchEntity {
return fmt.Errorf("failed to query Discussion: %w", err)
} else if err == db.ErrNoSuchEntity {
d.ID = update.ID
d.Source = string(update.Source)
d.Type = string(update.Type)
d.Subject = update.Subject
}
d.BugKeys = unique(append(d.BugKeys, newBugKeys...))
diff = d.addMessages(update.Messages)
if d.Type == string(dashapi.DiscussionPatch) {
diff.LastPatchMessage = diff.LastMessage
}
d.Summary.merge(diff)
_, err = db.Put(c, d.key(c), d)
if err != nil {
return fmt.Errorf("failed to put Discussion: %w", err)
}
return nil
}
err = db.RunInTransaction(c, tx, &db.TransactionOptions{Attempts: 15, XG: true})
if err != nil {
return err
}
// Update individual bug statistics.
// We have to do it outside of the main transaction, as we might hit the "operating on
// too many entity groups in a single transaction." error.
for _, key := range d.BugKeys {
err := db.RunInTransaction(c, func(c context.Context) error {
return mergeDiscussionSummary(c, key, d.Source, diff)
}, &db.TransactionOptions{Attempts: 15})
if err != nil {
return fmt.Errorf("failed to put update summary for %s: %w", key, err)
}
}
return nil
}
func mergeDiscussionSummary(c context.Context, key, source string, diff DiscussionSummary) error {
bug := new(Bug)
bugKey := db.NewKey(c, "Bug", key, 0, nil)
if err := db.Get(c, bugKey, bug); err != nil {
return fmt.Errorf("failed to get bug: %w", err)
}
var record *BugDiscussionInfo
for i, item := range bug.DiscussionInfo {
if item.Source == source {
record = &bug.DiscussionInfo[i]
}
}
if record == nil {
bug.DiscussionInfo = append(bug.DiscussionInfo, BugDiscussionInfo{
Source: source,
})
record = &bug.DiscussionInfo[len(bug.DiscussionInfo)-1]
}
record.Summary.merge(diff)
if _, err := db.Put(c, bugKey, bug); err != nil {
return fmt.Errorf("failed to put bug: %w", err)
}
return nil
}
func (ds *DiscussionSummary) merge(diff DiscussionSummary) {
ds.AllMessages += diff.AllMessages
ds.ExternalMessages += diff.ExternalMessages
if ds.LastMessage.Before(diff.LastMessage) {
ds.LastMessage = diff.LastMessage
}
if ds.LastPatchMessage.Before(diff.LastPatchMessage) {
ds.LastPatchMessage = diff.LastPatchMessage
}
}
func (bug *Bug) discussionSummary() DiscussionSummary {
// TODO: if there ever appear any non-public DiscussionSource, we'll need to consider
// their accessLevel as well.
var ret DiscussionSummary
for _, item := range bug.DiscussionInfo {
ret.merge(item.Summary)
}
return ret
}
const maxMessagesInDiscussion = 1500
func (d *Discussion) addMessages(messages []dashapi.DiscussionMessage) DiscussionSummary {
var diff DiscussionSummary
existingIDs := d.messageIDs()
for _, m := range messages {
if _, ok := existingIDs[m.ID]; ok {
continue
}
existingIDs[m.ID] = struct{}{}
diff.AllMessages++
if m.External {
diff.ExternalMessages++
}
if diff.LastMessage.Before(m.Time) {
diff.LastMessage = m.Time
}
d.Messages = append(d.Messages, DiscussionMessage{
ID: m.ID,
External: m.External,
Time: m.Time,
})
}
if len(d.Messages) == 0 {
return diff
}
sort.Slice(d.Messages, func(i, j int) bool {
return d.Messages[i].Time.Before(d.Messages[j].Time)
})
// Always keep the oldest message.
first := d.Messages[0]
if len(d.Messages) > maxMessagesInDiscussion {
d.Messages = append([]DiscussionMessage{first},
d.Messages[len(d.Messages)-maxMessagesInDiscussion+1:]...)
}
return diff
}
func (d *Discussion) messageIDs() map[string]struct{} {
ret := map[string]struct{}{}
for _, m := range d.Messages {
ret[m.ID] = struct{}{}
}
return ret
}
func (d *Discussion) link() string {
switch dashapi.DiscussionSource(d.Source) {
case dashapi.DiscussionLore:
return fmt.Sprintf("https://lore.kernel.org/all/%s/T/", strings.Trim(d.ID, "<>"))
}
return ""
}
func discussionByMessageID(c context.Context, source dashapi.DiscussionSource,
msgID string) (*Discussion, error) {
var discussions []*Discussion
keys, err := db.NewQuery("Discussion").
Filter("Source=", source).
Filter("Messages.ID=", msgID).
Limit(2).
GetAll(c, &discussions)
if err != nil {
return nil, err
} else if len(keys) == 0 {
return nil, db.ErrNoSuchEntity
} else if len(keys) == 2 {
// TODO: consider merging discussions in this case.
return nil, fmt.Errorf("message %s is present in several discussions", msgID)
}
return discussions[0], nil
}
func discussionsForBug(c context.Context, bugKey *db.Key) ([]*Discussion, error) {
var discussions []*Discussion
_, err := db.NewQuery("Discussion").
Filter("BugKeys=", bugKey.StringID()).
GetAll(c, &discussions)
if err != nil {
return nil, err
}
return discussions, nil
}
func getBugKeys(c context.Context, bugIDs []string) ([]string, error) {
keys := []string{}
for _, id := range bugIDs {
_, bugKey, err := findBugByReportingID(c, id)
if err != nil {
return nil, fmt.Errorf("failed to find bug for %s: %w", id, err)
}
keys = append(keys, bugKey.StringID())
}
return keys, nil
}
func unique(items []string) []string {
dup := map[string]struct{}{}
ret := []string{}
for _, item := range items {
if _, ok := dup[item]; ok {
continue
}
dup[item] = struct{}{}
ret = append(ret, item)
}
return ret
}