- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 7
feat: Support configuring JVM arguments #819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
        
      
    
  
     Merged
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ae8c919
              
                feat: Support configuring JVM arguments
              
              
                sbernauer a1ceebc
              
                Add test
              
              
                sbernauer 34befa5
              
                Update mem detection
              
              
                sbernauer 3c25660
              
                Merge branch 'main' into feat/jvm-arguments
              
              
                sbernauer 1b63108
              
                Fix rustdoc
              
              
                sbernauer 5160716
              
                Add docs
              
              
                sbernauer 48895af
              
                Update docs/modules/kafka/pages/usage-guide/overrides.adoc
              
              
                sbernauer 7338df2
              
                Fix variable names in test
              
              
                sbernauer 7112592
              
                Update rust/operator-binary/src/config/jvm.rs
              
              
                sbernauer 4297e61
              
                Merge branch 'main' into feat/jvm-arguments
              
              
                sbernauer File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| use snafu::{OptionExt, ResultExt, Snafu}; | ||
| use stackable_kafka_crd::{ | ||
| KafkaConfig, KafkaConfigFragment, JVM_SECURITY_PROPERTIES_FILE, METRICS_PORT, | ||
| STACKABLE_CONFIG_DIR, | ||
| }; | ||
| use stackable_operator::{ | ||
| memory::{BinaryMultiple, MemoryQuantity}, | ||
| role_utils::{self, GenericRoleConfig, JavaCommonConfig, JvmArgumentOverrides, Role}, | ||
| }; | ||
|  | ||
| const JAVA_HEAP_FACTOR: f32 = 0.8; | ||
|  | ||
| #[derive(Snafu, Debug)] | ||
| pub enum Error { | ||
| #[snafu(display("invalid memory resource configuration - missing default or value in crd?"))] | ||
| MissingMemoryResourceConfig, | ||
|  | ||
| #[snafu(display("invalid memory config"))] | ||
| InvalidMemoryConfig { | ||
| source: stackable_operator::memory::Error, | ||
| }, | ||
|  | ||
| #[snafu(display("failed to merge jvm argument overrides"))] | ||
| MergeJvmArgumentOverrides { source: role_utils::Error }, | ||
| } | ||
|  | ||
| /// All JVM arguments. | ||
| fn construct_jvm_args( | ||
| merged_config: &KafkaConfig, | ||
| role: &Role<KafkaConfigFragment, GenericRoleConfig, JavaCommonConfig>, | ||
| role_group: &str, | ||
| ) -> Result<Vec<String>, Error> { | ||
| let heap_size = MemoryQuantity::try_from( | ||
| merged_config | ||
| .resources | ||
| .memory | ||
| .limit | ||
| .as_ref() | ||
| .context(MissingMemoryResourceConfigSnafu)?, | ||
| ) | ||
| .context(InvalidMemoryConfigSnafu)? | ||
| .scale_to(BinaryMultiple::Mebi) | ||
| * JAVA_HEAP_FACTOR; | ||
| let java_heap = heap_size | ||
| .format_for_java() | ||
| .context(InvalidMemoryConfigSnafu)?; | ||
|  | ||
| let jvm_args = vec![ | ||
| // Heap settings | ||
| format!("-Xmx{java_heap}"), | ||
| format!("-Xms{java_heap}"), | ||
| format!("-Djava.security.properties={STACKABLE_CONFIG_DIR}/{JVM_SECURITY_PROPERTIES_FILE}"), | ||
| format!("-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar={METRICS_PORT}:/stackable/jmx/broker.yaml") | ||
| ]; | ||
|  | ||
| let operator_generated = JvmArgumentOverrides::new_with_only_additions(jvm_args); | ||
| let merged = role | ||
| .get_merged_jvm_argument_overrides(role_group, &operator_generated) | ||
| .context(MergeJvmArgumentOverridesSnafu)?; | ||
| Ok(merged | ||
| .effective_jvm_config_after_merging() | ||
| // Sorry for the clone, that's how operator-rs is currently modelled :P | ||
| .clone()) | ||
| } | ||
|  | ||
| /// Arguments that go into `EXTRA_ARGS`, so *not* the heap settings (which you cen get using | ||
|         
                  sbernauer marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| /// [`construct_heap_jvm_args`]). | ||
