Search in sources :

Example 1 with TieredIdentity

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));
}
Also used : Logger(org.slf4j.Logger) WorkerNetAddress(alluxio.wire.WorkerNetAddress) LoggerFactory(org.slf4j.LoggerFactory) TieredIdentity(alluxio.wire.TieredIdentity) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Pair(alluxio.collections.Pair) TieredIdentityUtils(alluxio.util.TieredIdentityUtils) PropertyKey(alluxio.conf.PropertyKey) Collectors(java.util.stream.Collectors) List(java.util.List) Constants(alluxio.Constants) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) Optional(java.util.Optional) NettyUtils(alluxio.util.network.NettyUtils) WorkerNetAddress(alluxio.wire.WorkerNetAddress) TieredIdentity(alluxio.wire.TieredIdentity)

Example 2 with TieredIdentity

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);
}
Also used : TieredIdentity(alluxio.wire.TieredIdentity) ArrayList(java.util.ArrayList) LocalityTier(alluxio.wire.TieredIdentity.LocalityTier) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with TieredIdentity

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);
    }
}
Also used : TieredIdentity(alluxio.wire.TieredIdentity) Closeable(java.io.Closeable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ConfigurationRule(alluxio.ConfigurationRule) LocalityTier(alluxio.wire.TieredIdentity.LocalityTier) Test(org.junit.Test)

Example 4 with TieredIdentity

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);
    }
}
Also used : TieredIdentity(alluxio.wire.TieredIdentity) Closeable(java.io.Closeable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ConfigurationRule(alluxio.ConfigurationRule) LocalityTier(alluxio.wire.TieredIdentity.LocalityTier) Test(org.junit.Test)

Example 5 with TieredIdentity

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();
}
Also used : TieredIdentity(alluxio.wire.TieredIdentity) Closeable(java.io.Closeable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) ConfigurationRule(alluxio.ConfigurationRule) LocalityTier(alluxio.wire.TieredIdentity.LocalityTier) Test(org.junit.Test)

Aggregations

TieredIdentity (alluxio.wire.TieredIdentity)10 LocalityTier (alluxio.wire.TieredIdentity.LocalityTier)8 Test (org.junit.Test)6 ConfigurationRule (alluxio.ConfigurationRule)5 Closeable (java.io.Closeable)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 ArrayList (java.util.ArrayList)3 WorkerNetAddress (alluxio.wire.WorkerNetAddress)2 File (java.io.File)2 Constants (alluxio.Constants)1 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)1 Pair (alluxio.collections.Pair)1 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 PropertyKey (alluxio.conf.PropertyKey)1 TieredIdentityUtils (alluxio.util.TieredIdentityUtils)1 NettyUtils (alluxio.util.network.NettyUtils)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1