Store PatchSetApprovals instead of labels in the index

The goal of storing SubmitRecord.Labels in the index was to render the
change table, but that requires both the results of the submit rule
evaluator and, in fact, all of the current PatchSetApprovals.

Instead, store PatchSetApprovals, which are trivial to find at
indexing time. Note that we do not just use the formatted label
representation stored in the index because it does not contain all the
necessary columns (e.g. timestamp). These may not be strictly speaking
required for rendering the change table, but having them set to null
in some cases might have caused errors down the line.

Delete the old label-based code entirely and change schema v2 rather
than revving to v3. This mistake was short-lived enough that we don't
expect people to have production data using it, and the old code was
sufficiently complex and incorrect that keeping it around isn't a good
idea.

Change-Id: I805a8fd6c60d4818910aed299260f56ef3006287
This commit is contained in:
Dave Borowitz
2013-09-12 13:25:58 -07:00
parent cce486f760
commit 24b14d5053
8 changed files with 75 additions and 264 deletions

View File

@@ -380,10 +380,6 @@ public class ChangeJson {
if (!standard && !detailed) {
return null;
}
if (cd.getSubmitRecordLabels() != null && standard && !detailed) {
// Use saved labels to avoid recomputing on the fly.
return labelsFromSavedState(cd.getSubmitRecordLabels());
}
ChangeControl ctl = control(cd);
if (ctl == null) {
@@ -398,15 +394,6 @@ public class ChangeJson {
}
}
private Map<String, LabelInfo> labelsFromSavedState(
List<SubmitRecord.Label> savedLabels) {
Map<String, LabelInfo> labels = Maps.newLinkedHashMap();
for (SubmitRecord.Label r : savedLabels) {
labels.put(r.label, newLabelFromSubmitRecord(r, true));
}
return labels;
}
private Map<String, LabelInfo> labelsForOpenChange(ChangeData cd,
LabelTypes labelTypes, boolean standard, boolean detailed)
throws OrmException {
@@ -447,34 +434,29 @@ public class ChangeJson {
for (SubmitRecord.Label r : rec.labels) {
LabelInfo p = labels.get(r.label);
if (p == null || p._status.compareTo(r.status) < 0) {
labels.put(r.label, newLabelFromSubmitRecord(r, standard));
LabelInfo n = new LabelInfo();
n._status = r.status;
if (standard) {
switch (r.status) {
case OK:
n.approved = accountLoader.get(r.appliedBy);
break;
case REJECT:
n.rejected = accountLoader.get(r.appliedBy);
break;
default:
break;
}
}
n.optional = n._status == SubmitRecord.Label.Status.MAY ? true : null;
labels.put(r.label, n);
}
}
}
return labels;
}
private LabelInfo newLabelFromSubmitRecord(SubmitRecord.Label r,
boolean standard) {
LabelInfo n = new LabelInfo();
n._status = r.status;
if (standard) {
switch (r.status) {
case OK:
n.approved = accountLoader.get(r.appliedBy);
break;
case REJECT:
n.rejected = accountLoader.get(r.appliedBy);
break;
default:
break;
}
}
n.optional = n._status == SubmitRecord.Label.Status.MAY ? true : null;
return n;
}
private void setLabelScores(LabelType type,
LabelInfo label, short score, Account.Id accountId)
throws OrmException {

View File

@@ -21,7 +21,7 @@ import com.google.inject.Provider;
import org.eclipse.jgit.lib.Config;
public class EmailExpanderProvider implements Provider<EmailExpander> {
class EmailExpanderProvider implements Provider<EmailExpander> {
private final EmailExpander expander;
@Inject

View File

@@ -14,9 +14,7 @@
package com.google.gerrit.server.index;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
@@ -24,11 +22,8 @@ import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.SubmitRecord;
import com.google.gerrit.reviewdb.client.TrackingId;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
import com.google.gerrit.server.query.change.ChangeStatusPredicate;
@@ -42,7 +37,6 @@ import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -265,63 +259,28 @@ public class ChangeField {
/** Serialized change object, used for pre-populating results. */
public static final ChangeProtoField CHANGE = new ChangeProtoField();
public static class SubmitLabelProtoField
public static class PatchSetApprovalProtoField
extends FieldDef.Repeatable<ChangeData, byte[]> {
public static final ProtobufCodec<SubmitRecord.Label> CODEC =
CodecFactory.encoder(SubmitRecord.Label.class);
public static final ProtobufCodec<PatchSetApproval> CODEC =
CodecFactory.encoder(PatchSetApproval.class);
private SubmitLabelProtoField() {
super("_label", FieldType.STORED_ONLY, true);
private PatchSetApprovalProtoField() {
super("_approval", FieldType.STORED_ONLY, true);
}
@Override
public Iterable<byte[]> get(ChangeData input, FillArgs args)
throws OrmException {
// Flatten the highest-valued labels to mimic the results from ChangeJson
// with standard labels.
Map<String, SubmitRecord.Label> labels = Maps.newLinkedHashMap();
for (SubmitRecord rec : getSubmitRecords(input, args)) {
if (rec.labels == null) {
continue;
}
for (SubmitRecord.Label r : rec.labels) {
SubmitRecord.Label p = labels.get(r.label);
if (p == null || p.status.compareTo(r.status) < 0) {
labels.put(r.label, r);
}
}
}
return toProtos(CODEC, labels.values());
}
private List<SubmitRecord> getSubmitRecords(ChangeData input,
FillArgs args) throws OrmException {
ChangeControl ctl;
try {
// Use the ChangeControl for InternalUser. This will give bogus
// results for whether or not the change is submittable, but does
// not affect label calculation.
ctl = args.changeControlFor(input.change(args.db));
} catch (NoSuchChangeException e) {
throw new OrmException(e);
}
if (ctl == null) {
return ImmutableList.of();
}
PatchSet ps = input.currentPatchSet(args.db);
if (ps == null) {
return ImmutableList.of();
}
return ctl.canSubmit(args.db.get(), ps, input, true, true, true);
return toProtos(CODEC, input.currentApprovals(args.db));
}
}
/**
* Serialized labels from the submit rule evaluator, used for pre-populating
* Serialized approvals for the current patch set, used for pre-populating
* results.
*/
public static final SubmitLabelProtoField SUBMIT_RECORD_LABEL =
new SubmitLabelProtoField();
public static final PatchSetApprovalProtoField APPROVAL =
new PatchSetApprovalProtoField();
public static String formatLabel(String label, int value) {
return formatLabel(label, value, null);

View File

@@ -69,7 +69,7 @@ public class ChangeSchemas {
ChangeField.COMMIT_MESSAGE,
ChangeField.COMMENT,
ChangeField.CHANGE,
ChangeField.SUBMIT_RECORD_LABEL);
ChangeField.APPROVAL);
private static Schema<ChangeData> release(FieldDef<ChangeData, ?>... fields) {
return new Schema<ChangeData>(true, Arrays.asList(fields));

View File

@@ -14,13 +14,9 @@
package com.google.gerrit.server.index;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.InternalUser;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -64,31 +60,14 @@ public abstract class FieldDef<I, T> {
final Provider<ReviewDb> db;
final GitRepositoryManager repoManager;
final PatchListCache patchListCache;
final ChangeControl.GenericFactory changeControlFactory;
private final InternalUser internalUser;
@Inject
FillArgs(Provider<ReviewDb> db, GitRepositoryManager repoManager,
PatchListCache patchListCache,
ChangeControl.GenericFactory changeControlFactory,
InternalUser internalUser) {
FillArgs(Provider<ReviewDb> db,
GitRepositoryManager repoManager,
PatchListCache patchListCache) {
this.db = db;
this.repoManager = repoManager;
this.patchListCache = patchListCache;
this.changeControlFactory = changeControlFactory;
this.internalUser = internalUser;
}
/**
* Create a change control.
*
* @param change change object.
* @return change control as seen by Gerrit's internal user; index fields
* may not depend on any per-user state.
*/
ChangeControl changeControlFor(Change change) throws NoSuchChangeException {
return changeControlFactory.controlFor(change, internalUser);
}
}

View File

@@ -148,7 +148,6 @@ public class ChangeData {
private ChangeControl changeControl;
private List<ChangeMessage> messages;
private List<SubmitRecord> submitRecords;
private List<SubmitRecord.Label> submitRecordLabels;
private boolean patchesLoaded;
public ChangeData(final Change.Id id) {
@@ -312,6 +311,10 @@ public class ChangeData {
return currentApprovals;
}
public void setCurrentApprovals(List<PatchSetApproval> approvals) {
currentApprovals = approvals;
}
public String commitMessage(GitRepositoryManager repoManager,
Provider<ReviewDb> db) throws IOException, OrmException {
if (commitMessage == null) {
@@ -460,12 +463,4 @@ public class ChangeData {
public List<SubmitRecord> getSubmitRecords() {
return submitRecords;
}
public void setSubmitRecordLabels(List<SubmitRecord.Label> labels) {
submitRecordLabels = labels;
}
public List<SubmitRecord.Label> getSubmitRecordLabels() {
return submitRecordLabels;
}
}