Search in sources :

Example 1 with Type

use of io.strimzi.kafka.config.model.Type in project strimzi by strimzi.

the class DynamicConfSharedST method generateTestCases.

/**
 * Method, which dynamically generate test cases based on Kafka version
 * @param kafkaVersion specific kafka version
 * @return String generated test cases
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity" })
private static Map<String, Object> generateTestCases(String kafkaVersion) {
    Map<String, ConfigModel> dynamicProperties = KafkaUtils.getDynamicConfigurationProperties(kafkaVersion);
    Map<String, Object> testCases = new HashMap<>();
    dynamicProperties.forEach((key, value) -> {
        Type type = value.getType();
        Object stochasticChosenValue;
        switch(type) {
            case STRING:
                switch(key) {
                    case "compression.type":
                        List<String> compressionTypes = Arrays.asList("snappy", "gzip", "lz4", "zstd");
                        stochasticChosenValue = compressionTypes.get(ThreadLocalRandom.current().nextInt(0, compressionTypes.size() - 1));
                        break;
                    case "log.message.timestamp.type":
                        stochasticChosenValue = "LogAppendTime";
                        break;
                    case "ssl.protocol":
                        stochasticChosenValue = "TLSv1.1";
                        break;
                    default:
                        stochasticChosenValue = " ";
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case INT:
            case LONG:
                switch(key) {
                    case "num.recovery.threads.per.data.dir":
                    case "log.cleaner.threads":
                    case "num.network.threads":
                    case "min.insync.replicas":
                    case "num.replica.fetchers":
                    case "num.partitions":
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(2, 3);
                        break;
                    case "log.cleaner.io.buffer.load.factor":
                    case "log.retention.ms":
                    case "max.connections":
                    case "max.connections.per.ip":
                    case "background.threads":
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(4, 20);
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(100, 50_000);
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case DOUBLE:
                switch(key) {
                    case "log.cleaner.min.cleanable.dirty.ratio":
                    case "log.cleaner.min.cleanable.ratio":
                        stochasticChosenValue = ThreadLocalRandom.current().nextDouble(0, 1);
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextDouble(1, 20);
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case BOOLEAN:
                switch(key) {
                    case "unclean.leader.election.enable":
                    case "log.preallocate":
                        stochasticChosenValue = true;
                        break;
                    case "log.message.downconversion.enable":
                        stochasticChosenValue = false;
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(2) == 0;
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case LIST:
                // log.cleanup.policy = [delete, compact] -> default delete
                switch(key) {
                    case "log.cleanup.policy":
                        stochasticChosenValue = "compact";
                        break;
                    case "ssl.enabled.protocols":
                        stochasticChosenValue = "TLSv1.1";
                        break;
                    default:
                        stochasticChosenValue = " ";
                }
                testCases.put(key, stochasticChosenValue);
        }
        // skipping these configuration, which doesn't work appear in the kafka pod (TODO: investigate why!)
        testCases.remove("num.recovery.threads.per.data.dir");
        testCases.remove("num.io.threads");
        testCases.remove("log.cleaner.dedupe.buffer.size");
        testCases.remove("num.partitions");
        // skipping these configuration exceptions
        testCases.remove("ssl.cipher.suites");
        testCases.remove("zookeeper.connection.timeout.ms");
        testCases.remove("zookeeper.connect");
    });
    return testCases;
}
Also used : Type(io.strimzi.kafka.config.model.Type) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) HashMap(java.util.HashMap)

Example 2 with Type

use of io.strimzi.kafka.config.model.Type in project strimzi-kafka-operator by strimzi.

the class KafkaConfigModelGenerator method configs.

private static Map<String, ConfigModel> configs() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    ConfigDef def = brokerConfigs();
    Map<String, String> dynamicUpdates = brokerDynamicUpdates();
    Method getConfigValueMethod = def.getClass().getDeclaredMethod("getConfigValue", ConfigDef.ConfigKey.class, String.class);
    getConfigValueMethod.setAccessible(true);
    Method sortedConfigs = ConfigDef.class.getDeclaredMethod("sortedConfigs");
    sortedConfigs.setAccessible(true);
    List<ConfigDef.ConfigKey> keys = (List) sortedConfigs.invoke(def);
    Map<String, ConfigModel> result = new TreeMap<>();
    for (ConfigDef.ConfigKey key : keys) {
        String configName = String.valueOf(getConfigValueMethod.invoke(def, key, "Name"));
        Type type = parseType(String.valueOf(getConfigValueMethod.invoke(def, key, "Type")));
        Scope scope = parseScope(dynamicUpdates.getOrDefault(key.name, "read-only"));
        ConfigModel descriptor = new ConfigModel();
        descriptor.setType(type);
        descriptor.setScope(scope);
        if (key.validator instanceof ConfigDef.Range) {
            descriptor = range(key, descriptor);
        } else if (key.validator instanceof ConfigDef.ValidString) {
            descriptor.setValues(enumer(key.validator));
        } else if (key.validator instanceof ConfigDef.ValidList) {
            descriptor.setItems(validList(key));
        } else if (key.validator instanceof ApiVersionValidator$) {
            Iterator<ApiVersion> iterator = ApiVersion$.MODULE$.allVersions().iterator();
            LinkedHashSet<String> versions = new LinkedHashSet<>();
            while (iterator.hasNext()) {
                ApiVersion next = iterator.next();
                ApiVersion$.MODULE$.apply(next.shortVersion());
                versions.add(Pattern.quote(next.shortVersion()) + "(\\.[0-9]+)*");
                ApiVersion$.MODULE$.apply(next.version());
                versions.add(Pattern.quote(next.version()));
            }
            descriptor.setPattern(String.join("|", versions));
        } else if (key.validator instanceof ConfigDef.NonEmptyString) {
            descriptor.setPattern(".+");
        } else if (key.validator instanceof RaftConfig.ControllerQuorumVotersValidator) {
            continue;
        } else if (key.validator != null) {
            throw new IllegalStateException("Invalid validator class " + key.validator.getClass() + " for option " + configName);
        }
        result.put(configName, descriptor);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) Iterator(scala.collection.Iterator) List(java.util.List) RaftConfig(org.apache.kafka.raft.RaftConfig) ApiVersionValidator$(kafka.api.ApiVersionValidator$) ApiVersion(kafka.api.ApiVersion) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Type(io.strimzi.kafka.config.model.Type) Scope(io.strimzi.kafka.config.model.Scope) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 3 with Type

use of io.strimzi.kafka.config.model.Type in project strimzi by strimzi.

the class KafkaConfigModelGenerator method configs.

private static Map<String, ConfigModel> configs() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    ConfigDef def = brokerConfigs();
    Map<String, String> dynamicUpdates = brokerDynamicUpdates();
    Method getConfigValueMethod = def.getClass().getDeclaredMethod("getConfigValue", ConfigDef.ConfigKey.class, String.class);
    getConfigValueMethod.setAccessible(true);
    Method sortedConfigs = ConfigDef.class.getDeclaredMethod("sortedConfigs");
    sortedConfigs.setAccessible(true);
    List<ConfigDef.ConfigKey> keys = (List) sortedConfigs.invoke(def);
    Map<String, ConfigModel> result = new TreeMap<>();
    for (ConfigDef.ConfigKey key : keys) {
        String configName = String.valueOf(getConfigValueMethod.invoke(def, key, "Name"));
        Type type = parseType(String.valueOf(getConfigValueMethod.invoke(def, key, "Type")));
        Scope scope = parseScope(dynamicUpdates.getOrDefault(key.name, "read-only"));
        ConfigModel descriptor = new ConfigModel();
        descriptor.setType(type);
        descriptor.setScope(scope);
        if (key.validator instanceof ConfigDef.Range) {
            descriptor = range(key, descriptor);
        } else if (key.validator instanceof ConfigDef.ValidString) {
            descriptor.setValues(enumer(key.validator));
        } else if (key.validator instanceof ConfigDef.ValidList) {
            descriptor.setItems(validList(key));
        } else if (key.validator instanceof ApiVersionValidator$) {
            Iterator<ApiVersion> iterator = ApiVersion$.MODULE$.allVersions().iterator();
            LinkedHashSet<String> versions = new LinkedHashSet<>();
            while (iterator.hasNext()) {
                ApiVersion next = iterator.next();
                ApiVersion$.MODULE$.apply(next.shortVersion());
                versions.add(Pattern.quote(next.shortVersion()) + "(\\.[0-9]+)*");
                ApiVersion$.MODULE$.apply(next.version());
                versions.add(Pattern.quote(next.version()));
            }
            descriptor.setPattern(String.join("|", versions));
        } else if (key.validator instanceof ConfigDef.NonEmptyString) {
            descriptor.setPattern(".+");
        } else if (key.validator instanceof RaftConfig.ControllerQuorumVotersValidator) {
            continue;
        } else if (key.validator != null) {
            throw new IllegalStateException("Invalid validator class " + key.validator.getClass() + " for option " + configName);
        }
        result.put(configName, descriptor);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) Iterator(scala.collection.Iterator) List(java.util.List) RaftConfig(org.apache.kafka.raft.RaftConfig) ApiVersionValidator$(kafka.api.ApiVersionValidator$) ApiVersion(kafka.api.ApiVersion) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Type(io.strimzi.kafka.config.model.Type) Scope(io.strimzi.kafka.config.model.Scope) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 4 with Type

use of io.strimzi.kafka.config.model.Type in project strimzi-kafka-operator by strimzi.

the class DynamicConfSharedST method generateTestCases.

/**
 * Method, which dynamically generate test cases based on Kafka version
 * @param kafkaVersion specific kafka version
 * @return String generated test cases
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity" })
private static Map<String, Object> generateTestCases(String kafkaVersion) {
    Map<String, ConfigModel> dynamicProperties = KafkaUtils.getDynamicConfigurationProperties(kafkaVersion);
    Map<String, Object> testCases = new HashMap<>();
    dynamicProperties.forEach((key, value) -> {
        Type type = value.getType();
        Object stochasticChosenValue;
        switch(type) {
            case STRING:
                switch(key) {
                    case "compression.type":
                        List<String> compressionTypes = Arrays.asList("snappy", "gzip", "lz4", "zstd");
                        stochasticChosenValue = compressionTypes.get(ThreadLocalRandom.current().nextInt(0, compressionTypes.size() - 1));
                        break;
                    case "log.message.timestamp.type":
                        stochasticChosenValue = "LogAppendTime";
                        break;
                    case "ssl.protocol":
                        stochasticChosenValue = "TLSv1.1";
                        break;
                    default:
                        stochasticChosenValue = " ";
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case INT:
            case LONG:
                switch(key) {
                    case "num.recovery.threads.per.data.dir":
                    case "log.cleaner.threads":
                    case "num.network.threads":
                    case "min.insync.replicas":
                    case "num.replica.fetchers":
                    case "num.partitions":
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(2, 3);
                        break;
                    case "log.cleaner.io.buffer.load.factor":
                    case "log.retention.ms":
                    case "max.connections":
                    case "max.connections.per.ip":
                    case "background.threads":
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(4, 20);
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(100, 50_000);
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case DOUBLE:
                switch(key) {
                    case "log.cleaner.min.cleanable.dirty.ratio":
                    case "log.cleaner.min.cleanable.ratio":
                        stochasticChosenValue = ThreadLocalRandom.current().nextDouble(0, 1);
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextDouble(1, 20);
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case BOOLEAN:
                switch(key) {
                    case "unclean.leader.election.enable":
                    case "log.preallocate":
                        stochasticChosenValue = true;
                        break;
                    case "log.message.downconversion.enable":
                        stochasticChosenValue = false;
                        break;
                    default:
                        stochasticChosenValue = ThreadLocalRandom.current().nextInt(2) == 0;
                }
                testCases.put(key, stochasticChosenValue);
                break;
            case LIST:
                // log.cleanup.policy = [delete, compact] -> default delete
                switch(key) {
                    case "log.cleanup.policy":
                        stochasticChosenValue = "compact";
                        break;
                    case "ssl.enabled.protocols":
                        stochasticChosenValue = "TLSv1.1";
                        break;
                    default:
                        stochasticChosenValue = " ";
                }
                testCases.put(key, stochasticChosenValue);
        }
        // skipping these configuration, which doesn't work appear in the kafka pod (TODO: investigate why!)
        testCases.remove("num.recovery.threads.per.data.dir");
        testCases.remove("num.io.threads");
        testCases.remove("log.cleaner.dedupe.buffer.size");
        testCases.remove("num.partitions");
        // skipping these configuration exceptions
        testCases.remove("ssl.cipher.suites");
        testCases.remove("zookeeper.connection.timeout.ms");
        testCases.remove("zookeeper.connect");
    });
    return testCases;
}
Also used : Type(io.strimzi.kafka.config.model.Type) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) HashMap(java.util.HashMap)

Aggregations

ConfigModel (io.strimzi.kafka.config.model.ConfigModel)4 Type (io.strimzi.kafka.config.model.Type)4 Scope (io.strimzi.kafka.config.model.Scope)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 ApiVersion (kafka.api.ApiVersion)2 ApiVersionValidator$ (kafka.api.ApiVersionValidator$)2 ConfigDef (org.apache.kafka.common.config.ConfigDef)2 RaftConfig (org.apache.kafka.raft.RaftConfig)2 Iterator (scala.collection.Iterator)2