@Override public List<String> resolve(List<String> names) { // normalize all input names to be in the form of IP addresses names = NetUtils.normalizeHostNames(names); List <String> result = new ArrayList<String>(names.size()); if (names.isEmpty()) { return result; } List<String> uncachedHosts = getUncachedHosts(names); // Resolve the uncached hosts List<String> resolvedHosts = rawMapping.resolve(uncachedHosts); //cache them cacheResolvedHosts(uncachedHosts, resolvedHosts); //now look up the entire list in the cache return getCachedHosts(names); }
/** * Delegate the switch topology query to the raw mapping, via * {@link AbstractDNSToSwitchMapping#isMappingSingleSwitch(DNSToSwitchMapping)} * @return true iff the raw mapper is considered single-switch. */ @Override public boolean isSingleSwitch() { return isMappingSingleSwitch(rawMapping); }
@Override public void reloadCachedMappings() { super.reloadCachedMappings(); getRawMapping().reloadCachedMappings(); }
@Override public void setConf(Configuration conf) { super.setConf(conf); getRawMapping().setConf(conf); }
/** * Verify that if the inner mapping is multi-switch, so is the cached one * @throws Throwable on any problem */ @Test public void testCachingRelaysMultiSwitchQueries() throws Throwable { StaticMapping staticMapping = newInstance("top"); assertMultiSwitch(staticMapping); CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); LOG.info("Mapping: " + cachedMap + "\n" + cachedMap.dumpTopology()); assertMultiSwitch(cachedMap); }
/** * This test verifies that resultion queries get relayed to the inner rack * @throws Throwable on any problem */ @Test public void testCachingCachesNegativeEntries() throws Throwable { StaticMapping staticMapping = newInstance(); CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); assertMapSize(cachedMap, 0); assertMapSize(staticMapping, 0); List<String> resolved = cachedMap.resolve(createQueryList()); //and verify the cache is no longer empty while the static map is assertMapSize(staticMapping, 0); assertMapSize(cachedMap, 2); }
/** * Verify the cached mapper delegates the switch mapping query to the inner * mapping, which again handles arbitrary DNSToSwitchMapping implementations * * @throws Throwable on any problem */ @Test public void testCachingRelaysStringOperationsToNullScript() throws Throwable { Configuration conf = new Configuration(); ScriptBasedMapping scriptMapping = new ScriptBasedMapping(conf); assertTrue("Did not find " + ScriptBasedMapping.NO_SCRIPT + " in " + scriptMapping, scriptMapping.toString().contains(ScriptBasedMapping.NO_SCRIPT)); CachedDNSToSwitchMapping mapping = new CachedDNSToSwitchMapping(scriptMapping); assertTrue("Did not find " + ScriptBasedMapping.NO_SCRIPT + " in " + mapping, mapping.toString().contains(ScriptBasedMapping.NO_SCRIPT)); }
/** * Verify the cached mapper delegates the switch mapping query to the inner * mapping, which again handles arbitrary DNSToSwitchMapping implementations * * @throws Throwable on any problem */ @Test public void testCachingRelays() throws Throwable { CachedDNSToSwitchMapping mapping = new CachedDNSToSwitchMapping(new StandaloneSwitchMapping()); assertFalse("Expected to be multi switch " + mapping, mapping.isSingleSwitch()); }
public synchronized static void init(Configuration conf) { if (initCalled) { return; } initCalled = true; Class<? extends DNSToSwitchMapping> dnsToSwitchMappingClass = conf.getClass( CommonConfigurationKeysPublic.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY, ScriptBasedMapping.class, DNSToSwitchMapping.class); try { DNSToSwitchMapping newInstance = ReflectionUtils.newInstance( dnsToSwitchMappingClass, conf); // Wrap around the configured class with the Cached implementation so as // to save on repetitive lookups. // Check if the impl is already caching, to avoid double caching. dnsToSwitchMapping = ((newInstance instanceof CachedDNSToSwitchMapping) ? newInstance : new CachedDNSToSwitchMapping(newInstance)); } catch (Exception e) { throw new RuntimeException(e); } }
/** * {@inheritDoc} * <p/> * This will get called in the superclass constructor, so a check is needed * to ensure that the raw mapping is defined before trying to relaying a null * configuration. * @param conf */ @Override public void setConf(Configuration conf) { super.setConf(conf); getRawMapping().setConf(conf); }
/** * Verify that if the inner mapping is multi-switch, so is the cached one * @throws Throwable on any problem */ @Test public void testCachingRelaysMultiSwitchQueries() throws Throwable { StaticMapping staticMapping = newInstance("top"); assertMultiSwitch(staticMapping); CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); LOG.info("Mapping: " + cachedMap + "\n" + cachedMap.dumpTopology()); assertMultiSwitch(cachedMap); }
/** * This test verifies that resultion queries get relayed to the inner rack * @throws Throwable on any problem */ @Test public void testCachingCachesNegativeEntries() throws Throwable { StaticMapping staticMapping = newInstance(); CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); assertMapSize(cachedMap, 0); assertMapSize(staticMapping, 0); List<String> resolved = cachedMap.resolve(createQueryList()); //and verify the cache is no longer empty while the static map is assertMapSize(staticMapping, 0); assertMapSize(cachedMap, 2); }
/** * Verify the cached mapper delegates the switch mapping query to the inner * mapping, which again handles arbitrary DNSToSwitchMapping implementations * * @throws Throwable on any problem */ @Test public void testCachingRelaysStringOperations() throws Throwable { Configuration conf = new Configuration(); String scriptname = "mappingscript.sh"; conf.set(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY, scriptname); ScriptBasedMapping scriptMapping = new ScriptBasedMapping(conf); assertTrue("Did not find " + scriptname + " in " + scriptMapping, scriptMapping.toString().contains(scriptname)); CachedDNSToSwitchMapping mapping = new CachedDNSToSwitchMapping(scriptMapping); assertTrue("Did not find " + scriptname + " in " + mapping, mapping.toString().contains(scriptname)); }
/** * Verify the cached mapper delegates the switch mapping query to the inner * mapping, which again handles arbitrary DNSToSwitchMapping implementations * * @throws Throwable on any problem */ @Test public void testCachingRelays() throws Throwable { CachedDNSToSwitchMapping mapping = new CachedDNSToSwitchMapping(new StandaloneSwitchMapping()); assertFalse("Expected to be multi switch " + mapping, mapping.isSingleSwitch()); }
public synchronized static void init(Configuration conf) { if (initCalled) { return; } else { initCalled = true; } Class<? extends DNSToSwitchMapping> dnsToSwitchMappingClass = conf.getClass( CommonConfigurationKeysPublic.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY, ScriptBasedMapping.class, DNSToSwitchMapping.class); try { DNSToSwitchMapping newInstance = ReflectionUtils.newInstance( dnsToSwitchMappingClass, conf); // Wrap around the configured class with the Cached implementation so as // to save on repetitive lookups. // Check if the impl is already caching, to avoid double caching. dnsToSwitchMapping = ((newInstance instanceof CachedDNSToSwitchMapping) ? newInstance : new CachedDNSToSwitchMapping(newInstance)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public List<String> resolve(List<String> names) { // normalize all input names to be in the form of IP addresses names = NetUtils.normalizeHostNames(names); List <String> result = new ArrayList<String>(names.size()); if (names.isEmpty()) { return result; } List<String> uncachedHosts = getUncachedHosts(names); // Resolve the uncached hosts List<String> resolvedHosts = rawMapping.resolve(uncachedHosts); //cache them cacheResolvedHosts(uncachedHosts, resolvedHosts); //now look up the entire list in the cache return getCachedHosts(names); }
@Override public void setConf(Configuration conf) { super.setConf(conf); getRawMapping().setConf(conf); }
/** * Verify that if the inner mapping is single-switch, so is the cached one * @throws Throwable on any problem */ @Test public void testCachingRelaysSingleSwitchQueries() throws Throwable { //create a single switch map StaticMapping staticMapping = newInstance(null); assertSingleSwitch(staticMapping); CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); LOG.info("Mapping: " + cachedMap + "\n" + cachedMap.dumpTopology()); assertSingleSwitch(cachedMap); }
/** * This test verifies that resultion queries get relayed to the inner rack * @throws Throwable on any problem */ @Test public void testCachingRelaysResolveQueries() throws Throwable { StaticMapping mapping = newInstance(); mapping.setConf(createConf("top")); StaticMapping staticMapping = mapping; CachedDNSToSwitchMapping cachedMap = new CachedDNSToSwitchMapping(staticMapping); assertMapSize(cachedMap, 0); //add a node to the static map StaticMapping.addNodeToRack("n1", "/r1"); //verify it is there assertMapSize(staticMapping, 1); //verify that the cache hasn't picked it up yet assertMapSize(cachedMap, 0); //now relay the query cachedMap.resolve(createQueryList()); //and verify the cache is no longer empty assertMapSize(cachedMap, 2); }
/** * Verify the cached mapper delegates the switch mapping query to the inner * mapping, which again handles arbitrary DNSToSwitchMapping implementations * * @throws Throwable on any problem */ @Test public void testCachingRelaysStringOperations() throws Throwable { Configuration conf = new Configuration(); String scriptname = "mappingscript.sh"; conf.set(CommonConfigurationKeys.NET_TOPOLOGY_SCRIPT_FILE_NAME_KEY, scriptname); ScriptBasedMapping scriptMapping = new ScriptBasedMapping(conf); assertTrue("Did not find " + scriptname + " in " + scriptMapping, scriptMapping.toString().contains(scriptname)); CachedDNSToSwitchMapping mapping = new CachedDNSToSwitchMapping(scriptMapping); assertTrue("Did not find " + scriptname + " in " + mapping, mapping.toString().contains(scriptname)); }