From 9960d35435ef9e1f51a2291d9f375bd913f671cd Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Thu, 24 Sep 2015 16:24:40 -0700 Subject: [PATCH] MergeOp: Record the change set We want to record a unique identifier which can be queried later to obtain all changes which were submitted together. Change-Id: I5ba29aa52c5dc20b37f3b7965a026fd6e84b814b Signed-off-by: Stefan Beller --- .../google/gerrit/reviewdb/client/Change.java | 16 ++++++++ .../com/google/gerrit/server/git/MergeOp.java | 40 +++++++++++++++---- .../gerrit/server/schema/SchemaVersion.java | 2 +- .../gerrit/server/schema/Schema_112.java | 30 ++++++++++++++ 4 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_112.java diff --git a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/Change.java b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/Change.java index 07017710f4..6aa66990f2 100644 --- a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/Change.java +++ b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/Change.java @@ -453,6 +453,13 @@ public final class Change { @Column(id = 17, notNull = false) protected String originalSubject; + /** + * Unique id for the changes submitted together assigned during merging. + * Only set if the status is MERGED. + */ + @Column(id = 18, notNull = false) + protected String submissionId; + protected Change() { } @@ -479,6 +486,7 @@ public final class Change { currentPatchSetId = other.currentPatchSetId; subject = other.subject; originalSubject = other.originalSubject; + submissionId = other.submissionId; topic = other.topic; } @@ -562,6 +570,14 @@ public final class Change { } } + public String getSubmissionId() { + return submissionId; + } + + public void setSubmissionId(String id) { + this.submissionId = id; + } + public Status getStatus() { return Status.forCode(status); } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/MergeOp.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/MergeOp.java index 8ec8cbc17e..c5367ff287 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/git/MergeOp.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/MergeOp.java @@ -27,6 +27,8 @@ import com.google.common.collect.ListMultimap; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Table; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; import com.google.gerrit.common.ChangeHooks; import com.google.gerrit.common.TimeUtil; import com.google.gerrit.common.data.SubmitRecord; @@ -91,6 +93,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; @@ -143,7 +147,19 @@ public class MergeOp { private final Map> records; private final Map commits; - private String logPrefix; + + private static final String MACHINE_ID; + static { + String id; + try { + id = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + id = "unknown"; + } + MACHINE_ID = id; + } + private String staticSubmissionId; + private String submissionId; private ProjectState destProject; private ReviewDb db; @@ -336,10 +352,19 @@ public class MergeOp { } } + private void updateSubmissionId(Change change) { + Hasher h = Hashing.sha1().newHasher(); + h.putLong(Thread.currentThread().getId()) + .putUnencodedChars(MACHINE_ID); + staticSubmissionId = h.hash().toString().substring(0, 8); + submissionId = change.getId().get() + "-" + TimeUtil.nowMs() + + "-" + staticSubmissionId; + } + public void merge(ReviewDb db, Change change, IdentifiedUser caller, boolean checkSubmitRules) throws NoSuchChangeException, OrmException, ResourceConflictException { - logPrefix = String.format("[%s]: ", String.valueOf(change.hashCode())); + updateSubmissionId(change); this.db = db; logDebug("Beginning integration of {}", change); try { @@ -997,6 +1022,7 @@ public class MergeOp { @Override public Change update(Change c) { c.setStatus(Change.Status.MERGED); + c.setSubmissionId(submissionId); if (!merged.equals(c.currentPatchSetId())) { // Uncool; the patch set changed after we merged it. // Go back to the patch set that was actually merged. @@ -1253,28 +1279,28 @@ public class MergeOp { private void logDebug(String msg, Object... args) { if (log.isDebugEnabled()) { - log.debug(logPrefix + msg, args); + log.debug("[" + submissionId + "]" + msg, args); } } private void logWarn(String msg, Throwable t) { if (log.isWarnEnabled()) { - log.warn(logPrefix + msg, t); + log.warn("[" + submissionId + "]" + msg, t); } } private void logWarn(String msg) { if (log.isWarnEnabled()) { - log.warn(logPrefix + msg); + log.warn("[" + submissionId + "]" + msg); } } private void logError(String msg, Throwable t) { if (log.isErrorEnabled()) { if (t != null) { - log.error(logPrefix + msg, t); + log.error("[" + submissionId + "]" + msg, t); } else { - log.error(logPrefix + msg); + log.error("[" + submissionId + "]" + msg); } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/SchemaVersion.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/SchemaVersion.java index 769329dd78..9022bfe224 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/schema/SchemaVersion.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/SchemaVersion.java @@ -32,7 +32,7 @@ import java.util.List; /** A version of the database schema. */ public abstract class SchemaVersion { /** The current schema version. */ - public static final Class C = Schema_111.class; + public static final Class C = Schema_112.class; public static int getBinaryVersion() { return guessVersion(C); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_112.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_112.java new file mode 100644 index 0000000000..6364c480c2 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_112.java @@ -0,0 +1,30 @@ +// Copyright (C) 2015 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.schema; + +import com.google.gerrit.reviewdb.server.ReviewDb; +import com.google.gwtorm.jdbc.JdbcSchema; +import com.google.inject.Inject; +import com.google.inject.Provider; + +import java.sql.SQLException; +import java.sql.Statement; + +public class Schema_112 extends SchemaVersion { + @Inject + Schema_112(Provider prior) { + super(prior); + } +}