halitpeker / servicestack

Automatically exported from code.google.com/p/servicestack
0 stars 0 forks source link

Transaction doesn't receive data from get requests #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
        public IDictionary<string, int> HarvestRedis(IRedisClient redis, DateTime date)
        {
                var keys = redis.SearchKeys("foo*");
                if (keys.Count > 0)
                {
                    var dict = new Dictionary<string, int>();

                    using(var transaction = redis.CreateTransaction())
                    {
                        foreach(var key in keys)
                        {
                            var y = key;
                            transaction.QueueCommand(x => x.Get<int>(y));
                        }
                        transaction.Commit();
                    }

                    return dict;
                }
                return new Dictionary<string, int>();
        }

What is the expected output? What do you see instead?
expect dict to contain the values from redis, it comes back empty.

What version of the product are you using? On what operating system?
servicestack.redis 1.3.1

Please provide any additional information below.
we're using redis 2.0rc1 as well.

Original issue reported on code.google.com by pedlar.b...@gmail.com on 17 Jun 2010 at 6:41

GoogleCodeExporter commented 9 years ago
        public IDictionary<string, int> HarvestRedis(IRedisClient redis, DateTime date)
        {
                var keys = redis.SearchKeys("foo*");
                if (keys.Count > 0)
                {
                    var dict = new Dictionary<string, int>();

                    using(var transaction = redis.CreateTransaction())
                    {
                        foreach(var key in keys)
                        {
                            var y = key;
                            transaction.QueueCommand(x => x.IncrementValueBy(y,0), val=>dict.Add(y, val));
                        }
                        transaction.Commit();
                    }

                    return dict;
                }
                return new Dictionary<string, int>();
        }

works how i want it to

Original comment by pedlar.b...@gmail.com on 17 Jun 2010 at 7:24

GoogleCodeExporter commented 9 years ago
Hi Brian,

Unfortunately the way RedisTransaction works is that the callbacks are not 
processed until the transaction is committed. So if you want access to the 
return values you will need to provide callbacks. 

Your first code example is a little strange as there is no where are you 
populating the dictionary. 
Try adding a callback like so:

public IDictionary<string, int> HarvestRedis(IRedisClient redis, DateTime date)
{
    var keys = redis.SearchKeys("foo*");
    if (keys.Count > 0)
    {
        var dict = new Dictionary<string, int>();
        using(var transaction = redis.CreateTransaction())
        {
        foreach(var key in keys)
        {
            var y = key;
            transaction.QueueCommand(x => x.Get<int>(y), v => dict[key] = v);
        }
        transaction.Commit(); //Callbacks are processed here
        }
        return dict;
    }
    return new Dictionary<string, int>();
}

Note: Since the sample is not complete I haven't been able to test it but it 
should work. Also note there are batch requests for a lot of redis 
datastructures like 'redis.GetValues(keys)' which are atomic operations 
themselves and in this case would be more efficient then using a transaction.

Let me know if the above is helpful in solving your problem.

Cheers,
Demis

Original comment by demis.be...@gmail.com on 17 Jun 2010 at 9:30

GoogleCodeExporter commented 9 years ago
right, sorry about the bad first sample.

this sample doesn't work. the reason i'm doing this is because we need to 
atomically remove the keys after we get their values. I wanted to use 
redis.GetValues inside of the transaction, but the transaction doesn't have an 
overload i could figure out on it. the only thing that allows me to get values 
and then delete them is incrementing by 0. the first sample doesn't seem to hit 
any of the callbacks, the second sample throws a null-reference exception. 

        public IDictionary<string, int> HarvestRedis(IRedisClient redis, DateTime date)
        {
                var keys = redis.SearchKeys("foo*");
                if (keys.Count > 0)
                {
                    var dict = new Dictionary<string, int>();

                    using(var transaction = redis.CreateTransaction())
                    {
                        foreach(var key in keys)
                        {
                            var y = key;
                            transaction.QueueCommand(x => x.Get<int>(y), val=>dict.Add(y, val));
                        }
                        //transaction.QueueCommand(x => x.RemoveAll(keys));
                        transaction.Commit();
                    }

                    return dict;
                }
                return new Dictionary<string, int>();
        }

        public IList<string> HarvestRedis(IRedisClient redis, DateTime date)
        {
                var keys = redis.SearchKeys("foo*");
                if (keys.Count > 0)
                {
                    var values = new List<string>();

                    using(var transaction = redis.CreateTransaction())
                    {
                        transaction.QueueCommand(x => x.GetValues(keys), val => values = val);
                        //transaction.QueueCommand(x => x.RemoveAll(keys));
                        transaction.Commit();
                    }

                    return values;
                }
                return new List<string>();
        }

Original comment by pedlar.b...@gmail.com on 17 Jun 2010 at 9:54

GoogleCodeExporter commented 9 years ago
Hi Brian,

Sorry I was late resolving this issue, I was a little swamped yesterday fixing 
another issue with ServiceStack. 
Anyway happy to report that I've re-factored your above sample and included 
them as part of the test-suite which you can see here:
http://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Re
dis/ServiceStack.Redis.Tests/Issues/TransactionIssueTests.cs

I've added a couple of fixes to the client so now both tests pass! Basically a 
result of a couple of oversights that more regression testing would've picked 
up.

Anyway, the fix is available in v1.34 of the ServiceStack or Redis binaries the 
latter of which you can get here:
http://servicestack.googlecode.com/files/ServiceStack.Redis.zip

Let me know if it fixes your problem so I can close this ticket.

Cheers,
Demis

Original comment by demis.be...@gmail.com on 18 Jun 2010 at 10:15

GoogleCodeExporter commented 9 years ago
i'm using the GetValues method to load out the keys with the new binaries and 
am still seeing the same null reference exception. also tried the Get method, 
still no values.

Original comment by pedlar.b...@gmail.com on 18 Jun 2010 at 4:05

GoogleCodeExporter commented 9 years ago
Well that's the expected result after I uploaded the older version of the Redis 
Client instead of the new build :)

I've tried again, so the new version should be up now!
http://servicestack.googlecode.com/files/ServiceStack.Redis.zip

Original comment by demis.be...@gmail.com on 18 Jun 2010 at 4:15

GoogleCodeExporter commented 9 years ago
Well that's the expected result after I uploaded the older version of the Redis 
Client instead of the new build :)

I've tried again, so the new version should be up now!
http://servicestack.googlecode.com/files/ServiceStack.Redis.zip

Original comment by demis.be...@gmail.com on 18 Jun 2010 at 4:15

GoogleCodeExporter commented 9 years ago
the timestamp on the one you've uploaded is 6/2/2010. same results.

Original comment by pedlar.b...@gmail.com on 18 Jun 2010 at 4:46

GoogleCodeExporter commented 9 years ago
Actually, I'm having some problems uploading a new binary for some reason. 
Can you try again with file attached to this comment.

Original comment by demis.be...@gmail.com on 18 Jun 2010 at 4:49

Attachments:

GoogleCodeExporter commented 9 years ago
that does it. the Get code snippet i sent works now. thanks!

Original comment by pedlar.b...@gmail.com on 18 Jun 2010 at 4:52

GoogleCodeExporter commented 9 years ago
Awesome, closing.

Original comment by demis.be...@gmail.com on 18 Jun 2010 at 6:30