Add id to PushOne and print it in the log and in show-queue command

This id allows to correlate a replication task displayed by show-queue
command and its log entries in the replication_log.

Change-Id: I0d8dfb73e74ddb622e54a7b16921784ad3bd8c96
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java b/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
index 8382606..b699e0d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
@@ -34,6 +34,7 @@
 import com.google.gerrit.server.git.VisibleRefFilter;
 import com.google.gerrit.server.project.NoSuchProjectException;
 import com.google.gerrit.server.project.ProjectControl;
+import com.google.gerrit.server.util.IdGenerator;
 import com.google.gwtorm.server.OrmException;
 import com.google.gwtorm.server.SchemaFactory;
 import com.google.inject.Inject;
@@ -60,6 +61,7 @@
 import org.eclipse.jgit.transport.Transport;
 import org.eclipse.jgit.transport.URIish;
 import org.slf4j.Logger;
+import org.slf4j.MDC;
 
 import java.io.IOException;
 import java.util.Collection;
@@ -82,6 +84,7 @@
   private static final ReplicationStateLogger stateLog =
       new ReplicationStateLogger(repLog);
   static final String ALL_REFS = "..all..";
+  static final String ID_MDC_KEY = "pushOneId";
 
   interface Factory {
     PushOne create(Project.NameKey d, URIish u);
@@ -109,6 +112,7 @@
       LinkedListMultimap.create();
   private final int maxLockRetries;
   private int lockRetryCount;
+  private final int id;
 
   @Inject
   PushOne(final GitRepositoryManager grm,
@@ -120,6 +124,7 @@
       final PerThreadRequestScope.Scoper ts,
       final ChangeCache cc,
       final ReplicationQueue rq,
+      final IdGenerator ig,
       @Assisted final Project.NameKey d,
       @Assisted final URIish u) {
     gitManager = grm;
@@ -135,6 +140,7 @@
     uri = u;
     lockRetryCount = 0;
     maxLockRetries = pool.getLockErrorMaxRetries();
+    id = ig.next();
   }
 
   @Override
@@ -154,10 +160,12 @@
 
   @Override
   public String toString() {
-    if (retryCount == 0) {
-      return "push " + uri;
+    String print = "[" + IdGenerator.format(id) + "] push " + uri;
+
+    if (retryCount > 0) {
+      print = "(retry " + retryCount + ") " + print;
     }
-    return "(retry " + retryCount + ") " + "push " + uri;
+    return print;
   }
 
   boolean isRetrying() {
@@ -262,6 +270,7 @@
     // we start replication (instead a new instance, with the same URI, is
     // created and scheduled for a future point in time.)
     //
+    MDC.put(ID_MDC_KEY, IdGenerator.format(id));
     if (!pool.requestRunway(this)) {
       if (!canceled) {
         repLog.info("Rescheduling replication to " + uri
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
index 5af7f21..aeb0519 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
@@ -37,7 +37,8 @@
         LogManager.getLogger(ReplicationQueue.REPLICATION_LOG_NAME);
     replicationLogger.removeAllAppenders();
     replicationLogger.addAppender(systemLog.createAsyncAppender(
-        replicationLogger.getName(), new PatternLayout("[%d] %m%n")));
+        replicationLogger.getName(), new PatternLayout("[%d] [%X{"
+            + PushOne.ID_MDC_KEY + "}] %m%n")));
     replicationLogger.setAdditivity(false);
   }