diff --git a/pkg/dhash/dhash_test.go b/pkg/dhash/dhash_test.go index 2b7568d..075a184 100644 --- a/pkg/dhash/dhash_test.go +++ b/pkg/dhash/dhash_test.go @@ -8,6 +8,9 @@ import ( ) type hashTest struct { + provider *ProviderImpl + sess Session + mem *MemTableMock db *HashDatabaseMock pipe *CachePipelineMock @@ -58,12 +61,16 @@ func newHashTest(ns string, options ...SessionOption) *hashTest { p.timer = timer db := &HashDatabaseMock{} + sess := p.NewSession(options...) h := &hashTest{ + provider: p, + sess: sess, + mem: mem, db: db, pipe: pipeline, - hash: p.NewSession(options...).NewHash(ns, db), + hash: sess.NewHash(ns, db), timer: timer, } @@ -73,6 +80,10 @@ func newHashTest(ns string, options ...SessionOption) *hashTest { return h } +func (h *hashTest) finish() { + h.sess.Finish() +} + func (h *hashTest) stubMemTable() { h.mem.GetNumFunc = func(key string) (uint64, bool) { return 0, false @@ -105,6 +116,7 @@ func (h *hashTest) stubPipeline() { h.pipe.DeleteFunc = func(key string) func() error { return func() error { return nil } } + h.pipe.FinishFunc = func() {} } func (h *hashTest) stubDB() { @@ -240,6 +252,13 @@ func TestSelectEntries__Second_Slot_Found(t *testing.T) { entries, err := h.hash.SelectEntries(newContext(), 0xfc345678)() assert.Equal(t, nil, err) assert.Equal(t, []Entry{newEntry(0xfc345678, 1, 2, 3)}, entries) + + h.finish() + assert.Equal(t, 1, len(h.pipe.FinishCalls())) + assert.Equal(t, uint64(1), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(1), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(0), h.provider.HashSizeLogMissCount()) + assert.Equal(t, uint64(0), h.provider.HashBucketMissCount()) } func TestSelectEntries__First_Slot_Found(t *testing.T) { @@ -317,6 +336,10 @@ func TestSelectEntries__When_Client_Get_Size_Log_Granted__Call_Get_Size_Log_From _, _ = h.hash.SelectEntries(newContext(), 0xfc345678)() assert.Equal(t, 1, len(h.db.GetSizeLogCalls())) assert.Equal(t, newContext(), h.db.GetSizeLogCalls()[0].Ctx) + + h.finish() + assert.Equal(t, uint64(1), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(1), h.provider.HashSizeLogMissCount()) } func TestSelectEntries__When_Client_Get_Size_Log_Granted__Do_MemTable_SetNum(t *testing.T) { @@ -335,6 +358,10 @@ func TestSelectEntries__When_Client_Get_Size_Log_Granted__Do_MemTable_SetNum(t * assert.Equal(t, 1, len(h.mem.SetNumCalls())) assert.Equal(t, "sample", h.mem.SetNumCalls()[0].Key) assert.Equal(t, uint64(7), h.mem.SetNumCalls()[0].Num) + + h.finish() + assert.Equal(t, uint64(1), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(1), h.provider.HashSizeLogMissCount()) } func TestSelectEntries__When_Client_Get_Size_Log_Granted__Do_Cache_Client_Lease_Set(t *testing.T) { @@ -376,6 +403,12 @@ func TestSelectEntries__When_Client_Get_Size_Log_Granted__Returns_Entry_From_Cli assert.Equal(t, []Entry{ newEntry(0xfc345678, 1, 2, 3), }, entries) + + h.finish() + assert.Equal(t, uint64(1), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(1), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(1), h.provider.HashSizeLogMissCount()) + assert.Equal(t, uint64(0), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Client_Get_Size_Log_Reject__Do_Retries(t *testing.T) { @@ -410,6 +443,10 @@ func TestSelectEntries__When_Client_Get_Size_Log_Reject__Do_Retries(t *testing.T start.Add(10 * time.Millisecond), start.Add(30 * time.Millisecond), }, h.leaseGetTimes) + + h.finish() + assert.Equal(t, uint64(3), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(3), h.provider.HashSizeLogMissCount()) } func TestSelectEntries__When_Client_Get_Size_Log_Reject__Retries_All_Times(t *testing.T) { @@ -428,6 +465,10 @@ func TestSelectEntries__When_Client_Get_Size_Log_Reject__Retries_All_Times(t *te assert.Nil(t, entries) assert.Equal(t, 4, len(h.pipe.LeaseGetCalls())) + + h.finish() + assert.Equal(t, uint64(4), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(4), h.provider.HashSizeLogMissCount()) } func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get(t *testing.T) { @@ -444,6 +485,9 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get(t *testing. assert.Equal(t, 2, len(h.pipe.LeaseGetCalls())) assert.Equal(t, "sample:size-log", h.pipe.LeaseGetCalls()[0].Key) assert.Equal(t, "sample:5:f8000000", h.pipe.LeaseGetCalls()[1].Key) + + h.finish() + assert.Equal(t, uint64(2), h.provider.HashBucketAccessCount()) } func TestSelectEntries__When_Client_SizeLog_Too_Different__Get_Buckets_Again(t *testing.T) { @@ -477,6 +521,10 @@ func TestSelectEntries__When_Client_SizeLog_Too_Different__Get_Buckets_Again(t * assert.Equal(t, []Entry{ newEntry(0xdc345678, 8, 8, 8), }, entries) + + h.finish() + assert.Equal(t, uint64(2), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(0), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_DB_SizeLog_Too_Different__Get_Buckets_Again(t *testing.T) { @@ -551,6 +599,10 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Select_Entries_From_DB(t *te assert.Equal(t, 1, len(h.db.SelectEntriesCalls())) assert.Equal(t, uint32(0xd8000000), h.db.SelectEntriesCalls()[0].HashBegin) assert.Equal(t, newNullUint32(0xe0000000), h.db.SelectEntriesCalls()[0].HashEnd) + + h.finish() + assert.Equal(t, uint64(2), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(2), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Both_Bucket_Not_Found__Returns_Entry_From_DB__And_Set_ClientCache(t *testing.T) { @@ -599,6 +651,10 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Returns_Entry_From_DB__And_S assert.Equal(t, marshalEntries(dbEntries), h.pipe.LeaseSetCalls()[0].Value) assert.Equal(t, uint64(7788), h.pipe.LeaseSetCalls()[0].LeaseID) assert.Equal(t, uint32(0), h.pipe.LeaseSetCalls()[0].TTL) + + h.finish() + assert.Equal(t, uint64(2), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(2), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_Rejected__Call_Second_Times(t *testing.T) { @@ -626,6 +682,10 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_Rejected__C assert.Equal(t, []time.Duration{ 10 * time.Millisecond, }, h.timer.sleepCalls) + + h.finish() + assert.Equal(t, uint64(3), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(3), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_Rejected_All_Times__Returns_Err(t *testing.T) { @@ -661,6 +721,10 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_Rejected_Al 20 * time.Millisecond, 50 * time.Millisecond, }, h.timer.sleepCalls) + + h.finish() + assert.Equal(t, uint64(5), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(5), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_OK__Returns_Client_Entries(t *testing.T) { @@ -705,6 +769,10 @@ func TestSelectEntries__When_Both_Bucket_Not_Found__Client_Lease_Get_OK__Returns }, h.timer.sleepCalls) assert.Equal(t, 0, len(h.db.SelectEntriesCalls())) + + h.finish() + assert.Equal(t, uint64(3), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(2), h.provider.HashBucketMissCount()) } func TestSelectEntries__When_Request_Size_Log_Duplicated__And_Client_Size_Log_Granted(t *testing.T) { @@ -750,4 +818,10 @@ func TestSelectEntries__When_Request_Size_Log_Duplicated__And_Client_Size_Log_Gr assert.Equal(t, "sample:size-log", h.pipe.LeaseSetCalls()[0].Key) assert.Equal(t, []byte("5"), h.pipe.LeaseSetCalls()[0].Value) assert.Equal(t, uint64(7788), h.pipe.LeaseSetCalls()[0].LeaseID) + + h.finish() + assert.Equal(t, uint64(2), h.provider.HashSizeLogAccessCount()) + assert.Equal(t, uint64(2), h.provider.HashBucketAccessCount()) + assert.Equal(t, uint64(2), h.provider.HashSizeLogMissCount()) + assert.Equal(t, uint64(0), h.provider.HashBucketMissCount()) } diff --git a/pkg/dhash/select.go b/pkg/dhash/select.go index 916cf58..6b7142a 100644 --- a/pkg/dhash/select.go +++ b/pkg/dhash/select.go @@ -227,6 +227,8 @@ func (h *hashSelectAction) handleBucketsWithOutput() ([]Entry, error) { } func (h *hashSelectAction) getBucketFromCacheClientForLeasing() { + h.root.sess.hashBucketAccessCount++ + key := computeBucketKey(h.root.namespace, int(h.sizeLog.Int64), h.hash) h.bucketLeaseGet = h.root.pipeline.LeaseGet(key) h.root.sess.addNextCall(func() { @@ -252,6 +254,9 @@ func (h *hashSelectAction) handleGetBucketFromDBWithError() error { h.results = entries return nil } + + h.root.sess.hashBucketMissCount++ + if bucketGetOutput.Type == LeaseGetTypeRejected { sess := h.root.sess diff --git a/service/readonly/repository_hash.go b/service/readonly/repository_hash.go index 167356c..b69406b 100644 --- a/service/readonly/repository_hash.go +++ b/service/readonly/repository_hash.go @@ -83,6 +83,10 @@ func newBlacklistCustomerHashDB(repo repository.Blacklist) dhash.HashDatabase { }) } +func newCampaignHashDB(_ repository.Campaign) dhash.HashDatabase { + return nil +} + func marshalBlacklistMerchant(m model.BlacklistMerchant) []byte { msg := promopb.BlacklistMerchantData{ Hash: m.Hash, diff --git a/service/readonly/repository_hash_test.go b/service/readonly/repository_hash_test.go index 8788491..9fef423 100644 --- a/service/readonly/repository_hash_test.go +++ b/service/readonly/repository_hash_test.go @@ -157,3 +157,8 @@ func TestBlacklistCustomerHashDB__Select_Entries__Returns_Correct_Data(t *testin }, }, entries2) } + +func TestCampaignHashDB__Select_Entries__Returns_Correct_Data(*testing.T) { + //repo := &repository.BlacklistMock{} + //db := newBlacklistCustomerHashDB(repo) +}