zhaih commented on code in PR #13581: URL: https://github.com/apache/lucene/pull/13581#discussion_r1698827513
########## lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java: ########## @@ -327,12 +331,14 @@ private void addDiverseNeighbors(int level, int node, NeighborArray candidates) continue; } int nbr = candidates.nodes()[i]; - NeighborArray nbrsOfNbr = hnsw.getNeighbors(level, nbr); - nbrsOfNbr.rwlock.writeLock().lock(); - try { + if (hnswLock != null) { + try (HnswLock.LockedRow rowLock = hnswLock.write(level, nbr)) { + NeighborArray nbrsOfNbr = rowLock.row; Review Comment: This way we're not forcing people to use the lock if it presents, e.g. someone can still carelessly just use `hnsw.getNeighbors` without getting any errors. I wonder whether we could put the lock in `OnHeapHnswGraph` and expose a `getNeighbors(int level, int node, Consumer<NeighborArray> operation, bool read)` method instead? Also we can throw exception in original method in case the lock exists? ########## lucene/core/src/java/org/apache/lucene/util/hnsw/HnswLock.java: ########## @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.util.hnsw; + +import java.io.Closeable; +import java.util.Objects; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Provide (read-and-write) locked access to rows of an OnHeapHnswGraph. For use by + * HnswConcurrentMerger and its HnswGraphBuilders. + */ +public class HnswLock { Review Comment: package-private since no other method is public? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org