/
cc_Zipf.go
102 lines (84 loc) · 2.21 KB
/
cc_Zipf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package testbed
import (
"math/rand"
"time"
"github.com/totemtang/cc-testbed/clog"
)
type ZipfKey struct {
partIndex int
nParts int
nKeys int64
pKeysArray []int64
isZipf bool
isPartition bool
hp *HashPartitioner
wholeZipf *rand.Zipf
partZipf []*rand.Zipf
wholeUniform *rand.Rand
partUniform []*rand.Rand
}
// Index of partition starts from 0
// Integer Key starts from 0 also
func NewZipfKey(partIndex int, nKeys int64, nParts int, pKeysArray []int64, s float64, hp *HashPartitioner) *ZipfKey {
zk := &ZipfKey{
partIndex: partIndex,
nParts: nParts,
nKeys: nKeys,
pKeysArray: pKeysArray,
hp: hp,
}
zk.isPartition = (*SysType == PARTITION) || *PhyPart
zk.wholeUniform = rand.New(rand.NewSource(time.Now().Unix() / int64(partIndex+1)))
if zk.isPartition {
zk.partUniform = make([]*rand.Rand, nParts)
for i := 0; i < nParts; i++ {
zk.partUniform[i] = rand.New(rand.NewSource(time.Now().Unix() / int64(partIndex*13+i*7+1)))
}
}
// Uniform distribution
if s == 1 {
zk.isZipf = false
} else {
zk.isZipf = true
// Generate Zipf for whole store
zk.wholeZipf = rand.NewZipf(zk.wholeUniform, s, 1, uint64(nKeys-1))
if zk.isPartition {
// Generate Zipf for for each part
zk.partZipf = make([]*rand.Zipf, nParts)
for i := 0; i < nParts; i++ {
zk.partZipf[i] = rand.NewZipf(zk.partUniform[i], s, 1, uint64(pKeysArray[i]-1))
}
}
}
return zk
}
func (zk *ZipfKey) GetKey() Key {
if zk.isZipf {
return Key(zk.wholeZipf.Uint64())
} else {
return Key(zk.wholeUniform.Int63n(zk.nKeys))
}
}
func (zk *ZipfKey) GetSelfKey() Key {
if !zk.isPartition {
clog.Error("Should not be invoked for non-partition CC")
}
pi := zk.partIndex
if zk.isZipf {
rank := int64(zk.partZipf[pi].Uint64())
return zk.hp.GetKey(pi, int64(rank))
} else {
return zk.hp.GetKey(pi, zk.partUniform[pi].Int63n(zk.pKeysArray[pi]))
}
}
func (zk *ZipfKey) GetOtherKey(pi int) Key {
if !zk.isPartition {
clog.Error("Should not be invoked for non-partition CC")
}
if zk.isZipf {
rank := int64(zk.partZipf[pi].Uint64())
return zk.hp.GetKey(pi, int64(rank))
} else {
return zk.hp.GetKey(pi, zk.partUniform[pi].Int63n(zk.pKeysArray[pi]))
}
}