Skip to content

Address FileChannel.lock runtime exception, address aliquot rollup deadlock #6721

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion api/src/org/labkey/api/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
Expand Down Expand Up @@ -1078,6 +1079,17 @@ public static void copyFile(File src, File dst) throws IOException
}
}

private static FileLock acquireLock(FileChannel out) throws IOException
{
try
{
return out.lock();
}
catch (OverlappingFileLockException e)
{
throw new IOException(e);
}
}

// FileUtil.copyFile() does not use transferTo() or sync()
public static void copyFile(ReadableByteChannel in, long expected, File dst) throws IOException
Expand All @@ -1092,7 +1104,7 @@ public static void copyFile(ReadableByteChannel in, long expected, File dst) thr

try (FileOutputStream os = new FileOutputStream(dst);
FileChannel out = os.getChannel();
FileLock lockOut = out.lock())
FileLock lockOut = acquireLock(out))
{
do
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -1700,7 +1701,7 @@ private Map<Integer, Pair<Integer, String>> getSampleAliquotCounts(Collection<In
.append(")-1 AS CreatedAliquotCount FROM exp.material AS m WHERE m.rowid\s");
dialect.appendInClauseSql(sql, sampleIds);

Map<Integer, Pair<Integer, String>> sampleAliquotCounts = new HashMap<>();
Map<Integer, Pair<Integer, String>> sampleAliquotCounts = new TreeMap<>(); // Order sample by rowId to reduce probability of deadlock with search indexer
try (ResultSet rs = new SqlSelector(dbSchema, sql).getResultSet())
{
while (rs.next())
Expand Down Expand Up @@ -1761,7 +1762,7 @@ SELECT RootMaterialRowId as rootRowId, COUNT(*) as aliquotCount
}
dialect.appendInClauseSql(sql, sampleIds);

Map<Integer, Pair<Integer, String>> sampleAliquotCounts = new HashMap<>();
Map<Integer, Pair<Integer, String>> sampleAliquotCounts = new TreeMap<>(); // Order by rowId to reduce deadlock with search indexer
try (ResultSet rs = new SqlSelector(dbSchema, sql).getResultSet())
{
while (rs.next())
Expand Down