use of io.airlift.event.client.EventClient in project presto by prestodb.
the class PrestoVerifier method run.
public int run(String[] args) throws Exception {
if (args.length > 0) {
System.setProperty("config", args[0]);
}
ImmutableList.Builder<Module> builder = ImmutableList.<Module>builder().add(new PrestoVerifierModule()).addAll(getAdditionalModules());
Bootstrap app = new Bootstrap(builder.build());
Injector injector = app.strictConfig().initialize();
try {
VerifierConfig config = injector.getInstance(VerifierConfig.class);
injector.injectMembers(this);
Set<String> supportedEventClients = injector.getInstance(Key.get(new TypeLiteral<Set<String>>() {
}, Names.named(SUPPORTED_EVENT_CLIENTS)));
for (String clientType : config.getEventClients()) {
checkArgument(supportedEventClients.contains(clientType), "Unsupported event client: %s", clientType);
}
Set<EventClient> eventClients = injector.getInstance(Key.get(new TypeLiteral<Set<EventClient>>() {
}));
VerifierDao dao = new DBI(config.getQueryDatabase()).onDemand(VerifierDao.class);
ImmutableList.Builder<QueryPair> queriesBuilder = ImmutableList.builder();
for (String suite : config.getSuites()) {
queriesBuilder.addAll(dao.getQueriesBySuite(suite, config.getMaxQueries()));
}
List<QueryPair> queries = queriesBuilder.build();
queries = applyOverrides(config, queries);
queries = filterQueryTypes(new SqlParser(getParserOptions()), config, queries);
queries = filterQueries(queries);
if (config.getShadowWrites()) {
Sets.SetView<QueryType> allowedTypes = Sets.union(config.getTestQueryTypes(), config.getControlQueryTypes());
checkArgument(!Sets.intersection(allowedTypes, ImmutableSet.of(CREATE, MODIFY)).isEmpty(), "CREATE or MODIFY queries must be allowed in test or control to use write shadowing");
queries = rewriteQueries(new SqlParser(getParserOptions()), config, queries);
}
// Load jdbc drivers if needed
if (config.getAdditionalJdbcDriverPath() != null) {
List<URL> urlList = getUrls(config.getAdditionalJdbcDriverPath());
URL[] urls = new URL[urlList.size()];
urlList.toArray(urls);
if (config.getTestJdbcDriverName() != null) {
loadJdbcDriver(urls, config.getTestJdbcDriverName());
}
if (config.getControlJdbcDriverName() != null) {
loadJdbcDriver(urls, config.getControlJdbcDriverName());
}
}
// TODO: construct this with Guice
Verifier verifier = new Verifier(System.out, config, eventClients);
return verifier.run(queries);
} finally {
injector.getInstance(LifeCycleManager.class).stop();
}
}
use of io.airlift.event.client.EventClient in project presto by prestodb.
the class Verifier method run.
// Returns number of failed queries
public int run(List<QueryPair> queries) throws InterruptedException {
ExecutorService executor = newFixedThreadPool(threadCount);
CompletionService<Validator> completionService = new ExecutorCompletionService<>(executor);
int totalQueries = queries.size() * config.getSuiteRepetitions();
log.info("Total Queries: %d", totalQueries);
log.info("Whitelisted Queries: %s", Joiner.on(',').join(whitelist));
int queriesSubmitted = 0;
for (int i = 0; i < config.getSuiteRepetitions(); i++) {
for (QueryPair query : queries) {
for (int j = 0; j < config.getQueryRepetitions(); j++) {
// If a whitelist exists, only run the tests on the whitelist
if (!whitelist.isEmpty() && !whitelist.contains(query.getName())) {
log.debug("Query %s is not whitelisted", query.getName());
continue;
}
if (blacklist.contains(query.getName())) {
log.debug("Query %s is blacklisted", query.getName());
continue;
}
Validator validator = new Validator(config.getControlGateway(), config.getTestGateway(), config.getControlTimeout(), config.getTestTimeout(), config.getMaxRowCount(), config.isExplainOnly(), config.getDoublePrecision(), isCheckCorrectness(query), true, config.isVerboseResultsComparison(), config.getControlTeardownRetries(), config.getTestTeardownRetries(), query);
completionService.submit(validator::valid, validator);
queriesSubmitted++;
}
}
}
log.info("Allowed Queries: %d", queriesSubmitted);
log.info("Skipped Queries: %d", (totalQueries - queriesSubmitted));
log.info("---------------------");
executor.shutdown();
int total = 0;
int valid = 0;
int failed = 0;
int skipped = 0;
double lastProgress = 0;
while (total < queriesSubmitted) {
total++;
Validator validator = takeUnchecked(completionService);
if (validator.isSkipped()) {
if (!config.isQuiet()) {
log.warn("%s", validator.getSkippedMessage());
}
skipped++;
continue;
}
if (validator.valid()) {
valid++;
} else {
failed++;
}
for (EventClient eventClient : eventClients) {
eventClient.post(buildEvent(validator));
}
double progress = (((double) total) / totalQueries) * 100;
if (!config.isQuiet() || (progress - lastProgress) > 1) {
log.info("Progress: %s valid, %s failed, %s skipped, %.2f%% done", valid, failed, skipped, progress);
lastProgress = progress;
}
}
log.info("Results: %s / %s (%s skipped)", valid, failed, skipped);
log.info("");
for (EventClient eventClient : eventClients) {
if (eventClient instanceof Closeable) {
try {
((Closeable) eventClient).close();
} catch (IOException ignored) {
}
log.info("");
}
}
return failed;
}
use of io.airlift.event.client.EventClient in project airlift by airlift.
the class TestHttpServerModule method testHttpRequestEvent.
@Test
public void testHttpRequestEvent() throws Exception {
Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
SingleUseEventClient eventClient = new SingleUseEventClient();
Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new InMemoryEventModule(), new TraceTokenModule(), binder -> newSetBinder(binder, EventClient.class).addBinding().toInstance(eventClient), binder -> binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON));
Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
HttpServer server = injector.getInstance(HttpServer.class);
server.start();
URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
String userAgent = "my-user-agent";
String referrer = "http://www.google.com";
String token = "this is a trace token";
String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
String requestContentType = "request/type";
int responseCode = 555;
String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
String responseContentType = "response/type";
echoServlet.responseBody = responseBody;
echoServlet.responseStatusCode = responseCode;
echoServlet.responseHeaders.put("Content-Type", responseContentType);
long beforeRequest = System.currentTimeMillis();
long afterRequest;
HttpRequestEvent event;
try (JettyHttpClient client = new JettyHttpClient()) {
// test servlet bound correctly
StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
afterRequest = System.currentTimeMillis();
assertEquals(response.getStatusCode(), responseCode);
assertEquals(response.getBody(), responseBody);
assertEquals(response.getHeader("Content-Type"), responseContentType);
event = (HttpRequestEvent) eventClient.getEvent().get(10, TimeUnit.SECONDS);
} finally {
server.stop();
}
assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
assertEquals(event.getProtocol(), "http");
assertEquals(event.getMethod(), "POST");
assertEquals(event.getRequestUri(), requestUri.getPath());
assertNull(event.getUser());
assertEquals(event.getAgent(), userAgent);
assertEquals(event.getReferrer(), referrer);
assertEquals(event.getTraceToken(), token);
assertEquals(event.getRequestSize(), requestBody.length());
assertEquals(event.getRequestContentType(), requestContentType);
assertEquals(event.getResponseSize(), responseBody.length());
assertEquals(event.getResponseCode(), responseCode);
assertEquals(event.getResponseContentType(), responseContentType);
assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
assertNotNull(event.getTimeToFirstByte());
assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
Aggregations