/** * Factory method that makes a new CacheDirectiveInfo by applying fields in a * CacheDirectiveInfo to an existing CacheDirective. * * @param info with some or all fields set. * @param defaults directive providing default values for unset fields in * info. * * @return new CacheDirectiveInfo of the info applied to the defaults. */ private static CacheDirectiveInfo createFromInfoAndDefaults( CacheDirectiveInfo info, CacheDirective defaults) { // Initialize the builder with the default values CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder(defaults.toInfo()); // Replace default with new value if present if (info.getPath() != null) { builder.setPath(info.getPath()); } if (info.getReplication() != null) { builder.setReplication(info.getReplication()); } if (info.getPool() != null) { builder.setPool(info.getPool()); } if (info.getExpiration() != null) { builder.setExpiration(info.getExpiration()); } return builder.build(); }
private void removeInternal(CacheDirective directive) throws InvalidRequestException { assert namesystem.hasWriteLock(); // Remove the corresponding entry in directivesByPath. String path = directive.getPath(); List<CacheDirective> directives = directivesByPath.get(path); if (directives == null || !directives.remove(directive)) { throw new InvalidRequestException("Failed to locate entry " + directive.getId() + " by path " + directive.getPath()); } if (directives.size() == 0) { directivesByPath.remove(path); } // Fix up the stats from removing the pool final CachePool pool = directive.getPool(); directive.addBytesNeeded(-directive.getBytesNeeded()); directive.addFilesNeeded(-directive.getFilesNeeded()); directivesById.remove(directive.getId()); pool.getDirectiveList().remove(directive); assert directive.getPool() == null; setNeedsRescan(); }
/** * Load cache directives from the fsimage */ private void loadDirectives(DataInput in) throws IOException { StartupProgress prog = NameNode.getStartupProgress(); Step step = new Step(StepType.CACHE_ENTRIES); prog.beginStep(Phase.LOADING_FSIMAGE, step); int numDirectives = in.readInt(); prog.setTotal(Phase.LOADING_FSIMAGE, step, numDirectives); Counter counter = prog.getCounter(Phase.LOADING_FSIMAGE, step); for (int i = 0; i < numDirectives; i++) { CacheDirectiveInfo info = FSImageSerialization.readCacheDirectiveInfo(in); // Get pool reference by looking it up in the map final String poolName = info.getPool(); CacheDirective directive = new CacheDirective(info.getId(), info.getPath().toUri().getPath(), info.getReplication(), info.getExpiration().getAbsoluteMillis()); addCacheDirective(poolName, directive); counter.increment(); } prog.endStep(Phase.LOADING_FSIMAGE, step); }
/** * Get a CacheDirective by ID, validating the ID and that the directive * exists. */ private CacheDirective getById(long id) throws InvalidRequestException { // Check for invalid IDs. if (id <= 0) { throw new InvalidRequestException("Invalid negative ID."); } // Find the directive. CacheDirective directive = directivesById.get(id); if (directive == null) { throw new InvalidRequestException("No directive with ID " + id + " found."); } return directive; }
/** * Adds a directive, skipping most error checking. This should only be called * internally in special scenarios like edit log replay. */ CacheDirectiveInfo addDirectiveFromEditLog(CacheDirectiveInfo directive) throws InvalidRequestException { long id = directive.getId(); CacheDirective entry = new CacheDirective(directive); CachePool pool = cachePools.get(directive.getPool()); addInternal(entry, pool); if (nextDirectiveId <= id) { nextDirectiveId = id + 1; } return entry.toInfo(); }
/** * Modifies a directive, skipping most error checking. This is for careful * internal use only. modifyDirective can be non-deterministic since its error * checking depends on current system time, which poses a problem for edit log * replay. */ void modifyDirectiveFromEditLog(CacheDirectiveInfo info) throws InvalidRequestException { // Check for invalid IDs. Long id = info.getId(); if (id == null) { throw new InvalidRequestException("Must supply an ID."); } CacheDirective prevEntry = getById(id); CacheDirectiveInfo newInfo = createFromInfoAndDefaults(info, prevEntry); removeInternal(prevEntry); addInternal(new CacheDirective(newInfo), getCachePool(newInfo.getPool())); }
private void saveDirectives(DataOutputStream out, String sdPath) throws IOException { StartupProgress prog = NameNode.getStartupProgress(); Step step = new Step(StepType.CACHE_ENTRIES, sdPath); prog.beginStep(Phase.SAVING_CHECKPOINT, step); prog.setTotal(Phase.SAVING_CHECKPOINT, step, directivesById.size()); Counter counter = prog.getCounter(Phase.SAVING_CHECKPOINT, step); out.writeInt(directivesById.size()); for (CacheDirective directive : directivesById.values()) { FSImageSerialization.writeCacheDirectiveInfo(out, directive.toInfo()); counter.increment(); } prog.endStep(Phase.SAVING_CHECKPOINT, step); }
private void resetStatistics() { for (CachePool pool: cacheManager.getCachePools()) { pool.resetStatistics(); } for (CacheDirective directive: cacheManager.getCacheDirectives()) { directive.resetStatistics(); } }