MemCache分布式缓存的一个bug

Memcached分布式缓存策略不是由服务器端至支持的,多台服务器之间并不知道彼此的存在。分布式的实现是由客户端代码(Memcached.ClientLibrary)通过缓存key-server映射来实现的,基本原理就是对缓存key求hash值,用hash值对服务器数量进行模运算,该key值被分配到模运算结果为索引的那台server上。

Memcached.ClientLibrary对缓存key计算hashcode的核心算法如下:


  1 /// <summary>
2 /// Returns appropriate SockIO object given
3 /// string cache key and optional hashcode.
4 ///
5 /// Trys to get SockIO from pool. Fails over
6 /// to additional pools in event of server failure.
7 /// </summary>
8 /// <param name="key">hashcode for cache key</param>
9 /// <param name="hashCode">if not null, then the int hashcode to use</param>
10 /// <returns>SockIO obj connected to server</returns>
11 public SockIO GetSock(string key, object hashCode)
12 {
13 string hashCodeString = "<null>";
14 if(hashCode != null)
15 hashCodeString = hashCode.ToString();
16
17 if(Log.IsDebugEnabled)
18 {
19 Log.Debug(GetLocalizedString("cache socket pick").Replace("$$Key$$", key).Replace("$$HashCode$$", hashCodeString));
20 }
21
22 if (key == null || key.Length == 0)
23 {
24 if(Log.IsDebugEnabled)
25 {
26 Log.Debug(GetLocalizedString("null key"));
27 }
28 return null;
29 }
30
31 if(!_initialized)
32 {
33 if(Log.IsErrorEnabled)
34 {
35 Log.Error(GetLocalizedString("get socket from uninitialized pool"));
36 }
37 return null;
38 }
39
40 // if no servers return null
41 if(_buckets.Count == 0)
42 return null;
43
44 // if only one server, return it
45 if(_buckets.Count == 1)
46 return GetConnection((string)_buckets[0]);
47
48 int tries = 0;
49
50 // generate hashcode
51 int hv;
52 if(hashCode != null)
53 {
54 hv = (int)hashCode;
55 }
56 else
57 {
58
59 // NATIVE_HASH = 0
60 // OLD_COMPAT_HASH = 1
61 // NEW_COMPAT_HASH = 2
62 switch(_hashingAlgorithm)
63 {
64 case HashingAlgorithm.Native:
65 hv = key.GetHashCode();
66 break;
67
68 case HashingAlgorithm.OldCompatibleHash:
69 hv = OriginalHashingAlgorithm(key);
70 break;
71
72 case HashingAlgorithm.NewCompatibleHash:
73 hv = NewHashingAlgorithm(key);
74 break;
75
76 default:
77 // use the native hash as a default
78 hv = key.GetHashCode();
79 _hashingAlgorithm = HashingAlgorithm.Native;
80 break;
81 }
82 }
83
84 // keep trying different servers until we find one
85 while(tries++ <= _buckets.Count)
86 {
87 // get bucket using hashcode
88 // get one from factory
89 int bucket = hv % _buckets.Count;
90 if(bucket < 0)
91 bucket += _buckets.Count;
92
93 SockIO sock = GetConnection((string)_buckets[bucket]);
94
95 if(Log.IsDebugEnabled)
96 {
97 Log.Debug(GetLocalizedString("cache choose").Replace("$$Bucket$$", _buckets[bucket].ToString()).Replace("$$Key$$", key));
98 }
99
100 if(sock != null)
101 return sock;
102
103 // if we do not want to failover, then bail here
104 if(!_failover)
105 return null;
106
107 // if we failed to get a socket from this server
108 // then we try again by adding an incrementer to the
109 // current key and then rehashing
110 switch(_hashingAlgorithm)
111 {
112 case HashingAlgorithm.Native:
113 hv += ((string)("" + tries + key)).GetHashCode();
114 break;
115
116 case HashingAlgorithm.OldCompatibleHash:
117 hv += OriginalHashingAlgorithm("" + tries + key);
118 break;
119
120 case HashingAlgorithm.NewCompatibleHash:
121 hv += NewHashingAlgorithm("" + tries + key);
122 break;
123
124 default:
125 // use the native hash as a default
126 hv += ((string)("" + tries + key)).GetHashCode();
127 _hashingAlgorithm = HashingAlgorithm.Native;
128 break;
129 }
130 }
131
132 return null;
133 }

根据缓存key得到服务器的核心代码

从源码中(62--82行代码)可以发现,计算hashcode的算法共三种:

(1)HashingAlgorithm.Native:
即使用.NET本身的hash算法,速度快,但与其他client可能不兼容,例如需要和java、ruby的client共享缓存的情况;

