Add ssh command to restart online indexer

In case the online indexer fails, this command allows admin to restart
it without having to restart Gerrit.

Change-Id: I73435de62c357537d173c5eafd6ad60d3b49fc73
This commit is contained in:
Hugo Arès
2015-06-29 16:04:59 -04:00
parent 34100c4ce4
commit 3a593351d6
8 changed files with 171 additions and 12 deletions

View File

@@ -43,7 +43,7 @@ import java.util.List;
import java.util.TreeMap;
@Singleton
class LuceneVersionManager implements LifecycleListener {
public class LuceneVersionManager implements LifecycleListener {
private static final Logger log = LoggerFactory
.getLogger(LuceneVersionManager.class);
@@ -90,6 +90,7 @@ class LuceneVersionManager implements LifecycleListener {
private final LuceneChangeIndex.Factory indexFactory;
private final IndexCollection indexes;
private final OnlineReindexer.Factory reindexerFactory;
private OnlineReindexer reindexer;
@Inject
LuceneVersionManager(
@@ -160,7 +161,37 @@ class LuceneVersionManager implements LifecycleListener {
int latest = write.get(0).version;
if (latest != search.version) {
reindexerFactory.create(latest).start();
reindexer = reindexerFactory.create(latest);
reindexer.start();
}
}
/**
* Start the online reindexer if the current index is not already the latest.
*
* @return true if started, otherwise false.
* @throws ReindexerAlreadyRunningException
*/
public synchronized boolean startReindexer()
throws ReindexerAlreadyRunningException {
validateReindexerNotRunning();
if (!isCurrentIndexVersionLatest()) {
reindexer.start();
return true;
}
return false;
}
private boolean isCurrentIndexVersionLatest() {
return reindexer == null
|| reindexer.getVersion() == indexes.getSearchIndex().getSchema()
.getVersion();
}
private void validateReindexerNotRunning()
throws ReindexerAlreadyRunningException {
if (reindexer != null && reindexer.isRunning()) {
throw new ReindexerAlreadyRunningException();
}
}

View File

@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class OnlineReindexer {
private static final Logger log = LoggerFactory
@@ -42,6 +43,7 @@ public class OnlineReindexer {
private final SiteIndexer batchIndexer;
private final ProjectCache projectCache;
private final int version;
private final AtomicBoolean running = new AtomicBoolean();
@Inject
OnlineReindexer(
@@ -56,15 +58,29 @@ public class OnlineReindexer {
}
public void start() {
Thread t = new Thread() {
@Override
public void run() {
reindex();
}
};
t.setName(String.format("Reindex v%d-v%d",
version(indexes.getSearchIndex()), version));
t.start();
if (running.compareAndSet(false, true)) {
Thread t = new Thread() {
@Override
public void run() {
try {
reindex();
} finally {
running.set(false);
}
}
};
t.setName(String.format("Reindex v%d-v%d",
version(indexes.getSearchIndex()), version));
t.start();
}
}
public boolean isRunning() {
return running.get();
}
public int getVersion() {
return version;
}
private static int version(ChangeIndex i) {

View File

@@ -0,0 +1,24 @@
// 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.lucene;
public class ReindexerAlreadyRunningException extends Exception {
private static final long serialVersionUID = 1L;
public ReindexerAlreadyRunningException() {
super("Reindexer is already running.");
}
}