/
sharded_counter.go
119 lines (109 loc) · 2.71 KB
/
sharded_counter.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package sharded_counter
import (
"fmt"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
"math/rand"
)
type counterConfig struct {
Shards int
}
type shard struct {
Name string
Count int
}
const (
defaultShards = 20
configKind = "GeneralCounterShardConfig"
shardKind = "GeneralCounterShard"
)
func memcacheKey(name string) string {
return shardKind + ":" + name
}
// Count retrieves the value of the named counter.
func Count(c context.Context, name string) (int, error) {
total := 0
mkey := memcacheKey(name)
if _, err := memcache.JSON.Get(c, mkey, &total); err == nil {
return total, nil
}
pKey := datastore.NewKey(c, configKind, name, 0, nil)
q := datastore.NewQuery(shardKind).Ancestor(pKey).Filter("Name =", name)
for t := q.Run(c); ; {
var s shard
_, err := t.Next(&s)
if err == datastore.Done {
break
}
if err != nil {
return total, err
}
total += s.Count
}
memcache.JSON.Set(c, &memcache.Item{
Key: mkey,
Object: &total,
})
return total, nil
}
// Increment increments the named counter.
func Increment(c context.Context, name string) error {
// Get counter config.
var cfg counterConfig
ckey := datastore.NewKey(c, configKind, name, 0, nil)
err := datastore.RunInTransaction(c, func(c context.Context) error {
err := datastore.Get(c, ckey, &cfg)
if err == datastore.ErrNoSuchEntity {
cfg.Shards = defaultShards
_, err = datastore.Put(c, ckey, &cfg)
}
return err
}, nil)
if err != nil {
return err
}
err = datastore.RunInTransaction(c, func(c context.Context) error {
shardName := fmt.Sprintf("shard%d", rand.Intn(cfg.Shards))
key := datastore.NewKey(c, shardKind, shardName, 0, ckey)
var s shard
err := datastore.Get(c, key, &s)
// A missing entity and a present entity will both work.
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
s.Count++
s.Name = name
_, err = datastore.Put(c, key, &s)
return err
}, nil)
if err != nil {
return err
}
memcache.IncrementExisting(c, memcacheKey(name), 1)
return nil
}
// IncreaseShards increases the number of shards for the named counter to n.
// It will never decrease the number of shards.
func IncreaseShards(c context.Context, name string, n int) error {
ckey := datastore.NewKey(c, configKind, name, 0, nil)
return datastore.RunInTransaction(c, func(c context.Context) error {
var cfg counterConfig
mod := false
err := datastore.Get(c, ckey, &cfg)
if err == datastore.ErrNoSuchEntity {
cfg.Shards = defaultShards
mod = true
} else if err != nil {
return err
}
if cfg.Shards < n {
cfg.Shards = n
mod = true
}
if mod {
_, err = datastore.Put(c, ckey, &cfg)
}
return err
}, nil)
}