Is there a way to make "increment" thread safe?


#1

I was happy using the increment as a ID generator.
Everything was fine, until i started to work with more threads.

I now get repeated ID’s, usually between get 4-8 duplicates / 1K increments.
This isnt good - especially since readdoc&upsert with CAS reduces speed to a crawl ( essentially locks speed down to single thread ping time again )

Any suggestions?


#2

Increment operations are thread safe already. I can’t explain the repeated IDs. Given that the implementation on the cluster side is so simple, I suspect maybe there’s a bug in the client or application code.

Do you by chance have a test case that shows the duplicates?

Also, what can you tell me about your deployment? Which version of Couchbase and what client SDK? Do you have XDCR in the deployment? Note that atomic counters are at a single cluster level, not across clusters.


#3

Im using Couchbase 3.0.1
.NET 2.00 beta 2
Just made sample code:
ConcurrentBag Results = new ConcurrentBag();
using (var bucket = CBClient.Instance.OpenBucket(“default”))
{
Parallel.For(0, 1000, (i) =>
{
var res = bucket.Increment(“test”);// 4.5 seconds
Results.Add(res.Value);
});
}
var R = Results.Distinct().ToList();
if (R.Count != Results.Count)
Console.WriteLine(“ERROR: DOUBLES!!!=” + R.Count + " GOT=" + Results.Count);

The same thing happens in a serial for loop, as long as i have multiple instaces running.


#4

CBClient is just a config class and a Cluster instance holder that will live during the application lifetime.
Instance = new Cluster(Cconfig);
I have a 2 server setup, in case that matters.
1 single cluster - nothing fancy


#5

Maybe it’s something with the .NET client beta 2? Can you have a look @jmorris?


#6

Running the same code posted above with the exception to changing the ConcurrentBag instantiation to: ConcurrentBag Results = new ConcurrentBag() (compiler error, ConcurrenBag requires a T param), I can’t replicate. I tried on a single node 2.5.1 instance and on 2 node 3.0.0 cluster.

@Lennartos do you think you could create a Jira ticket and post an example project? You can create the NCBC (jira) ticket here: http://www.couchbase.com/issues/browse/NCBC.

Thanks!

Jeff


#7

Ok, i had added a lot of error handling for the increment in my old version - just tried to break on first tile success = false and i got this;
“Non-numeric server-side value for incr or decr”

Maybe that helps in tracking it down?


#8

same to me 1% value repeat


#9

i think is error on init key will repeat


#10

@ingenthr @jnordberg should has a safe model to init empty value ,when not has key


#11

I think you meant to ping @jmorris perhaps. Do you have a code sample that demonstrates what you’re seeing that is incorrect?