golang bloomfilter踩過的坑(https://github.com/mmczoo/dgobloom)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

2016-10-27


golang bloomfilter踩過的坑(https://github.com/mmczoo/dgobloom)




幾周前應該工作的需要,使用開源的bloomfilter(避免重複造輪子,自己也懶....),起初謝了簡單測試,發現沒有問題,
就開始正式使用了,幾天前無意中發現久遠資料新出現,於是就產生了各種懷疑......自己程式問題?多個程式同事跑的問題?
等等!




最後排除代碼問題後,開始懷疑使用的bloomfilter有問題,於是開始簡潔測試環境,閱讀bloomfilter源碼,添加調試,
各種測想各種搞,兩天時間無果(自己智商太低).


最終找到了問題所在,
先直接上代碼:
bloomfilter查詢介面:
146 func (bf *bloomFilter) Exists(b []byte) bool {147148     for _, s := range bf.Salts {149         bf.H.Reset()150         bf.H.Write(s)151         bf.H.Write(b)152153         if bf.Filter.get(uint32(uint64(bf.H.Sum32())%bf.Bits)) == 0 {154             dlog.Info("==============%d %s", s, str(b))155             return false156         }157     }158159     return true160 }




其實問題就在bf.H,我們繼續追蹤bf.H:
 69 type bloomFilter struct {...... 74     H        hash.Hash32 `json:"h"` 75     Salts    [][]byte    `json:"salts"` 76 }


我使用的H是fnv.New32a():
 53         bf := bloom.NewBloomFilter(bloomConf.BFCap, bloomConf.BFfpr, fnv.New32a(), salts)  38 func New32a() hash.Hash32 { 39     var s sum32a = offset32 40     return &s 41 }




 
 其實就是定義了一個uint32的新資料類型,注意這裡是&s.
 現在大家應該知道Exists中的bf.H問題了吧,bf.H在這裡是共用資料,不支援重入。
 而我們的訪問量很大,導致self.H重入了,這樣出現了錯誤!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.