Search in sources :

Example 21 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class MapReduceBeanTest method testNoResults.

@Test(expected = NoResultsException.class)
public void testNoResults() throws Exception {
    Job mockJob = createMock(Job.class);
    bean.setJob(mockJob);
    MapReduceJobConfiguration cfg = new MapReduceJobConfiguration() {

        @Override
        public final void initializeConfiguration(String jobId, Job job, Map<String, String> runtimeParameters, DatawavePrincipal serverPrincipal) throws Exception {
            throw new NoResultsException(new QueryException(DatawaveErrorCode.NO_RANGES));
        }
    };
    MapReduceConfiguration mrConfig = applicationContext.getBean(MapReduceConfiguration.class);
    mrConfig.getJobConfiguration().clear();
    mrConfig.getJobConfiguration().put("TestJob", cfg);
    // BulkResultsJob uses AccumuloInputFormat, MapReduceJobs.xml in
    // src/test/resources specifies something else
    expect(ctx.getCallerPrincipal()).andReturn(principal);
    replayAll();
    bean.submit("TestJob", "queryId:1243;format:XML");
    verifyAll();
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) QueryException(datawave.webservice.query.exception.QueryException) MapReduceJobConfiguration(datawave.webservice.mr.configuration.MapReduceJobConfiguration) MapReduceConfiguration(datawave.webservice.mr.configuration.MapReduceConfiguration) Job(org.apache.hadoop.mapreduce.Job) Map(java.util.Map) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class ModelBean method listModelNames.

