Commit 36099feb authored by Miguel Martinez Pedreira's avatar Miguel Martinez Pedreira
Browse files

Add servlet to Cassandra benchmark to explore cache content

parent 3984a434
......@@ -408,7 +408,7 @@ public class LFN_CSD implements Comparable<LFN_CSD>, CatalogEntity {
return null;
}
dirCache.put(pathAppended, path_id, 20 * 60 * 1000); // 20 minutes
dirCache.put(pathAppended, path_id, 5 * 60 * 1000); // 5 minutes
dirCache_put++;
}
else {
......@@ -811,7 +811,7 @@ public class LFN_CSD implements Comparable<LFN_CSD>, CatalogEntity {
if (type == 'd') {
// pfns = new HashMap<>();
// pfns.put(Integer.valueOf(-1), "");
dirCache.put(this.canonicalName, id, 20 * 60 * 1000); // 20 minutes
dirCache.put(this.canonicalName, id, 5 * 60 * 1000); // 5 minutes
dirCache_put++;
}
......
......@@ -15,6 +15,10 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.servlet.ServletException;
import org.apache.catalina.LifecycleException;
import com.datastax.driver.core.ConsistencyLevel;
import alien.catalogue.LFN;
......@@ -23,6 +27,8 @@ import alien.catalogue.LFN_CSD;
import alien.catalogue.PFN;
import alien.monitoring.Monitor;
import alien.monitoring.MonitorFactory;
import alien.test.cassandra.tomcat.EmbeddedTomcat;
import alien.test.cassandra.tomcat.servlet.LocalCache;
/**
*
......@@ -144,6 +150,11 @@ public class CatalogueTestWhereisGenerated {
*/
static Integer base_for_insert = Integer.valueOf(500000000);
/**
* Tomcat server
*/
static EmbeddedTomcat tomcat = null;
/**
* auto-generated paths
*
......@@ -200,6 +211,8 @@ public class CatalogueTestWhereisGenerated {
if (nargs > 10)
lfntable = args[10];
startLocalCacheTomcat();
System.out.println("Printing output to: out" + logs_suffix);
out = new PrintWriter(new FileOutputStream("out" + logs_suffix));
out.println("Starting: " + new Date());
......@@ -470,4 +483,27 @@ public class CatalogueTestWhereisGenerated {
}
private static void startLocalCacheTomcat() {
try {
tomcat = new EmbeddedTomcat("*");
} catch (final ServletException se) {
System.err.println("Cannot create the Tomcat server: " + se.getMessage());
return;
}
tomcat.addServlet(LocalCache.class.getName(), "/*");
// Start the server
try {
tomcat.start();
} catch (final LifecycleException le) {
System.err.println("Cannot start the Tomcat server: " + le.getMessage());
return;
}
System.out.println("Ready to accept HTTP calls on " + tomcat.getAddress() + ":" + tomcat.getPort());
return;
}
}
......@@ -31,8 +31,6 @@ public class DBCassandra {
if (dbc == null) {
dbc = new DBCassandra();
System.out.println("INSIDE CREATEINSTANCE: " + dbc.toString());
// Create the connection pool
final PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 56, 56).setConnectionsPerHost(HostDistance.REMOTE, 56, 56);
......
package alien.test.cassandra.tomcat;
import java.io.File;
import java.util.logging.LogManager;
import javax.servlet.ServletException;
import org.apache.catalina.Globals;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.ErrorReportValve;
/**
* Configure an embedded Tomcat instance
*
* @author costing
* @since 2017-10-13
*/
public class EmbeddedTomcat extends Tomcat {
static {
System.setProperty(Globals.CATALINA_HOME_PROP, System.getProperty("java.io.tmpdir"));
}
final int debugLevel;
final String address;
final StandardContext ctx;
/**
* @param defaultAddress
* default listening address for the Tomcat server. Either "localhost" (default for testing servers) or "*" for production instances.
* @throws ServletException
*/
public EmbeddedTomcat(final String defaultAddress) throws ServletException {
super();
// This is to disable Tomcat from creating work directories, nothing needs to be compiled on the fly
debugLevel = Options.getIntOption("tomcat.debug", 1);
// Disable all console logging by default
if (debugLevel < 2)
LogManager.getLogManager().reset();
address = Options.getOption("tomcat.address", defaultAddress);
setPort(Options.getIntOption("tomcat.port", 8500));
getConnector().setProperty("address", address);
getConnector().setProperty("maxKeepAliveRequests", String.valueOf(Options.getIntOption("maxKeepAliveRequests", 1000)));
getConnector().setProperty("connectionTimeout", String.valueOf(Options.getIntOption("connectionTimeout", 10000))); // clients should be quick
getConnector().setProperty("disableUploadTimeout", "false");
getConnector().setProperty("connectionUploadTimeout", String.valueOf(Options.getIntOption("connectionTimeout", 300000))); // 5 minutes max to upload an object
// Add a dummy ROOT context
ctx = (StandardContext) addWebapp("", new File(System.getProperty("java.io.tmpdir")).getAbsolutePath());
// disable per context work directories too
ctx.setWorkDir(".");
}
/**
* @param className
* @param mapping
* @return the newly created wrapper around the
*/
public Wrapper addServlet(final String className, final String mapping) {
final Wrapper wrapper = Tomcat.addServlet(ctx, className.substring(className.lastIndexOf('.') + 1), className);
wrapper.addMapping(mapping);
wrapper.setLoadOnStartup(0);
return wrapper;
}
@Override
public void start() throws LifecycleException {
// TODO Auto-generated method stub
super.start();
if (getService().findConnectors()[0].getState() == LifecycleState.FAILED) {
System.err.println("Failed to start the embedded Tomcat listening on " + address + ":" + port + ".");
if (debugLevel < 2)
System.err.println("Set -Dtomcat.debug=2 (or export TOMCAT_DEBUG=2) to see the logging messages from the server.");
throw new LifecycleException("Cannot bind on " + address + ":" + port);
}
final StandardHost host = (StandardHost) getHost();
for (final Valve v : host.getPipeline().getValves())
if (v instanceof ErrorReportValve) {
final ErrorReportValve erv = (ErrorReportValve) v;
erv.setShowServerInfo(false);
}
}
/**
* @return the port for the default connector
*/
public int getPort() {
return port;
}
/**
* Block forever waiting for the server to exit (will never do normally)
*/
public void blockWaiting() {
getServer().await();
}
/**
* @return the address for the default connector
*/
public String getAddress() {
return address;
}
}
package alien.test.cassandra.tomcat;
/**
* @author costing
* @since 2017-09-28
*/
public class Options {
/**
* Search the JVM arguments for the given key falling back to the environment variable (in uppercase and with '_' instead of '.') to extract the value for this variable. If no other option is
* available then return the default value. For example passing key="some.key" would check if '-Dsome.key=value' was passed to JVM or if 'SOME_KEY' is set in the environment.
*
* @param key
* @param defaultValue
* @return the value for this key, from JVM arguments, environment or finally the default value if nothing else is available.
*/
public static String getOption(final String key, final String defaultValue) {
String tmp = System.getProperty(key);
if (tmp != null && tmp.length() > 0)
return tmp;
final String envKey = key.toUpperCase().replace('.', '_');
tmp = System.getenv(envKey);
if (tmp != null && tmp.length() > 0)
return tmp;
return defaultValue;
}
/**
* Similar to {@link #getOption(String, String)} but casting the value to integer before returning it, falling back to the default value if no other integer value is found in either JVM arguments
* or the environment.
*
* @param key
* @param defaultValue
* @return integer value for this key
*/
public static int getIntOption(final String key, final int defaultValue) {
final String value = getOption(key, null);
if (value != null)
try {
return Integer.parseInt(value);
} catch (@SuppressWarnings("unused") final NumberFormatException nfe) {
// ignore
}
return defaultValue;
}
}
package alien.test.cassandra.tomcat.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import alien.catalogue.LFN_CSD;
import lazyj.RequestWrapper;
/**
* Servlet to print local process cache statistics for Cassandra benchmark.
*
*/
@WebServlet("/*")
public class LocalCache extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_LIMIT = 500;
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
@SuppressWarnings("resource")
PrintWriter pw = response.getWriter();
RequestWrapper rw = new RequestWrapper(request);
int limit = rw.geti("limit", DEFAULT_LIMIT);
String path = rw.gets("path", null);
ArrayList<String> keys_array = new ArrayList<>(LFN_CSD.dirCache.getKeys());
if (path != null) {
if (keys_array.contains(path)) {
pw.println(LFN_CSD.dirCache.get(path));
}
else {
pw.println("Entry not found: " + path);
}
}
else {
int counter = 0;
for (int i = keys_array.size() - 1; i >= 0; i--) {
pw.println(keys_array.get(i));
counter++;
if (counter > limit)
break;
}
}
pw.flush();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment