/
admin.go
188 lines (179 loc) · 4.86 KB
/
admin.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
// Copyright 2017 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 dash
import (
"fmt"
"net/http"
"golang.org/x/net/context"
db "google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
// dropNamespace drops all entities related to a single namespace.
// Use with care. There is no undo.
// This functionality is intentionally not connected to any handler.
// To use it, first make a backup of the db. Then, specify the target
// namespace in the ns variable, connect the function to a handler, invoke it
// and double check the output. Finally, set dryRun to false and invoke again.
func dropNamespace(c context.Context, w http.ResponseWriter, r *http.Request) error {
ns := "non-existent"
dryRun := true
if !dryRun {
log.Criticalf(c, "dropping namespace %v", ns)
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "dropping namespace %v\n", ns)
if err := dropNamespaceReportingState(c, w, ns, dryRun); err != nil {
return err
}
type Entity struct {
name string
child string
}
entities := []Entity{
{textPatch, ""},
{textReproC, ""},
{textReproSyz, ""},
{textKernelConfig, ""},
{"Job", ""},
{textLog, ""},
{textError, ""},
{textCrashLog, ""},
{textCrashReport, ""},
{"Build", ""},
{"Manager", "ManagerStats"},
{"Bug", "Crash"},
}
for _, entity := range entities {
keys, err := db.NewQuery(entity.name).
Filter("Namespace=", ns).
KeysOnly().
GetAll(c, nil)
if err != nil {
return err
}
fmt.Fprintf(w, "%v: %v\n", entity.name, len(keys))
if entity.child != "" {
var childKeys []*db.Key
for _, key := range keys {
keys1, err := db.NewQuery(entity.child).
Ancestor(key).
KeysOnly().
GetAll(c, nil)
if err != nil {
return err
}
childKeys = append(childKeys, keys1...)
}
fmt.Fprintf(w, " %v: %v\n", entity.child, len(childKeys))
if err := dropEntities(c, childKeys, dryRun); err != nil {
return err
}
}
if err := dropEntities(c, keys, dryRun); err != nil {
return err
}
}
return nil
}
func dropNamespaceReportingState(c context.Context, w http.ResponseWriter, ns string, dryRun bool) error {
tx := func(c context.Context) error {
state, err := loadReportingState(c)
if err != nil {
return err
}
newState := new(ReportingState)
for _, ent := range state.Entries {
if ent.Namespace != ns {
newState.Entries = append(newState.Entries, ent)
}
}
if !dryRun {
if err := saveReportingState(c, newState); err != nil {
return err
}
}
fmt.Fprintf(w, "ReportingState: %v\n", len(state.Entries)-len(newState.Entries))
return nil
}
return db.RunInTransaction(c, tx, nil)
}
func dropEntities(c context.Context, keys []*db.Key, dryRun bool) error {
if dryRun {
return nil
}
for len(keys) != 0 {
batch := 100
if batch > len(keys) {
batch = len(keys)
}
if err := db.DeleteMulti(c, keys[:batch]); err != nil {
return err
}
keys = keys[batch:]
}
return nil
}
// updateBugReporting adds missing reporting stages to bugs in a single namespace.
// Use with care. There is no undo.
// This can be used to migrate datastore to a new config with more reporting stages.
// This functionality is intentionally not connected to any handler.
// Before invoking it is recommended to stop all connected instances just in case.
func updateBugReporting(c context.Context, w http.ResponseWriter, r *http.Request) error {
if accessLevel(c, r) != AccessAdmin {
return fmt.Errorf("admin only")
}
ns := r.FormValue("ns")
if ns == "" {
return fmt.Errorf("no ns parameter")
}
var bugs []*Bug
keys, err := db.NewQuery("Bug").
Filter("Namespace=", ns).
GetAll(c, &bugs)
if err != nil {
return err
}
log.Warningf(c, "fetched %v bugs for namespce %v", len(bugs), ns)
cfg := config.Namespaces[ns]
var batchKeys []*db.Key
const batchSize = 20
for i, bug := range bugs {
if len(bug.Reporting) >= len(cfg.Reporting) {
continue
}
batchKeys = append(batchKeys, keys[i])
if len(batchKeys) == batchSize {
if err := updateBugReportingBatch(c, cfg, batchKeys); err != nil {
return err
}
batchKeys = nil
}
}
if len(batchKeys) != 0 {
if err := updateBugReportingBatch(c, cfg, batchKeys); err != nil {
return err
}
}
return nil
}
func updateBugReportingBatch(c context.Context, cfg *Config, keys []*db.Key) error {
tx := func(c context.Context) error {
bugs := make([]*Bug, len(keys))
if err := db.GetMulti(c, keys, bugs); err != nil {
return err
}
for _, bug := range bugs {
createBugReporting(bug, cfg)
}
_, err := db.PutMulti(c, keys, bugs)
return err
}
err := db.RunInTransaction(c, tx, &db.TransactionOptions{XG: true})
log.Warningf(c, "updated %v bugs: %v", len(keys), err)
return err
}
// Prevent warnings about dead code.
var (
_ = dropNamespace
_ = updateBugReporting
)