Skip to content
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

Resolve the role query and the number of docs lazily #48036

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.lucene.util.BitSetIterator;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.CombinedBitSet;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.cache.Cache;
import org.elasticsearch.common.cache.CacheBuilder;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static DocumentSubsetDirectoryReader wrap(DirectoryReader in, DocumentSub
/**
* Compute the number of live documents. This method is SLOW.
*/
private static int computeNumDocs(LeafReader reader, Query roleQuery, BitSet roleQueryBits) {
private static int computeNumDocs(LeafReader reader, BitSet roleQueryBits) {
final Bits liveDocs = reader.getLiveDocs();
if (roleQueryBits == null) {
return 0;
Expand Down Expand Up @@ -103,7 +104,7 @@ private static int getNumDocs(LeafReader reader, Query roleQuery, BitSet roleQue
throw e;
}
}
return perReaderCache.computeIfAbsent(roleQuery, q -> computeNumDocs(reader, roleQuery, roleQueryBits));
return perReaderCache.computeIfAbsent(roleQuery, q -> computeNumDocs(reader, roleQueryBits));
}

public static final class DocumentSubsetDirectoryReader extends FilterDirectoryReader {
Expand Down Expand Up @@ -152,21 +153,44 @@ public CacheHelper getReaderCacheHelper() {
}
}

private final BitSet roleQueryBits;
private final int numDocs;
private final DocumentSubsetBitsetCache bitsetCache;
private final Query roleQuery;
// we don't use a volatile here because the bitset is resolved before numDocs in the synchronized block
// so any thread that see numDocs != -1 should also see the true value of the roleQueryBits (happens-before).
private BitSet roleQueryBits;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this one need to be volatile too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it wouldn't be needed since we access the variable after the synchronized block ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about access from getLiveDocs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed offline and I added a comment in the code to explain why this is not needed (happens-before):
9e98a69

private volatile int numDocs = -1;

private DocumentSubsetReader(final LeafReader in, DocumentSubsetBitsetCache bitsetCache, final Query roleQuery) throws Exception {
private DocumentSubsetReader(final LeafReader in, DocumentSubsetBitsetCache bitsetCache, final Query roleQuery) {
super(in);
this.roleQueryBits = bitsetCache.getBitSet(roleQuery, in.getContext());
this.numDocs = getNumDocs(in, roleQuery, roleQueryBits);
this.bitsetCache = bitsetCache;
this.roleQuery = roleQuery;
}

/**
* Resolve the role query and the number of docs lazily
*/
private void computeNumDocsIfNeeded() {
if (numDocs == -1) {
synchronized (this) {
if (numDocs == -1) {
try {
roleQueryBits = bitsetCache.getBitSet(roleQuery, in.getContext());
numDocs = getNumDocs(in, roleQuery, roleQueryBits);
} catch (Exception e) {
throw new ElasticsearchException("Failed to load role query", e);
}
}
}
}
}

@Override
public Bits getLiveDocs() {
computeNumDocsIfNeeded();
final Bits actualLiveDocs = in.getLiveDocs();
if (roleQueryBits == null) {
// If we would a <code>null</code> liveDocs then that would mean that no docs are marked as deleted,
// but that isn't the case. No docs match with the role query and therefor all docs are marked as deleted
// If we would return a <code>null</code> liveDocs then that would mean that no docs are marked as deleted,
// but that isn't the case. No docs match with the role query and therefore all docs are marked as deleted
return new Bits.MatchNoBits(in.maxDoc());
} else if (actualLiveDocs == null) {
return roleQueryBits;
Expand All @@ -178,6 +202,7 @@ public Bits getLiveDocs() {

@Override
public int numDocs() {
computeNumDocsIfNeeded();
return numDocs;
}

Expand Down