use of alluxio.wire.TieredIdentity in project alluxio by Alluxio.
the class BlockLocationUtils method nearest.
/**
* @param tieredIdentity the tiered identity
* @param addresses the candidate worker addresses
* @param conf Alluxio configuration
* @return the first in the pair indicates the address closest to this one. If none of the
* identities match, the first address is returned. the second in the pair indicates
* whether or not the location is local
*/
public static Optional<Pair<WorkerNetAddress, Boolean>> nearest(TieredIdentity tieredIdentity, List<WorkerNetAddress> addresses, AlluxioConfiguration conf) {
if (conf.getBoolean(PropertyKey.WORKER_DATA_SERVER_DOMAIN_SOCKET_AS_UUID)) {
// Determine by inspecting the file system if worker is local
for (WorkerNetAddress addr : addresses) {
if (NettyUtils.isDomainSocketAccessible(addr, conf)) {
LOG.debug("Found local worker by file system inspection of path {}", addr.getDomainSocketPath());
// Returns the first local worker and does not shuffle
return Optional.of(new Pair<>(addr, true));
}
}
}
// Find nearest tiered identity
Optional<TieredIdentity> nearestIdentity = TieredIdentityUtils.nearest(tieredIdentity, addresses.stream().map(addr -> addr.getTieredIdentity()).collect(Collectors.toList()), conf);
if (!nearestIdentity.isPresent()) {
return Optional.empty();
}
boolean isLocal = tieredIdentity.getTier(0).getTierName().equals(Constants.LOCALITY_NODE) && tieredIdentity.topTiersMatch(nearestIdentity.get());
Optional<WorkerNetAddress> dataSource = addresses.stream().filter(addr -> addr.getTieredIdentity().equals(nearestIdentity.get())).findFirst();
if (!dataSource.isPresent()) {
return Optional.empty();
}
return Optional.of(new Pair<>(dataSource.get(), isLocal));
}
use of alluxio.wire.TieredIdentity in project alluxio by Alluxio.
the class TieredIdentityFactory method create.
/**
* Creates a tiered identity based on configuration.
*
* @return the created tiered identity
*/
@VisibleForTesting
static TieredIdentity create(AlluxioConfiguration conf) {
TieredIdentity scriptIdentity = fromScript(conf);
List<LocalityTier> tiers = new ArrayList<>();
List<String> orderedTierNames = conf.getList(PropertyKey.LOCALITY_ORDER, ",");
for (int i = 0; i < orderedTierNames.size(); i++) {
String tierName = orderedTierNames.get(i);
String value = null;
if (scriptIdentity != null) {
LocalityTier scriptTier = scriptIdentity.getTier(i);
Preconditions.checkState(scriptTier.getTierName().equals(tierName));
value = scriptTier.getValue();
}
// Explicit configuration overrides script output.
if (conf.isSet(Template.LOCALITY_TIER.format(tierName))) {
value = conf.getString(Template.LOCALITY_TIER.format(tierName));
}
tiers.add(new LocalityTier(tierName, value));
}
// If the user doesn't specify the value of the "node" tier, we fill in a sensible default.
if (tiers.size() > 0 && tiers.get(0).getTierName().equals(Constants.LOCALITY_NODE) && tiers.get(0).getValue() == null) {
String name = NetworkAddressUtils.getLocalNodeName(conf);
tiers.set(0, new LocalityTier(Constants.LOCALITY_NODE, name));
}
return new TieredIdentity(tiers);
}
use of alluxio.wire.TieredIdentity in project alluxio by Alluxio.
the class TieredIdentityFactoryTest method fromScript.
@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);
}
}
use of alluxio.wire.TieredIdentity in project alluxio by Alluxio.
the class TieredIdentityFactoryTest method overrideScript.
@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);
}
}
use of alluxio.wire.TieredIdentity in project alluxio by Alluxio.
the class TieredIdentityFactoryTest method fromScriptClasspath.
@Test
public void fromScriptClasspath() throws Exception {
String customScriptName = "my-alluxio-locality.sh";
File dir = mFolder.newFolder("fromScriptClasspath");
CommonUtils.classLoadURL(dir.getCanonicalPath());
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();
}
Aggregations