/**
 * Get the names of the models
 *
 * @param modelTableName
 *            name of the table that contains the model
 * @return datawave.webservice.model.ModelList
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 *
 * @HTTP 200 success
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/list")
@GZIP
@Interceptors(ResponseInterceptor.class)
public ModelList listModelNames(@QueryParam("modelTableName") String modelTableName) {
    if (modelTableName == null) {
        modelTableName = defaultModelTableName;
    }
    ModelList response = new ModelList(jqueryUri, dataTablesUri, modelTableName);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String user = p.getName();
    Set<Authorizations> cbAuths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal cp = (DatawavePrincipal) p;
        user = cp.getShortName();
        for (Collection<String> auths : cp.getAuthorizations()) {
            cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
        }
    }
    log.trace(user + " has authorizations " + cbAuths);
    Connector connector = null;
    HashSet<String> modelNames = new HashSet<>();
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
            for (Entry<Key, Value> entry : scanner) {
                String colf = entry.getKey().getColumnFamily().toString();
                if (!RESERVED_COLF_VALUES.contains(colf) && !modelNames.contains(colf)) {
                    String[] parts = colf.split(ModelKeyParser.NULL_BYTE);
                    if (parts.length == 1)
                        modelNames.add(colf);
                    else if (parts.length == 2)
                        modelNames.add(parts[0]);
                }
            }
        }
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.MODEL_NAME_LIST_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    response.setNames(modelNames);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) ModelList(datawave.webservice.model.ModelList) QueryException(datawave.webservice.query.exception.QueryException) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 23 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class ModelBean method getModel.

/**
 * Retrieve the model and all of its mappings
 *
 * @param name
 *            model name
 * @param modelTableName
 *            name of the table that contains the model
 * @return datawave.webservice.model.Model
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 *
 * @HTTP 200 success
 * @HTTP 404 model not found
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/{name}")
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
public datawave.webservice.model.Model getModel(@Required("name") @PathParam("name") String name, @QueryParam("modelTableName") String modelTableName) {
    if (modelTableName == null) {
        modelTableName = defaultModelTableName;
    }
    datawave.webservice.model.Model response = new datawave.webservice.model.Model(jqueryUri, dataTablesUri);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String user = p.getName();
    Set<Authorizations> cbAuths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal cp = (DatawavePrincipal) p;
        user = cp.getShortName();
        for (Collection<String> auths : cp.getAuthorizations()) {
            cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
        }
    }
    log.trace(user + " has authorizations " + cbAuths);
    Connector connector = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
            IteratorSetting cfg = new IteratorSetting(21, "colfRegex", RegExFilter.class.getName());
            cfg.addOption(RegExFilter.COLF_REGEX, "^" + name + "(\\x00.*)?");
            scanner.addScanIterator(cfg);
            for (Entry<Key, Value> entry : scanner) {
                FieldMapping mapping = ModelKeyParser.parseKey(entry.getKey(), cbAuths);
                response.getFields().add(mapping);
            }
        }
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.MODEL_FETCH_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    // return 404 if model not found
    if (response.getFields().isEmpty()) {
        throw new NotFoundException(null, response);
    }
    response.setName(name);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) FieldMapping(datawave.webservice.model.FieldMapping) NotFoundException(datawave.webservice.common.exception.NotFoundException) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) RegExFilter(org.apache.accumulo.core.iterators.user.RegExFilter) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) HashSet(java.util.HashSet) Authorizations(org.apache.accumulo.core.security.Authorizations) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) QueryException(datawave.webservice.query.exception.QueryException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 24 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class BulkResultsJobConfiguration method _initializeConfiguration.

@Override
public void _initializeConfiguration(Job job, Path jobDir, String jobId, Map<String, String> runtimeParameters, DatawavePrincipal serverPrincipal) throws IOException, QueryException {
    String queryId = runtimeParameters.get("queryId");
    SerializationFormat format = SerializationFormat.valueOf(runtimeParameters.get("format"));
    String outputFormatParameter = runtimeParameters.get("outputFormat");
    if (outputFormatParameter != null && outputFormatParameter.equalsIgnoreCase("TEXT")) {
        this.outputFormatClass = TextOutputFormat.class;
    }
    if (runtimeParameters.containsKey("outputTableName"))
        this.tableName = runtimeParameters.get("outputTableName");
    // Initialize the Query
    QueryLogic<?> logic;
    GenericQueryConfiguration queryConfig;
    String base64EncodedQuery;
    Class<? extends Query> queryImplClass;
    Set<Authorizations> runtimeQueryAuthorizations;
    try {
        QuerySettings settings = setupQuery(sid, queryId, principal);
        logic = settings.getLogic();
        queryConfig = settings.getQueryConfig();
        base64EncodedQuery = settings.getBase64EncodedQuery();
        queryImplClass = settings.getQueryImplClass();
        runtimeQueryAuthorizations = settings.getRuntimeQueryAuthorizations();
    } catch (QueryException qe) {
        log.error("Error getting Query for id: " + queryId, qe);
        throw qe;
    } catch (Exception e) {
        log.error("Error setting up Query for id: " + queryId, e);
        throw new QueryException(e);
    }
    // Setup and run the MapReduce job
    try {
        setupJob(job, jobDir, queryConfig, logic, base64EncodedQuery, queryImplClass, runtimeQueryAuthorizations, serverPrincipal);
        if (null == this.tableName) {
            // Setup job for output to HDFS
            // set the mapper
            job.setMapperClass(BulkResultsFileOutputMapper.class);
            job.getConfiguration().set(BulkResultsFileOutputMapper.RESULT_SERIALIZATION_FORMAT, format.name());
            // Setup the output
            job.setOutputFormatClass(outputFormatClass);
            job.setOutputKeyClass(Key.class);
            job.setOutputValueClass(Value.class);
            if (this.outputFormatClass.equals(SequenceFileOutputFormat.class)) {
                SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);
            } else if (this.outputFormatClass.equals(TextOutputFormat.class)) {
                // if we are writing Text output to hdfs, we don't want to write key-tab-value, we want just the value
                // this property gets fetched in the Mapper to skip writing the key
                job.setOutputKeyClass(NullWritable.class);
            }
            job.setNumReduceTasks(0);
            SequenceFileOutputFormat.setOutputPath(job, new Path(this.getResultsDir()));
        } else {
            // Setup job for output to table.
            // set the mapper
            job.setMapperClass(BulkResultsTableOutputMapper.class);
            job.getConfiguration().set(BulkResultsTableOutputMapper.TABLE_NAME, tableName);
            job.getConfiguration().set(BulkResultsFileOutputMapper.RESULT_SERIALIZATION_FORMAT, format.name());
            // Setup the output
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Mutation.class);
            job.setNumReduceTasks(0);
            job.setOutputFormatClass(AccumuloOutputFormat.class);
            AccumuloOutputFormat.setZooKeeperInstance(job, ClientConfiguration.loadDefault().withInstance(instanceName).withZkHosts(zookeepers));
            AccumuloOutputFormat.setConnectorInfo(job, user, new PasswordToken(password));
            AccumuloOutputFormat.setCreateTables(job, true);
            AccumuloOutputFormat.setDefaultTableName(job, tableName);
            // AccumuloOutputFormat.loglevel
            AccumuloOutputFormat.setLogLevel(job, Level.INFO);
            // AccumuloOutputFormat.maxlatency
            // AccumuloOutputFormat.maxmemory
            // AccumuloOutputFormat.writethreads
            AccumuloOutputFormat.setBatchWriterOptions(job, new BatchWriterConfig().setMaxLatency(30, TimeUnit.SECONDS).setMaxMemory(10485760).setMaxWriteThreads(2));
        }
    } catch (WebApplicationException wex) {
        throw wex;
    } catch (Exception e) {
        log.error("Error starting job", e);
        throw new QueryException(DatawaveErrorCode.JOB_STARTING_ERROR, e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Authorizations(org.apache.accumulo.core.security.Authorizations) WebApplicationException(javax.ws.rs.WebApplicationException) NullWritable(org.apache.hadoop.io.NullWritable) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration) WebApplicationException(javax.ws.rs.WebApplicationException) NoResultsException(datawave.webservice.common.exception.NoResultsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) QueryException(datawave.webservice.query.exception.QueryException) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) SerializationFormat(datawave.webservice.mr.bulkresults.map.SerializationFormat) TextOutputFormat(org.apache.hadoop.mapreduce.lib.output.TextOutputFormat) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig)

Example 25 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class BulkResultsJobConfiguration method setupQuery.

private QuerySettings setupQuery(String sid, String queryId, Principal principal) throws Exception {
    Connector connector = null;
    QueryLogic<?> logic = null;
    try {
        // Get the query by the query id
        Query q = getQueryById(queryId);
        if (!sid.equals(q.getOwner()))
            throw new QueryException("This query does not belong to you. expected: " + q.getOwner() + ", value: " + sid, Response.Status.UNAUTHORIZED.getStatusCode());
        // will throw IllegalArgumentException if not defined
        logic = queryFactory.getQueryLogic(q.getQueryLogicName(), principal);
        // Get an accumulo connection
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(logic.getConnectionPriority(), trackingMap);
        // Merge user auths with the auths that they use in the Query
        Set<Authorizations> runtimeQueryAuthorizations = AuthorizationsUtil.getDowngradedAuthorizations(q.getQueryAuthorizations(), principal);
        // Initialize the logic so that the configuration contains all of the iterator options
        GenericQueryConfiguration queryConfig = logic.initialize(connector, q, runtimeQueryAuthorizations);
        String base64EncodedQuery = BulkResultsFileOutputMapper.serializeQuery(q);
        return new QuerySettings(logic, queryConfig, base64EncodedQuery, q.getClass(), runtimeQueryAuthorizations);
    } finally {
        if (null != logic && null != connector)
            connectionFactory.returnConnection(connector);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) QueryException(datawave.webservice.query.exception.QueryException) Authorizations(org.apache.accumulo.core.security.Authorizations) Query(datawave.webservice.query.Query) RunningQuery(datawave.webservice.query.runner.RunningQuery) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration)

Aggregations

QueryException (datawave.webservice.query.exception.QueryException)131 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)63 IOException (java.io.IOException)62 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)57 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)51 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)47 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)45 Produces (javax.ws.rs.Produces)44 NoResultsException (datawave.webservice.common.exception.NoResultsException)40 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)39 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)36 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)36 Interceptors (javax.interceptor.Interceptors)36 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)34 GZIP (org.jboss.resteasy.annotations.GZIP)34 Principal (java.security.Principal)32 WebApplicationException (javax.ws.rs.WebApplicationException)31 BadRequestException (datawave.webservice.common.exception.BadRequestException)29 Path (javax.ws.rs.Path)28 Timed (com.codahale.metrics.annotation.Timed)26