Exemplo n.º 1
0
        public async Task <bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tableVersion)
        {
            bool ret = false;

            try
            {
                while (true)
                {
                    long ticks = DateTime.UtcNow.Ticks;
                    await database.StringSetAsync("locktoken", ticks.ToString(), new TimeSpan(0, 0, 10));

                    if (string.IsNullOrEmpty(etag))
                    {
                        etag = "0";
                    }


                    var rentry = RedisMembershipEntry.Create(clusterId, entry, etag);
                    var val    = await database.StringGetAsync(clusterId);

                    if (!val.IsNull)
                    {
                        RedisMembershipCollection collection = serializer.Deserialize <RedisMembershipCollection>(val);
                        var items = collection.Where((x) => x.DeploymentId == clusterId && x.Address.ToParsableString() == rentry.Address.ToParsableString());
                        if (items != null && items.Count() > 0)
                        {
                            RedisMembershipEntry oldEntry = items.First();
                            rentry.LastIndex = oldEntry.LastIndex++;
                            collection.Remove(oldEntry);
                            collection.Add(rentry);

                            string tokenValue = await database.StringGetAsync("locktoken");

                            if (tokenValue != ticks.ToString())
                            {
                                continue;
                            }
                            else
                            {
                                ret = await database.StringSetAsync(clusterId, serializer.Serialize(collection));

                                break;
                            }
                        }
                    }
                }

                return(ret);
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Redis membership table key '{0}' failed update row.", clusterId);
                throw ex;
            }
        }
Exemplo n.º 2
0
        public async Task <bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tableVersion)
        {
            try
            {
                bool ret = false;

                if (string.IsNullOrEmpty(etag))
                {
                    etag = "0";
                }


                var rentry = RedisMembershipEntry.Create(clusterId, entry, etag);
                var val    = await database.StringGetAsync(clusterId);

                if (!val.IsNull)
                {
                    RedisMembershipCollection collection = serializer.Deserialize <RedisMembershipCollection>(val);
                    var items = collection.Where((x) => x.DeploymentId == clusterId && x.Address.ToParsableString() == rentry.Address.ToParsableString());
                    if (items != null && items.Count() > 0)
                    {
                        RedisMembershipEntry oldEntry = items.First();
                        rentry.LastIndex = oldEntry.LastIndex++;
                        collection.Remove(oldEntry);
                        collection.Add(rentry);
                        ret = await database.StringSetAsync(clusterId, serializer.Serialize(collection));
                    }
                }
                return(ret);
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Redis membership table key '{0}' failed update row.", clusterId);
                throw ex;
            }
        }