diff --git a/README.md b/README.md index a5cd988f87f..9bd6ee4c888 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ In order to deploy both the stacks the user needs to provide a set of required a | account (Optional) | string | User provided aws account | | dataNodeStorage (Optional) | string | User provided ebs block storage size, defaults to 100Gb | | mlNodeStorage (Optional) | string | User provided ebs block storage size, defaults to 100Gb | -| jvmHeapSize (Optional) | integer | User provided JVM heap memory size (integer) in Gb, defaults to 1Gb, e.g. `--context jvmHeapSize=2` to set heap to 2Gb | +| use50PercentHeap (Optional) | boolean | Boolean flag to use 50% of physical memory as heap. Default is 1GB. e.g., `--context use50PercentHeap=true` | diff --git a/lib/infra/infra-stack.ts b/lib/infra/infra-stack.ts index 74a05fc34a3..3350cae586b 100644 --- a/lib/infra/infra-stack.ts +++ b/lib/infra/infra-stack.ts @@ -55,7 +55,7 @@ export interface infraProps extends StackProps{ readonly mlNodeStorage: number, readonly jvmSysPropsString?: string, readonly additionalConfig?: string, - readonly jvmHeapSize?: number, + readonly use50PercentHeap: boolean, } export class InfraStack extends Stack { @@ -471,11 +471,13 @@ export class InfraStack extends Stack { // Check if JVM Heap Memory is set. Default is 1G in the jvm.options file // @ts-ignore - if (props.jvmHeapSize > 1) { - const minHeap = `-Xms${props.jvmHeapSize}g`; - const maxHeap = `-Xmx${props.jvmHeapSize}g`; - cfnInitConfig.push(InitCommand.shellCommand(`set -ex; cd opensearch; sed -i -e "s/^-Xms[0-9a-z]*$/${minHeap}/g" config/jvm.options; - sed -i -e "s/^-Xmx[0-9a-z]*$/${maxHeap}/g" config/jvm.options;`, { + if (props.use50PercentHeap) { + cfnInitConfig.push(InitCommand.shellCommand(`set -ex; cd opensearch; + totalMem=\`expr $(free -g | awk '/^Mem:/{print $2}') + 1\`; + heapSizeInGb=\`expr $totalMem / 2\`; + if [ $heapSizeInGb -lt 32 ];then minHeap="-Xms"$heapSizeInGb"g";maxHeap="-Xmx"$heapSizeInGb"g";else minHeap="-Xms32g";maxHeap="-Xmx32g";fi + sed -i -e "s/^-Xms[0-9a-z]*$/$minHeap/g" config/jvm.options; + sed -i -e "s/^-Xmx[0-9a-z]*$/$maxHeap/g" config/jvm.options;`, { cwd: '/home/ec2-user', ignoreErrors: false, })); diff --git a/lib/os-cluster-entrypoint.ts b/lib/os-cluster-entrypoint.ts index 93c51c6d4c9..c9de89d6b87 100644 --- a/lib/os-cluster-entrypoint.ts +++ b/lib/os-cluster-entrypoint.ts @@ -37,7 +37,6 @@ export class OsClusterEntrypoint { let dataNodeStorage: number; let mlNodeStorage: number; let ymlConfig: string = 'undefined'; - let jvmHeapSize: number = 1; const vpcId: string = scope.node.tryGetContext('vpcId'); const securityGroupId = scope.node.tryGetContext('securityGroupId'); @@ -149,13 +148,8 @@ export class OsClusterEntrypoint { const suffix = `${scope.node.tryGetContext('suffix')}`; - const heapSize = `${scope.node.tryGetContext('jvmHeapSize')}`; - if (heapSize !== 'undefined') { - jvmHeapSize = parseInt(heapSize, 10); - if (Number.isNaN(jvmHeapSize)) { - throw new Error('heapSize parameter has to be a number'); - } - } + const use50heap = `${scope.node.tryGetContext('use50PercentHeap')}`; + const use50PercentHeap = use50heap === 'true'; const network = new NetworkStack(scope, 'opensearch-network-stack', { cidrBlock: cidrRange, @@ -201,7 +195,7 @@ export class OsClusterEntrypoint { mlNodeStorage, jvmSysPropsString: jvmSysProps, additionalConfig: ymlConfig, - jvmHeapSize, + use50PercentHeap, ...props, });