You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Shawn Du <sh...@neulion.com.cn> on 2016/11/24 07:11:15 UTC
答复: Lock and Failed to unlock keys exception
Fixed by myself.
There are two errors in my code/configuration.
1) lock are only supported in TRANSACTIONAL mode. I missed it in my
configuration.
2) within a lock block, can't start cache:
IgniteCache<String, NamedSequence> cache =
ignite.cache(NamedSequence.CACHE_NAME_ID);
Above code should be called out of the lock block.
Thanks
Shawn
-----邮件原件-----
发件人: Shawn Du [mailto:shawn.du@neulion.com.cn]
发送时间: 2016年11月24日 14:20
收件人: user@ignite.apache.org
主题: Lock and Failed to unlock keys exception
Hi,
I am trying to implement distributed ID generators using lock and caches.
I wrote some codes like this, but it always throw exception "Failed to
unlock keys exception".
The code may runs in different JVM/hosts.
Do I misuse the lock feature? Thanks in advance.
----------------------------------------------------
Lock lock = osCache.lock(Os.CACHE_LOCK); try {
lock.lock();
os = osCache.get(videoMessage.os());
if (os == null)
{
os = new Os(videoMessage.os(),
idGenerator.next(Os.class.getName()));
osCache.put(os.getName(), os);
}
videoMessage.os(os.getId() + "");
}
finally
{
lock.unlock();
}
-------------------------------------------------------End
IdGenerator.next looks like this:
-------------------------------------------------------
public int next(String name)
{
IgniteCache<String, NamedSequence> cache =
ignite.cache(NamedSequence.CACHE_NAME_ID);
NamedSequence sequence = cache.get(name);
if (sequence == null)
{
sequence = new NamedSequence(name);
}
else
{
sequence.increase();
}
cache.put(sequence.getName(), sequence);
return sequence.getSequence();
}
----------------------------------------------------------
Re: Lock and Failed to unlock keys exception
Posted by Denis Magda <dm...@apache.org>.
Just want to let you know that Ignite already provide a highly performant and stable implementation of a distributed ID generator
https://apacheignite.readme.io/docs/id-generator <https://apacheignite.readme.io/docs/id-generator>
—
Denis
> On Nov 23, 2016, at 11:11 PM, Shawn Du <sh...@neulion.com.cn> wrote:
>
> Fixed by myself.
>
> There are two errors in my code/configuration.
>
> 1) lock are only supported in TRANSACTIONAL mode. I missed it in my
> configuration.
> 2) within a lock block, can't start cache:
> IgniteCache<String, NamedSequence> cache =
> ignite.cache(NamedSequence.CACHE_NAME_ID);
>
> Above code should be called out of the lock block.
>
> Thanks
> Shawn
>
> -----邮件原件-----
> 发件人: Shawn Du [mailto:shawn.du@neulion.com.cn]
> 发送时间: 2016年11月24日 14:20
> 收件人: user@ignite.apache.org
> 主题: Lock and Failed to unlock keys exception
>
> Hi,
>
> I am trying to implement distributed ID generators using lock and caches.
>
> I wrote some codes like this, but it always throw exception "Failed to
> unlock keys exception".
> The code may runs in different JVM/hosts.
>
> Do I misuse the lock feature? Thanks in advance.
>
> ----------------------------------------------------
> Lock lock = osCache.lock(Os.CACHE_LOCK); try {
> lock.lock();
> os = osCache.get(videoMessage.os());
> if (os == null)
> {
> os = new Os(videoMessage.os(),
> idGenerator.next(Os.class.getName()));
> osCache.put(os.getName(), os);
> }
> videoMessage.os(os.getId() + "");
> }
> finally
> {
> lock.unlock();
> }
>
> -------------------------------------------------------End
> IdGenerator.next looks like this:
> -------------------------------------------------------
> public int next(String name)
> {
> IgniteCache<String, NamedSequence> cache =
> ignite.cache(NamedSequence.CACHE_NAME_ID);
> NamedSequence sequence = cache.get(name);
> if (sequence == null)
> {
> sequence = new NamedSequence(name);
> }
> else
> {
> sequence.increase();
> }
> cache.put(sequence.getName(), sequence);
> return sequence.getSequence();
> }
> ----------------------------------------------------------
>