Lucene 4.0 BytesRefHash的一个bug
2017-09-01 22:16
369 查看
线上搜索忽然发现一堆死循环线程,最后全部卡在内存索引的BytesRefHashState的查找方法get上,而这个哈希表实现基本参考了lucene-core中的BytesRefHash方法。
这个再散列方法比较特别,inc = ((code >> 8) + code) | 1,code += inc,既不是线性探测也不是二次探测,哈希表的长度为2的幂,并且保证哈希表负载因子小于0.5。我理解这里不使用链表解决冲突是为了节约内存,不用二次探测和线性探测是为了避免聚集和二次聚集,但这同样带来了缓存不友好的问题,最重要的是是否会死循环?
模拟了一下,果然有死循环:
Current period=72350, oldStep=23220, currentCode=1542835881
也就是hashCode为1542835881时,经过49130次哈希,就会回到原来位置,只要表长超过98260就会出现问题。
翻了一下最新的lucene 6.6.0版本,BytesRef实现已经变更:哈希方法改为murmurhash3,冲突解决方式改为线性探测。更好的哈希方法避免聚集,线性探测增加缓存友好。
具体改动为(Lucene-5604):https://issues.apache.org/jira/browse/LUCENE-5604
这个再散列方法比较特别,inc = ((code >> 8) + code) | 1,code += inc,既不是线性探测也不是二次探测,哈希表的长度为2的幂,并且保证哈希表负载因子小于0.5。我理解这里不使用链表解决冲突是为了节约内存,不用二次探测和线性探测是为了避免聚集和二次聚集,但这同样带来了缓存不友好的问题,最重要的是是否会死循环?
模拟了一下,果然有死循环:
public static void main(String[] args) { int code = 0; HashMap<Integer, Integer> stepMap = new HashMap<Integer, Integer>(); int inc = 0, period = 0; for (; ;) { if ((period % 10000000) == 0) { System.err.println("Period=" + period + ",Size=" + stepMap.size()); } inc = (((code >> 8) + code) | 1); code += inc; if (!stepMap.containsKey(code)) { stepMap.put(code, period); ++period; } else { int oldStep = stepMap.get(code); System.err.println(String.format("Current period=%d, oldStep=%d, currentCode=%d", period, oldStep, code)); return; } } }输出:
Current period=72350, oldStep=23220, currentCode=1542835881
也就是hashCode为1542835881时,经过49130次哈希,就会回到原来位置,只要表长超过98260就会出现问题。
翻了一下最新的lucene 6.6.0版本,BytesRef实现已经变更:哈希方法改为murmurhash3,冲突解决方式改为线性探测。更好的哈希方法避免聚集,线性探测增加缓存友好。
具体改动为(Lucene-5604):https://issues.apache.org/jira/browse/LUCENE-5604
相关文章推荐
- struts2的default-action-ref 的一个“BUG”(其实并非bug)
- 关于 default-action-ref 的一个BUG 即解决方法
- 一个伴随ASP.NET从1.0到4.0的OutputCache Bug介绍
- 一个伴随ASP.NET从1.0到4.0的OutputCache Bug
- QQ超市模拟排配2D版1.13 (XNA4.0) (修正双格货架移动的一个bug和3-5地图)
- Entity Framework 4.0 的一个bug :DefaultValue问题
- 关于 default-action-ref 的一个BUG
- lucene全文检索的一个小BUG
- Lucene.net 的一个小bug
- lucene 中文分词器中的一个Bug
- Lucene 1.9 多目录搜索的的一个bug
- lucene 中文分词器中的一个Bug
- 发现了lucene一个bug
- MONGODB 2.6.8 的一个BUG [Assertion: 13548:BufBuilder attempted to grow() to 134217728 bytes, past the ]
- 2005ReportingServices的一个bug及其解决方法
- nvd3使用(1)——多线图中的一个很隐蔽的bug
- freemind的一个大bug,一周的数据啊
- 发现新浪微博的一个bug
- WinSock中关于阻塞接收/发送超时的一个BUG
- 网上看到的关于bug度量的一个公式