kill: Support killing any queued task by interrupting it
We probably don't have enough cancellation points in our code to really kill most tasks before they fully complete. For example, most of the JGit upload-pack and receive-pack code path uses NullProgressMonitor, which unfortunately does not check if the calling thread has been interrupted. Change-Id: I57379ba77d7aefc720421d74771fb65ab6be61fa Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
parent
0f28761ff1
commit
3f1a13afb3
@ -90,6 +90,12 @@ link:cmd-show-queue.html[gerrit show-queue]::
|
||||
link:cmd-replicate.html[gerrit replicate]::
|
||||
Manually trigger replication, to recover a node.
|
||||
|
||||
link:cmd-kill.html[kill]::
|
||||
Kills a scheduled or running task.
|
||||
|
||||
link:cmd-show-queue.html[ps]::
|
||||
Alias for 'gerrit show-queue'.
|
||||
|
||||
GERRIT
|
||||
------
|
||||
Part of link:index.html[Gerrit Code Review]
|
||||
|
29
Documentation/cmd-kill.txt
Normal file
29
Documentation/cmd-kill.txt
Normal file
@ -0,0 +1,29 @@
|
||||
kill
|
||||
====
|
||||
|
||||
NAME
|
||||
----
|
||||
kill - Cancel or abort a background task
|
||||
|
||||
SYNOPSIS
|
||||
--------
|
||||
[verse]
|
||||
'ssh' -p <port> <host> 'kill' <ID> ...
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
Cancels a scheduled task from the queue. If the task has already
|
||||
been started, requests for the task to cancel as soon as it reaches
|
||||
its next cancellation point (which is usually blocking IO).
|
||||
|
||||
ACCESS
|
||||
------
|
||||
Caller must be a member of the privileged 'Administrators' group.
|
||||
|
||||
SCRIPTING
|
||||
---------
|
||||
Intended for interactive use only.
|
||||
|
||||
GERRIT
|
||||
------
|
||||
Part of link:index.html[Gerrit Code Review]
|
@ -33,6 +33,11 @@ OPTIONS
|
||||
DISPLAY
|
||||
-------
|
||||
|
||||
Session::
|
||||
Unique session identifier on this server. Session
|
||||
identifiers have a period of 2\^32-1 and start from a
|
||||
random value.
|
||||
|
||||
Start::
|
||||
Time (local to the server) that this connection started.
|
||||
|
||||
@ -52,34 +57,24 @@ Remote Host::
|
||||
Reverse lookup hostname, or if -n option is used, the remote
|
||||
IP address.
|
||||
|
||||
[ second line ]::
|
||||
Command(s) actively being executed on this connection.
|
||||
With SSH channel multiplexing a single connection can
|
||||
perform multiple commands, or a connection can be idle,
|
||||
performing nothing at all.
|
||||
|
||||
EXAMPLES
|
||||
--------
|
||||
|
||||
With reverse DNS lookup (default):
|
||||
====
|
||||
$ ssh -p 29418 review.example.com gerrit show-connections
|
||||
Start Idle User Remote Host
|
||||
Session Start Idle User Remote Host
|
||||
--------------------------------------------------------------
|
||||
14:02:47 00:00:00 jdoe jdoe-desktop.example.com
|
||||
[ gerrit-show-connections ]
|
||||
|
||||
3abf31e6 20:09:02 00:00:00 jdoe jdoe-desktop.example.com
|
||||
--
|
||||
====
|
||||
|
||||
Without reverse DNS lookup:
|
||||
====
|
||||
$ ssh -p 29418 review.example.com gerrit show-connections -n
|
||||
Start Idle User Remote Host
|
||||
Session Start Idle User Remote Host
|
||||
--------------------------------------------------------------
|
||||
14:02:48 00:00:00 a/1001240 10.0.0.1
|
||||
[ gerrit-show-connections -n ]
|
||||
|
||||
3abf31e6 20:09:02 00:00:00 a/1001240 10.0.0.1
|
||||
--
|
||||
====
|
||||
|
||||
|
@ -8,14 +8,15 @@ gerrit show-queue - Display the background work queues, including replication
|
||||
SYNOPSIS
|
||||
--------
|
||||
[verse]
|
||||
'ssh' -p <port> <host> 'ps'
|
||||
'ssh' -p <port> <host> 'gerrit show-queue'
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
Presents a table of the pending background activity the Gerrit
|
||||
daemon will perform in the near future. Gerrit contains an internal
|
||||
scheduler, similar to cron, that it uses to queue and dispatch both
|
||||
short and long term background activity.
|
||||
Presents a table of the pending activity the Gerrit daemon
|
||||
is currently performing, or will perform in the near future.
|
||||
Gerrit contains an internal scheduler, similar to cron, that it
|
||||
uses to queue and dispatch both short and long term activity.
|
||||
|
||||
Tasks that are completed or cancelled exit the queue very quickly
|
||||
once they enter this state, but it can be possible to observe tasks
|
||||
@ -32,22 +33,26 @@ Intended for interactive use only.
|
||||
DISPLAY
|
||||
-------
|
||||
|
||||
S::
|
||||
Current state of the task. States are:
|
||||
+
|
||||
* `D`: task is complete, but hasn't released its worker yet.
|
||||
* `C`: task has been cancelled, but hasn't left the queue yet.
|
||||
* `R`: task is actively running on a worker thread.
|
||||
* `W`: task is ready to run, waiting for a worker thread.
|
||||
* `S`: task is sleeping until its Start time.
|
||||
|
||||
Start::
|
||||
Time (local to the server) that this task will begin
|
||||
execution. Blank if the task is completed, running, or
|
||||
ready to run but is waiting for a worker thread to become
|
||||
available.
|
||||
|
||||
Task::
|
||||
Unique task identifier on this server. May be passed into
|
||||
link:cmd-kill.html[kill] to cancel or terminate the task.
|
||||
Task identifiers have a period of 2\^32-1, and start from
|
||||
a random value.
|
||||
|
||||
State::
|
||||
If running, blank.
|
||||
+
|
||||
If the task has completed, but has not yet been reaped, 'done'.
|
||||
If the task has been killed, but has not yet halted or been removed
|
||||
from the queue, 'killed'.
|
||||
+
|
||||
If the task is ready to execute but is waiting for an idle thread
|
||||
in its associated thread pool, 'waiting'.
|
||||
+
|
||||
Otherwise the time (local to the server) that this task will begin
|
||||
execution.
|
||||
|
||||
Command::
|
||||
Short text description of the task that will be performed
|
||||
at the given time.
|
||||
|
||||
@ -60,20 +65,14 @@ and `dst2`:
|
||||
|
||||
====
|
||||
$ ssh -p 29418 review.example.com gerrit show-queue
|
||||
S Start Task
|
||||
--------------------------------------------------------------
|
||||
S 14:31:15.435 mirror dst1:/home/git/tools/gerrit.git
|
||||
S 14:31:25.434 mirror dst2:/var/cache/tools/gerrit.git
|
||||
--------------------------------------------------------------
|
||||
Task State Command
|
||||
------------------------------------------------------------------------------
|
||||
7aae09b2 14:31:15.435 mirror dst1:/home/git/tools/gerrit.git
|
||||
9ad09d27 14:31:25.434 mirror dst2:/var/cache/tools/gerrit.git
|
||||
------------------------------------------------------------------------------
|
||||
2 tasks
|
||||
====
|
||||
|
||||
DEFECTS
|
||||
-------
|
||||
There is a small race condition where tasks may disappear from the
|
||||
output of this command while they transition from the ready/waiting
|
||||
(`W`) or sleeping (`S`) state to the running (`R`) state.
|
||||
|
||||
GERRIT
|
||||
------
|
||||
Part of link:index.html[Gerrit Code Review]
|
||||
|
@ -59,6 +59,7 @@ import com.google.gerrit.server.patch.PatchListCacheImpl;
|
||||
import com.google.gerrit.server.patch.PatchSetInfoFactory;
|
||||
import com.google.gerrit.server.project.ProjectCacheImpl;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.server.workflow.FunctionState;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@ -99,6 +100,7 @@ public class GerritGlobalModule extends FactoryModule {
|
||||
bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class).toProvider(
|
||||
GerritPersonIdentProvider.class);
|
||||
|
||||
bind(IdGenerator.class);
|
||||
bind(CachePool.class);
|
||||
install(AccountByEmailCacheImpl.module());
|
||||
install(AccountCacheImpl.module());
|
||||
|
@ -15,14 +15,14 @@
|
||||
package com.google.gerrit.server.git;
|
||||
|
||||
import com.google.gerrit.lifecycle.LifecycleListener;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Delayed;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@ -32,6 +32,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/** Delayed execution of tasks using a background thread pool. */
|
||||
@ -56,8 +57,14 @@ public class WorkQueue {
|
||||
}
|
||||
|
||||
private Executor defaultQueue;
|
||||
private final CopyOnWriteArrayList<Executor> queues =
|
||||
new CopyOnWriteArrayList<Executor>();
|
||||
private final IdGenerator idGenerator;
|
||||
private final CopyOnWriteArrayList<Executor> queues;
|
||||
|
||||
@Inject
|
||||
WorkQueue(final IdGenerator idGenerator) {
|
||||
this.idGenerator = idGenerator;
|
||||
this.queues = new CopyOnWriteArrayList<Executor>();
|
||||
}
|
||||
|
||||
/** Get the default work queue, for miscellaneous tasks. */
|
||||
public synchronized Executor getDefaultQueue() {
|
||||
@ -85,6 +92,23 @@ public class WorkQueue {
|
||||
return r;
|
||||
}
|
||||
|
||||
/** Locate a task by its unique id, null if no task matches. */
|
||||
public Task<?> getTask(final int id) {
|
||||
Task<?> result = null;
|
||||
for (final Executor e : queues) {
|
||||
final Task<?> t = e.getTask(id);
|
||||
if (t != null) {
|
||||
if (result != null) {
|
||||
// Don't return the task if we have a duplicate. Lie instead.
|
||||
return null;
|
||||
} else {
|
||||
result = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
for (final Executor p : queues) {
|
||||
p.shutdown();
|
||||
@ -101,8 +125,8 @@ public class WorkQueue {
|
||||
}
|
||||
|
||||
/** An isolated queue. */
|
||||
public static class Executor extends ScheduledThreadPoolExecutor {
|
||||
private final Set<Task<?>> active = new HashSet<Task<?>>();
|
||||
public class Executor extends ScheduledThreadPoolExecutor {
|
||||
private final ConcurrentHashMap<Integer, Task<?>> all;
|
||||
|
||||
Executor(final int corePoolSize, final String prefix) {
|
||||
super(corePoolSize, new ThreadFactory() {
|
||||
@ -116,12 +140,25 @@ public class WorkQueue {
|
||||
return t;
|
||||
}
|
||||
});
|
||||
|
||||
all = new ConcurrentHashMap<Integer, Task<?>>( //
|
||||
corePoolSize << 1, // table size
|
||||
0.75f, // load factor
|
||||
corePoolSize + 4 // concurrency level
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <V> RunnableScheduledFuture<V> decorateTask(
|
||||
final Runnable runnable, final RunnableScheduledFuture<V> task) {
|
||||
return new Task<V>(runnable, super.decorateTask(runnable, task));
|
||||
final Runnable runnable, RunnableScheduledFuture<V> r) {
|
||||
r = super.decorateTask(runnable, r);
|
||||
for (;;) {
|
||||
final int id = idGenerator.next();
|
||||
final Task<V> task = new Task<V>(runnable, r, this, id);
|
||||
if (all.putIfAbsent(task.getTaskId(), task) == null) {
|
||||
return task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -131,31 +168,30 @@ public class WorkQueue {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforeExecute(Thread t, Runnable r) {
|
||||
super.beforeExecute(t, r);
|
||||
synchronized (active) {
|
||||
active.add((Task<?>) r);
|
||||
}
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
remove((Task<?>) r);
|
||||
super.afterExecute(r, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
super.afterExecute(r, t);
|
||||
synchronized (active) {
|
||||
active.remove(r);
|
||||
}
|
||||
void remove(final Task<?> task) {
|
||||
all.remove(task.getTaskId(), task);
|
||||
}
|
||||
|
||||
Task<?> getTask(final int id) {
|
||||
return all.get(id);
|
||||
}
|
||||
|
||||
void addAllTo(final List<Task<?>> list) {
|
||||
synchronized (active) {
|
||||
list.addAll(active);
|
||||
}
|
||||
for (final Runnable task : getQueue()) { // iterator is thread safe
|
||||
list.add((Task<?>) task);
|
||||
}
|
||||
list.addAll(all.values()); // iterator is thread safe
|
||||
}
|
||||
}
|
||||
|
||||
/** Runnable needing to know it was canceled. */
|
||||
public interface CancelableRunnable extends Runnable {
|
||||
/** Notifies the runnable it was canceled. */
|
||||
public void cancel();
|
||||
}
|
||||
|
||||
/** A wrapper around a scheduled Runnable, as maintained in the queue. */
|
||||
public static class Task<V> implements RunnableScheduledFuture<V> {
|
||||
/**
|
||||
@ -179,25 +215,30 @@ public class WorkQueue {
|
||||
|
||||
private final Runnable runnable;
|
||||
private final RunnableScheduledFuture<V> task;
|
||||
private volatile boolean running;
|
||||
private final Executor executor;
|
||||
private final int taskId;
|
||||
private final AtomicBoolean running;
|
||||
|
||||
Task(Runnable runnable, RunnableScheduledFuture<V> task) {
|
||||
Task(Runnable runnable, RunnableScheduledFuture<V> task, Executor executor,
|
||||
int taskId) {
|
||||
this.runnable = runnable;
|
||||
this.task = task;
|
||||
this.executor = executor;
|
||||
this.taskId = taskId;
|
||||
this.running = new AtomicBoolean();
|
||||
}
|
||||
|
||||
/** Get the Runnable this task executes. */
|
||||
public Runnable getRunnable() {
|
||||
return runnable;
|
||||
public int getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
if (isDone() && !isPeriodic()) {
|
||||
return State.DONE;
|
||||
} else if (isRunning()) {
|
||||
return State.RUNNING;
|
||||
} else if (isCancelled()) {
|
||||
if (isCancelled()) {
|
||||
return State.CANCELLED;
|
||||
} else if (isDone() && !isPeriodic()) {
|
||||
return State.DONE;
|
||||
} else if (running.get()) {
|
||||
return State.RUNNING;
|
||||
}
|
||||
|
||||
final long delay = getDelay(TimeUnit.MILLISECONDS);
|
||||
@ -211,7 +252,24 @@ public class WorkQueue {
|
||||
}
|
||||
|
||||
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||
return task.cancel(mayInterruptIfRunning);
|
||||
if (task.cancel(mayInterruptIfRunning)) {
|
||||
// Tiny abuse of running: if the task needs to know it was
|
||||
// canceled (to clean up resources) and it hasn't started
|
||||
// yet the task's run method won't execute. So we tag it
|
||||
// as running and allow it to clean up. This ensures we do
|
||||
// not invoke cancel twice.
|
||||
//
|
||||
if (runnable instanceof CancelableRunnable
|
||||
&& running.compareAndSet(false, true)) {
|
||||
((CancelableRunnable) runnable).cancel();
|
||||
}
|
||||
executor.remove(this);
|
||||
executor.purge();
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int compareTo(Delayed o) {
|
||||
@ -235,10 +293,6 @@ public class WorkQueue {
|
||||
return task.isCancelled();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return task.isDone();
|
||||
}
|
||||
@ -248,11 +302,14 @@ public class WorkQueue {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
running = true;
|
||||
task.run();
|
||||
} finally {
|
||||
running = false;
|
||||
if (running.compareAndSet(false, true)) {
|
||||
try {
|
||||
task.run();
|
||||
} finally {
|
||||
if (isPeriodic()) {
|
||||
running.set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,89 @@
|
||||
// Copyright (C) 2009 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.util;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/** Simple class to produce 4 billion keys randomly distributed. */
|
||||
@Singleton
|
||||
public class IdGenerator {
|
||||
/** Format an id created by this class as a hex string. */
|
||||
public static String format(int id) {
|
||||
final char[] r = new char[8];
|
||||
for (int p = 7; 0 <= p; p--) {
|
||||
final int h = id & 0xf;
|
||||
r[p] = h < 10 ? (char) ('0' + h) : (char) ('a' + (h - 10));
|
||||
id >>= 4;
|
||||
}
|
||||
return new String(r);
|
||||
}
|
||||
|
||||
private final AtomicInteger gen;
|
||||
|
||||
@Inject
|
||||
IdGenerator() {
|
||||
gen = new AtomicInteger(new Random().nextInt());
|
||||
}
|
||||
|
||||
/** Produce the next identifier. */
|
||||
public int next() {
|
||||
return mix(gen.getAndIncrement());
|
||||
}
|
||||
|
||||
private static final int salt = 0x9e3779b9;
|
||||
|
||||
/** A very simple bit permutation to mask a simple incrementer. */
|
||||
static int mix(final int in) {
|
||||
short v0 = hi16(in);
|
||||
short v1 = lo16(in);
|
||||
v0 += ((v1 << 2) + 0 ^ v1) + (salt ^ (v1 >>> 3)) + 1;
|
||||
v1 += ((v0 << 2) + 2 ^ v0) + (salt ^ (v0 >>> 3)) + 3;
|
||||
return result(v0, v1);
|
||||
}
|
||||
|
||||
/* For testing only. */
|
||||
static int unmix(final int in) {
|
||||
short v0 = hi16(in);
|
||||
short v1 = lo16(in);
|
||||
v1 -= ((v0 << 2) + 2 ^ v0) + (salt ^ (v0 >>> 3)) + 3;
|
||||
v0 -= ((v1 << 2) + 0 ^ v1) + (salt ^ (v1 >>> 3)) + 1;
|
||||
return result(v0, v1);
|
||||
}
|
||||
|
||||
private static short hi16(final int in) {
|
||||
return (short) ( //
|
||||
((in >>> 24 & 0xff)) | //
|
||||
((in >>> 16 & 0xff) << 8) //
|
||||
);
|
||||
}
|
||||
|
||||
private static short lo16(final int in) {
|
||||
return (short) ( //
|
||||
((in >>> 8 & 0xff)) | //
|
||||
((in & 0xff) << 8) //
|
||||
);
|
||||
}
|
||||
|
||||
private static int result(final short v0, final short v1) {
|
||||
return ((v0 & 0xff) << 24) | //
|
||||
(((v0 >>> 8) & 0xff) << 16) | //
|
||||
((v1 & 0xff) << 8) | //
|
||||
((v1 >>> 8) & 0xff);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2009 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.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class IdGeneratorTest extends TestCase {
|
||||
public void test1234() {
|
||||
final HashSet<Integer> seen = new HashSet<Integer>();
|
||||
for (int i = 0; i < 1 << 16; i++) {
|
||||
final int e = IdGenerator.mix(i);
|
||||
assertTrue("no duplicates", seen.add(e));
|
||||
assertEquals("mirror image", i, IdGenerator.unmix(e));
|
||||
}
|
||||
assertEquals(0x801234ab, IdGenerator.unmix(IdGenerator.mix(0x801234ab)));
|
||||
assertEquals(0xc0ffee12, IdGenerator.unmix(IdGenerator.mix(0xc0ffee12)));
|
||||
assertEquals(0xdeadbeef, IdGenerator.unmix(IdGenerator.mix(0xdeadbeef)));
|
||||
assertEquals(0x0b966b11, IdGenerator.unmix(IdGenerator.mix(0x0b966b11)));
|
||||
}
|
||||
|
||||
public void testFormat() {
|
||||
assertEquals("0000000f", IdGenerator.format(0xf));
|
||||
assertEquals("801234ab", IdGenerator.format(0x801234ab));
|
||||
assertEquals("deadbeef", IdGenerator.format(0xdeadbeef));
|
||||
}
|
||||
}
|
@ -14,9 +14,9 @@
|
||||
|
||||
package com.google.gerrit.sshd;
|
||||
|
||||
import com.google.gerrit.reviewdb.Account;
|
||||
import com.google.gerrit.server.RequestCleanup;
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.git.WorkQueue.CancelableRunnable;
|
||||
import com.google.gerrit.server.project.NoSuchChangeException;
|
||||
import com.google.gerrit.server.project.NoSuchProjectException;
|
||||
import com.google.gerrit.sshd.SshScopes.Context;
|
||||
@ -256,12 +256,6 @@ public abstract class BaseCommand implements Command {
|
||||
* command's logic.
|
||||
*/
|
||||
protected synchronized void startThread(final CommandRunnable thunk) {
|
||||
final List<Command> active =
|
||||
SshScopes.getContext().session.getAttribute(SshUtil.ACTIVE);
|
||||
synchronized (active) {
|
||||
active.add(BaseCommand.this);
|
||||
}
|
||||
|
||||
final TaskThunk tt = new TaskThunk(thunk);
|
||||
if (isAdminCommand()) {
|
||||
// Admin commands should not block the main work threads (there
|
||||
@ -374,7 +368,7 @@ public abstract class BaseCommand implements Command {
|
||||
return commandPrefix + " " + commandLine;
|
||||
}
|
||||
|
||||
private final class TaskThunk implements Runnable {
|
||||
private final class TaskThunk implements CancelableRunnable {
|
||||
private final CommandRunnable thunk;
|
||||
private final Context context;
|
||||
|
||||
@ -383,13 +377,23 @@ public abstract class BaseCommand implements Command {
|
||||
this.context = SshScopes.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
try {
|
||||
SshScopes.current.set(context);
|
||||
onExit(15);
|
||||
} finally {
|
||||
SshScopes.current.set(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final Thread thisThread = Thread.currentThread();
|
||||
final String thisName = thisThread.getName();
|
||||
int rc = 0;
|
||||
try {
|
||||
thisThread.setName(toString());
|
||||
thisThread.setName("SSH " + toString());
|
||||
SshScopes.current.set(context);
|
||||
try {
|
||||
thunk.run();
|
||||
@ -412,10 +416,6 @@ public abstract class BaseCommand implements Command {
|
||||
rc = handleError(e);
|
||||
} finally {
|
||||
try {
|
||||
List<Command> active = context.session.getAttribute(SshUtil.ACTIVE);
|
||||
synchronized (active) {
|
||||
active.remove(BaseCommand.this);
|
||||
}
|
||||
onExit(rc);
|
||||
} finally {
|
||||
SshScopes.current.set(null);
|
||||
@ -428,8 +428,7 @@ public abstract class BaseCommand implements Command {
|
||||
public String toString() {
|
||||
final ServerSession session = context.session;
|
||||
final String who = session.getUsername();
|
||||
final Account.Id id = session.getAttribute(SshUtil.CURRENT_ACCOUNT);
|
||||
return "SSH " + getFullCommandLine() + " / " + who + " " + id;
|
||||
return getFullCommandLine() + " (" + who + ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ package com.google.gerrit.sshd;
|
||||
import com.google.gerrit.lifecycle.LifecycleListener;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.Singleton;
|
||||
@ -143,7 +144,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
@Inject
|
||||
SshDaemon(final CommandFactory commandFactory,
|
||||
final PublickeyAuthenticator userAuth,
|
||||
final KeyPairProvider hostKeyProvider,
|
||||
final KeyPairProvider hostKeyProvider, final IdGenerator idGenerator,
|
||||
@GerritServerConfig final Config cfg) {
|
||||
setPort(IANA_SSH_PORT /* never used */);
|
||||
|
||||
@ -178,7 +179,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
|
||||
|
||||
final ServerSession s = (ServerSession) super.createSession(io);
|
||||
s.setAttribute(SshUtil.REMOTE_PEER, io.getRemoteAddress());
|
||||
s.setAttribute(SshUtil.ACTIVE, new ArrayList<Command>(2));
|
||||
s.setAttribute(SshUtil.SESSION_ID, idGenerator.next());
|
||||
s.setAttribute(SshScopes.sessionMap, new HashMap<Key<?>, Object>());
|
||||
return s;
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.sshd.common.KeyPairProvider;
|
||||
import org.apache.sshd.common.Session.AttributeKey;
|
||||
import org.apache.sshd.common.util.Buffer;
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -34,7 +33,6 @@ import java.security.PublicKey;
|
||||
import java.security.interfaces.DSAPublicKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.util.List;
|
||||
|
||||
/** Utilities to support SSH operations. */
|
||||
public class SshUtil {
|
||||
@ -46,9 +44,9 @@ public class SshUtil {
|
||||
public static final AttributeKey<SocketAddress> REMOTE_PEER =
|
||||
new AttributeKey<SocketAddress>();
|
||||
|
||||
/** Server session attribute holding the current commands. */
|
||||
public static final AttributeKey<List<Command>> ACTIVE =
|
||||
new AttributeKey<List<Command>>();
|
||||
/** Server session attribute holding a unique session id. */
|
||||
public static final AttributeKey<Integer> SESSION_ID =
|
||||
new AttributeKey<Integer>();
|
||||
|
||||
/**
|
||||
* Parse a public key into its Java type.
|
||||
|
@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2009 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.sshd.commands;
|
||||
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.git.WorkQueue.Task;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.sshd.AdminCommand;
|
||||
import com.google.gerrit.sshd.BaseCommand;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.apache.sshd.server.Environment;
|
||||
import org.kohsuke.args4j.Argument;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/** Kill a task in the work queue. */
|
||||
@AdminCommand
|
||||
final class AdminKill extends BaseCommand {
|
||||
@Inject
|
||||
private WorkQueue workQueue;
|
||||
|
||||
private final Set<Integer> taskIds = new HashSet<Integer>();
|
||||
|
||||
@Argument(index = 0, multiValued = true, required = true, metaVar = "ID")
|
||||
void addTaskId(final String taskId) {
|
||||
int p = 0;
|
||||
while (p < taskId.length() - 1 && taskId.charAt(p) == '0') {
|
||||
p++;
|
||||
}
|
||||
taskIds.add((int) Long.parseLong(taskId.substring(p), 16));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(final Environment env) {
|
||||
startThread(new CommandRunnable() {
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
parseCommandLine();
|
||||
AdminKill.this.commitMurder();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void commitMurder() {
|
||||
final PrintWriter p = toPrintWriter(err);
|
||||
for (final Integer id : taskIds) {
|
||||
final Task<?> task = workQueue.getTask(id);
|
||||
if (task != null) {
|
||||
task.cancel(true);
|
||||
} else {
|
||||
p.print("kill: " + IdGenerator.format(id) + ": No such task\n");
|
||||
}
|
||||
}
|
||||
p.flush();
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.sshd.commands;
|
||||
|
||||
import com.google.gerrit.reviewdb.Account;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.sshd.AdminCommand;
|
||||
import com.google.gerrit.sshd.BaseCommand;
|
||||
import com.google.gerrit.sshd.SshDaemon;
|
||||
@ -23,7 +24,6 @@ import com.google.inject.Inject;
|
||||
|
||||
import org.apache.mina.core.service.IoAcceptor;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.apache.sshd.server.Environment;
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
import org.kohsuke.args4j.Option;
|
||||
@ -84,33 +84,33 @@ final class AdminShowConnections extends BaseCommand {
|
||||
});
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
p.print(String.format(" %8s %8s %-15s %s\n", "Start", "Idle", "User",
|
||||
"Remote Host"));
|
||||
p.print(String.format("%-8s %8s %8s %-15s %s\n", //
|
||||
"Session", "Start", "Idle", "User", "Remote Host"));
|
||||
p.print("--------------------------------------------------------------\n");
|
||||
for (final IoSession io : list) {
|
||||
ServerSession s = (ServerSession) ServerSession.getSession(io, true);
|
||||
List<Command> active = s != null ? s.getAttribute(SshUtil.ACTIVE) : null;
|
||||
|
||||
final SocketAddress remoteAddress = io.getRemoteAddress();
|
||||
final long start = io.getCreationTime();
|
||||
final long idle = now - io.getLastIoTime();
|
||||
final Integer id = s != null ? s.getAttribute(SshUtil.SESSION_ID) : null;
|
||||
|
||||
p.print(String.format(" %8s %8s %-15.15s %.30s\n", time(now, start),
|
||||
age(idle), username(s), hostname(remoteAddress)));
|
||||
if (active != null) {
|
||||
synchronized (active) {
|
||||
for (final Command cmd : active) {
|
||||
p.print(String.format(" [ %s ]\n", cmd.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
p.print("\n");
|
||||
p.print(String.format("%8s %8s %8s %-15.15s %.30s\n", //
|
||||
id(id), //
|
||||
time(now, start), //
|
||||
age(idle), //
|
||||
username(s), //
|
||||
hostname(remoteAddress)));
|
||||
}
|
||||
p.print("--\n");
|
||||
|
||||
p.flush();
|
||||
}
|
||||
|
||||
private static String id(final Integer id) {
|
||||
return id != null ? IdGenerator.format(id) : "";
|
||||
}
|
||||
|
||||
private static String time(final long now, final long time) {
|
||||
if (time - now < 24 * 60 * 60 * 1000L) {
|
||||
return new SimpleDateFormat("HH:mm:ss").format(new Date(time));
|
||||
|
@ -16,11 +16,13 @@ package com.google.gerrit.sshd.commands;
|
||||
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.git.WorkQueue.Task;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.sshd.AdminCommand;
|
||||
import com.google.gerrit.sshd.BaseCommand;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.apache.sshd.server.Environment;
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -33,13 +35,27 @@ import java.util.concurrent.TimeUnit;
|
||||
/** Display the current work queue. */
|
||||
@AdminCommand
|
||||
final class AdminShowQueue extends BaseCommand {
|
||||
@Option(name = "-w", usage = "display without line width truncation")
|
||||
private boolean wide;
|
||||
|
||||
@Inject
|
||||
private WorkQueue workQueue;
|
||||
|
||||
private PrintWriter p;
|
||||
private int columns = 80;
|
||||
private int taskNameWidth;
|
||||
|
||||
@Override
|
||||
public void start(final Environment env) {
|
||||
String s = env.getEnv().get(Environment.ENV_COLUMNS);
|
||||
if (s != null && !s.isEmpty()) {
|
||||
try {
|
||||
columns = Integer.parseInt(s);
|
||||
} catch (NumberFormatException err) {
|
||||
columns = 80;
|
||||
}
|
||||
}
|
||||
|
||||
startThread(new CommandRunnable() {
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
@ -74,8 +90,12 @@ final class AdminShowQueue extends BaseCommand {
|
||||
}
|
||||
});
|
||||
|
||||
p.print(String.format(" %1s %-12s %s\n", "S", "Start", "Task"));
|
||||
p.print("--------------------------------------------------------------\n");
|
||||
taskNameWidth = wide ? Integer.MAX_VALUE : columns - 8 - 12 - 8 - 4;
|
||||
|
||||
p.print(String.format("%-8s %-12s %-8s %s\n", //
|
||||
"Task", "State", "", "Command"));
|
||||
p.print("----------------------------------------------"
|
||||
+ "--------------------------------\n");
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
for (final Task<?> task : pending) {
|
||||
@ -88,22 +108,27 @@ final class AdminShowQueue extends BaseCommand {
|
||||
case CANCELLED:
|
||||
case RUNNING:
|
||||
case READY:
|
||||
start = "";
|
||||
start = format(state);
|
||||
break;
|
||||
default:
|
||||
start = time(now, delay);
|
||||
break;
|
||||
}
|
||||
|
||||
p.print(String.format(" %1s %12s %s\n", format(state), start,
|
||||
format(task)));
|
||||
p.print(String.format("%8s %-12s %-8s %s\n", //
|
||||
id(task.getTaskId()), start, "", format(task)));
|
||||
}
|
||||
p.print("--------------------------------------------------------------\n");
|
||||
p.print("----------------------------------------------"
|
||||
+ "--------------------------------\n");
|
||||
p.print(" " + pending.size() + " tasks\n");
|
||||
|
||||
p.flush();
|
||||
}
|
||||
|
||||
private static String id(final int id) {
|
||||
return IdGenerator.format(id);
|
||||
}
|
||||
|
||||
private static String time(final long now, final long delay) {
|
||||
final Date when = new Date(now + delay);
|
||||
if (delay < 24 * 60 * 60 * 1000L) {
|
||||
@ -112,24 +137,29 @@ final class AdminShowQueue extends BaseCommand {
|
||||
return new SimpleDateFormat("MMM-dd HH:mm").format(when);
|
||||
}
|
||||
|
||||
private static String format(final Task<?> task) {
|
||||
return task.getRunnable().toString();
|
||||
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) {
|
||||
switch (state) {
|
||||
case DONE:
|
||||
return "D";
|
||||
return "....... done";
|
||||
case CANCELLED:
|
||||
return "C";
|
||||
return "..... killed";
|
||||
case RUNNING:
|
||||
return "R";
|
||||
return "";
|
||||
case READY:
|
||||
return "W";
|
||||
return "waiting ....";
|
||||
case SLEEPING:
|
||||
return "S";
|
||||
return "sleeping";
|
||||
default:
|
||||
return " ";
|
||||
return state.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,8 @@ public class DefaultCommandModule extends CommandModule {
|
||||
command(git, "receive-pack").to(Commands.key(gerrit, "receive-pack"));
|
||||
command(git, "upload-pack").to(Upload.class);
|
||||
|
||||
command("ps").to(AdminShowCaches.class);
|
||||
command("kill").to(AdminKill.class);
|
||||
command("scp").to(ScpCommand.class);
|
||||
|
||||
// Honor the legacy hyphenated forms as aliases for the non-hyphenated forms
|
||||
|
Loading…
x
Reference in New Issue
Block a user