Search in sources :

Example 16 with QueryException

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

the class ShardIndexQueryTable method setupQuery.

@Override
public void setupQuery(GenericQueryConfiguration genericConfig) throws QueryException, TableNotFoundException, IOException, ExecutionException {
    if (!genericConfig.getClass().getName().equals(ShardIndexQueryConfiguration.class.getName())) {
        throw new QueryException("Did not receive a ShardIndexQueryConfiguration instance!!");
    }
    ShardIndexQueryConfiguration config = (ShardIndexQueryConfiguration) genericConfig;
    final List<Entry<BatchScanner, Boolean>> batchscanners = Lists.newLinkedList();
    for (Entry<String, String> termEntry : config.getNormalizedTerms().entries()) {
        // scan the table
        BatchScanner bs = configureBatchScannerForDiscovery(config, this.scannerFactory, TableName.SHARD_INDEX, Collections.singleton(config.getRangesForTerms().get(termEntry)), Collections.singleton(termEntry.getValue()), Collections.emptySet(), config.getTableName().equals(config.getReverseIndexTableName()), false, Collections.singleton(termEntry.getKey()));
        batchscanners.add(Maps.immutableEntry(bs, false));
    }
    for (Entry<String, String> patternEntry : config.getNormalizedPatterns().entries()) {
        Entry<Range, Boolean> rangeEntry = config.getRangesForPatterns().get(patternEntry);
        String tName = rangeEntry.getValue() ? TableName.SHARD_RINDEX : TableName.SHARD_INDEX;
        // scan the table
        BatchScanner bs = configureBatchScannerForDiscovery(config, this.scannerFactory, tName, Collections.singleton(rangeEntry.getKey()), Collections.emptySet(), Collections.singleton(patternEntry.getValue()), rangeEntry.getValue(), false, Collections.singleton(patternEntry.getKey()));
        batchscanners.add(Maps.immutableEntry(bs, rangeEntry.getValue()));
    }
    final Iterator<Entry<BatchScanner, Boolean>> batchScannerIterator = batchscanners.iterator();
    this.iterator = concat(transform(new CloseableIterator(batchScannerIterator), new Function<Entry<Key, Value>, Iterator<DiscoveredThing>>() {

        DataInputBuffer in = new DataInputBuffer();

        @Override
        public Iterator<DiscoveredThing> apply(Entry<Key, Value> from) {
            Value value = from.getValue();
            in.reset(value.get(), value.getSize());
            ArrayWritable aw = new ArrayWritable(DiscoveredThing.class);
            try {
                aw.readFields(in);
            } catch (IOException e) {
                return null;
            }
            ArrayList<DiscoveredThing> thangs = Lists.newArrayListWithCapacity(aw.get().length);
            for (Writable w : aw.get()) {
                thangs.add((DiscoveredThing) w);
            }
            return thangs.iterator();
        }
    }));
    this.scanner = new ScannerBase() {

        @Override
        public void addScanIterator(IteratorSetting cfg) {
        }

        @Override
        public void clearColumns() {
        }

        @Override
        public void clearScanIterators() {
        }

        @Override
        public void close() {
        }

        @Override
        public Authorizations getAuthorizations() {
            return null;
        }

        @Override
        public void setSamplerConfiguration(SamplerConfiguration samplerConfiguration) {
        }

        @Override
        public SamplerConfiguration getSamplerConfiguration() {
            return null;
        }

        @Override
        public void clearSamplerConfiguration() {
        }

        @Override
        public void setBatchTimeout(long l, TimeUnit timeUnit) {
        }

        @Override
        public long getBatchTimeout(TimeUnit timeUnit) {
            return 0;
        }

        @Override
        public void setClassLoaderContext(String s) {
        }

        @Override
        public void clearClassLoaderContext() {
        }

        @Override
        public String getClassLoaderContext() {
            return null;
        }

        @Override
        public void fetchColumn(Text colFam, Text colQual) {
        }

        @Override
        public void fetchColumn(IteratorSetting.Column column) {
        }

        @Override
        public void fetchColumnFamily(Text col) {
        }

        @Override
        public long getTimeout(TimeUnit timeUnit) {
            return 0;
        }

        @Override
        public Iterator<Entry<Key, Value>> iterator() {
            return null;
        }

        @Override
        public void removeScanIterator(String iteratorName) {
        }

        @Override
        public void setTimeout(long timeOut, TimeUnit timeUnit) {
        }

        @Override
        public void updateScanIteratorOption(String iteratorName, String key, String value) {
        }
    };
}
Also used : DiscoveredThing(datawave.query.discovery.DiscoveredThing) BatchScanner(org.apache.accumulo.core.client.BatchScanner) ArrayList(java.util.ArrayList) SamplerConfiguration(org.apache.accumulo.core.client.sample.SamplerConfiguration) Writable(org.apache.hadoop.io.Writable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) ShardIndexQueryConfiguration(datawave.query.config.ShardIndexQueryConfiguration) Entry(java.util.Map.Entry) ArrayWritable(org.apache.hadoop.io.ArrayWritable) Iterator(java.util.Iterator) DiscoveryIterator(datawave.query.discovery.DiscoveryIterator) TimeUnit(java.util.concurrent.TimeUnit) Authorizations(org.apache.accumulo.core.security.Authorizations) ScannerBase(org.apache.accumulo.core.client.ScannerBase) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) LongRange(org.apache.commons.lang.math.LongRange) Range(org.apache.accumulo.core.data.Range) QueryException(datawave.webservice.query.exception.QueryException) DataInputBuffer(org.apache.hadoop.io.DataInputBuffer) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) Key(org.apache.accumulo.core.data.Key)

