Merge "Support flushing a set of caches at once via REST"

This commit is contained in:
Edwin Kempin
2014-07-04 08:35:33 +00:00
committed by Gerrit Code Review
6 changed files with 225 additions and 92 deletions

View File

@@ -1,33 +0,0 @@
// 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.common.cache.Cache;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
abstract class CacheCommand extends SshCommand {
@Inject
protected DynamicMap<Cache<?, ?>> cacheMap;
protected String cacheNameOf(String plugin, String name) {
if ("gerrit".equals(plugin)) {
return name;
} else {
return plugin + "-" + name;
}
}
}

View File

@@ -14,21 +14,19 @@
package com.google.gerrit.sshd.commands;
import static com.google.gerrit.server.config.PostCaches.Operation.FLUSH;
import static com.google.gerrit.server.config.PostCaches.Operation.FLUSH_ALL;
import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
import com.google.common.cache.Cache;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.config.CacheResource;
import com.google.gerrit.server.config.ConfigResource;
import com.google.gerrit.server.config.FlushCache;
import com.google.gerrit.server.config.ListCaches;
import com.google.gerrit.server.config.ListCaches.OutputFormat;
import com.google.gerrit.server.config.PostCaches;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -41,7 +39,7 @@ import java.util.List;
@RequiresCapability(GlobalCapability.FLUSH_CACHES)
@CommandMetaData(name = "flush-caches", description = "Flush some/all server caches from memory",
runsAt = MASTER_OR_SLAVE)
final class FlushCaches extends CacheCommand {
final class FlushCaches extends SshCommand {
@Option(name = "--cache", usage = "flush named cache", metaVar = "NAME")
private List<String> caches = new ArrayList<>();
@@ -51,9 +49,6 @@ final class FlushCaches extends CacheCommand {
@Option(name = "--list", usage = "list available caches")
private boolean list;
@Inject
private FlushCache flushCache;
@Inject
private Provider<ListCaches> listCaches;
@@ -84,54 +79,25 @@ final class FlushCaches extends CacheCommand {
postCaches.apply(new ConfigResource(),
new PostCaches.Input(FLUSH_ALL));
} else {
List<String> names = cacheNames();
for (String n : caches) {
if (!names.contains(n)) {
throw error("error: cache \"" + n + "\" not recognized");
}
}
doBulkFlush();
postCaches.apply(new ConfigResource(),
new PostCaches.Input(FLUSH, caches));
}
} catch (RestApiException e) {
throw die(e.getMessage());
}
}
private static UnloggedFailure error(final String msg) {
private static UnloggedFailure error(String msg) {
return new UnloggedFailure(1, msg);
}
@SuppressWarnings("unchecked")
private List<String> cacheNames() {
return (List<String>) listCaches.get().setFormat(OutputFormat.LIST)
.apply(new ConfigResource());
}
private void doList() throws RestApiException {
for (String name : cacheNames()) {
private void doList() {
for (String name : (List<String>) listCaches.get()
.setFormat(OutputFormat.LIST).apply(new ConfigResource())) {
stderr.print(name);
stderr.print('\n');
}
stderr.flush();
}
private void doBulkFlush() {
try {
for (DynamicMap.Entry<Cache<?, ?>> e : cacheMap) {
String n = cacheNameOf(e.getPluginName(), e.getExportName());
if (caches.contains(n)) {
try {
flushCache.apply(
new CacheResource(e.getPluginName(), e.getExportName(),
e.getProvider()), null);
} catch (RestApiException err) {
stderr.println("error: cannot flush cache \"" + n + "\": "
+ err.getMessage());
}
}
}
} finally {
stderr.flush();
}
}
}