Search in sources :

Example 21 with Stopwatch

use of com.google.common.base.Stopwatch in project presto by prestodb.

the class Validator method executeQuery.

private QueryResult executeQuery(String url, String username, String password, Query query, String sql, Duration timeout, Map<String, String> sessionProperties) {
    String queryId = null;
    try (Connection connection = DriverManager.getConnection(url, username, password)) {
        trySetConnectionProperties(query, connection);
        for (Map.Entry<String, String> entry : sessionProperties.entrySet()) {
            connection.unwrap(PrestoConnection.class).setSessionProperty(entry.getKey(), entry.getValue());
        }
        try (Statement statement = connection.createStatement()) {
            TimeLimiter limiter = new SimpleTimeLimiter();
            Stopwatch stopwatch = Stopwatch.createStarted();
            Statement limitedStatement = limiter.newProxy(statement, Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
            if (explainOnly) {
                sql = "EXPLAIN " + sql;
            }
            long start = System.nanoTime();
            PrestoStatement prestoStatement = limitedStatement.unwrap(PrestoStatement.class);
            ProgressMonitor progressMonitor = new ProgressMonitor();
            prestoStatement.setProgressMonitor(progressMonitor);
            try {
                boolean isSelectQuery = limitedStatement.execute(sql);
                List<List<Object>> results = null;
                if (isSelectQuery) {
                    results = limiter.callWithTimeout(getResultSetConverter(limitedStatement.getResultSet()), timeout.toMillis() - stopwatch.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, true);
                } else {
                    results = ImmutableList.of(ImmutableList.of(limitedStatement.getLargeUpdateCount()));
                }
                prestoStatement.clearProgressMonitor();
                QueryStats queryStats = progressMonitor.getFinalQueryStats();
                if (queryStats == null) {
                    throw new VerifierException("Cannot fetch query stats");
                }
                Duration queryCpuTime = new Duration(queryStats.getCpuTimeMillis(), TimeUnit.MILLISECONDS);
                queryId = queryStats.getQueryId();
                return new QueryResult(State.SUCCESS, null, nanosSince(start), queryCpuTime, queryId, results);
            } catch (AssertionError e) {
                if (e.getMessage().startsWith("unimplemented type:")) {
                    return new QueryResult(State.INVALID, null, null, null, queryId, ImmutableList.of());
                }
                throw e;
            } catch (SQLException | VerifierException e) {
                throw e;
            } catch (UncheckedTimeoutException e) {
                return new QueryResult(State.TIMEOUT, null, null, null, queryId, ImmutableList.of());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw Throwables.propagate(e);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    } catch (SQLException e) {
        Exception exception = e;
        if (("Error executing query".equals(e.getMessage()) || "Error fetching results".equals(e.getMessage())) && (e.getCause() instanceof Exception)) {
            exception = (Exception) e.getCause();
        }
        State state = isPrestoQueryInvalid(e) ? State.INVALID : State.FAILED;
        return new QueryResult(state, exception, null, null, null, null);
    } catch (VerifierException e) {
        return new QueryResult(State.TOO_MANY_ROWS, e, null, null, null, null);
    }
}
Also used : SQLException(java.sql.SQLException) Stopwatch(com.google.common.base.Stopwatch) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Duration(io.airlift.units.Duration) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) QueryStats(com.facebook.presto.jdbc.QueryStats) State(com.facebook.presto.verifier.QueryResult.State) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Map(java.util.Map) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 22 with Stopwatch

use of com.google.common.base.Stopwatch in project wire by square.

the class WireGenerateSourcesMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    // Add the directory into which generated sources are placed as a compiled source root.
    project.addCompileSourceRoot(generatedSourceDirectory);
    try {
        List<String> directories = protoPaths != null && protoPaths.length > 0 ? Arrays.asList(protoPaths) : Collections.singletonList(protoSourceDirectory);
        List<String> protoFilesList = Arrays.asList(protoFiles);
        Schema schema = loadSchema(directories, protoFilesList);
        Profile profile = loadProfile(schema);
        IdentifierSet identifierSet = identifierSet();
        if (!identifierSet.isEmpty()) {
            schema = retainRoots(identifierSet, schema);
        }
        JavaGenerator javaGenerator = JavaGenerator.get(schema).withAndroid(emitAndroid).withCompact(emitCompact).withProfile(profile);
        for (ProtoFile protoFile : schema.protoFiles()) {
            if (!protoFilesList.isEmpty() && !protoFilesList.contains(protoFile.location().path())) {
                // Don't emit anything for files not explicitly compiled.
                continue;
            }
            for (Type type : protoFile.types()) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                TypeSpec typeSpec = javaGenerator.generateType(type);
                ClassName javaTypeName = javaGenerator.generatedTypeName(type);
                writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly());
                getLog().info(String.format("Generated %s in %s", javaTypeName, stopwatch));
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Wire Plugin: Failure compiling proto sources.", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) JavaGenerator(com.squareup.wire.java.JavaGenerator) IdentifierSet(com.squareup.wire.schema.IdentifierSet) Profile(com.squareup.wire.java.Profile) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Type(com.squareup.wire.schema.Type) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 23 with Stopwatch

use of com.google.common.base.Stopwatch in project wire by square.

the class WireGenerateSourcesMojo method loadSchema.

private Schema loadSchema(List<String> directories, List<String> protos) throws IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    SchemaLoader schemaLoader = new SchemaLoader();
    for (String directory : directories) {
        schemaLoader.addSource(new File(directory));
    }
    for (String proto : protos) {
        schemaLoader.addProto(proto);
    }
    Schema schema = schemaLoader.load();
    getLog().info(String.format("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch));
    return schema;
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) Schema(com.squareup.wire.schema.Schema) Stopwatch(com.google.common.base.Stopwatch) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 24 with Stopwatch

use of com.google.common.base.Stopwatch in project wire by square.

the class WireGenerateSourcesMojo method retainRoots.

private Schema retainRoots(IdentifierSet identifierSet, Schema schema) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    int oldSize = countTypes(schema);
    Schema prunedSchema = schema.prune(identifierSet);
    int newSize = countTypes(prunedSchema);
    for (String rule : identifierSet.unusedIncludes()) {
        getLog().warn(String.format("Unused include: %s", rule));
    }
    for (String rule : identifierSet.unusedExcludes()) {
        getLog().warn(String.format("Unused exclude: %s", rule));
    }
    getLog().info(String.format("Pruned schema from %s types to %s types in %s", oldSize, newSize, stopwatch));
    return prunedSchema;
}
Also used : Schema(com.squareup.wire.schema.Schema) Stopwatch(com.google.common.base.Stopwatch)

Example 25 with Stopwatch

use of com.google.common.base.Stopwatch in project wire by square.

the class CodegenSample method loadSchema.

private Schema loadSchema() throws IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    SchemaLoader loader = new SchemaLoader();
    for (String source : sources) {
        loader.addSource(new File(source));
    }
    for (String proto : protos) {
        loader.addProto(proto);
    }
    Schema schema = loader.load();
    log.info("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch);
    return schema;
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) Schema(com.squareup.wire.schema.Schema) Stopwatch(com.google.common.base.Stopwatch) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)296 IOException (java.io.IOException)75 ArrayList (java.util.ArrayList)29 ExecutionException (java.util.concurrent.ExecutionException)27 Test (org.junit.Test)17 Map (java.util.Map)16 File (java.io.File)15 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)15 Path (org.apache.hadoop.fs.Path)14 HashMap (java.util.HashMap)13 List (java.util.List)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)11 DBCollection (com.mongodb.DBCollection)9 ISE (io.druid.java.util.common.ISE)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 OptionsParser (com.google.devtools.common.options.OptionsParser)8 MongoException (com.mongodb.MongoException)8 Connection (java.sql.Connection)8 CountDownLatch (java.util.concurrent.CountDownLatch)8