Example 17 with QueryException

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

the class MapReduceStatePersisterBean method updateState.

/**
 * Update the state of the Bulk Results entry
 *
 * @param mapReduceJobId
 *            job id
 * @param state
 *            new state
 * @throws QueryException
 *             when zero or more than one result is found for the id
 */
@PermitAll
public void updateState(String mapReduceJobId, MapReduceState state) throws QueryException {
    // We have the mapreduce job id and the new state, but we need to find out which id and sid this relates to
    // so that we can create a new mutation to put into the table.
    List<MapReduceServiceJobIndex> results = null;
    // Find the index entry for the jobid
    Connector c = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        c = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.ADMIN, trackingMap);
        tableCheck(c);
        try (Scanner scanner = ScannerHelper.createScanner(c, INDEX_TABLE_NAME, Collections.singleton(new Authorizations()))) {
            Range range = new Range(mapReduceJobId, mapReduceJobId);
            scanner.setRange(range);
            for (Entry<Key, Value> entry : scanner) {
                if (null == results)
                    results = new ArrayList<>();
                results.add(MapReduceServiceJobIndex.parse(entry.getKey(), state));
            }
        }
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.JOB_ID_LOOKUP_ERROR, e, MessageFormat.format("job_id: {0}", mapReduceJobId));
        log.error(qe);
        throw qe;
    } finally {
        try {
            connectionFactory.returnConnection(c);
        } catch (Exception e) {
            log.error("Error returning connection to pool", e);
        }
    }
    if (null == results)
        throw new NotFoundQueryException(DatawaveErrorCode.NO_QUERY_OBJECT_MATCH);
    if (results.size() > 1)
        throw new NotFoundQueryException(DatawaveErrorCode.TOO_MANY_QUERY_OBJECT_MATCHES);
    else {
        MapReduceServiceJobIndex r = results.get(0);
        // We will insert a new history column in the table
        Mutation m = new Mutation(r.getId());
        m.put(r.getUser(), STATE + NULL + r.getMapReduceJobId(), new Value(r.getState().getBytes()));
        c = null;
        BatchWriter writer = null;
        try {
            Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
            c = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.ADMIN, trackingMap);
            tableCheck(c);
            writer = c.createBatchWriter(TABLE_NAME, new BatchWriterConfig().setMaxLatency(10, TimeUnit.SECONDS).setMaxMemory(10240L).setMaxWriteThreads(1));
            writer.addMutation(m);
            writer.flush();
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            QueryException qe = new QueryException(DatawaveErrorCode.JOB_STATE_UPDATE_ERROR, e, MessageFormat.format("job_id: {0}", mapReduceJobId));
            log.error(qe);
            throw qe;
        } finally {
            try {
                if (null != writer)
                    writer.close();
                connectionFactory.returnConnection(c);
            } catch (Exception e) {
                log.error("Error creating query", e);
            }
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) ArrayList(java.util.ArrayList) Range(org.apache.accumulo.core.data.Range) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) QueryException(datawave.webservice.query.exception.QueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) Mutation(org.apache.accumulo.core.data.Mutation) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Key(org.apache.accumulo.core.data.Key) PermitAll(javax.annotation.security.PermitAll)

