Elasticsearch7.x 配置解析jvm参数

如题所述

第1个回答  2022-06-01
源码地址   https://github.com/elastic/elasticsearch

ES配置文件在$ES_HOME/config目录下,包含三个配置文件

jvm.options 关于JVM配置相关的

## JVM configuration    

################################################################

## IMPORTANT: JVM heap size  重要的配置 堆内存 

################################################################

##

## You should always set the min and max JVM heap

## size to the same value. For example, to set

## the heap to 4 GB, set:

##  初始堆和最大堆 设置相等 原因 : JVM初始分配的内存由-Xms指定,默认是物理内存的1/64;JVM最大分配的内存由-Xmx指定,默认是物理内存的1/4。默认空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制;空余堆内存大于70%时,JVM会减少堆直到-Xms的最小限制。因此服务器一般设置-Xms、-Xmx相等以避免在每次GC后调整堆的大小。

## -Xms4g

## -Xmx4g

## 关于堆的大小,参考官方文章,64位机器上大内存的使用最佳建议。

## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html

## for more information

##

################################################################

# Xms represents the initial size of total heap space

# Xmx represents the maximum size of total heap space

-Xms${heap.min}

-Xmx${heap.max}

################################################################

## Expert settings  顾名思义,没事不要乱动

################################################################

##

## All settings below this section are considered

## expert settings. Don't tamper with them unless

## you understand what you are doing

##

################################################################

## GC configuration   CMS收集器 重点说下这两个参数,一般是成对出现,

XX:CMSInitiatingOccupancyFraction  指设定CMS在对内存占用率达到75%的时候开始GC

-XX+UseCMSInitiatingOccupancyOnly  JVM通过CMSInitiatingOccupancyFraction设置的阀值来启动gc动作,划重点 1每一次 2 意味着gc动作不是通过运行时收集的数据来启动。

-XX:+UseConcMarkSweepGC

-XX:CMSInitiatingOccupancyFraction=75   

-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration

# NOTE: G1GC is only supported on JDK version 10 or later.  g1就不多说了,生产环境10+的应该很少。

# To use G1GC uncomment the lines below.

# 10-:-XX:-UseConcMarkSweepGC

# 10-:-XX:-UseCMSInitiatingOccupancyOnly

# 10-:-XX:+UseG1GC

# 10-:-XX:G1ReservePercent=25

# 10-:-XX:InitiatingHeapOccupancyPercent=30

那么,到底是选择CMS还是G1还是ZGC 其实取决你最终的节点内存配置,按照官方建议不超过64G的话,小于32G CMS 大于32小于64 G1。

## DNS cache policy  

稍微解释一下,ES本身用自己的DNS缓存策略覆盖了JDK默认策略,还是贴个源码来看

启动类  org.elasticsearch.bootstrap.Elasticsearch.java

private static void overrideDnsCachePolicyProperties() {

for(final String property:new String[] {"networkaddress.cache.ttl","networkaddress.cache.negative.ttl"}) {

final String overrideProperty = "es."+property;

final String overrideValue = System.getProperty(overrideProperty);

if(overrideValue!=null) {

try{

//为啥是Security.setProperty 自己领悟。历史原因!!!

Security.setProperty(property,Integer.toString(Integer.valueOf(overrideValue)));

}catch(finalNumberFormatExceptione) {

thrownewIllegalArgumentException(

"failed to parse ["+overrideProperty+"] with value ["+overrideValue+"]", e);

                }

            }

        }

    }

这两个参数解释下 

networkaddress.cache.ttl DNS解析成功缓存时间

networkaddress.cache.negative.ttl  DNS解析失败缓存时间

当然你也可以直接修改jdk默认值来实现,jdk1.8.0_202.jdk/Contents/Home/jre/lib/security/java.security 默认分别是-1永不过期 10秒过期

# cache ttl in seconds for positive DNS lookups noting that this overrides the

# JDK security property networkaddress.cache.ttl; set to -1 to cache forever

-Des.networkaddress.cache.ttl=60

# cache ttl in seconds for negative DNS lookups noting that this overrides the

# JDK security property networkaddress.cache.negative ttl; set to -1 to cache

# forever

-Des.networkaddress.cache.negative.ttl=10

## optimizations

# pre-touch memory pages used by the JVM during initialization

这个参数至关重要,虽然说配置了JVM内存,但是真实的内存诉求要等到JVM真正使用的时候才分配。一句话,操作系统给JVM画的内存饼啥时候实现?

优势 1加速Eden分配速度 2 减少YGC时间

劣势 进程启动需要更多时间

自行选择吧

-XX:+AlwaysPreTouch

## basic

# explicitly set the stack size  不多讲了,栈的大小自行调节。

-Xss1m

# set to headless, just in case 不重要的参数,可以不关注了。。。。

-Djava.awt.headless=true

# ensure UTF-8 encoding by default (e.g. filenames)

-Dfile.encoding=UTF-8

# use our provided JNA always versus the system one

-Djna.nosys=true

# turn off a JDK optimization that throws away stack traces for common

# exceptions because stack traces are important for debugging

-XX:-OmitStackTraceInFastThrow

# flags to configure Netty

-Dio.netty.noUnsafe=true

-Dio.netty.noKeySetOptimization=true

-Dio.netty.recycler.maxCapacityPerThread=0

-Dio.netty.allocator.numDirectArenas=0

# log4j 2

-Dlog4j.shutdownHookEnabled=false

-Dlog4j2.disable.jmx=true

-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails

# heap dumps are created in the working directory of the JVM

-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and

# has sufficient space

${heap.dump.path}

# specify an alternative path for JVM fatal error logs

${error.file}

## JDK 8 GC logging

8:-XX:+PrintGCDetails

8:-XX:+PrintGCDateStamps

8:-XX:+PrintTenuringDistribution

8:-XX:+PrintGCApplicationStoppedTime

8:-Xloggc:${loggc}

8:-XX:+UseGCLogFileRotation

8:-XX:NumberOfGCLogFiles=32

8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging

9-:-Xlog:gc*,gc+age=trace,safepoint:file=${loggc}:utctime,pid,tags:filecount=32,filesize=64m

# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise

# time/date parsing will break in an incompatible way for some date patterns and locals

9-:-Djava.locale.providers=COMPAT
相似回答