- Notifications
You must be signed in to change notification settings - Fork 25.6k
Cache modification time of translog writer file #95107
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
Conversation
… cost time of nodes and indices stats Change-Id: I74ddbce3cdd8f4c3263a8cd51fa8b76bd097ae17
| Pinging @elastic/es-distributed (Team:Distributed) |
Change-Id: I241b669b6913a94a7d41ca08dade693663494ca6
| @astefan can you help to invite a reviewer for this pr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! Left a few comments to consider. But overall makes sense to me to cache this value.
| | ||
| @Override | ||
| protected boolean needsRefresh() { | ||
| LastModifiedTime cached = getNoRefresh(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the modification time is connected to the written file, I wonder if it would be better to judge the need for refresh based on the getWrittenOffset() value (which is directly got from the file channel that the translog writes to) rather than on the combination of operationCounter and lastSyncedCheckpoint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kingherc thanks for your time and patient. I think it wouldn't be better if we judge the need for refresh base on getWrittenOffset, because getWrittenOffset also invokes a syscall every time that is same as BaseTranslogReader#getLastModifiedTime
server/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java Outdated Show resolved Hide resolved
| return lastModifiedTime.getOrRefresh().lastModifiedTime; | ||
| } catch (UncheckedIOException e) { | ||
| // wrapped in the cache and unwrap here | ||
| throw e.getCause(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using a SingleObjectCache, for which you basically do not use the time refresh functionality, I wonder if simply extending the LastModifiedTime record with a similar function .getOrRefresh() would be more straightforward and would also enable throwing the involved IOException directly rather than doing this juggling with the UncheckedIOException.
org.elasticsearch.index.translog.TranslogReader#getLastModifiedTime also has such a simple caching (rather than using the SingleObjectCache).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, fixed
…SingleObjectCache Change-Id: I723a19f282ff261ffd3bf1124f4fd91d571e6320
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some final comments.
Could you also add some testing around the new last modified time behavior? E.g., in server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java there are some tests around TranslogWriter and you could assert that the last modified time is modified once there's a sync of the translog or some operations added.
Thanks again!
| | ||
| @Override | ||
| public long getLastModifiedTime() throws IOException { | ||
| if (lastModifiedTime.totalOffset() != totalOffset || lastModifiedTime.syncedOffset() != lastSyncedCheckpoint.offset) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delved a bit more into the code. I think this is correct apart from one weird edge case I see: org.elasticsearch.index.translog.TranslogWriter#readBytes() seems to be writing buffered ops into the channel file without touching totalOffset nor lastSyncedCheckpoint. Would it make sense to reset the lastModifiedTime (e.g., set its totalOffset to -1 in readBytes() in that if statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find usage of org.elasticsearch.index.translog.TranslogWriter#getLastModifiedTime(),the last mtime of writer file is only used to find earliest last modified age of translog files to construct translog stats,so it seems not need an exact value
If only judge the need for refresh based on lastSyncedCheckpoint, the cached last mtime maybe lag too much when translog durability is set async and sync_interval is set too large, so adding totalOffset condition to get mtime more frequent to decrease the lag
I think writing buffered ops into channel may not change mtime, so not need set totalOffset to -1 in readBytes() to keep cache logic simple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed I see it's only used in stats, so I agree there is no need to make it more complicated for this rather unusual edge case.
server/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java Outdated Show resolved Hide resolved
| @elasticmachine test this please |
Change-Id: I26330eee653ba13588669c24b55e354e33d28f60
@kingherc I add a test, thanks again for you time and patient ❤️ |
| @elasticmachine test this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR! Feel free to merge it.
Change-Id: I99c0b28a2da7ab69d0e100485fed406ef698156d
…/elasticsearch into cache_translog_stats Change-Id: I52d20ad9242b4265404442670554545a826bb057
| @elasticmachine test this please |
| @loupipalien I merged this. Feel free to tell me if there was anything else remaining and we can do another PR. Thanks for this! |


Usually, several indices are written even the cluster has a lot of indices, so we can cache modified time of translog writer file which already not be written to reduce cost time of nodes and indices stats