Comments (12)
It's not correct that the JVM ReentrantLock allows multi-threaded usage. It wouldn't be very useful if it did. Maybe I'm misunderstanding your use case. Here's an example I wrote with ReentrantLock.
public void test() throws Exception
{
final CountDownLatch latch = new CountDownLatch(2);
final ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread
(
new Runnable()
{
@Override
public void run()
{
lock.lock();
latch.countDown();
}
}
);
Thread t2 = new Thread
(
new Runnable()
{
@Override
public void run()
{
lock.lock();
latch.countDown();
}
}
);
t1.start();
t2.start();
if ( latch.await(10, TimeUnit.SECONDS) )
{
System.out.println("yep");
}
else
{
System.out.println("nope");
}
}
from curator.
Thanks for replying. The point I wanted to get across was that I felt that the acquire method should block within the same jvm like it does across jvms. If inter process mutex is called from 2 different threads at the same time in the same jvm right now, the first one will get access and the second call will result in an illegal monitor exception (rather than blocking).
I work around this with a simple java lock in my trait that wraps the mutex, just thought it would be nice if this was kept within the library.
from curator.
OK - I understand now. I consider this a bug and I'll work on a fix. As a workaround, you can allocate a new InterProcessMutex in each thread.
from curator.
I wonder if it is not better to leverage Google Collections' Function class and put the acquire and release methods together.
Adding synchronized to my lock method works fine for now: (scala)
trait Lockable extends ZooKeeperHarness {
val nodePathToLockUpon: String
private[cluster] lazy val lock: InterProcessLock = {
if (!exists(nodePathToLockUpon)) create(nodePathToLockUpon, PERSISTENT)
new InterProcessMutex(zkClient, nodePathToLockUpon)
}
def lock(functionToExecuteWhileLocked: => Unit) {
//Only one thread per Lockable instance can use the InterProcessMutex (throws IllegalMonitorException otherwise)
synchronized {
lock.acquire()
try {
functionToExecuteWhileLocked
} finally {
lock.release()
}
}
}
}
The InterProcessMutex per thread solution would work as well; these locks are used infrequently so I am not really concerned about the contention.
from curator.
I've pushed a fix - I'll build new binaries when I can. Thanks for finding this.
from curator.
@Randgalt Could you show me the push url? Thanks.
from curator.
Sorry @r7raul1984 - I don't know what you mean. Also, Curator is now at Apache. See https://curator.apache.org
from curator.
@Randgalt About InterProcessMutex ,I wanted to get across was that I felt that the acquire method should block within the same jvm like it does across jvms. How to fix it? Which version of Curator support this ?
from curator.
acquire
does block in the same JVM. It acts just like the JDK lock.
from curator.
@Randgalt OK. Thank you! I should create new CuratorFramework client for every InterProcessMutex. Many InterProcessMutex instance can't share one CuratorFramework client. Is that correct?
from curator.
No - 1 CuratorFramework instance is enough for your entire application.
from curator.
@Randgalt Thanks.
from curator.
Related Issues (20)
- java.nio.channels.CancelledKeyException HOT 1
- java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.build HOT 4
- adding nodes to zookeeper from local properties through archaius HOT 1
- InterProcessMutex is not releasing when called inside a future's onSuccess function. HOT 1
- Curator's Watch triggered two times for the same notification HOT 3
- Curator integration with Exhibitor-values to be provided for ExhibitorEnsembleProvider arguments HOT 1
- What's the plan for releasing changes to this? HOT 3
- org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists HOT 3
- curator use this.client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, data); but I find zk didnot have this node. HOT 2
- Link in wiki is broken HOT 1
- Examples Link in Table of Contents leads to 404 HOT 2
- NoSuchMethodError exception HOT 2
- Curator connecting to a secured SASL zookeeper HOT 1
- Background operation retry gave up HOT 1
- ..
- The result of event.getPath() in BackgroundCallback confused me
- Missing method declaration addAuthInfo
- TestingServer cannot start, it always throw FailedServerStartException
- curator 5.3 connect docker zookeeper cluster
- the parent node never delete cause thousands of parent node
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curator.