(2)HashingAlgorithm.OldCompatibleHash:
可以与其他客户端兼容,但速度慢;

(3)HashingAlgorithm.NewCompatibleHash:
可以与其他客户端兼容,据称速度快。

进一步分析发现,Memcached.ClientLibrary默认计算缓存key的hashcode的方式就是HashingAlgorithm.Native,而HashingAlgorithm.Native计算hashcode的算法为“hv
= key.GetHashCode()”,即用了.net类库string类型自带的GetHashCode()方法。

Bug就要浮现出来了,根据微软(http://msdn.microsoft.com/zh-cn/library/system.object.gethashcode.aspx)对GetHashCode的解释:the
.NET Framework does not guarantee the default implementation of the GetHashCode method, and the value this method
returns may differ between .NET Framework versions and platforms, such as 32-bit
and 64-bit platforms。string类型的GetHashCode()函数并不能保证不同平台同一个字符串返回的hash值相同,这样问题就出来了,对于不同服务器的同一缓存key来说,产生的hashcode可能不同,同一key对应的数据可能缓存到了不同的MemCache服务器上,数据的一致性无法保证,清除缓存的代码也可能失效。


// 64位 4.0
[__DynamicallyInvokable, ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), SecuritySafeCritical]
public unsafe override int GetHashCode()
{
if (HashHelpers.s_UseRandomizedStringHashing)
{
return string.InternalMarvin32HashString(this, this.Length, 0L);
}
IntPtr arg_25_0;
IntPtr expr_1C = arg_25_0 = this;
if (expr_1C != 0)
{
arg_25_0 = (IntPtr)((int)expr_1C + RuntimeHelpers.OffsetToStringData);
}
char* ptr = arg_25_0;
int num = 5381;
int num2 = num;
char* ptr2 = ptr;
int num3;
while ((num3 = (int)(*(ushort*)ptr2)) != 0)
{
num = ((num << 5) + num ^ num3);
num3 = (int)(*(ushort*)(ptr2 + (IntPtr)2 / 2));
if (num3 == 0)
{
break;
}
num2 = ((num2 << 5) + num2 ^ num3);
ptr2 += (IntPtr)4 / 2;
}
return num + num2 * 1566083941;
}

// 64位 2.0
// string
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public unsafe override int GetHashCode()
{
IntPtr arg_0F_0;
IntPtr expr_06 = arg_0F_0 = this;
if (expr_06 != 0)
{
arg_0F_0 = (IntPtr)((int)expr_06 + RuntimeHelpers.OffsetToStringData);
}
char* ptr = arg_0F_0;
int num = 5381;
int num2 = num;
char* ptr2 = ptr;
int num3;
while ((num3 = (int)(*(ushort*)ptr2)) != 0)
{
num = ((num << 5) + num ^ num3);
num3 = (int)(*(ushort*)(ptr2 + (IntPtr)2 / 2));
if (num3 == 0)
{
break;
}
num2 = ((num2 << 5) + num2 ^ num3);
ptr2 += (IntPtr)4 / 2;
}
return num + num2 * 1566083941;
}

//32位 4.0
[__DynamicallyInvokable, ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), SecuritySafeCritical]
public unsafe override int GetHashCode()
{
if (HashHelpers.s_UseRandomizedStringHashing)
{
return string.InternalMarvin32HashString(this, this.Length, 0L);
}
IntPtr arg_25_0;
IntPtr expr_1C = arg_25_0 = this;
if (expr_1C != 0)
{
arg_25_0 = (IntPtr)((int)expr_1C + RuntimeHelpers.OffsetToStringData);
}
char* ptr = arg_25_0;
int num = 352654597;
int num2 = num;
int* ptr2 = (int*)ptr;
int i;
for (i = this.Length; i > 2; i -= 4)
{
num = ((num << 5) + num + (num >> 27) ^ *ptr2);
num2 = ((num2 << 5) + num2 + (num2 >> 27) ^ ptr2[(IntPtr)4 / 4]);
ptr2 += (IntPtr)8 / 4;
}
if (i > 0)
{
num = ((num << 5) + num + (num >> 27) ^ *ptr2);
}
return num + num2 * 1566083941;
}

GetHashCode几种版本的实现代码

解决问题的方法就是不要用MemCache默认的hash算法,实现方式有两种:

(1)初始化MemCache服务器的时候,指定为MemCahce自带其它的hash算法,代码为“this.pool.HashingAlgorithm
= HashingAlgorithm.OldCompatibleHash;”。

(2)自定义hash算法,调用set()、get()、delete()等方式时传递hash值,这几个方法有参数传递hashcode的重载。

