/** * Add a component * @param serviceClass service class to use under ~user * @param componentName component name * @param record record to put * @throws IOException */ public void putComponent(String serviceClass, String serviceName, String componentName, ServiceRecord record) throws IOException { String path = RegistryUtils.componentPath( user, serviceClass, serviceName, componentName); registryOperations.mknode(RegistryPathUtils.parentOf(path), true); registryOperations.bind(path, record, BindFlags.OVERWRITE); }
/** * Get the absolute path to where the service has registered itself. * This includes the base registry path * Null until the service is registered * @return the service registration path. */ public String getAbsoluteSelfRegistrationPath() { if (selfRegistrationPath == null) { return null; } String root = registryOperations.getConfig().getTrimmed( RegistryConstants.KEY_REGISTRY_ZK_ROOT, RegistryConstants.DEFAULT_ZK_REGISTRY_ROOT); return RegistryPathUtils.join(root, selfRegistrationPath); }
/** * Buld the user path -switches to the system path if the user is "". * It also cross-converts the username to ascii via punycode * @param username username or "" * @return the path to the user */ public static String homePathForUser(String username) { Preconditions.checkArgument(username != null, "null user"); // catch recursion if (username.startsWith(RegistryConstants.PATH_USERS)) { return username; } if (username.isEmpty()) { return RegistryConstants.PATH_SYSTEM_SERVICES; } // convert username to registry name String convertedName = convertUsername(username); return RegistryPathUtils.join(RegistryConstants.PATH_USERS, encodeForRegistry(convertedName)); }
/** * Returns the DNS name constructed from the container role/component name. * * @return the DNS naem. * @throws PathNotFoundException * @throws TextParseException */ protected Name getContainerName() throws PathNotFoundException, TextParseException { String service = RegistryPathUtils.lastPathEntry( RegistryPathUtils.parentOf(RegistryPathUtils.parentOf(getPath()))); String description = getRecord().description.toLowerCase(); String user = RegistryPathUtils.getUsername(getPath()); return Name.fromString(MessageFormat.format("{0}.{1}.{2}.{3}", description, service, user, domain)); }
/** * Returns the DNS name constructed from the YARN container ID. * * @return the container ID name. * @throws TextParseException */ protected Name getContainerIDName() throws TextParseException { String containerID = RegistryPathUtils.lastPathEntry(getPath()); return Name.fromString(String.format("%s.%s", containerID, domain)); }
/** * Convert the given user name formatted for the registry. * * @param shortUserName * @return converted user name */ public static String registryUser(String shortUserName) { String encodedName = encodeForRegistry(shortUserName); // DNS name doesn't allow "_", replace it with "-" encodedName = RegistryUtils.convertUsername(encodedName); return encodedName.replace("_", "-"); }
/** * Get the last entry in a path; for an empty path * returns "". The split logic is that of * {@link #split(String)} * @param path path of operation * @return the last path entry or "" if none. */ public static String lastPathEntry(String path) { List<String> splits = split(path); if (splits.isEmpty()) { // empty path. Return "" return ""; } else { return splits.get(splits.size() - 1); } }
/** * Create a full path from the registry root and the supplied subdir * @param path path of operation * @return an absolute path * @throws InvalidPathnameException if the path is invalid */ public static String createFullPath(String base, String path) throws InvalidPathnameException { Preconditions.checkArgument(path != null, "null path"); Preconditions.checkArgument(base != null, "null path"); return validateZKPath(join(base, path)); }
/** * Get the service's DNS name for registration. * * @return the service DNS name. * @throws TextParseException */ protected Name getServiceName() throws TextParseException { String user = RegistryPathUtils.getUsername(getPath()); String service = String.format("%s.%s.%s", RegistryPathUtils.lastPathEntry(getPath()), user, domain); return Name.fromString(service); }
/** * Handler for {@link UnregisterComponentInstance} * * unregister a component. At the time this message is received, * the component may not have been registered * @param id the component */ public void unregisterComponent(ContainerId id) { log.info("Unregistering component {}", id); if (yarnRegistryOperations == null) { log.warn("Processing unregister component event before initialization " + "completed; init flag ={}", initCompleted); return; } String cid = RegistryPathUtils.encodeYarnID(id.toString()); try { yarnRegistryOperations.deleteComponent(cid); } catch (IOException e) { log.warn("Failed to delete container {} : {}", id, e, e); } }
/** * Create a full path from the registry root and the supplied subdir * @param path path of operation * @return an absolute path * @throws IllegalArgumentException if the path is invalide */ protected String createFullPath(String path) throws IOException { return RegistryPathUtils.createFullPath(registryRoot, path); }
private void assertValidPath(String path) throws InvalidPathnameException { validateZKPath(path); }
@Test public void testGetUserFromPath() throws Exception { assertEquals("bob", RegistryPathUtils .getUsername("/registry/users/bob/services/yarn-service/test1/")); assertEquals("bob-dev", RegistryPathUtils .getUsername("/registry/users/bob-dev/services/yarn-service/test1")); assertEquals("bob.dev", RegistryPathUtils .getUsername("/registry/users/bob.dev/services/yarn-service/test1")); }
/** * Buld the user path -switches to the system path if the user is "". * It also cross-converts the username to ascii via punycode * @param username username or "" * @return the path to the user */ public static String homePathForUser(String username) { Preconditions.checkArgument(username != null, "null user"); // catch recursion if (username.startsWith(RegistryConstants.PATH_USERS)) { return username; } if (username.isEmpty()) { return RegistryConstants.PATH_SYSTEM_SERVICES; } // convert username to registry name String convertedName = convertUsername(username); return RegistryPathUtils.join(RegistryConstants.PATH_USERS, encodeForRegistry(convertedName)); }
/** * Return the DNS name constructed from the component name. * * @return the DNS naem. * @throws PathNotFoundException * @throws TextParseException */ protected Name getComponentName() throws PathNotFoundException, TextParseException { String service = RegistryPathUtils.lastPathEntry( RegistryPathUtils.parentOf(RegistryPathUtils.parentOf(getPath()))); String component = getRecord().get("yarn:component").toLowerCase(); String user = RegistryPathUtils.getUsername(getPath()); return Name.fromString(MessageFormat.format("{0}.{1}.{2}.{3}", component, service, user, domain)); }
@Override public void nodeAdded(String path) throws IOException { // get a listing of service records String relativePath = getPathRelativeToRegistryRoot(path); String child = RegistryPathUtils.lastPathEntry(path); Map<String, RegistryPathStatus> map = new HashMap<>(); map.put(child, registryOperations.stat(relativePath)); Map<String, ServiceRecord> records = RegistryUtils.extractServiceRecords(registryOperations, getAdjustedParentPath(path), map); processServiceRecords(records, register); pathToRecordMap.putAll(records); }
/** * Get the current user path formatted for the registry * <p> * In an insecure cluster, the environment variable * <code>HADOOP_USER_NAME </code> is queried <i>first</i>. * <p> * This means that in a YARN container where the creator set this * environment variable to propagate their identity, the defined * user name is used in preference to the actual user. * <p> * In a secure cluster, the kerberos identity of the current user is used. * @return the encoded shortname of the current user * @throws RuntimeException if the current user identity cannot be determined * from the OS/kerberos. * */ public static String currentUser() { String shortUserName = currentUsernameUnencoded(); return encodeForRegistry(shortUserName); }
/** * Get the last entry in a path; for an empty path * returns "". The split logic is that of * {@link #split(String)} * @param path path of operation * @return the last path entry or "" if none. */ public static String lastPathEntry(String path) { List<String> splits = split(path); if (splits.isEmpty()) { // empty path. Return "" return ""; } else { return splits.get(splits.size() - 1); } }
/** * Create a full path from the registry root and the supplied subdir * @param path path of operation * @return an absolute path * @throws InvalidPathnameException if the path is invalid */ public static String createFullPath(String base, String path) throws InvalidPathnameException { Preconditions.checkArgument(path != null, "null path"); Preconditions.checkArgument(base != null, "null path"); return validateZKPath(join(base, path)); }
/** * Handler for {@link UnregisterComponentInstance} * * unregister a component. At the time this message is received, * the component may not have been registered * @param id the component */ public void unregisterComponent(ContainerId id) { log.info("Unregistering component {}", id); if (yarnRegistryOperations == null) { log.warn("Processing unregister component event before initialization " + "completed; init flag ={}", initCompleted); return; } String cid = RegistryPathUtils.encodeYarnID(id.toString()); try { yarnRegistryOperations.deleteComponent(cid); } catch (IOException e) { log.warn("Failed to delete container {} : {}", id, e, e); } }