A few random points after browsing this issue...
We can not use current versionsHash (unless we cache all the individual version numbers), as it is not additive.
The current versionsHash is additive (it must be, because as you say segments may not line up between leader and replica, and document order may differ). When caching per segment, keep this property by simply adding the segment fingerprints together. Am I missing something here?
private final Map<LeafReaderContext, ...
LeafReaderContext objects are not reused between changed indexes, so it would not make an effective cache key.
Use the core cache key, as FieldCache does.
We should keep in mind that previously written segments can change if there are deletes.
Right... the core cache key does not change, even if there are deletes for the segment.
One way to handle this w/o recomputing everything is:
1) compute the hash w/o regard to deleted docs
2) compute the hash of deleted docs only
3) subtract the two values to obtain your hash
4) keep track of the numDocs in the segment as well... if that doesn't change, no need to recompute. If it does change, then there were additional deletions. Recompute the hash for the deleted docs.