You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vk...@apache.org on 2016/06/17 12:01:28 UTC
[2/4] ignite git commit: ignite-3239 Fix of java.io.IOException:
Resource deadlock avoided
ignite-3239 Fix of java.io.IOException: Resource deadlock avoided
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/07733323
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/07733323
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/07733323
Branch: refs/heads/master
Commit: 0773332335f965af3ec18a474138e3ea6157fc6e
Parents: 0fd1ab6
Author: agura <ag...@gridgain.com>
Authored: Thu Jun 16 13:03:03 2016 +0300
Committer: agura <ag...@gridgain.com>
Committed: Fri Jun 17 14:39:42 2016 +0300
----------------------------------------------------------------------
.../ignite/internal/MarshallerContextImpl.java | 29 ++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/07733323/modules/core/src/main/java/org/apache/ignite/internal/MarshallerContextImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/MarshallerContextImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/MarshallerContextImpl.java
index b4c9607..504ea6e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/MarshallerContextImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/MarshallerContextImpl.java
@@ -25,9 +25,11 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.List;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Lock;
import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryListenerException;
@@ -201,7 +203,7 @@ public class MarshallerContextImpl extends MarshallerContextAdapter {
File file = new File(workDir, fileName);
try (FileInputStream in = new FileInputStream(file)) {
- FileLock fileLock = in.getChannel().lock(0L, Long.MAX_VALUE, true);
+ FileLock fileLock = fileLock(in.getChannel(), true);
assert fileLock != null : fileName;
@@ -235,6 +237,26 @@ public class MarshallerContextImpl extends MarshallerContextAdapter {
}
/**
+ * @param ch File channel.
+ * @param shared Shared.
+ */
+ private static FileLock fileLock(
+ FileChannel ch,
+ boolean shared
+ ) throws IOException, IgniteInterruptedCheckedException {
+ ThreadLocalRandom rnd = ThreadLocalRandom.current();
+
+ while (true) {
+ FileLock fileLock = ch.tryLock(0L, Long.MAX_VALUE, shared);
+
+ if (fileLock == null)
+ U.sleep(rnd.nextLong(50));
+ else
+ return fileLock;
+ }
+ }
+
+ /**
*/
private static class ContinuousQueryListener implements CacheEntryUpdatedListener<Integer, String> {
/** */
@@ -270,7 +292,7 @@ public class MarshallerContextImpl extends MarshallerContextAdapter {
File file = new File(workDir, fileName);
try (FileOutputStream out = new FileOutputStream(file)) {
- FileLock fileLock = out.getChannel().lock(0L, Long.MAX_VALUE, false);
+ FileLock fileLock = fileLock(out.getChannel(), false);
assert fileLock != null : fileName;
@@ -284,6 +306,9 @@ public class MarshallerContextImpl extends MarshallerContextAdapter {
U.error(log, "Failed to write class name to file [id=" + evt.getKey() +
", clsName=" + evt.getValue() + ", file=" + file.getAbsolutePath() + ']', e);
}
+ catch (IgniteInterruptedCheckedException e) {
+ U.error(log, "Interrupted while waiting for acquiring file lock: " + file, e);
+ }
}
finally {
lock.unlock();