| pub fn construct_non_heap_jvm_args( | ||
| merged_config: &KafkaConfig, | ||
| role: &Role<KafkaConfigFragment, GenericRoleConfig, JavaCommonConfig>, | ||
| role_group: &str, | ||
| ) -> Result<String, Error> { | ||
| let mut jvm_args = construct_jvm_args(merged_config, role, role_group)?; | ||
| jvm_args.retain(|arg| !is_heap_jvm_argument(arg)); | ||
|  | ||
| Ok(jvm_args.join(" ")) | ||
| } | ||
|  | ||
| /// Arguments that go into `KAFKA_HEAP_OPTS`. | ||
| /// You can get the normal JVM arguments using [`construct_non_heap_jvm_args`]. | ||
| pub fn construct_heap_jvm_args( | ||
| merged_config: &KafkaConfig, | ||
| role: &Role<KafkaConfigFragment, GenericRoleConfig, JavaCommonConfig>, | ||
| role_group: &str, | ||
| ) -> Result<String, Error> { | ||
| let mut jvm_args = construct_jvm_args(merged_config, role, role_group)?; | ||
| jvm_args.retain(|arg| is_heap_jvm_argument(arg)); | ||
|  | ||
| Ok(jvm_args.join(" ")) | ||
| } | ||
|  | ||
| fn is_heap_jvm_argument(jvm_argument: &str) -> bool { | ||
| let lowercase = jvm_argument.to_lowercase(); | ||
|  | ||
| lowercase.starts_with("-xms") || lowercase.starts_with("-xmx") | ||
| } | ||
|  | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use stackable_kafka_crd::{KafkaCluster, KafkaRole}; | ||
|  | ||
| use super::*; | ||
|  | ||
| #[test] | ||
| fn test_construct_jvm_arguments_defaults() { | ||
| let input = r#" | ||
| apiVersion: kafka.stackable.tech/v1alpha1 | ||
| kind: KafkaCluster | ||
| metadata: | ||
| name: simple-kafka | ||
| spec: | ||
| image: | ||
| productVersion: 3.7.1 | ||
| clusterConfig: | ||
| zookeeperConfigMapName: xyz | ||
| brokers: | ||
| roleGroups: | ||
| default: | ||
| replicas: 1 | ||
| "#; | ||
| let (kafka_role, role, merged_config) = construct_boilerplate(input); | ||
| let non_heap_jvm_args = | ||
| construct_non_heap_jvm_args(&kafka_role, &role, &merged_config).unwrap(); | ||
| let heap_jvm_args = construct_heap_jvm_args(&kafka_role, &role, &merged_config).unwrap(); | ||
|  | ||
| assert_eq!( | ||
| non_heap_jvm_args, | ||
| "-Djava.security.properties=/stackable/config/security.properties \ | ||
| -javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=9606:/stackable/jmx/broker.yaml" | ||
| ); | ||
| assert_eq!(heap_jvm_args, "-Xmx819m -Xms819m"); | ||
| } | ||
|  | ||
| #[test] | ||
| fn test_construct_jvm_argument_overrides() { | ||
| let input = r#" | ||
| apiVersion: kafka.stackable.tech/v1alpha1 | ||
| kind: KafkaCluster | ||
| metadata: | ||
| name: simple-kafka | ||
| spec: | ||
| image: | ||
| productVersion: 3.7.1 | ||
| clusterConfig: | ||
| zookeeperConfigMapName: xyz | ||
| brokers: | ||
| config: | ||
| resources: | ||
| memory: | ||
| limit: 42Gi | ||
| jvmArgumentOverrides: | ||
| add: | ||
| - -Dhttps.proxyHost=proxy.my.corp | ||
| - -Dhttps.proxyPort=8080 | ||
| - -Djava.net.preferIPv4Stack=true | ||
| roleGroups: | ||
| default: | ||
| replicas: 1 | ||
| jvmArgumentOverrides: | ||
| # We need more memory! | ||
| removeRegex: | ||
| - -Xmx.* | ||
| - -Dhttps.proxyPort=.* | ||
| add: | ||
| - -Xmx40000m | ||
| - -Dhttps.proxyPort=1234 | ||
| "#; | ||
| let (kafka_role, role, merged_config) = construct_boilerplate(input); | ||
| let non_heap_jvm_args = | ||
| construct_non_heap_jvm_args(&kafka_role, &role, &merged_config).unwrap(); | ||
| let heap_jvm_args = construct_heap_jvm_args(&kafka_role, &role, &merged_config).unwrap(); | ||
|  | ||
| assert_eq!( | ||
| non_heap_jvm_args, | ||
| "-Djava.security.properties=/stackable/config/security.properties \ | ||
| -javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=9606:/stackable/jmx/broker.yaml \ | ||
| -Dhttps.proxyHost=proxy.my.corp \ | ||
| -Djava.net.preferIPv4Stack=true \ | ||
| -Dhttps.proxyPort=1234" | ||
| ); | ||
| assert_eq!(heap_jvm_args, "-Xms34406m -Xmx40000m"); | ||
| } | ||
|  | ||
| fn construct_boilerplate( | ||
| kafka_cluster: &str, | ||
| ) -> ( | ||
| KafkaConfig, | ||
| Role<KafkaConfigFragment, GenericRoleConfig, JavaCommonConfig>, | ||
| String, | ||
| ) { | ||
| let kafka: KafkaCluster = serde_yaml::from_str(kafka_cluster).expect("illegal test input"); | ||
|  | ||
| let kafka_role = KafkaRole::Broker; | ||
| let rolegroup_ref = kafka.broker_rolegroup_ref("default"); | ||
| let merged_config = kafka.merged_config(&kafka_role, &rolegroup_ref).unwrap(); | ||
| let role = kafka.spec.brokers.unwrap(); | ||
|  | ||
| (merged_config, role, "default".to_owned()) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod jvm; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.