ilock | Inter-process named lock library | File Utils library
kandi X-RAY | ilock Summary
kandi X-RAY | ilock Summary
This is inter-process named lock library. It provides only one class ILock with very simple interface. Based on locking unique temporary files with portalocker.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Try to acquire the lock .
- The exit function
- Initialize the lock .
ilock Key Features
ilock Examples and Code Snippets
Community Discussions
Trending Discussions on ilock
QUESTION
I want to learn how to use the synchronized method in java and implemented the following code.
...ANSWER
Answered 2020-Oct-27 at 18:19If you are trying to make sure that the run()
method will be completed by one thread before the other one start; then you need to synchronize the content inside the run method, not the thread creation part.
In your specific example, you need to have an object that can be accessed by both the threads, and then acquire the lock of that object.
Let me add an example below by changing your code; but this may not be the best way; just trying to explain the point.
QUESTION
I see that Hazelcast 3.12 has introduced the CPSubsystem()
for systems with 3-7 nodes. I understand the reasoning. However, if I am trying to design a solution that can run with anywhere between 1-n nodes, do I need to use different logic to validate if the CPSubsystem is enabled? How do I even check that?
I would have thought/hoped that simply calling
...ANSWER
Answered 2019-Jul-30 at 06:34As you already know, CPSubsystem
is a CP
system in terms of CAP
theorem. It has to be enabled explicitly to use, because it has some limitations and prerequisites. One of them is, at least 3 Hazelcast members should exist in the cluster. Actually, 2 members is sufficient but Hazelcast's CPSubsystem
rejects to work with 2 members because majority of 2 members is 2 again, and it's prone to be unavailable once one of the members crashes.
HazelcastInstance.getLock()
uses async replication of Hazelcast and cannot provide CP
guarantees under failures. This is fine for some systems/applications but not for all. That's why choosing between a best-effort locking mechanism vs CP based locking mechanism should be explicit and applications relying on the lock should be designed depending on this choice. See Daniel Abadi's The dangers of conditional consistency guarantees
post related to this choice. That's why, CPSubsystem().getLock()
does not fallback to best-effort/unsafe locking mechanism when cluster size is below 3.
HazelcastInstance.getLock()
is deprecated in 3.12 and will be removed in 4.0. But Hazelcast will provide an unsafe (development) mode for CP data structures, which will work with any number of members and will be based on async replication similar to Hazelcast AP data structures.
QUESTION
I have a Spring boot application which has Hazelcast for caching in it. When multiple instances are clustered with Hazelcast, I am having this exception on the unlock operation:
...ANSWER
Answered 2019-Jul-03 at 10:30Since Hazelcast locks are distributed, they are protected by thread ownership so that only the thread who locked it can unlock.
In your code, you use tryLock
which may return false
. However, finally
block executes unlock operation in any case. So, each time you can't get the lock (because it is locked by some other thread in the cluster), you will try to unlock it and you will get that exception.
It is a good idea to use lock.isLockedByCurrentThread()
to check the ownership.
QUESTION
The printf
function calls write
(re. forktest.c):
ANSWER
Answered 2018-Apr-22 at 23:29fd==1
refers to stdout
, or Standard Out. It's a common feature of Unix-like Operatin Systems. The kernel knows that it's not a real file. Writes to stdout
are mapped to terminal output.
QUESTION
I need to run some jobs in a cluster, only one at a time. Because my team uses Hazelcast, I ended up with a solution based on Hazelcast ILock implementation. For the purpose of the question, I am going to make a generalisation about it. Let's suppose we have the following interfaces (that could be easily implemented e.g. by Hazelcast or Reddison (Redis)):
...ANSWER
Answered 2019-Jan-28 at 15:54it is a correct approach when using Reactor, because you took care of offsetting the blocking portion into a dedicated Scheduler
/Thread
.
But I'd say mutually exclusive code like this is not a very good fit for reactive programming in general: you lose one of the key benefits of doing more with less threads, you risk blocking other parts of the application should you forget to publishOn
a dedicated thread, etc...
QUESTION
I'm trying to acheive method synchronization in Hazelcast, but it doesn't seem to work even on a single cluster.
I'm using a simple Hazelcast config
...ANSWER
Answered 2018-Sep-18 at 12:29System.getTimestamp()
might not be unique. You can call it twice and get the same result.
You could get Hazelcast to generate the id for you https://docs.hazelcast.org//docs/3.10.5/javadoc/com/hazelcast/flakeidgen/FlakeIdGenerator.html
QUESTION
Hello fellow developers,
I have serializeable object that is stored across two Hazelcast members.
Object contains some info that can be changed on only one of the Hazelcast member at the same time.
For this purpose I am using ILock.
Everything is working fine until, Hazelcast member that acquired and holds lock for object decides to leave.
I want current Hazelcast member to wait for release its acquired and held locks before shutdown, so it can finish editing object.
...ANSWER
Answered 2018-Oct-12 at 09:11As we discussed in comments (with @mdogan), there's no built-in mechanism for that.
To achieve what I wanted, I have to:
- Store of all acquired lock keys by current Hazelcast member in
ConcurrentSkipListSet
. - Before shutdown, loop through
ConcurrentSkipListSet
check ifILock.isLocked()
, iftrue
then wait for release. - Shutdown current Hazelcast member.
QUESTION
I have a use case where I have a set of items, DiagnosticRun
s, that are submitted to my cluster. I want to process them serially (to avoid conflicts). I am trying to use a Hazelcast Queue
protected by a Lock
to make sure the items are processed one at a time. Hazelcast is running in embedded mode in my cluster. If I register an ItemListener
with the Queue
, is it safe to call take()
on the Queue
from within the itemAdded()
method? For example:
ANSWER
Answered 2018-Sep-24 at 20:02First I'll qualify that I'm not exactly an expert on which threads these are executed on and when so some may disagree but here're my thoughts on this so anyone please chime in as this looks to be an interesting case. Your first solution mixes the Hazelcast event threading with it's operation threading. In fact you're triggering three operations to be invoked as a result of the single event. If you put some arbitrary latency in your call to updateProcductDeviceTable, you'll see that eventually, it will slow down but resume up again after some time. This will cause your local event queue to pile up while operations are invoked. You could put everything you're doing in a separate thread which you can "wake" up on #itemAdded or if you can afford to have a bit of latency, do what you're doing on your second solution. I would, however, make a couple changes in
listenToDiagnosticsRuns() method:
QUESTION
Suppose we have many randomly incoming threads accessing same resource in parallel. To access the resource thread needs to acquire a lock. If we could pack N incoming threads into one request resource usage would be N times more efficient. Also we need to answer individual request as fast as possible. What is the best way/pattern to do that in C#?
Currently I have something like that:
...ANSWER
Answered 2018-May-28 at 06:58If it is OK to process the task outside the scope of the current request, i.e. to queue it for later, then you can think of a sequence like this1:
Implement a resource lock (monitor) and a List
of tasks.
For each request:
Lock the List, Add current task to the List, remember nr. of tasks in the List, unlock the List.
Try to acquire the lock.
If unsuccessful:
- If the nr. of tasks in the list < threshold X, then Return.
- Else Acquire the Lock (will block)
Lock the List, move it's contents to a temp list, unlock the List.
If temp list is not empty
Execute the tasks in the temp list.
Repeat from step 5.
Release the lock.
The first request will go through the whole sequence. Subsequent requests, if the first is still executing, will short-circuit at step 4.
Tune for the optimal threshold X (or change it to a time-based threshold).
1 If you need to wait for the task in the scope of the request, then you need to extend the process slightly:
Add two fields to the Task class: completion flag and exception.
At step 4, before Returning, wait for the task to complete (Monitor.Wait
) until its completion flag becomes true
. If exception is not null
, throw it.
At step 6, for each task, set the completion flag and optionally the exception and then notify the waiters (Monitor.PulseAll
).
QUESTION
We have a Java application that has a JNI layer that is multi-threaded (pthread) and will call back to the Java level upon messages received from the underlying network.
We notice that every time it crashes, it is caused by a gc. We can even simulate such a crash by manually trigger a gc by calling jmap -histo
while the JNI layer is receiving messages from the network.
Given the information that we have read about the behaviours in JVM during GC in this post, https://stackoverflow.com/a/39401467/4523221, we still couldn't figure out why such crash is related to gc since JNI function calls are blocked during gc.
If anyone can shed light on this, it will be great. Thanks in advance.
The following is a stack trace that we have collected after a crash in our application.
...ANSWER
Answered 2017-May-25 at 02:00After spending days investigating this JNI issue, we have finally found out the reason and I would like to share our experience here so that hopefully it will help others.
First of all, the reason why we needed to use JNI in the first place was because we needed to make use of a 3rd party network library that was a Linux native lib, and unfortunately that was the cause of our problem.
The library provided us a callback handle that we implemented to receive incoming network messages from it, and this callback, we later found out, was simply a signal handler. So, it means that this signal handler would get called whenever a signal popped up, even during gc.
Since C threads keep running during safepoints in JVM, it would have been fine if those C threads weren't attached to the JVM, otherwise disasters would certainly strike.
Here is kind of what we thought had happened. (everything below happened in the JNI layer)
- App starts. We init and cached JNI resources, e.g. Jmv*, Method ID, etc.
- We registered a C function to the library to receive messages. The C function is a function that would call JNI APIs to allocate memory to accommodate the received messages and pass them onto Java. After that, we just started to wait for incoming messages.
- When a message finally arrived, the C function mentioned above was called to handle the message, but wait... what was this thread that's handling the callback. That would have been the main thread or hmm... any available threads.
- As taught in any JNI textbook, we attached the thread to JVM first if not yet done so before calling any JNI APIs. Great!
- Now, during a GC, all Java threads were blocked, but the C layer was still running. At this critical moment, if a message arrived, some thread (any threads) was called up to handle the message. But what threads were still available during gc? All application threads were blocked and the only ones that were still running at this moment (our guess) were unfortunately the gc threads.
The gdb stacktrace that we were seeing was basically what happened when a gc thread that was actually in a middle of doing some work on the heap and then got a call from our application to do some application work and then a few JNI API calls... BOOM
Solution:
- Have a C thread that handles the library callback
- Never attach that thread to JVM
- Have other threads attach to the JVM to do the Native-Java transition.
p.s. maybe some of the details weren't exactly accurate, so any JVM expert advice is welcomed. I will try to correct them as advised.
Thanks
Update.1 (@apangin): We have another gdb stacktrace here. Just wondering if the GangWorker at #18 was a parallel GC thread.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ilock
You can use ilock like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page