-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarm-cache-1.0.groovy
More file actions
168 lines (138 loc) · 4.74 KB
/
warm-cache-1.0.groovy
File metadata and controls
168 lines (138 loc) · 4.74 KB
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
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.gerrit.common.data.GlobalCapability
import com.google.gerrit.entities.AccountGroup
import com.google.gerrit.sshd.*
import com.google.gerrit.extensions.annotations.*
import com.google.gerrit.server.project.*
import com.google.gerrit.server.account.*
import com.google.gerrit.server.IdentifiedUser
import com.google.inject.*
abstract class BaseSshCommand extends SshCommand {
void println(String msg) {
stdout.println msg
stdout.flush()
}
}
@Export("projects")
@CommandMetaData(description = "Warm-up project_list and projects caches")
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
class WarmProjectsCache extends BaseSshCommand {
@Inject
ProjectCache cache
public void run() {
println "Loading project list ..."
def start = System.currentTimeMillis()
def allProjects = cache.all()
def totProjects = allProjects.size()
def loaded = 0
for ( project in allProjects ) {
cache.get(project)
loaded++
if (loaded%1000==0) {
println "$loaded of $totProjects projects"
}
}
def elapsed = (System.currentTimeMillis()-start)/1000
println "$loaded projects loaded in $elapsed secs"
}
}
@Export("groups")
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
class WarmGroupsCache extends WarmProjectsCache {
@Inject
GroupCache groupCache
@Inject
GroupIncludeCache groupIncludeCache
private HashSet<AccountGroup.UUID> allGroupsUUIDs() {
def allGroupsUuids = new HashSet<AccountGroup.UUID>()
for (project in cache.all()) {
def groupUuids = cache.get(project).stream().flatMap { it.config.allGroupUUIDs.stream() }
allGroupsUuids.addAll(groupUuids.collect())
}
allGroupsUuids.addAll(groupIncludeCache.allExternalMembers())
return allGroupsUuids;
}
public void run() {
println "Loading groups list ..."
def start = System.currentTimeMillis()
def allGroupsUuids = allGroupsUUIDs();
def totGroups = allGroupsUuids.size()
def groupsLoaded = 0
for (groupUuid in allGroupsUuids) {
groupIncludeCache.parentGroupsOf(groupUuid)
def group = groupCache.get(groupUuid)
group.ifPresent { internalGroup ->
groupCache.get(internalGroup.getNameKey())
groupCache.get(internalGroup.getId())
}
groupsLoaded++
if (groupsLoaded%1000==0) {
println "$groupsLoaded of $totGroups groups"
}
}
def elapsed = (System.currentTimeMillis()-start)/1000
println "$groupsLoaded groups loaded in $elapsed secs"
}
}
@Export("accounts")
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
class WarmAccountsCache extends BaseSshCommand {
@Inject
AccountCache cache
@Inject
Accounts accounts
public void run() {
println "Loading accounts ..."
def start = System.currentTimeMillis()
def allAccounts = accounts.all()
def loaded = 0
for (account in allAccounts) {
cache.get(account.account().id())
loaded++
if (loaded%1000==0) {
println "$loaded accounts"
}
}
def elapsed = (System.currentTimeMillis()-start)/1000
println "$loaded accounts loaded in $elapsed secs"
}
}
@Export("groups-backends")
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
class WarmGroupsBackendsCache extends WarmAccountsCache {
@Inject
IdentifiedUser.GenericFactory userFactory
public void run() {
println "Loading groups ..."
def start = System.currentTimeMillis()
def allAccounts = accounts.all()
def loaded = 0
def allGroupsUUIDs = new HashSet<AccountGroup.UUID>()
def lastDisplay = 0
for (account in allAccounts) {
def user = userFactory.create(account.account().id())
def groupsUUIDs = user?.getEffectiveGroups()?.getKnownGroups()
if (groupsUUIDs != null) { allGroupsUUIDs.addAll(groupsUUIDs) }
loaded = allGroupsUUIDs.size()
if (loaded.intdiv(1000) > lastDisplay) {
println "$loaded groups"
lastDisplay = loaded.intdiv(1000)
}
}
def elapsed = (System.currentTimeMillis()-start)/1000
println "$loaded groups loaded in $elapsed secs"
}
}
commands = [ WarmProjectsCache, WarmGroupsCache, WarmAccountsCache, WarmGroupsBackendsCache ]