use of com.google.gerrit.metrics.Timer0 in project gerrit by GerritCodeReview.
the class ExternalIdReader method all.
/** Reads and returns all external IDs. */
private Set<ExternalId> all(Repository repo, ObjectId rev) throws IOException {
if (rev.equals(ObjectId.zeroId())) {
return ImmutableSet.of();
}
try (Timer0.Context ctx = readAllLatency.start();
RevWalk rw = new RevWalk(repo)) {
NoteMap noteMap = readNoteMap(rw, rev);
Set<ExternalId> extIds = new HashSet<>();
for (Note note : noteMap) {
byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
try {
extIds.add(ExternalId.parse(note.getName(), raw));
} catch (Exception e) {
log.error(String.format("Ignoring invalid external ID note %s", note.getName()), e);
}
}
return extIds;
}
}
use of com.google.gerrit.metrics.Timer0 in project gerrit by GerritCodeReview.
the class ReviewersUtil method suggestAccountGroups.
private List<SuggestedReviewerInfo> suggestAccountGroups(SuggestReviewers suggestReviewers, ProjectControl projectControl, VisibilityControl visibilityControl, int limit) throws OrmException, IOException {
try (Timer0.Context ctx = metrics.queryGroupsLatency.start()) {
List<SuggestedReviewerInfo> groups = new ArrayList<>();
for (GroupReference g : suggestAccountGroups(suggestReviewers, projectControl)) {
GroupAsReviewer result = suggestGroupAsReviewer(suggestReviewers, projectControl.getProject(), g, visibilityControl);
if (result.allowed || result.allowedWithConfirmation) {
GroupBaseInfo info = new GroupBaseInfo();
info.id = Url.encode(g.getUUID().get());
info.name = g.getName();
SuggestedReviewerInfo suggestedReviewerInfo = new SuggestedReviewerInfo();
suggestedReviewerInfo.group = info;
suggestedReviewerInfo.count = result.size;
if (result.allowedWithConfirmation) {
suggestedReviewerInfo.confirm = true;
}
groups.add(suggestedReviewerInfo);
if (groups.size() >= limit) {
break;
}
}
}
return groups;
}
}
use of com.google.gerrit.metrics.Timer0 in project gerrit by GerritCodeReview.
the class Helper method kerberosOpen.
private DirContext kerberosOpen(Properties env) throws IOException, LoginException, NamingException {
LoginContext ctx = new LoginContext("KerberosLogin");
try (Timer0.Context ignored = loginLatencyTimer.start()) {
ctx.login();
}
Subject subject = ctx.getSubject();
try {
return Subject.doAs(subject, (PrivilegedExceptionAction<DirContext>) () -> createContext(env));
} catch (PrivilegedActionException e) {
Throwables.throwIfInstanceOf(e.getException(), IOException.class);
Throwables.throwIfInstanceOf(e.getException(), NamingException.class);
Throwables.throwIfInstanceOf(e.getException(), RuntimeException.class);
logger.atWarning().withCause(e.getException()).log("Internal error");
return null;
} finally {
ctx.logout();
}
}
use of com.google.gerrit.metrics.Timer0 in project gerrit by GerritCodeReview.
the class ChangeJson method toChangeInfos.
private List<ChangeInfo> toChangeInfos(List<ChangeData> changes, Map<Change.Id, ChangeInfo> cache, ImmutableListMultimap<Change.Id, PluginDefinedInfo> pluginInfosByChange) {
try (Timer0.Context ignored = metrics.toChangeInfosLatency.start()) {
List<ChangeInfo> changeInfos = new ArrayList<>(changes.size());
for (int i = 0; i < changes.size(); i++) {
// We can only cache and re-use an entity if it's not the last in the list. The last entity
// may later get _moreChanges set. If it was cached or re-used, that setting would propagate
// to the original entity yielding wrong results.
// This problem has two sides where 'last in the list' has to be respected:
// (1) Caching
// (2) Reusing
boolean isCacheable = i != changes.size() - 1;
ChangeData cd = changes.get(i);
ChangeInfo info = cache.get(cd.getId());
if (info != null && isCacheable) {
changeInfos.add(info);
continue;
}
// Compute and cache if possible
try {
ensureLoaded(Collections.singleton(cd));
info = format(cd, Optional.empty(), false, pluginInfosByChange.get(cd.getId()));
changeInfos.add(info);
if (isCacheable) {
cache.put(Change.id(info._number), info);
}
} catch (RuntimeException e) {
Optional<RequestCancelledException> requestCancelledException = RequestCancelledException.getFromCausalChain(e);
if (requestCancelledException.isPresent()) {
throw e;
}
logger.atWarning().withCause(e).log("Omitting corrupt change %s from results", cd.getId());
}
}
return changeInfos;
}
}
use of com.google.gerrit.metrics.Timer0 in project gerrit by GerritCodeReview.
the class SubmitRuleEvaluator method evaluate.
/**
* Evaluate the submit rules.
*
* @return List of {@link SubmitRecord} objects returned from the evaluated rules, including any
* errors.
* @param cd ChangeData to evaluate
*/
public List<SubmitRecord> evaluate(ChangeData cd) {
logger.atFine().log("Evaluate submit rules for change %d (caller: %s)", cd.change().getId().get(), callerFinder.findCallerLazy());
try (Timer0.Context ignored = submitRuleEvaluationLatency.start()) {
Change change;
ProjectState projectState;
try {
change = cd.change();
if (change == null) {
throw new StorageException("Change not found");
}
Project.NameKey name = cd.project();
Optional<ProjectState> projectStateOptional = projectCache.get(name);
if (!projectStateOptional.isPresent()) {
throw new NoSuchProjectException(name);
}
projectState = projectStateOptional.get();
} catch (NoSuchProjectException e) {
throw new IllegalStateException("Unable to find project while evaluating submit rule", e);
}
if (change.isClosed() && (!opts.recomputeOnClosedChanges() || OnlineReindexMode.isActive())) {
return cd.notes().getSubmitRecords().stream().map(r -> {
SubmitRecord record = r.deepCopy();
if (record.status == SubmitRecord.Status.OK) {
// Submit records that were OK when they got merged are CLOSED now.
record.status = SubmitRecord.Status.CLOSED;
}
return record;
}).collect(toImmutableList());
}
// and then we collect the results in one list.
return Streams.stream(submitRules).filter(projectState.hasPrologRules() ? rule -> !(rule.get() instanceof DefaultSubmitRule) : rule -> true).map(c -> c.call(s -> {
Optional<SubmitRecord> record = s.evaluate(cd);
if (record.isPresent() && record.get().ruleName == null) {
// Only back-fill the ruleName if it was not populated by the "submit
// rule".
record.get().ruleName = c.getPluginName() + "~" + s.getClass().getSimpleName();
}
return record;
})).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
}
}
Aggregations