Search in sources :

Example 11 with LeveldbIterator

use of org.apache.hadoop.yarn.server.utils.LeveldbIterator in project hadoop by apache.

the class HistoryServerLeveldbStateStoreService method loadTokenMasterKeys.

private int loadTokenMasterKeys(HistoryServerState state) throws IOException {
    int numKeys = 0;
    LeveldbIterator iter = null;
    try {
        iter = new LeveldbIterator(db);
        iter.seek(bytes(TOKEN_MASTER_KEY_KEY_PREFIX));
        while (iter.hasNext()) {
            Entry<byte[], byte[]> entry = iter.next();
            String key = asString(entry.getKey());
            if (!key.startsWith(TOKEN_MASTER_KEY_KEY_PREFIX)) {
                break;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Loading master key from " + key);
            }
            try {
                loadTokenMasterKey(state, entry.getValue());
            } catch (IOException e) {
                throw new IOException("Error loading token master key from " + key, e);
            }
            ++numKeys;
        }
    } catch (DBException e) {
        throw new IOException(e);
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
    return numKeys;
}
Also used : DBException(org.iq80.leveldb.DBException) LeveldbIterator(org.apache.hadoop.yarn.server.utils.LeveldbIterator) JniDBFactory.asString(org.fusesource.leveldbjni.JniDBFactory.asString) IOException(java.io.IOException)

Example 12 with LeveldbIterator

use of org.apache.hadoop.yarn.server.utils.LeveldbIterator in project hadoop by apache.

the class TestLeveldbTimelineStore method deleteNextEntity.

private boolean deleteNextEntity(String entityType, byte[] ts) throws IOException, InterruptedException {
    LeveldbIterator iterator = null;
    LeveldbIterator pfIterator = null;
    try {
        iterator = ((LeveldbTimelineStore) store).getDbIterator(false);
        pfIterator = ((LeveldbTimelineStore) store).getDbIterator(false);
        return ((LeveldbTimelineStore) store).deleteNextEntity(entityType, ts, iterator, pfIterator, false);
    } catch (DBException e) {
        throw new IOException(e);
    } finally {
        IOUtils.cleanup(null, iterator, pfIterator);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) LeveldbIterator(org.apache.hadoop.yarn.server.utils.LeveldbIterator) IOException(java.io.IOException)

Example 13 with LeveldbIterator

use of org.apache.hadoop.yarn.server.utils.LeveldbIterator in project hadoop by apache.

the class LeveldbTimelineStore method discardOldEntities.

/**
   * Discards entities with start timestamp less than or equal to the given
   * timestamp.
   */
@VisibleForTesting
void discardOldEntities(long timestamp) throws IOException, InterruptedException {
    byte[] reverseTimestamp = writeReverseOrderedLong(timestamp);
    long totalCount = 0;
    long t1 = System.currentTimeMillis();
    try {
        List<String> entityTypes = getEntityTypes();
        for (String entityType : entityTypes) {
            LeveldbIterator iterator = null;
            LeveldbIterator pfIterator = null;
            long typeCount = 0;
            try {
                deleteLock.writeLock().lock();
                iterator = getDbIterator(false);
                pfIterator = getDbIterator(false);
                if (deletionThread != null && deletionThread.isInterrupted()) {
                    throw new InterruptedException();
                }
                boolean seeked = false;
                while (deleteNextEntity(entityType, reverseTimestamp, iterator, pfIterator, seeked)) {
                    typeCount++;
                    totalCount++;
                    seeked = true;
                    if (deletionThread != null && deletionThread.isInterrupted()) {
                        throw new InterruptedException();
                    }
                }
            } catch (IOException e) {
                LOG.error("Got IOException while deleting entities for type " + entityType + ", continuing to next type", e);
            } finally {
                IOUtils.cleanup(LOG, iterator, pfIterator);
                deleteLock.writeLock().unlock();
                if (typeCount > 0) {
                    LOG.info("Deleted " + typeCount + " entities of type " + entityType);
                }
            }
        }
    } finally {
        long t2 = System.currentTimeMillis();
        LOG.info("Discarded " + totalCount + " entities for timestamp " + timestamp + " and earlier in " + (t2 - t1) / 1000.0 + " seconds");
    }
}
Also used : LeveldbIterator(org.apache.hadoop.yarn.server.utils.LeveldbIterator) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with LeveldbIterator

use of org.apache.hadoop.yarn.server.utils.LeveldbIterator in project hadoop by apache.

the class LeveldbTimelineStore method getDomains.

@Override
public TimelineDomains getDomains(String owner) throws IOException {
    LeveldbIterator iterator = null;
    try {
        byte[] prefix = KeyBuilder.newInstance().add(OWNER_LOOKUP_PREFIX).add(owner).getBytesForLookup();
        List<TimelineDomain> domains = new ArrayList<TimelineDomain>();
        for (iterator = new LeveldbIterator(db), iterator.seek(prefix); iterator.hasNext(); ) {
            byte[] key = iterator.peekNext().getKey();
            if (!prefixMatches(prefix, prefix.length, key)) {
                break;
            }
            // Iterator to parse the rows of an individual domain
            KeyParser kp = new KeyParser(key, prefix.length);
            String domainId = kp.getNextString();
            byte[] prefixExt = KeyBuilder.newInstance().add(OWNER_LOOKUP_PREFIX).add(owner).add(domainId).getBytesForLookup();
            TimelineDomain domainToReturn = getTimelineDomain(iterator, domainId, prefixExt);
            if (domainToReturn != null) {
                domains.add(domainToReturn);
            }
        }
        // Sort the domains to return
        Collections.sort(domains, new Comparator<TimelineDomain>() {

            @Override
            public int compare(TimelineDomain domain1, TimelineDomain domain2) {
                int result = domain2.getCreatedTime().compareTo(domain1.getCreatedTime());
                if (result == 0) {
                    return domain2.getModifiedTime().compareTo(domain1.getModifiedTime());
                } else {
                    return result;
                }
            }
        });
        TimelineDomains domainsToReturn = new TimelineDomains();
        domainsToReturn.addDomains(domains);
        return domainsToReturn;
    } 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)

Example 15 with LeveldbIterator

use of org.apache.hadoop.yarn.server.utils.LeveldbIterator in project hadoop by apache.

the class LeveldbTimelineStore method getEntity.

@Override
public TimelineEntity getEntity(String entityId, String entityType, EnumSet<Field> fields) throws IOException {
    Long revStartTime = getStartTimeLong(entityId, entityType);
    if (revStartTime == null) {
        return null;
    }
    byte[] prefix = KeyBuilder.newInstance().add(ENTITY_ENTRY_PREFIX).add(entityType).add(writeReverseOrderedLong(revStartTime)).add(entityId).getBytesForLookup();
    LeveldbIterator iterator = null;
    try {
        iterator = new LeveldbIterator(db);
        iterator.seek(prefix);
        if (fields == null) {
            fields = EnumSet.allOf(Field.class);
        }
        return getEntity(entityId, entityType, revStartTime, fields, iterator, prefix, prefix.length);
    } catch (DBException e) {
        throw new IOException(e);
    } finally {
        IOUtils.cleanup(LOG, iterator);
    }
}
Also used : LeveldbIterator(org.apache.hadoop.yarn.server.utils.LeveldbIterator) GenericObjectMapper.writeReverseOrderedLong(org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.writeReverseOrderedLong) GenericObjectMapper.readReverseOrderedLong(org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.readReverseOrderedLong) IOException(java.io.IOException)

Aggregations

LeveldbIterator (org.apache.hadoop.yarn.server.utils.LeveldbIterator)26 IOException (java.io.IOException)25 DBException (org.iq80.leveldb.DBException)18 JniDBFactory.asString (org.fusesource.leveldbjni.JniDBFactory.asString)15 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 KeyParser (org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyParser)3 ContainerId (org.apache.hadoop.yarn.api.records.ContainerId)2 GenericObjectMapper.readReverseOrderedLong (org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.readReverseOrderedLong)2 GenericObjectMapper.writeReverseOrderedLong (org.apache.hadoop.yarn.server.timeline.GenericObjectMapper.writeReverseOrderedLong)2 KeyBuilder (org.apache.hadoop.yarn.server.timeline.util.LeveldbUtils.KeyBuilder)2 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Pattern (java.util.regex.Pattern)1 Path (org.apache.hadoop.fs.Path)1 DelegationKey (org.apache.hadoop.security.token.delegation.DelegationKey)1 ApplicationAttemptId (org.apache.hadoop.yarn.api.records.ApplicationAttemptId)1 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)1