@Override public Security getSecurity(Cluster cluster, SecurityContext securityContext, Subject subject) { try (KafkaMetadataService kms = KafkaMetadataService.newInstance(environmentService, cluster.getId(), securityContext)) { return kms.getSecurity(); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Creates and starts a {@link ZookeeperClient} connection as part of the object construction process. * The connection must be closed. See {@link KafkaMetadataService} */ public static KafkaMetadataService newInstance( EnvironmentService environmentService, Long clusterId, SecurityContext securityContext) throws ServiceConfigurationNotFoundException, IOException, ServiceNotFoundException, ServiceComponentNotFoundException { final KafkaZkConnection kafkaZkConnection = KafkaZkConnection.newInstance( getZkStringRaw(environmentService, clusterId, AMBARI_JSON_CONFIG_KAFKA_BROKER)); final ZookeeperClient zkCli = ZookeeperClient.newInstance(kafkaZkConnection); zkCli.start(); return new KafkaMetadataService(zkCli, kafkaZkConnection, securityContext, getKafkaBrokerComponent(environmentService, clusterId), getKafkaBrokers(environmentService, clusterId), getServiceConfig(environmentService, clusterId, AMBARI_JSON_CONFIG_KAFKA_BROKER), getServiceConfig(environmentService, clusterId, AMBARI_JSON_CONFIG_KAFKA_ENV)); }
public KafkaBrokersInfo<HostPort> getBrokerHostPortFromStreamsJson() throws ServiceNotFoundException, ServiceComponentNotFoundException, IOException { return KafkaBrokersInfo.hostPort(kafkaBrokerProcesses, getSecurity(), getKafkaBrokerListeners()); }
@Override public Map<String, Object> getHintsOnCluster(Cluster cluster, SecurityContext securityContext, Subject subject) { Map<String, Object> hintClusterMap = new HashMap<>(); try (KafkaMetadataService kms = KafkaMetadataService.newInstance(environmentService, cluster.getId(), securityContext)) { KafkaTopics topics = kms.getTopicsFromZk(); hintClusterMap.put(FIELD_NAME_TOPIC, topics.list()); fillZookeeperHints(cluster, hintClusterMap); final Map<KafkaBrokerListeners.Protocol, List<String>> protocolToHostsWithPort = kms.getKafkaBrokerListeners().getProtocolToHostsWithPort(); hintClusterMap.put(FIELD_NAME_BOOTSTRAP_SERVERS, mapValuesJoiner(protocolToHostsWithPort, ",")); hintClusterMap.put(FIELD_NAME_SECURITY_PROTOCOL, protocolToHostsWithPort.keySet()); hintClusterMap.put(FIELD_NAME_KAFKA_SERVICE_NAME, kms.getKafkaServiceName()); } catch (ServiceNotFoundException e) { // we access it from mapping information so shouldn't be here throw new IllegalStateException("Service " + Constants.Kafka.SERVICE_NAME + " in cluster " + cluster.getName() + " not found but mapping information exists."); } catch (ServiceConfigurationNotFoundException e) { // there's KAFKA service but not enough configuration info. } catch (Exception e) { throw new RuntimeException(e); } return hintClusterMap; }
@GET @Path("/clusters/{clusterId}/services/kafka/topics") @Timed public Response getTopicsByClusterId(@PathParam("clusterId") Long clusterId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkPermissions(authorizer, securityContext, Cluster.NAMESPACE, clusterId, READ); try(final KafkaMetadataService kafkaMetadataService = KafkaMetadataService .newInstance(environmentService, clusterId, securityContext)) { return WSUtils.respondEntity(kafkaMetadataService.getTopicsFromZk(), OK); } catch (EntityNotFoundException ex) { throw com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException.byId(ex.getMessage()); } } }
@GET @Path("/clusters/{clusterId}/services/kafka/brokers") @Timed public Response getBrokersByClusterId(@PathParam("clusterId") Long clusterId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkPermissions(authorizer, securityContext, Cluster.NAMESPACE, clusterId, READ); try(final KafkaMetadataService kafkaMetadataService = KafkaMetadataService .newInstance(environmentService, clusterId, securityContext)) { return WSUtils.respondEntity(kafkaMetadataService.getBrokerHostPortFromStreamsJson(), OK); } catch (EntityNotFoundException ex) { throw com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException.byId(ex.getMessage()); } }
public Security getSecurity() throws IOException { return new Security(securityContext, new Authorizer(false), getPrincipals(), getKeytabs()); }
private static ServiceConfiguration getServiceConfig(EnvironmentService environmentService, Long clusterId, String configName) throws ServiceNotFoundException, IOException, ServiceConfigurationNotFoundException { final ServiceConfiguration serviceConfig = environmentService.getServiceConfigurationByName( getKafkaServiceId(environmentService, clusterId), configName); if (serviceConfig == null || serviceConfig.getConfigurationMap() == null) { throw new ServiceConfigurationNotFoundException(clusterId, ServiceConfigurations.KAFKA.name(), configName); } return serviceConfig; }
public KafkaBrokersInfo<KafkaBrokersInfo.BrokerId> getBrokerIdsFromZk() throws ZookeeperClientException, IOException { final List<String> brokerIds = zkCli.getChildren(kafkaZkConnection.buildZkRootPath(ZK_RELATIVE_PATH_KAFKA_BROKERS_IDS)); return KafkaBrokersInfo.brokerIds(brokerIds, getSecurity(), getKafkaBrokerListeners()); }
public KafkaBrokersInfo<String> getBrokerInfoFromZk() throws ZookeeperClientException, IOException { final String brokerIdsZkPath = kafkaZkConnection.buildZkRootPath(ZK_RELATIVE_PATH_KAFKA_BROKERS_IDS); final List<String> brokerIds = zkCli.getChildren(brokerIdsZkPath); List<String> brokerInfo = null; if (brokerIds != null) { brokerInfo = new ArrayList<>(); for (String bkId : brokerIds) { final byte[] bytes = zkCli.getData(brokerIdsZkPath + "/" + bkId); brokerInfo.add(new String(bytes, "UTF-8")); } } return KafkaBrokersInfo.fromZk(brokerInfo, getSecurity(), getKafkaBrokerListeners()); }