Merge branch 'stable-2.8'

* stable-2.8:
  Bugfix: Changing Task state breaks comparator in ShowQueue
This commit is contained in:
Shawn Pearce
2013-12-07 09:51:51 -08:00
3 changed files with 124 additions and 45 deletions

View File

@@ -0,0 +1,19 @@
// Copyright (C) 2013 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.git;
public interface TaskInfoFactory<T> {
T getTaskInfo(WorkQueue.Task<?> task);
}

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.git; package com.google.gerrit.server.git;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.events.LifecycleListener; import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.lifecycle.LifecycleModule; import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.reviewdb.client.Project.NameKey; import com.google.gerrit.reviewdb.client.Project.NameKey;
@@ -26,6 +27,7 @@ import org.slf4j.LoggerFactory;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@@ -116,6 +118,16 @@ public class WorkQueue {
return r; return r;
} }
public <T> List<T> getTaskInfos(TaskInfoFactory<T> factory) {
List<T> taskInfos = Lists.newArrayList();
for (Executor exe : queues) {
for (Task<?> task : exe.getTasks()) {
taskInfos.add(factory.getTaskInfo(task));
}
}
return taskInfos;
}
/** Locate a task by its unique id, null if no task matches. */ /** Locate a task by its unique id, null if no task matches. */
public Task<?> getTask(final int id) { public Task<?> getTask(final int id) {
Task<?> result = null; Task<?> result = null;
@@ -187,7 +199,7 @@ public class WorkQueue {
Task<V> task; Task<V> task;
if (runnable instanceof ProjectRunnable) { if (runnable instanceof ProjectRunnable) {
task = new ProjectTask<V>((ProjectRunnable)runnable, r, this, id); task = new ProjectTask<V>((ProjectRunnable) runnable, r, this, id);
} else { } else {
task = new Task<V>(runnable, r, this, id); task = new Task<V>(runnable, r, this, id);
} }
@@ -215,6 +227,10 @@ public class WorkQueue {
void addAllTo(final List<Task<?>> list) { void addAllTo(final List<Task<?>> list) {
list.addAll(all.values()); // iterator is thread safe list.addAll(all.values()); // iterator is thread safe
} }
Collection<Task<?>> getTasks() {
return all.values();
}
} }
/** Runnable needing to know it was canceled. */ /** Runnable needing to know it was canceled. */
@@ -358,8 +374,9 @@ public class WorkQueue {
} }
} }
/** Same as Task class, but with a reference to ProjectRunnable, used to retrieve /**
* the project name from the operation queued * Same as Task class, but with a reference to ProjectRunnable, used to
* retrieve the project name from the operation queued
**/ **/
public static class ProjectTask<V> extends Task<V> implements ProjectRunnable { public static class ProjectTask<V> extends Task<V> implements ProjectRunnable {

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.sshd.commands;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.git.TaskInfoFactory;
import com.google.gerrit.server.git.WorkQueue; import com.google.gerrit.server.git.WorkQueue;
import com.google.gerrit.server.git.WorkQueue.ProjectTask; import com.google.gerrit.server.git.WorkQueue.ProjectTask;
import com.google.gerrit.server.git.WorkQueue.Task; import com.google.gerrit.server.git.WorkQueue.Task;
@@ -73,29 +74,8 @@ final class ShowQueue extends SshCommand {
@Override @Override
protected void run() { protected void run() {
final List<Task<?>> pending = workQueue.getTasks();
Collections.sort(pending, new Comparator<Task<?>>() {
public int compare(Task<?> a, Task<?> b) {
final Task.State aState = a.getState();
final Task.State bState = b.getState();
if (aState != bState) {
return aState.ordinal() - bState.ordinal();
}
final long aDelay = a.getDelay(TimeUnit.MILLISECONDS);
final long bDelay = b.getDelay(TimeUnit.MILLISECONDS);
if (aDelay < bDelay) {
return -1;
} else if (aDelay > bDelay) {
return 1;
}
return format(a).compareTo(format(b));
}
});
taskNameWidth = wide ? Integer.MAX_VALUE : columns - 8 - 12 - 12 - 4 - 4; taskNameWidth = wide ? Integer.MAX_VALUE : columns - 8 - 12 - 12 - 4 - 4;
final List<QueueTaskInfo> pending = getSortedTaskInfoList();
stdout.print(String.format("%-8s %-12s %-12s %-4s %s\n", // stdout.print(String.format("%-8s %-12s %-12s %-4s %s\n", //
"Task", "State", "StartTime", "", "Command")); "Task", "State", "StartTime", "", "Command"));
@@ -106,9 +86,9 @@ final class ShowQueue extends SshCommand {
final long now = TimeUtil.nowMs(); final long now = TimeUtil.nowMs();
final boolean viewAll = currentUser.getCapabilities().canViewQueue(); final boolean viewAll = currentUser.getCapabilities().canViewQueue();
for (final Task<?> task : pending) { for (final QueueTaskInfo taskInfo : pending) {
final long delay = task.getDelay(TimeUnit.MILLISECONDS); final long delay = taskInfo.delayMillis;
final Task.State state = task.getState(); final Task.State state = taskInfo.state;
final String start; final String start;
switch (state) { switch (state) {
@@ -132,11 +112,9 @@ final class ShowQueue extends SshCommand {
String remoteName = null; String remoteName = null;
if (!viewAll) { if (!viewAll) {
if (task instanceof ProjectTask<?>) { projectName = taskInfo.getProjectNameKey();
projectName = ((ProjectTask<?>)task).getProjectNameKey(); remoteName = taskInfo.getRemoteName();
remoteName = ((ProjectTask<?>)task).getRemoteName(); hasCustomizedPrint = taskInfo.hasCustomizedPrint();
hasCustomizedPrint = ((ProjectTask<?>)task).hasCustomizedPrint();
}
ProjectState e = null; ProjectState e = null;
if (projectName != null) { if (projectName != null) {
@@ -150,12 +128,13 @@ final class ShowQueue extends SshCommand {
} }
} }
String startTime = startTime(task.getStartTime()); String startTime = startTime(taskInfo.getStartTime());
// Shows information about tasks depending on the user rights // Shows information about tasks depending on the user rights
if (viewAll || (!hasCustomizedPrint && regularUserCanSee)) { if (viewAll || (!hasCustomizedPrint && regularUserCanSee)) {
stdout.print(String.format("%8s %-12s %-12s %-4s %s\n", // stdout.print(String.format("%8s %-12s %-12s %-4s %s\n", //
id(task.getTaskId()), start, startTime, "", format(task))); id(taskInfo.getTaskId()), start, startTime, "",
taskInfo.getTaskString(taskNameWidth)));
} else if (regularUserCanSee) { } else if (regularUserCanSee) {
if (remoteName == null) { if (remoteName == null) {
remoteName = projectName.get(); remoteName = projectName.get();
@@ -164,7 +143,7 @@ final class ShowQueue extends SshCommand {
} }
stdout.print(String.format("%8s %-12s %-4s %s\n", // stdout.print(String.format("%8s %-12s %-4s %s\n", //
id(task.getTaskId()), start, startTime, remoteName)); id(taskInfo.getTaskId()), start, startTime, remoteName));
} }
} }
stdout.print("----------------------------------------------" stdout.print("----------------------------------------------"
@@ -177,6 +156,33 @@ final class ShowQueue extends SshCommand {
stdout.print(" " + numberOfPendingTasks + " tasks\n"); stdout.print(" " + numberOfPendingTasks + " tasks\n");
} }
private List<QueueTaskInfo> getSortedTaskInfoList() {
final List<QueueTaskInfo> taskInfos =
workQueue.getTaskInfos(new TaskInfoFactory<QueueTaskInfo>() {
@Override
public QueueTaskInfo getTaskInfo(Task<?> task) {
return new QueueTaskInfo(task);
}
});
Collections.sort(taskInfos, new Comparator<QueueTaskInfo>() {
@Override
public int compare(QueueTaskInfo a, QueueTaskInfo b) {
if (a.state != b.state) {
return a.state.ordinal() - b.state.ordinal();
}
int cmp = Long.signum(a.delayMillis - b.delayMillis);
if (cmp != 0) {
return cmp;
}
return a.getTaskString(taskNameWidth)
.compareTo(b.getTaskString(taskNameWidth));
}
});
return taskInfos;
}
private static String id(final int id) { private static String id(final int id) {
return IdGenerator.format(id); return IdGenerator.format(id);
} }
@@ -197,15 +203,6 @@ final class ShowQueue extends SshCommand {
return new SimpleDateFormat("MMM-dd HH:mm").format(when); return new SimpleDateFormat("MMM-dd HH:mm").format(when);
} }
private String format(final Task<?> task) {
String s = task.toString();
if (s.length() < taskNameWidth) {
return s;
} else {
return s.substring(0, taskNameWidth);
}
}
private static String format(final Task.State state) { private static String format(final Task.State state) {
switch (state) { switch (state) {
case DONE: case DONE:
@@ -222,4 +219,50 @@ final class ShowQueue extends SshCommand {
return state.toString(); return state.toString();
} }
} }
private static class QueueTaskInfo {
private final long delayMillis;
private final Task.State state;
private final Task<?> task;
QueueTaskInfo(Task<?> task) {
this.task = task;
this.delayMillis = task.getDelay(TimeUnit.MILLISECONDS);
this.state = task.getState();
}
String getRemoteName() {
if (task instanceof ProjectTask) {
return ((ProjectTask<?>) task).getRemoteName();
}
return null;
}
Project.NameKey getProjectNameKey() {
if (task instanceof ProjectTask<?>) {
return ((ProjectTask<?>) task).getProjectNameKey();
}
return null;
}
boolean hasCustomizedPrint() {
if (task instanceof ProjectTask<?>) {
return ((ProjectTask<?>) task).hasCustomizedPrint();
}
return false;
}
int getTaskId() {
return task.getTaskId();
}
Date getStartTime() {
return task.getStartTime();
}
String getTaskString(int maxLength) {
String s = task.toString();
return s.length() < maxLength ? s : s.substring(0, maxLength);
}
}
} }