From 8e094c0e55ea9ee0e286481d75508358b4f46ba7 Mon Sep 17 00:00:00 2001 From: dongxu Date: Mon, 16 Nov 2015 10:26:23 +0800 Subject: [PATCH 1/2] hbase-store: use rand instead of timestamp for selecing connection randomly --- store/hbase/kv.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/store/hbase/kv.go b/store/hbase/kv.go index c796214248..a91c9830f2 100644 --- a/store/hbase/kv.go +++ b/store/hbase/kv.go @@ -14,6 +14,7 @@ package hbasekv import ( + "math/rand" "strings" "sync" "time" @@ -59,6 +60,7 @@ var mc storeCache func init() { mc.cache = make(map[string]*hbaseStore) + rand.Seed(time.Now().UnixNano()) } type hbaseStore struct { @@ -72,7 +74,7 @@ func (s *hbaseStore) Begin() (kv.Transaction, error) { s.mu.Lock() defer s.mu.Unlock() // get random conn - hbaseCli := s.conns[time.Now().UnixNano()%hbaseConnPoolSize] + hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] t := themis.NewTxn(hbaseCli) txn := newHbaseTxn(t, s.storeName) txn.UnionStore = kv.NewUnionStore(newHbaseSnapshot(t, s.storeName)) @@ -81,7 +83,7 @@ func (s *hbaseStore) Begin() (kv.Transaction, error) { func (s *hbaseStore) GetSnapshot(ver kv.Version) (kv.MvccSnapshot, error) { // get random conn - hbaseCli := s.conns[time.Now().UnixNano()%hbaseConnPoolSize] + hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] t := themis.NewTxn(hbaseCli) return newHbaseSnapshot(t, s.storeName), nil } @@ -108,7 +110,7 @@ func (s *hbaseStore) UUID() string { } func (s *hbaseStore) CurrentVersion() (kv.Version, error) { - hbaseCli := s.conns[time.Now().UnixNano()%hbaseConnPoolSize] + hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] t := themis.NewTxn(hbaseCli) defer t.Release() From 4960a63b81e04d5297c07b4a5b65e9bcfa8a5fd8 Mon Sep 17 00:00:00 2001 From: dongxu Date: Mon, 16 Nov 2015 10:47:25 +0800 Subject: [PATCH 2/2] hbase-store: address review comments --- store/hbase/kv.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/store/hbase/kv.go b/store/hbase/kv.go index a91c9830f2..9f29023aaa 100644 --- a/store/hbase/kv.go +++ b/store/hbase/kv.go @@ -70,11 +70,15 @@ type hbaseStore struct { conns []hbase.HBaseClient } +func (s *hbaseStore) getHBaseClient() hbase.HBaseClient { + // return hbase connection randomly + return s.conns[rand.Intn(hbaseConnPoolSize)] +} + func (s *hbaseStore) Begin() (kv.Transaction, error) { s.mu.Lock() defer s.mu.Unlock() - // get random conn - hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] + hbaseCli := s.getHBaseClient() t := themis.NewTxn(hbaseCli) txn := newHbaseTxn(t, s.storeName) txn.UnionStore = kv.NewUnionStore(newHbaseSnapshot(t, s.storeName)) @@ -82,8 +86,7 @@ func (s *hbaseStore) Begin() (kv.Transaction, error) { } func (s *hbaseStore) GetSnapshot(ver kv.Version) (kv.MvccSnapshot, error) { - // get random conn - hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] + hbaseCli := s.getHBaseClient() t := themis.NewTxn(hbaseCli) return newHbaseSnapshot(t, s.storeName), nil } @@ -110,7 +113,7 @@ func (s *hbaseStore) UUID() string { } func (s *hbaseStore) CurrentVersion() (kv.Version, error) { - hbaseCli := s.conns[rand.Intn(hbaseConnPoolSize)] + hbaseCli := s.getHBaseClient() t := themis.NewTxn(hbaseCli) defer t.Release()