private NamespaceId validateAndGetScopedNamespace(NamespaceId namespace, ArtifactScope scope) throws NamespaceNotFoundException { try { namespaceQueryAdmin.get(namespace); } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP to interact with namespaces. // Within AppFabric, NamespaceAdmin is bound to DefaultNamespaceAdmin which directly interacts with MDS. // Hence, this should never happen. throw Throwables.propagate(e); } return ArtifactScope.SYSTEM.equals(scope) ? NamespaceId.SYSTEM : namespace; }
private NamespaceId validateAndGetNamespace(String namespace) throws NamespaceNotFoundException { NamespaceId namespaceId = new NamespaceId(namespace); try { namespaceQueryAdmin.get(namespaceId); } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP to interact with namespaces. // Within AppFabric, NamespaceAdmin is bound to DefaultNamespaceAdmin which directly interacts with MDS. // Hence, this should never happen. throw Throwables.propagate(e); } return namespaceId; }
private NamespaceId validateAndGetScopedNamespace(NamespaceId namespace, ArtifactScope scope) throws NamespaceNotFoundException { try { namespaceQueryAdmin.get(namespace); } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP to interact with namespaces. // Within AppFabric, NamespaceAdmin is bound to DefaultNamespaceAdmin which directly interacts with MDS. // Hence, this should never happen. throw Throwables.propagate(e); } return ArtifactScope.SYSTEM.equals(scope) ? NamespaceId.SYSTEM : namespace; }
private NamespaceId validateAndGetNamespace(String namespace) throws NamespaceNotFoundException { NamespaceId namespaceId = new NamespaceId(namespace); try { namespaceQueryAdmin.get(namespaceId); } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP to interact with namespaces. // Within AppFabric, NamespaceAdmin is bound to DefaultNamespaceAdmin which directly interacts with MDS. // Hence, this should never happen. throw Throwables.propagate(e); } return namespaceId; }
@GET @Path("/") public void listStreams(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception { // Check for namespace existence. Throws NotFoundException if namespace doesn't exist namespaceQueryAdmin.get(new NamespaceId(namespaceId)); List<StreamSpecification> specifications = streamAdmin.listStreams(new NamespaceId(namespaceId)); List<StreamDetail> streamDetails = new ArrayList<>(specifications.size()); for (StreamSpecification specification : specifications) { streamDetails.add(new StreamDetail(specification.getName())); } responder.sendJson(HttpResponseStatus.OK, GSON.toJson(streamDetails)); }
@Override public Location get(NamespaceId namespaceId) throws IOException { if (isReservedNamespace(namespaceId)) { return getNonCustomMappedLocation(namespaceId); } // since this is not a cdap reserved namespace we look up meta if there is a custom mapping try { return get(namespaceQueryAdmin.get(namespaceId)); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(String.format("Failed to get namespace meta for namespace %s", namespaceId), e); } }
@Nullable // returns null iff the input is null private String getHiveDatabase(@Nullable String namespace) { // null namespace implies that the operation happens across all databases if (isNullOrDefault(namespace)) { return namespace; } try { String customHiveDb = namespaceQueryAdmin.get(new NamespaceId(namespace)).getConfig().getHiveDatabase(); if (!Strings.isNullOrEmpty(customHiveDb)) { return customHiveDb; } } catch (Exception e) { throw Throwables.propagate(e); } return getCDAPFormatDBName(namespace); }
@Nullable // returns null iff the input is null private String getHiveDatabase(@Nullable String namespace) { // null namespace implies that the operation happens across all databases if (isNullOrDefault(namespace)) { return namespace; } try { String customHiveDb = namespaceQueryAdmin.get(new NamespaceId(namespace)).getConfig().getHiveDatabase(); if (!Strings.isNullOrEmpty(customHiveDb)) { return customHiveDb; } } catch (Exception e) { throw Throwables.propagate(e); } return getCDAPFormatDBName(namespace); }
private void deleteLocation(NamespaceId namespaceId) throws IOException { // TODO: CDAP-1581: Implement soft delete Location namespaceHome = namespacePathLocator.get(namespaceId); try { if (hasCustomLocation(namespaceQueryAdmin.get(namespaceId))) { LOG.debug("Custom location mapping {} was found while deleting namespace {}. Deleting all data inside it but" + "skipping namespace home directory delete.", namespaceHome, namespaceId); // delete everything inside the namespace home but not the namespace home as its user owned directory Locations.deleteContent(namespaceHome); } else { // a custom location was not provided for this namespace so cdap is responsible for managing the lifecycle of // the location hence delete it. if (namespaceHome.exists() && !namespaceHome.delete(true)) { throw new IOException(String.format("Error while deleting home directory '%s' for namespace '%s'", namespaceHome, namespaceId)); } } } catch (Exception e) { throw new IOException(String.format("Error while deleting home directory %s for namespace %s ", namespaceHome, namespaceId), e); } }
private void deleteLocation(NamespaceId namespaceId) throws IOException { // TODO: CDAP-1581: Implement soft delete Location namespaceHome = namespacedLocationFactory.get(namespaceId); try { if (hasCustomLocation(namespaceQueryAdmin.get(namespaceId))) { LOG.debug("Custom location mapping {} was found while deleting namespace {}. Deleting all data inside it but" + "skipping namespace home directory delete.", namespaceHome, namespaceId); // delete everything inside the namespace home but not the namespace home as its user owned directory Locations.deleteContent(namespaceHome); } else { // a custom location was not provided for this namespace so cdap is responsible for managing the lifecycle of // the location hence delete it. if (namespaceHome.exists() && !namespaceHome.delete(true)) { throw new IOException(String.format("Error while deleting home directory '%s' for namespace '%s'", namespaceHome, namespaceId)); } } } catch (Exception e) { throw new IOException(String.format("Error while deleting home directory %s for namespace %s ", namespaceHome, namespaceId), e); } }
private NamespaceConfig getNamespaceConfig(NamespacedEntityId entityId) throws IOException { try { if (entityId.getNamespaceId().equals(NamespaceId.SYSTEM)) { return NamespaceMeta.SYSTEM.getConfig(); } return namespaceQueryAdmin.get(entityId.getNamespaceId()).getConfig(); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); } }
public String getHBaseNamespace(NamespaceId namespaceId) throws IOException { // Convert CDAP Namespace to HBase namespace if (NamespaceId.SYSTEM.equals(namespaceId) || NamespaceId.CDAP.equals(namespaceId) || NamespaceId.DEFAULT.equals(namespaceId)) { return toCDAPManagedHBaseNamespace(namespaceId); } if (namespaceQueryAdmin == null) { throw new IOException(String.format("NamespaceQueryAdmin is not set and a non-reserved namespace " + "lookup is requested. Namespace %s", namespaceId.getNamespace())); } try { return getHBaseNamespace(namespaceQueryAdmin.get(namespaceId)); } catch (Exception ex) { throw new IOException(String.format("NamespaceQueryAdmin lookup to get NamespaceMeta failed. " + "Can't find mapping for %s", namespaceId.getNamespace()), ex); } }
public String getHBaseNamespace(NamespaceId namespaceId) throws IOException { // Convert CDAP Namespace to HBase namespace if (NamespaceId.SYSTEM.equals(namespaceId) || NamespaceId.CDAP.equals(namespaceId) || NamespaceId.DEFAULT.equals(namespaceId)) { return toCDAPManagedHBaseNamespace(namespaceId); } if (namespaceQueryAdmin == null) { throw new IOException(String.format("NamespaceQueryAdmin is not set and a non-reserved namespace " + "lookup is requested. Namespace %s", namespaceId.getNamespace())); } try { return getHBaseNamespace(namespaceQueryAdmin.get(namespaceId)); } catch (Exception ex) { throw new IOException(String.format("NamespaceQueryAdmin lookup to get NamespaceMeta failed. " + "Can't find mapping for %s", namespaceId.getNamespace()), ex); } }
@Override public Location get(NamespaceId namespaceId) throws IOException { if (NamespaceId.DEFAULT.equals(namespaceId) || NamespaceId.SYSTEM.equals(namespaceId) || NamespaceId.CDAP.equals(namespaceId)) { // since these are cdap reserved namespace we know there cannot be a custom mapping for this. // for optimization don't query for namespace meta return getNonCustomMappedLocation(namespaceId); } // since this is not a cdap reserved namespace we look up meta if there is a custom mapping try { return get(namespaceQueryAdmin.get(namespaceId)); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(String.format("Failed to get namespace meta for namespace %s", namespaceId), e); } }
private NamespaceId validateNamespace(String namespaceId) throws BadRequestException, NamespaceNotFoundException { NamespaceId namespace; if (namespaceId == null) { throw new BadRequestException("Path parameter namespace-id cannot be empty"); } if (EntityId.isValidId(namespaceId)) { namespace = new NamespaceId(namespaceId); } else { throw new BadRequestException(String.format("Invalid namespace '%s'", namespaceId)); } try { if (!namespace.equals(NamespaceId.SYSTEM)) { namespaceQueryAdmin.get(namespace); } } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP calls to interact with namespaces. // In AppFabricServer, NamespaceAdmin is bound to DefaultNamespaceAdmin, which interacts directly with the MDS. // Hence, this exception will never be thrown throw Throwables.propagate(e); } return namespace; }
private NamespaceId validateNamespace(String namespaceId) throws BadRequestException, NamespaceNotFoundException { NamespaceId namespace; if (namespaceId == null) { throw new BadRequestException("Path parameter namespace-id cannot be empty"); } if (EntityId.isValidId(namespaceId)) { namespace = new NamespaceId(namespaceId); } else { throw new BadRequestException(String.format("Invalid namespace '%s'", namespaceId)); } try { if (!namespace.equals(NamespaceId.SYSTEM)) { namespaceQueryAdmin.get(namespace); } } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { // This can only happen when NamespaceAdmin uses HTTP calls to interact with namespaces. // In AppFabricServer, NamespaceAdmin is bound to DefaultNamespaceAdmin, which interacts directly with the MDS. // Hence, this exception will never be thrown throw Throwables.propagate(e); } return namespace; }
/** * Get queue at namespace level if it is empty returns the default queue. * * @param namespaceId NamespaceId * @return schedule queue at namespace level or default queue. */ @Nullable public String getQueue(Id.Namespace namespaceId) throws IOException, NamespaceNotFoundException { if (namespaceId.equals(Id.Namespace.SYSTEM)) { return systemQueue; } NamespaceMeta meta; try { meta = namespaceQueryAdmin.get(namespaceId.toEntityId()); } catch (NamespaceNotFoundException e) { throw e; } catch (Exception e) { throw new IOException(e); } if (meta != null) { NamespaceConfig config = meta.getConfig(); String namespaceQueue = config.getSchedulerQueueName(); return Strings.isNullOrEmpty(namespaceQueue) ? getDefaultQueue() : namespaceQueue; } else { return getDefaultQueue(); } } }
@SuppressWarnings("ConstantConditions") @Override public void delete(NamespaceId namespaceId) throws IOException, ExploreException, SQLException { // delete namespace directory from filesystem super.delete(namespaceId); if (NamespaceId.DEFAULT.equals(namespaceId)) { return; } // delete HBase namespace NamespaceConfig namespaceConfig; try { namespaceConfig = namespaceQueryAdmin.get(namespaceId).getConfig(); } catch (Exception ex) { throw new IOException("Could not fetch custom HBase mapping.", ex); } if (!Strings.isNullOrEmpty(namespaceConfig.getHbaseNamespace())) { // custom namespace mapping is set for HBase, hence don't do anything during delete since the lifecycle of the // namespace will be managed by the user LOG.debug("Custom HBase mapping {} was found while deleting {}. Hence skipping deletion of HBase namespace", namespaceConfig.getHbaseNamespace(), namespaceId); return; } // delete HBase namespace String namespace = tableUtil.getHBaseNamespace(namespaceId); try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) { executor.deleteNamespaceIfExists(namespace); } } }
/** * Executes a program without blocking until its completion. */ public void execute(final ProgramId id, Map<String, String> sysArgs, Map<String, String> userArgs) throws Exception { String originalUserId = SecurityRequestContext.getUserId(); try { // if the program has a namespace user configured then set that user in the security request context. // See: CDAP-7396 String nsPrincipal = namespaceQueryAdmin.get(id.getNamespaceId()).getConfig().getPrincipal(); if (nsPrincipal != null && SecurityUtil.isKerberosEnabled(cConf)) { SecurityRequestContext.setUserId(new KerberosName(nsPrincipal).getServiceName()); } lifecycleService.runInternal(id, userArgs, sysArgs, false); } catch (ProgramNotFoundException | ApplicationNotFoundException e) { throw new TaskExecutionException(String.format(UserMessages.getMessage(UserErrors.PROGRAM_NOT_FOUND), id), e, false); } finally { SecurityRequestContext.setUserId(originalUserId); } } }
/** * Executes a program without blocking until its completion. */ public void execute(final ProgramId id, Map<String, String> sysArgs, Map<String, String> userArgs) throws Exception { String originalUserId = SecurityRequestContext.getUserId(); try { // if the program has a namespace user configured then set that user in the security request context. // See: CDAP-7396 String nsPrincipal = namespaceQueryAdmin.get(id.getNamespaceId()).getConfig().getPrincipal(); if (nsPrincipal != null && SecurityUtil.isKerberosEnabled(cConf)) { SecurityRequestContext.setUserId(new KerberosName(nsPrincipal).getServiceName()); } lifecycleService.runInternal(id, userArgs, sysArgs, false); } catch (ProgramNotFoundException | ApplicationNotFoundException e) { throw new TaskExecutionException(String.format(UserMessages.getMessage(UserErrors.PROGRAM_NOT_FOUND), id), e, false); } finally { SecurityRequestContext.setUserId(originalUserId); } } }