dsmiley commented on code in PR #2548: URL: https://github.com/apache/solr/pull/2548#discussion_r1664888729
########## solr/core/src/java/org/apache/solr/update/UpdateLocks.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.solr.update; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.lucene.util.BytesRef; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.util.IOFunction; + +/** + * Locks associated with updates in connection with the {@link UpdateLog}. + * + * @lucene.internal + */ +public class UpdateLocks { + // names are legacy oriented; TODO rename and use EnvUtils + private static final String SYS_PROP_BUCKET_VERSION_LOCK_TIMEOUT_MS = + "bucketVersionLockTimeoutMs"; + public static final int DEFAULT_TIMEOUT = + Integer.parseInt(System.getProperty(SYS_PROP_BUCKET_VERSION_LOCK_TIMEOUT_MS, "0")); + + private final long docLockTimeoutMs; + + private final ReadWriteLock lock = new ReentrantReadWriteLock(true); + + private final ConcurrentHashMap<BytesRef, LockAndCondition> idToLock = new ConcurrentHashMap<>(); + + public UpdateLocks(long docLockTimeoutMs) { + this.docLockTimeoutMs = docLockTimeoutMs; + } + + public <R> R runWithLock(BytesRef id, IOFunction<Condition, R> function) throws IOException { + final var startTimeNanos = System.nanoTime(); + + lockForUpdate(); + try { + + LockAndCondition lock; + while (true) { + lock = idToLock.computeIfAbsent(id, (k) -> new LockAndCondition()); + synchronized (lock) { + if (lock.refCount < 0) { + continue; // is being removed; try again + } + lock.refCount++; + break; + } + } + // try-finally ensuring we decrement the refCount + try { + return runWithLockInternal(id, function, lock, startTimeNanos); + } finally { + synchronized (lock) { + assert lock.refCount > 0; // because we incremented it + lock.refCount--; + if (lock.refCount == 0) { + idToLock.remove(id); + } + } + } + + } finally { + unlockForUpdate(); + } + } + + private <R> R runWithLockInternal( + BytesRef id, IOFunction<Condition, R> function, LockAndCondition lock, long startTimeNanos) + throws IOException { + // Acquire the lock + try { + if (docLockTimeoutMs == 0) { + lock.lock.lockInterruptibly(); + } else { + long remainingNs = + TimeUnit.MILLISECONDS.toNanos(docLockTimeoutMs) - (System.nanoTime() - startTimeNanos); + boolean timedOut = + !lock.lock.tryLock(remainingNs - System.nanoTime(), TimeUnit.NANOSECONDS); Review Comment: Doh! Good eye! -- 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...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org