summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/account/GroupIncludeCacheImpl.java
blob: 3bc663a703c9fb34028cc726bc63746dc4e2b1ff (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (C) 2011 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.

package com.google.gerrit.server.account;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.group.Groups;
import com.google.gerrit.server.group.InternalGroup;
import com.google.gerrit.server.index.group.GroupField;
import com.google.gerrit.server.index.group.GroupIndex;
import com.google.gerrit.server.index.group.GroupIndexCollection;
import com.google.gerrit.server.query.group.InternalGroupQuery;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Tracks group inclusions in memory for efficient access. */
@Singleton
public class GroupIncludeCacheImpl implements GroupIncludeCache {
  private static final Logger log = LoggerFactory.getLogger(GroupIncludeCacheImpl.class);
  private static final String PARENT_GROUPS_NAME = "groups_bysubgroup";
  private static final String SUBGROUPS_NAME = "groups_subgroups";
  private static final String GROUPS_WITH_MEMBER_NAME = "groups_bymember";
  private static final String EXTERNAL_NAME = "groups_external";

  public static Module module() {
    return new CacheModule() {
      @Override
      protected void configure() {
        cache(
                GROUPS_WITH_MEMBER_NAME,
                Account.Id.class,
                new TypeLiteral<ImmutableSet<AccountGroup.UUID>>() {})
            .loader(GroupsWithMemberLoader.class);

        cache(
                PARENT_GROUPS_NAME,
                AccountGroup.UUID.class,
                new TypeLiteral<ImmutableList<AccountGroup.UUID>>() {})
            .loader(ParentGroupsLoader.class);

        cache(
                SUBGROUPS_NAME,
                AccountGroup.UUID.class,
                new TypeLiteral<ImmutableList<AccountGroup.UUID>>() {})
            .loader(SubgroupsLoader.class);

        cache(EXTERNAL_NAME, String.class, new TypeLiteral<ImmutableList<AccountGroup.UUID>>() {})
            .loader(AllExternalLoader.class);

        bind(GroupIncludeCacheImpl.class);
        bind(GroupIncludeCache.class).to(GroupIncludeCacheImpl.class);
      }
    };
  }

  private final LoadingCache<Account.Id, ImmutableSet<AccountGroup.UUID>> groupsWithMember;
  private final LoadingCache<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> subgroups;
  private final LoadingCache<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> parentGroups;
  private final LoadingCache<String, ImmutableList<AccountGroup.UUID>> external;

  @Inject
  GroupIncludeCacheImpl(
      @Named(GROUPS_WITH_MEMBER_NAME)
          LoadingCache<Account.Id, ImmutableSet<AccountGroup.UUID>> groupsWithMember,
      @Named(SUBGROUPS_NAME)
          LoadingCache<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> subgroups,
      @Named(PARENT_GROUPS_NAME)
          LoadingCache<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> parentGroups,
      @Named(EXTERNAL_NAME) LoadingCache<String, ImmutableList<AccountGroup.UUID>> external) {
    this.groupsWithMember = groupsWithMember;
    this.subgroups = subgroups;
    this.parentGroups = parentGroups;
    this.external = external;
  }

  @Override
  public Collection<AccountGroup.UUID> getGroupsWithMember(Account.Id memberId) {
    try {
      return groupsWithMember.get(memberId);
    } catch (ExecutionException e) {
      log.warn("Cannot load groups containing {} as member", memberId.get());
      return ImmutableSet.of();
    }
  }

  @Override
  public Collection<AccountGroup.UUID> subgroupsOf(AccountGroup.UUID groupId) {
    try {
      return subgroups.get(groupId);
    } catch (ExecutionException e) {
      log.warn("Cannot load members of group", e);
      return Collections.emptySet();
    }
  }

  @Override
  public Collection<AccountGroup.UUID> parentGroupsOf(AccountGroup.UUID groupId) {
    try {
      return parentGroups.get(groupId);
    } catch (ExecutionException e) {
      log.warn("Cannot load included groups", e);
      return Collections.emptySet();
    }
  }

  @Override
  public void evictGroupsWithMember(Account.Id memberId) {
    if (memberId != null) {
      groupsWithMember.invalidate(memberId);
    }
  }

  @Override
  public void evictSubgroupsOf(AccountGroup.UUID groupId) {
    if (groupId != null) {
      subgroups.invalidate(groupId);
    }
  }

  @Override
  public void evictParentGroupsOf(AccountGroup.UUID groupId) {
    if (groupId != null) {
      parentGroups.invalidate(groupId);

      if (!AccountGroup.isInternalGroup(groupId)) {
        external.invalidate(EXTERNAL_NAME);
      }
    }
  }

  @Override
  public Collection<AccountGroup.UUID> allExternalMembers() {
    try {
      return external.get(EXTERNAL_NAME);
    } catch (ExecutionException e) {
      log.warn("Cannot load set of non-internal groups", e);
      return ImmutableList.of();
    }
  }

  static class GroupsWithMemberLoader
      extends CacheLoader<Account.Id, ImmutableSet<AccountGroup.UUID>> {
    private final SchemaFactory<ReviewDb> schema;
    private final Provider<GroupIndex> groupIndexProvider;
    private final Provider<InternalGroupQuery> groupQueryProvider;
    private final GroupCache groupCache;

    @Inject
    GroupsWithMemberLoader(
        SchemaFactory<ReviewDb> schema,
        GroupIndexCollection groupIndexCollection,
        Provider<InternalGroupQuery> groupQueryProvider,
        GroupCache groupCache) {
      this.schema = schema;
      groupIndexProvider = groupIndexCollection::getSearchIndex;
      this.groupQueryProvider = groupQueryProvider;
      this.groupCache = groupCache;
    }

    @Override
    public ImmutableSet<AccountGroup.UUID> load(Account.Id memberId)
        throws OrmException, NoSuchGroupException {
      GroupIndex groupIndex = groupIndexProvider.get();
      if (groupIndex != null && groupIndex.getSchema().hasField(GroupField.MEMBER)) {
        return groupQueryProvider.get().byMember(memberId).stream()
            .map(InternalGroup::getGroupUUID)
            .collect(toImmutableSet());
      }
      try (ReviewDb db = schema.open()) {
        return Groups.getGroupsWithMemberFromReviewDb(db, memberId)
            .map(groupCache::get)
            .flatMap(Streams::stream)
            .map(InternalGroup::getGroupUUID)
            .collect(toImmutableSet());
      }
    }
  }

  static class SubgroupsLoader
      extends CacheLoader<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> {
    private final SchemaFactory<ReviewDb> schema;
    private final Groups groups;

    @Inject
    SubgroupsLoader(SchemaFactory<ReviewDb> sf, Groups groups) {
      schema = sf;
      this.groups = groups;
    }

    @Override
    public ImmutableList<AccountGroup.UUID> load(AccountGroup.UUID key)
        throws OrmException, NoSuchGroupException {
      try (ReviewDb db = schema.open()) {
        return groups.getSubgroups(db, key).collect(toImmutableList());
      }
    }
  }

  static class ParentGroupsLoader
      extends CacheLoader<AccountGroup.UUID, ImmutableList<AccountGroup.UUID>> {
    private final SchemaFactory<ReviewDb> schema;
    private final Provider<GroupIndex> groupIndexProvider;
    private final Provider<InternalGroupQuery> groupQueryProvider;
    private final GroupCache groupCache;

    @Inject
    ParentGroupsLoader(
        SchemaFactory<ReviewDb> sf,
        GroupIndexCollection groupIndexCollection,
        Provider<InternalGroupQuery> groupQueryProvider,
        GroupCache groupCache) {
      schema = sf;
      this.groupIndexProvider = groupIndexCollection::getSearchIndex;
      this.groupQueryProvider = groupQueryProvider;
      this.groupCache = groupCache;
    }

    @Override
    public ImmutableList<AccountGroup.UUID> load(AccountGroup.UUID key) throws OrmException {
      GroupIndex groupIndex = groupIndexProvider.get();
      if (groupIndex != null && groupIndex.getSchema().hasField(GroupField.SUBGROUP)) {
        return groupQueryProvider.get().bySubgroup(key).stream()
            .map(InternalGroup::getGroupUUID)
            .collect(toImmutableList());
      }
      try (ReviewDb db = schema.open()) {
        return Groups.getParentGroupsFromReviewDb(db, key)
            .map(groupCache::get)
            .flatMap(Streams::stream)
            .map(InternalGroup::getGroupUUID)
            .collect(toImmutableList());
      }
    }
  }

  static class AllExternalLoader extends CacheLoader<String, ImmutableList<AccountGroup.UUID>> {
    private final SchemaFactory<ReviewDb> schema;
    private final Groups groups;

    @Inject
    AllExternalLoader(SchemaFactory<ReviewDb> sf, Groups groups) {
      schema = sf;
      this.groups = groups;
    }

    @Override
    public ImmutableList<AccountGroup.UUID> load(String key) throws Exception {
      try (ReviewDb db = schema.open()) {
        return groups.getExternalGroups(db).collect(toImmutableList());
      }
    }
  }
}