Search in sources :

Example 6 with KeyParser

use of org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser in project hadoop by apache.

the class LeveldbTimelineStore method addPrimaryFilter.

/**
   * Parses the primary filter from the given key at the given offset and
   * adds it to the given entity.
   */
private static void addPrimaryFilter(TimelineEntity entity, byte[] key, int offset) throws IOException {
    KeyParser kp = new KeyParser(key, offset);
    String name = kp.getNextString();
    Object value = GenericObjectMapper.read(key, kp.getOffset());
    entity.addPrimaryFilter(name, value);
}
Also used : KeyParser(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser)

Example 7 with KeyParser

use of org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser in project hadoop by apache.

the class LeveldbTimelineStore method getEntityTypes.

@VisibleForTesting
List<String> getEntityTypes() throws IOException {
    LeveldbIterator iterator = null;
    try {
        iterator = getDbIterator(false);
        List<String> entityTypes = new ArrayList<String>();
        iterator.seek(ENTITY_ENTRY_PREFIX);
        while (iterator.hasNext()) {
            byte[] key = iterator.peekNext().getKey();
            if (key[0] != ENTITY_ENTRY_PREFIX[0]) {
                break;
            }
            KeyParser kp = new KeyParser(key, ENTITY_ENTRY_PREFIX.length);
            String entityType = kp.getNextString();
            entityTypes.add(entityType);
            byte[] lookupKey = KeyBuilder.newInstance().add(ENTITY_ENTRY_PREFIX).add(entityType).getBytesForLookup();
            if (lookupKey[lookupKey.length - 1] != 0x0) {
                throw new IOException("Found unexpected end byte in lookup key");
            }
            lookupKey[lookupKey.length - 1] = 0x1;
            iterator.seek(lookupKey);
        }
        return entityTypes;
    } catch (DBException e) {
        throw new IOException(e);
    } finally {
        IOUtils.cleanup(LOG, iterator);
    }
}
Also used : LeveldbIterator(org.apache.hadoop.yarn.server.utils.LeveldbIterator) KeyParser(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with KeyParser

use of org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser in project hadoop by apache.

the class LeveldbTimelineStore method addRelatedEntity.

/**
   * Parses the related entity from the given key at the given offset and
   * adds it to the given entity.
   */
private static void addRelatedEntity(TimelineEntity entity, byte[] key, int offset) throws IOException {
    KeyParser kp = new KeyParser(key, offset);
    String type = kp.getNextString();
    String id = kp.getNextString();
    entity.addRelatedEntity(type, id);
}
Also used : KeyParser(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser)

Example 9 with KeyParser

use of org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser in project hadoop by apache.

the class RollingLevelDBTimelineStore method getEntityByTime.

/**
   * Retrieves a list of entities satisfying given parameters.
   *
   * @param base
   *          A byte array prefix for the lookup
   * @param entityType
   *          The type of the entity
   * @param limit
   *          A limit on the number of entities to return
   * @param starttime
   *          The earliest entity start time to retrieve (exclusive)
   * @param endtime
   *          The latest entity start time to retrieve (inclusive)
   * @param fromId
   *          Retrieve entities starting with this entity
   * @param fromTs
   *          Ignore entities with insert timestamp later than this ts
   * @param secondaryFilters
   *          Filter pairs that the entities should match
   * @param fields
   *          The set of fields to retrieve
   * @param usingPrimaryFilter
   *          true if this query is using a primary filter
   * @return A list of entities
   * @throws IOException
   */
private TimelineEntities getEntityByTime(byte[] base, String entityType, Long limit, Long starttime, Long endtime, String fromId, Long fromTs, Collection<NameValuePair> secondaryFilters, EnumSet<Field> fields, CheckAcl checkAcl, boolean usingPrimaryFilter) throws IOException {
    DBIterator iterator = null;
    try {
        KeyBuilder kb = KeyBuilder.newInstance().add(base).add(entityType);
        // only db keys matching the prefix (base + entity type) will be parsed
        byte[] prefix = kb.getBytesForLookup();
        if (endtime == null) {
            // if end time is null, place no restriction on end time
            endtime = Long.MAX_VALUE;
        }
        // Sanitize the fields parameter
        if (fields == null) {
            fields = EnumSet.allOf(Field.class);
        }
        // construct a first key that will be seeked to using end time or fromId
        long firstStartTime = Long.MAX_VALUE;
        byte[] first = null;
        if (fromId != null) {
            Long fromIdStartTime = getStartTimeLong(fromId, entityType);
            if (fromIdStartTime == null) {
                // no start time for provided id, so return empty entities
                return new TimelineEntities();
            }
            if (fromIdStartTime <= endtime) {
                // if provided id's start time falls before the end of the window,
                // use it to construct the seek key
                firstStartTime = fromIdStartTime;
                first = kb.add(writeReverseOrderedLong(fromIdStartTime)).add(fromId).getBytesForLookup();
            }
        }
        // if seek key wasn't constructed using fromId, construct it using end ts
        if (first == null) {
            firstStartTime = endtime;
            first = kb.add(writeReverseOrderedLong(endtime)).getBytesForLookup();
        }
        byte[] last = null;
        if (starttime != null) {
            // if start time is not null, set a last key that will not be
            // iterated past
            last = KeyBuilder.newInstance().add(base).add(entityType).add(writeReverseOrderedLong(starttime)).getBytesForLookup();
        }
        if (limit == null) {
            // if limit is not specified, use the default
            limit = DEFAULT_LIMIT;
        }
        TimelineEntities entities = new TimelineEntities();
        RollingLevelDB rollingdb = null;
        if (usingPrimaryFilter) {
            rollingdb = indexdb;
        } else {
            rollingdb = entitydb;
        }
        DB db = rollingdb.getDBForStartTime(firstStartTime);
        while (entities.getEntities().size() < limit && db != null) {
            iterator = db.iterator();
            iterator.seek(first);
            // or a start time has been specified and reached/exceeded
            while (entities.getEntities().size() < limit && iterator.hasNext()) {
                byte[] key = iterator.peekNext().getKey();
                if (!prefixMatches(prefix, prefix.length, key) || (last != null && WritableComparator.compareBytes(key, 0, key.length, last, 0, last.length) > 0)) {
                    break;
                }
                // read the start time and entity id from the current key
                KeyParser kp = new KeyParser(key, prefix.length);
                Long startTime = kp.getNextLong();
                String entityId = kp.getNextString();
                if (fromTs != null) {
                    long insertTime = readReverseOrderedLong(iterator.peekNext().getValue(), 0);
                    if (insertTime > fromTs) {
                        byte[] firstKey = key;
                        while (iterator.hasNext()) {
                            key = iterator.peekNext().getKey();
                            iterator.next();
                            if (!prefixMatches(firstKey, kp.getOffset(), key)) {
                                break;
                            }
                        }
                        continue;
                    }
                }
                // Even if other info and primary filter fields are not included, we
                // still need to load them to match secondary filters when they are
                // non-empty
                EnumSet<Field> queryFields = EnumSet.copyOf(fields);
                boolean addPrimaryFilters = false;
                boolean addOtherInfo = false;
                if (secondaryFilters != null && secondaryFilters.size() > 0) {
                    if (!queryFields.contains(Field.PRIMARY_FILTERS)) {
                        queryFields.add(Field.PRIMARY_FILTERS);
                        addPrimaryFilters = true;
                    }
                    if (!queryFields.contains(Field.OTHER_INFO)) {
                        queryFields.add(Field.OTHER_INFO);
                        addOtherInfo = true;
                    }
                }
                // parse the entity that owns this key, iterating over all keys for
                // the entity
                TimelineEntity entity = null;
                if (usingPrimaryFilter) {
                    entity = getEntity(entityId, entityType, queryFields);
                    iterator.next();
                } else {
                    entity = getEntity(entityId, entityType, startTime, queryFields, iterator, key, kp.getOffset());
                }
                // determine if the retrieved entity matches the provided secondary
                // filters, and if so add it to the list of entities to return
                boolean filterPassed = true;
                if (secondaryFilters != null) {
                    for (NameValuePair filter : secondaryFilters) {
                        Object v = entity.getOtherInfo().get(filter.getName());
                        if (v == null) {
                            Set<Object> vs = entity.getPrimaryFilters().get(filter.getName());
                            if (vs == null || !vs.contains(filter.getValue())) {
                                filterPassed = false;
                                break;
                            }
                        } else if (!v.equals(filter.getValue())) {
                            filterPassed = false;
                            break;
                        }
                    }
                }
                if (filterPassed) {
                    if (entity.getDomainId() == null) {
                        entity.setDomainId(DEFAULT_DOMAIN_ID);
                    }
                    if (checkAcl == null || checkAcl.check(entity)) {
                        // matching secondary filters
                        if (addPrimaryFilters) {
                            entity.setPrimaryFilters(null);
                        }
                        if (addOtherInfo) {
                            entity.setOtherInfo(null);
                        }
                        entities.addEntity(entity);
                    }
                }
            }
            db = rollingdb.getPreviousDB(db);
        }
        return entities;
    } finally {
        IOUtils.cleanup(LOG, iterator);
    }
}
Also used : TimelineEntity(org.apache.hadoop.yarn.api.records.timeline.TimelineEntity) DBIterator(org.iq80.leveldb.DBIterator) KeyBuilder(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyBuilder) TimelineEntities(org.apache.hadoop.yarn.api.records.timeline.TimelineEntities) GenericObjectMapper.writeReverseOrderedLong(org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.writeReverseOrderedLong) GenericObjectMapper.readReverseOrderedLong(org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.readReverseOrderedLong) KeyParser(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser) DB(org.iq80.leveldb.DB)

Example 10 with KeyParser

use of org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser in project hadoop by apache.

the class RollingLevelDBTimelineStore method getEntityEvent.

/**
   * Creates an event object from the given key, offset, and value. If the event
   * type is not contained in the specified set of event types, returns null.
   */
private static TimelineEvent getEntityEvent(Set<String> eventTypes, byte[] key, int offset, byte[] value) throws IOException {
    KeyParser kp = new KeyParser(key, offset);
    long ts = kp.getNextLong();
    String tstype = kp.getNextString();
    if (eventTypes == null || eventTypes.contains(tstype)) {
        TimelineEvent event = new TimelineEvent();
        event.setTimestamp(ts);
        event.setEventType(tstype);
        Object o = fstConf.asObject(value);
        if (o == null) {
            event.setEventInfo(null);
        } else if (o instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, Object> m = (Map<String, Object>) o;
            event.setEventInfo(m);
        } else {
            throw new IOException("Couldn't deserialize event info map");
        }
        return event;
    }
    return null;
}
Also used : TimelineEvent(org.apache.hadoop.yarn.api.records.timeline.TimelineEvent) KeyParser(org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser) IOException(java.io.IOException) Map(java.util.Map) TreeMap(java.util.TreeMap) LRUMap(org.apache.commons.collections.map.LRUMap)

Aggregations

KeyParser (org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser)12 IOException (java.io.IOException)6 KeyBuilder (org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyBuilder)3 LeveldbIterator (org.apache.hadoop.yarn.server.utils.LeveldbIterator)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 LRUMap (org.apache.commons.collections.map.LRUMap)2 GenericObjectMapper.readReverseOrderedLong (org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.readReverseOrderedLong)2 GenericObjectMapper.writeReverseOrderedLong (org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.writeReverseOrderedLong)2 DBIterator (org.iq80.leveldb.DBIterator)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TimelineDomain (org.apache.hadoop.yarn.api.records.timeline.TimelineDomain)1 TimelineDomains (org.apache.hadoop.yarn.api.records.timeline.TimelineDomains)1 TimelineEntities (org.apache.hadoop.yarn.api.records.timeline.TimelineEntities)1 TimelineEntity (org.apache.hadoop.yarn.api.records.timeline.TimelineEntity)1 TimelineEvent (org.apache.hadoop.yarn.api.records.timeline.TimelineEvent)1 DB (org.iq80.leveldb.DB)1