Example 18 with QueryException

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

the class MapReduceStatePersisterBean method find.

/**
 * Returns all MapReduce jobs for the current user
 *
 * @return list of map reduce information
 */
public MapReduceInfoResponseList find() {
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String sid = p.getName();
    Set<Authorizations> auths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        sid = dp.getShortName();
        for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
    }
    log.trace(sid + " has authorizations " + auths);
    MapReduceInfoResponseList result = new MapReduceInfoResponseList();
    Connector c = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        c = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.ADMIN, trackingMap);
        tableCheck(c);
        try (Scanner scanner = ScannerHelper.createScanner(c, TABLE_NAME, auths)) {
            scanner.fetchColumnFamily(new Text(sid));
            // We need to create a response for each job
            String previousRow = sid;
            Map<Key, Value> batch = new HashMap<>();
            for (Entry<Key, Value> entry : scanner) {
                if (!previousRow.equals(entry.getKey().getRow().toString()) && !batch.isEmpty()) {
                    MapReduceInfoResponse response = populateResponse(batch.entrySet());
                    if (null != response)
                        result.getResults().add(response);
                    batch.clear();
                } else {
                    batch.put(entry.getKey(), entry.getValue());
                }
                previousRow = entry.getKey().getRow().toString();
            }
            if (!batch.isEmpty()) {
                MapReduceInfoResponse response = populateResponse(batch.entrySet());
                if (null != response)
                    result.getResults().add(response);
                batch.clear();
            }
            return result;
        }
    } catch (IOException ioe) {
        QueryException qe = new QueryException(DatawaveErrorCode.RESPONSE_POPULATION_ERROR, ioe);
        log.error(qe);
        result.addException(qe);
        return result;
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_SETUP_ERROR, e);
        log.error(qe);
        result.addException(qe.getBottomQueryException());
        return result;
    } finally {
        try {
            connectionFactory.returnConnection(c);
        } catch (Exception e) {
            log.error("Error returning connection to connection pool", e);
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) HashMap(java.util.HashMap) Text(org.apache.hadoop.io.Text) MapReduceInfoResponseList(datawave.webservice.results.mr.MapReduceInfoResponseList) IOException(java.io.IOException) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) QueryException(datawave.webservice.query.exception.QueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) MapReduceInfoResponse(datawave.webservice.results.mr.MapReduceInfoResponse) Value(org.apache.accumulo.core.data.Value) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet)

Example 19 with QueryException

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

the class MapReduceStatePersisterBean method findById.

/**
 * Information for a specific map reduce id
 *
 * @param id
 *            map reduce id
 * @return list of map reduce information
 */
