private DescribeConfigsRequest createDescribeConfigsRequest(int version) { return new DescribeConfigsRequest.Builder(asList( new ConfigResource(ConfigResource.Type.BROKER, "0"), new ConfigResource(ConfigResource.Type.TOPIC, "topic"))) .build((short) version); }
private DescribeConfigsRequest createDescribeConfigsRequestWithConfigEntries(int version) { Map<ConfigResource, Collection<String>> resources = new HashMap<>(); resources.put(new ConfigResource(ConfigResource.Type.BROKER, "0"), asList("foo", "bar")); resources.put(new ConfigResource(ConfigResource.Type.TOPIC, "topic"), null); resources.put(new ConfigResource(ConfigResource.Type.TOPIC, "topic a"), Collections.<String>emptyList()); return new DescribeConfigsRequest.Builder(resources).build((short) version); }
private AlterConfigsResponse createAlterConfigsResponse() { Map<ConfigResource, ApiError> errors = new HashMap<>(); errors.put(new ConfigResource(ConfigResource.Type.BROKER, "0"), ApiError.NONE); errors.put(new ConfigResource(ConfigResource.Type.TOPIC, "topic"), new ApiError(Errors.INVALID_REQUEST, "This request is invalid")); return new AlterConfigsResponse(20, errors); }
public AlterConfigsResponse(Struct struct) { throttleTimeMs = struct.get(THROTTLE_TIME_MS); Object[] resourcesArray = struct.getArray(RESOURCES_KEY_NAME); errors = new HashMap<>(resourcesArray.length); for (Object resourceObj : resourcesArray) { Struct resourceStruct = (Struct) resourceObj; ApiError error = new ApiError(resourceStruct); ConfigResource.Type resourceType = ConfigResource.Type.forId(resourceStruct.getByte(RESOURCE_TYPE_KEY_NAME)); String resourceName = resourceStruct.getString(RESOURCE_NAME_KEY_NAME); errors.put(new ConfigResource(resourceType, resourceName), error); } }
public DescribeConfigsRequest(Struct struct, short version) { super(ApiKeys.DESCRIBE_CONFIGS, version); Object[] resourcesArray = struct.getArray(RESOURCES_KEY_NAME); resourceToConfigNames = new HashMap<>(resourcesArray.length); for (Object resourceObj : resourcesArray) { Struct resourceStruct = (Struct) resourceObj; ConfigResource.Type resourceType = ConfigResource.Type.forId(resourceStruct.getByte(RESOURCE_TYPE_KEY_NAME)); String resourceName = resourceStruct.getString(RESOURCE_NAME_KEY_NAME); Object[] configNamesArray = resourceStruct.getArray(CONFIG_NAMES_KEY_NAME); List<String> configNames = null; if (configNamesArray != null) { configNames = new ArrayList<>(configNamesArray.length); for (Object configNameObj : configNamesArray) configNames.add((String) configNameObj); } resourceToConfigNames.put(new ConfigResource(resourceType, resourceName), configNames); } this.includeSynonyms = struct.hasField(INCLUDE_SYNONYMS) ? struct.getBoolean(INCLUDE_SYNONYMS) : false; }
private AlterConfigsRequest createAlterConfigsRequest() { Map<ConfigResource, AlterConfigsRequest.Config> configs = new HashMap<>(); List<AlterConfigsRequest.ConfigEntry> configEntries = asList( new AlterConfigsRequest.ConfigEntry("config_name", "config_value"), new AlterConfigsRequest.ConfigEntry("another_name", "another value") ); configs.put(new ConfigResource(ConfigResource.Type.BROKER, "0"), new AlterConfigsRequest.Config(configEntries)); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, "topic"), new AlterConfigsRequest.Config(Collections.<AlterConfigsRequest.ConfigEntry>emptyList())); return new AlterConfigsRequest((short) 0, configs, false); }
private Config getKafkaBrokerConfig(AdminClient admin) throws Exception { final Collection<Node> nodes = admin.describeCluster().nodes().get(KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); if (nodes.isEmpty()) { throw new ConnectException("No brokers available to obtain default settings"); } String nodeId = nodes.iterator().next().idString(); Set<ConfigResource> resources = Collections.singleton(new ConfigResource(ConfigResource.Type.BROKER, nodeId)); final Map<ConfigResource, Config> configs = admin.describeConfigs(resources).all().get( KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS ); if (configs.isEmpty()) { throw new ConnectException("No configs have been received"); } return configs.values().iterator().next(); } }
private DescribeConfigsResponse createDescribeConfigsResponse() { Map<ConfigResource, DescribeConfigsResponse.Config> configs = new HashMap<>(); List<DescribeConfigsResponse.ConfigSynonym> synonyms = Collections.emptyList(); List<DescribeConfigsResponse.ConfigEntry> configEntries = asList( new DescribeConfigsResponse.ConfigEntry("config_name", "config_value", DescribeConfigsResponse.ConfigSource.DYNAMIC_BROKER_CONFIG, true, false, synonyms), new DescribeConfigsResponse.ConfigEntry("another_name", "another value", DescribeConfigsResponse.ConfigSource.DEFAULT_CONFIG, false, true, synonyms) ); configs.put(new ConfigResource(ConfigResource.Type.BROKER, "0"), new DescribeConfigsResponse.Config( ApiError.NONE, configEntries)); configs.put(new ConfigResource(ConfigResource.Type.TOPIC, "topic"), new DescribeConfigsResponse.Config( ApiError.NONE, Collections.<DescribeConfigsResponse.ConfigEntry>emptyList())); return new DescribeConfigsResponse(200, configs); }
public AlterConfigsRequest(Struct struct, short version) { super(ApiKeys.ALTER_CONFIGS, version); validateOnly = struct.getBoolean(VALIDATE_ONLY_KEY_NAME); Object[] resourcesArray = struct.getArray(RESOURCES_KEY_NAME); configs = new HashMap<>(resourcesArray.length); for (Object resourcesObj : resourcesArray) { Struct resourcesStruct = (Struct) resourcesObj; ConfigResource.Type resourceType = ConfigResource.Type.forId(resourcesStruct.getByte(RESOURCE_TYPE_KEY_NAME)); String resourceName = resourcesStruct.getString(RESOURCE_NAME_KEY_NAME); ConfigResource resource = new ConfigResource(resourceType, resourceName); Object[] configEntriesArray = resourcesStruct.getArray(CONFIG_ENTRIES_KEY_NAME); List<ConfigEntry> configEntries = new ArrayList<>(configEntriesArray.length); for (Object configEntriesObj: configEntriesArray) { Struct configEntriesStruct = (Struct) configEntriesObj; String configName = configEntriesStruct.getString(CONFIG_NAME); String configValue = configEntriesStruct.getString(CONFIG_VALUE); configEntries.add(new ConfigEntry(configName, configValue)); } Config config = new Config(configEntries); configs.put(resource, config); } }
@Test public void testDescribeConfigs() throws Exception { try (AdminClientUnitTestEnv env = mockClientEnv()) { env.kafkaClient().setNodeApiVersions(NodeApiVersions.create()); env.kafkaClient().prepareResponse(new DescribeConfigsResponse(0, Collections.singletonMap(new ConfigResource(ConfigResource.Type.BROKER, "0"), new DescribeConfigsResponse.Config(ApiError.NONE, Collections.emptySet())))); DescribeConfigsResult result2 = env.adminClient().describeConfigs(Collections.singleton( new ConfigResource(ConfigResource.Type.BROKER, "0"))); result2.all().get(); } }
ConfigResource.Type resourceType = ConfigResource.Type.forId(resourceStruct.getByte(RESOURCE_TYPE_KEY_NAME)); String resourceName = resourceStruct.getString(RESOURCE_NAME_KEY_NAME); ConfigResource resource = new ConfigResource(resourceType, resourceName);
Collections.singletonMap(new ConfigResource(ConfigResource.Type.TOPIC, "foo"), new DescribeConfigsResponse.Config(ApiError.NONE, Collections.emptySet())))); DescribeConfigsResult result2 = env.adminClient().describeConfigs(Collections.singleton( new ConfigResource(ConfigResource.Type.TOPIC, "foo"))); time.sleep(5000); result2.values().get(new ConfigResource(ConfigResource.Type.TOPIC, "foo")).get();
env.adminClient().describeConfigs(Collections.singleton(new ConfigResource(ConfigResource.Type.BROKER, "0"))).all().get(); fail("Expected an authentication error."); } catch (ExecutionException e) {
/** * Get the configuration for topic. */ public TopicConfig getTopicConfig(final String topic) { final ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, topic); return new TopicConfig(describeResource(configResource)); }
/** * Get the configuration for topic. */ public BrokerConfig getBrokerConfig(final String brokerId) { final ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, brokerId); return new BrokerConfig(describeResource(configResource)); }
/** * Return a singleton map from the topic {@link ConfigResource} for the given topic, * to the {@link Config} of the given topic. */ public static Map<ConfigResource, Config> toTopicConfig(Topic topic) { Set<ConfigEntry> configEntries = new HashSet<>(); for (Map.Entry<String, String> entry : topic.getConfig().entrySet()) { configEntries.add(new ConfigEntry(entry.getKey(), entry.getValue())); } Config config = new Config(configEntries); return Collections.singletonMap( new ConfigResource(ConfigResource.Type.TOPIC, topic.getTopicName().toString()), config); }
private Config getKafkaBrokerConfig(AdminClient admin) throws Exception { final Collection<Node> nodes = admin.describeCluster().nodes().get(KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); if (nodes.isEmpty()) { throw new ConnectException("No brokers available to obtain default settings"); } String nodeId = nodes.iterator().next().idString(); Set<ConfigResource> resources = Collections.singleton(new ConfigResource(ConfigResource.Type.BROKER, nodeId)); final Map<ConfigResource, Config> configs = admin.describeConfigs(resources).all().get( KAFKA_QUERY_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS ); if (configs.isEmpty()) { throw new ConnectException("No configs have been received"); } return configs.values().iterator().next(); } }
private Map<String, String> topicConfiguration(String topicName) { Map<String, String> configMap = Collections.emptyMap(); try { ConfigResource cr = new ConfigResource(ConfigResource.Type.TOPIC, topicName); DescribeConfigsResult dc = adminClient.describeConfigs(singleton(cr)); Map<ConfigResource, Config> configs = dc.all().get(); Config config = configs.get(cr); configMap = config.entries().stream().filter(x -> !x.isDefault() && !x.isReadOnly()).collect(toMap(x -> x.name(), x -> x.value())); log.debug("Existing configuration for topic {} is {}", topicName, configMap); } catch (InterruptedException | ExecutionException e) { log.warn("Exception occured during topic configuration retrieval. name: {}", topicName, e); } return configMap; }
/** * Modify configuration values for a specific topic. * @param topic The topic to modify. * @param configItems Map of Key to Value to modify. * @return boolean */ public TopicConfig alterTopicConfig(final String topic, final Map<String, String> configItems) { try { // Define the resource we want to modify, the topic. final ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, topic); final List<ConfigEntry> configEntries = new ArrayList<>(); for (final Map.Entry<String, String> entry : configItems.entrySet()) { configEntries.add( new ConfigEntry(entry.getKey(), entry.getValue()) ); } // Define the configuration set final Config config = new Config(configEntries); // Create the topic final AlterConfigsResult result = adminClient.alterConfigs(Collections.singletonMap(configResource, config)); // Wait for the async request to process. result.all().get(); // Lets return updated topic details return getTopicConfig(topic); } catch (final InterruptedException | ExecutionException exception) { // TODO Handle this throw new RuntimeException(exception.getMessage(), exception); } }
/** * Get a topic config via the Kafka AdminClient API, calling the given handler * (in a different thread) with the result. */ @Override public void topicMetadata(TopicName topicName, Handler<AsyncResult<TopicMetadata>> handler) { LOGGER.debug("Getting metadata for topic {}", topicName); ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName.toString()); KafkaFuture<TopicDescription> descriptionFuture = adminClient.describeTopics( Collections.singleton(topicName.toString())).values().get(topicName.toString()); KafkaFuture<Config> configFuture = adminClient.describeConfigs( Collections.singleton(resource)).values().get(resource); queueWork(new MetadataWork(descriptionFuture, configFuture, result -> handler.handle(result))); }