for (String localityTier : conf.getList(PropertyKey.LOCALITY_ORDER, ",")) { String value = tiers.containsKey(localityTier) ? tiers.get(localityTier) : null; tieredIdentity.add(new LocalityTier(localityTier, value));
value = conf.get(Template.LOCALITY_TIER.format(tierName)); tiers.add(new LocalityTier(tierName, value)); && tiers.get(0).getValue() == null) { String name = NetworkAddressUtils.getLocalNodeName(conf); tiers.set(0, new LocalityTier(Constants.LOCALITY_NODE, name));
private static LocalityTier createRandomLocalityTier() { Random random = new Random(); String tier = CommonUtils.randomAlphaNumString(random.nextInt(10) + 1); String value = CommonUtils.randomAlphaNumString(random.nextInt(10) + 1); return new LocalityTier(tier, value); } }
value = Configuration.get(Template.LOCALITY_TIER.format(tierName)); tiers.add(new LocalityTier(tierName, value)); && tiers.get(0).getValue() == null) { String name = NetworkAddressUtils.getLocalNodeName(); tiers.set(0, new LocalityTier(Constants.LOCALITY_NODE, name));
/** * Creates a new instance of {@link WorkerNetAddress} from thrift representation. * * @param address the thrift net address * @return the instance */ public static WorkerNetAddress fromThrift(alluxio.thrift.WorkerNetAddress address) { TieredIdentity tieredIdentity = TieredIdentity.fromThrift(address.getTieredIdentity()); if (tieredIdentity == null) { // This means the worker is pre-1.7.0. We handle this in post-1.7.0 clients by filling out // the tiered identity using the hostname field. tieredIdentity = new TieredIdentity( Arrays.asList(new LocalityTier(Constants.LOCALITY_NODE, address.getHost()))); } return new WorkerNetAddress() .setDataPort(address.getDataPort()) .setDomainSocketPath(address.getDomainSocketPath()) .setHost(address.getHost()) .setRpcPort(address.getRpcPort()) .setTieredIdentity(tieredIdentity) .setWebPort(address.getWebPort()); }
/** * @return the tiered identity */ public TieredIdentity getTieredIdentity() { if (mTieredIdentity != null) { return mTieredIdentity; } return new TieredIdentity(Arrays.asList(new LocalityTier(Constants.LOCALITY_NODE, mHost))); }
/** * @param localityTier a Thrift locality tier * @return the corresponding wire type locality tier */ public static LocalityTier fromThrift(alluxio.thrift.LocalityTier localityTier) { return new LocalityTier(localityTier.getTierName(), localityTier.getValue()); }
@Test public void fromScriptClasspath() throws Exception { String customScriptName = "my-alluxio-locality.sh"; File dir = mFolder.newFolder("fromScriptClasspath"); Whitebox.invokeMethod(ClassLoader.getSystemClassLoader(), "addURL", dir.toURI().toURL()); File script = new File(dir, customScriptName); setupScript("node=myhost,rack=myrack,custom=mycustom", script); try (Closeable c = new ConfigurationRule(ImmutableMap.of( PropertyKey.LOCALITY_ORDER, "node,rack,custom", PropertyKey.LOCALITY_SCRIPT, customScriptName), mConfiguration).toResource()) { TieredIdentity identity = TieredIdentityFactory.create(mConfiguration); TieredIdentity expected = new TieredIdentity(Arrays.asList( new LocalityTier("node", "myhost"), new LocalityTier("rack", "myrack"), new LocalityTier("custom", "mycustom"))); assertEquals(expected, identity); } script.delete(); }
@Test public void matchByStringEquality() { LocalityTier lt1 = new LocalityTier("node", "NonResolvableHostname-A"); LocalityTier lt2 = new LocalityTier("node", "NonResolvableHostname-A"); LocalityTier lt3 = new LocalityTier("node", "NonResolvableHostname-B"); LocalityTier lt4 = new LocalityTier("rack", "NonResolvableHostname-A"); LocalityTier lt5 = new LocalityTier("rack", "NonResolvableHostname-B"); LocalityTier lt6 = new LocalityTier("rack", "NonResolvableHostname-B"); LocalityTier lt7 = new LocalityTier("rack", ""); LocalityTier lt8 = new LocalityTier("node", "NonResolvableHostname-A"); LocalityTier lt9 = new LocalityTier("node", ""); assertTrue(TieredIdentityUtils.matches(lt1, lt1, true)); assertTrue(TieredIdentityUtils.matches(lt1, lt2, true)); assertFalse(TieredIdentityUtils.matches(lt2, lt3, true)); assertTrue(TieredIdentityUtils.matches(lt5, lt6, true)); assertFalse(TieredIdentityUtils.matches(lt4, lt5, true)); assertFalse(TieredIdentityUtils.matches(lt6, lt7, true)); assertFalse(TieredIdentityUtils.matches(lt8, lt9, true)); }
@Test public void fromScript() throws Exception { String scriptPath = setupScript("node=myhost,rack=myrack,custom=mycustom", mFolder.newFile()); try (Closeable c = new ConfigurationRule(ImmutableMap.of( PropertyKey.LOCALITY_ORDER, "node,rack,custom", PropertyKey.LOCALITY_SCRIPT, scriptPath), mConfiguration).toResource()) { TieredIdentity identity = TieredIdentityFactory.create(mConfiguration); TieredIdentity expected = new TieredIdentity(Arrays.asList( new LocalityTier("node", "myhost"), new LocalityTier("rack", "myrack"), new LocalityTier("custom", "mycustom"))); assertEquals(expected, identity); } }
@Test public void overrideScript() throws Exception { String scriptPath = setupScript("node=myhost,rack=myrack,custom=mycustom", mFolder.newFile()); try (Closeable c = new ConfigurationRule(ImmutableMap.of( Template.LOCALITY_TIER.format("node"), "overridden", PropertyKey.LOCALITY_ORDER, "node,rack,custom", PropertyKey.LOCALITY_SCRIPT, scriptPath), mConfiguration).toResource()) { TieredIdentity identity = TieredIdentityFactory.create(mConfiguration); TieredIdentity expected = new TieredIdentity(Arrays.asList( new LocalityTier("node", "overridden"), new LocalityTier("rack", "myrack"), new LocalityTier("custom", "mycustom"))); assertEquals(expected, identity); } }
private BlockWorkerInfo worker(long capacity, long used, String node, String rack) { WorkerNetAddress address = new WorkerNetAddress(); List<LocalityTier> tiers = new ArrayList<>(); if (node != null && !node.isEmpty()) { address.setHost(node); tiers.add(new LocalityTier(Constants.LOCALITY_NODE, node)); } if (rack != null && !rack.isEmpty()) { tiers.add(new LocalityTier(Constants.LOCALITY_RACK, rack)); } address.setTieredIdentity(new TieredIdentity(tiers)); return new BlockWorkerInfo(address, capacity, used); } }
private BlockWorkerInfo worker(long capacity, String node, String rack) { WorkerNetAddress address = new WorkerNetAddress(); List<LocalityTier> tiers = new ArrayList<>(); if (node != null && !node.isEmpty()) { address.setHost(node); tiers.add(new LocalityTier(Constants.LOCALITY_NODE, node)); } if (rack != null && !rack.isEmpty()) { tiers.add(new LocalityTier(Constants.LOCALITY_RACK, rack)); } address.setTieredIdentity(new TieredIdentity(tiers)); return new BlockWorkerInfo(address, capacity, 0); } }
/** * @return the tiered identity */ public TieredIdentity getTieredIdentity() { if (mTieredIdentity != null) { return mTieredIdentity; } return new TieredIdentity(Arrays.asList(new LocalityTier(Constants.LOCALITY_NODE, mHost))); }
public void string() { TieredIdentity identity = new TieredIdentity( Arrays.asList(new LocalityTier("k1", "v1"), new LocalityTier("k2", "v2"))); assertEquals("TieredIdentity(k1=v1, k2=v2)", identity.toString()); }
@Test public void matchByIpResolution() throws Exception { assumeTrue(InetAddress.getByName("localhost").getHostAddress().equals("127.0.0.1")); LocalityTier lt1 = new LocalityTier("node", "localhost"); LocalityTier lt2 = new LocalityTier("node", "127.0.0.1"); assertTrue(TieredIdentityUtils.matches(lt1, lt2, true)); assertFalse(TieredIdentityUtils.matches(lt1, lt2, false)); }
@Test public void outOfOrderScript() throws Exception { String scriptPath = setupScript("rack=myrack,node=myhost", mFolder.newFile()); try (Closeable c = new ConfigurationRule(ImmutableMap.of( PropertyKey.LOCALITY_SCRIPT, scriptPath), mConfiguration).toResource()) { TieredIdentity identity = TieredIdentityFactory.create(mConfiguration); TieredIdentity expected = new TieredIdentity(Arrays.asList( new LocalityTier("node", "myhost"), new LocalityTier("rack", "myrack"))); assertEquals(expected, identity); } }
@Test public void defaultConf() throws Exception { TieredIdentity identity = TieredIdentityFactory.create(mConfiguration); TieredIdentity expected = new TieredIdentity(Arrays.asList( new LocalityTier("node", NetworkAddressUtils.getLocalNodeName(mConfiguration)), new LocalityTier("rack", null))); assertEquals(expected, identity); }
/** * Converts a proto type to a wire type. * * @param localityPTier the proto type to convert * @return the converted wire type */ public static TieredIdentity.LocalityTier fromProto(alluxio.grpc.LocalityTier localityPTier) { return new TieredIdentity.LocalityTier(localityPTier.getTierName(), localityPTier.hasValue() ? localityPTier.getValue() : null); }
@Test public void fromString() throws Exception { assertEquals(new TieredIdentity(Arrays.asList( new LocalityTier("node", "b"), new LocalityTier("rack", "d") )), TieredIdentityFactory.fromString("node=b,rack=d", mConfiguration)); }