public MapReduceInfoResponseList findById(String id) {
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String sid = p.getName();
    Set<Authorizations> auths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        sid = dp.getShortName();
        for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
    }
    log.trace(sid + " has authorizations " + auths);
    MapReduceInfoResponseList result = new MapReduceInfoResponseList();
    Connector c = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        c = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.ADMIN, trackingMap);
        tableCheck(c);
        try (Scanner scanner = ScannerHelper.createScanner(c, TABLE_NAME, auths)) {
            Range range = new Range(id);
            scanner.setRange(range);
            scanner.fetchColumnFamily(new Text(sid));
            MapReduceInfoResponse response = populateResponse(scanner);
            if (null != response)
                result.getResults().add(response);
            return result;
        }
    } catch (IOException ioe) {
        QueryException qe = new QueryException(DatawaveErrorCode.RESPONSE_POPULATION_ERROR, ioe);
        log.error(qe);
        result.addException(qe);
        return result;
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_SETUP_ERROR, e);
        log.error(qe);
        result.addException(qe.getBottomQueryException());
        return result;
    } finally {
        try {
            connectionFactory.returnConnection(c);
        } catch (Exception e) {
            log.error("Error returning connection to connection pool", e);
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) Text(org.apache.hadoop.io.Text) MapReduceInfoResponseList(datawave.webservice.results.mr.MapReduceInfoResponseList) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) QueryException(datawave.webservice.query.exception.QueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) MapReduceInfoResponse(datawave.webservice.results.mr.MapReduceInfoResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) HashSet(java.util.HashSet)

Example 20 with QueryException

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

the class MapReduceStatePersisterBean method remove.

/**
 * Removes Bulk Results information and related directory in HDFS for the given job id.
 *
 * @param id
 *            bulk results id
 */
public void remove(String id) throws QueryException {
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String sid = p.getName();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        sid = dp.getShortName();
    }
    MapReduceInfoResponseList results = findById(id);
    if (null == results)
        throw new NotFoundQueryException(DatawaveErrorCode.NO_QUERY_OBJECT_MATCH);
    if (results.getResults().size() > 1)
        throw new NotFoundQueryException(DatawaveErrorCode.TOO_MANY_QUERY_OBJECT_MATCHES);
    else {
        MapReduceInfoResponse r = results.getResults().get(0);
        List<Mutation> indexEntries = new ArrayList<>();
        Mutation m = new Mutation(r.getId());
        m.putDelete(sid, WORKING_DIRECTORY);
        m.putDelete(sid, HDFS);
        m.putDelete(sid, JT);
        m.putDelete(sid, NAME);
        m.putDelete(sid, RESULTS_LOCATION);
        m.putDelete(sid, PARAMS);
        for (JobExecution job : r.getJobExecutions()) {
            m.putDelete(sid, STATE + NULL + job.getMapReduceJobId());
            Mutation i = new Mutation(job.getMapReduceJobId());
            i.putDelete(sid, r.getId());
            indexEntries.add(i);
        }
        Connector c = null;
        try {
            Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
            c = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.ADMIN, trackingMap);
            tableCheck(c);
            // using BatchWriter instead of MultiTableBatchWriter because Mock CB does not support
            // MultiTableBatchWriter
            BatchWriterConfig bwCfg = new BatchWriterConfig().setMaxLatency(10, TimeUnit.SECONDS).setMaxMemory(10240L).setMaxWriteThreads(1);
            try (BatchWriter tableWriter = c.createBatchWriter(TABLE_NAME, bwCfg);
                BatchWriter indexWriter = c.createBatchWriter(INDEX_TABLE_NAME, bwCfg)) {
                tableWriter.addMutation(m);
                for (Mutation i : indexEntries) indexWriter.addMutation(i);
            }
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            QueryException qe = new QueryException(DatawaveErrorCode.JOB_STATE_UPDATE_ERROR, e, MessageFormat.format("job_id: {0}", id));
            log.error(qe);
            throw new QueryException(qe);
        } finally {
            try {
                connectionFactory.returnConnection(c);
            } catch (Exception e) {
                log.error("Error creating query", e);
            }
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) ArrayList(java.util.ArrayList) MapReduceInfoResponseList(datawave.webservice.results.mr.MapReduceInfoResponseList) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) QueryException(datawave.webservice.query.exception.QueryException) JobExecution(datawave.webservice.results.mr.JobExecution) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) MapReduceInfoResponse(datawave.webservice.results.mr.MapReduceInfoResponse) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) Mutation(org.apache.accumulo.core.data.Mutation) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal)

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