参考资料:分析Memcached客户端如何把缓存数据分布到多个服务器上(转)memcached client - memcacheddotnet (Memcached.ClientLibrary)
1.1.5
memcache分布式实现Object.GetHashCode 方法关于 HashCode做key的可能性

时间: 2024-08-27 20:46:26

MemCache分布式缓存的一个bug的相关文章

C# Memcache分布式缓存简单入门

什么是Memcache?能做什么? 以下是百度的观点: memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速度提升效果十分显著[1]  .这是一套开放源代码软件,以BSD license授权发布. Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及

memcache 分布式缓存

转载地址:http://www.cnblogs.com/phpstudy2015-6/p/6713164.html 作者:那一叶随风 1.memcached分布式简介 memcached虽然称为"分布式"缓存服务器,但服务器端并没有"分布式"功能.Memcache集群主机不能够相互通信传输数据,它的"分布式"是基于客户端的程序逻辑算法进一步实现的. 请看下面简图: 根据上图我们简述分析分布式memcached的set与get的过程 set过程:

memcache分布式缓存

1 什么是memcache 以及memcache有什么作用 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度. 2 下载并安装(希望大家自己去动手 印象会更深) 下载memcache 进入cmd ,切换到 memcached.exe 文件所在目录 memcached.exe –d install (1)

什么是分布式缓存?

缓存这种能够提升指令和数据读取速度的特性,随着本地计算机系统向分布式系统的扩展,在分布式计算领域中得到了广泛的应用,称为分布式缓存. 中文名 分布式缓存 外文名 Distribute Cache 简介 分布式缓存能够处理大量的动态数据,因此比较适合应用在Web 2.0时代中的社交网站等需要由用户生成内容的场景.从本地缓存扩展到分布式缓存后,关注重点从CPU.内存.缓存之间的数据传输速度差异也扩展到了业务系统.数据库.分布式缓存之间的数据传输速度差异. 业务系统.数据库.分布式缓存之间的数据流 图

php5.4之分布式缓存memcache(windows7下安装配置)

一.安装memcache memcached在windows7上的安装问题 现在安装包:http://www.jb51.net/softs/44843.html   memcache的安装包 错误: 通过cmd命令行进入到D:\webEve\memcached(下载后的解压目录) 运行 memcached.exe -d install 报错" failed to install service or service already installed" 解决方法: www.2cto.c

分布式缓存--MVC+EF+Memcache

一.从单机到分布式 现在三台机器组成一个Web的应用集群,其中一台机器用户登录,然后其他另外两台机器如何共享登录状态? 解决方案: 1.AspNet进程外的Session . 2.用数据库存数等钱登录状态. 3.Memcache. 二.为什么用Memcache? 1.解决高并发访问数据库带来的死锁 2.多用户端共享缓存 三.Memcache原理 其实memcache是一种windows服务,客户端发来的请求,都会被Socket服务器端接受到.存数使用键值对存储的.客户端进行存储的时候,就是找最接

Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)

网站响应速度优化包括集群架构中很多方面的瓶颈因素,这里所说的将页面静态化.实现分布式高速缓存就是其中的一个很好的解决方案... 1)先来看看Nginx负载均衡 Nginx负载均衡依赖自带的 ngx_http_upstream_module . ngx_http_memcached_module两大功能模块,其中一致性hash算法Nginx本身是不支持的,可以借助第三方模块: ngx_http_upstream_consistent_hash 或者直接使用淘宝的Tengine: http://te

分布式缓存——memcache原理

内容:1.什么是Memcached 2.MemCache和MemCached的区别 3.memcache访问模型 4.Memcached作为高速运行的分布式缓存服务器具有以下特点 5.Memcached的内存算法 6.Memcached的缓存策略             7.分布式算法(Consistent Hashing)             8. MemCache的特性和限制总结 1.什么是Memcached        MemCache是一个自由.源码开放.高性能.分布式的分布式内存

分布式缓存Memcache和Redis

引言 针对于现在计算机的CPU和网络设施,对应用程序来说,执行效率的瓶颈,已经不是代码的长度(实现同一个功能)和带宽了,而是,代码访问资源的过程,即:让我们的程序慢下来的罪魁祸首就是IO操作. 程序从硬盘上读取数据是一个非常花费时间的操作,因为我们现在所使用的硬盘是机械式的,你想机械的运行速度和电的速度,那是一个级别上的选手吗? 为了解决程序的瓶颈,人们提出了一种想法:使用空间换取时间.程序访问硬盘用的时间长,那就让数据放到内存中,让程序访问内存,这样不就节省了时间.这样确实剩下了我们程序获取数