-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
235 lines (192 loc) · 5.54 KB
/
main.go
File metadata and controls
235 lines (192 loc) · 5.54 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"time"
ascache "github.com/sshaplygin/as-cache"
slfu "github.com/sshaplygin/as-cache/lfu"
hlru "github.com/hashicorp/golang-lru/v2"
"github.com/stitchfix/mab"
)
type UserProfile struct {
Name string
Email string
CreatedAt time.Time
}
func main() {
lruCache, err := hlru.New[string, *UserProfile](100)
if err != nil {
panic(err)
}
policiesList := []ascache.Policy[string, *UserProfile]{
ascache.NewCache(lruCache, ascache.LRU, 100),
}
lfuCache, err := slfu.New[string, *UserProfile](100)
if err != nil {
panic(err)
}
policiesList = append(policiesList, ascache.NewCache(lfuCache, ascache.LFU, 100))
armNames := []ascache.PolicyType{ascache.LRU, ascache.LFU}
myBandit := NewThompsonBanditAdapter(
armNames,
)
cache, err := ascache.NewAdaptiveCache(
policiesList,
myBandit,
&ascache.Settings{
EpochDuration: 5 * time.Minute,
},
)
if err != nil {
panic(err)
}
defer cache.Close()
val, ok := cache.Get("key")
fmt.Println(val, ok)
mux := http.NewServeMux()
mux.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
key := r.URL.Query().Get("key")
if key == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
val, ok := cache.Get(key)
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write([]byte(val.Name))
})
mux.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
key := r.URL.Query().Get("key")
if key == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
name := r.URL.Query().Get("name")
if name == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
email := r.URL.Query().Get("email")
if email == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
_ = cache.Add(key, &UserProfile{})
w.Write([]byte("ok"))
})
server := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
go func() {
log.Println("server started on :8080")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Panicf("error: %v", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Panicf("shutdown error: %v", err)
}
log.Println("server stopped")
}
func NewThompsonBanditAdapter(armNames []ascache.PolicyType) *StitchFixBanditAdapter {
rewardStore := NewCacheRewardSource(armNames)
return &StitchFixBanditAdapter{
bandit: &mab.Bandit{
RewardSource: rewardStore,
Strategy: mab.NewThompson(nil),
Sampler: mab.NewSha1Sampler(),
},
rewardStore: rewardStore,
armNames: armNames,
unitID: "adaptive-selection-cache",
}
}
type armStats struct {
// Beta distribution parameters: Alpha = Hits + 1, Beta = Misses + 1.
Hits float64
Misses float64
}
// CacheRewardSource implements the mab.RewardSource interface.
// It stores per-arm statistics supplied by the MAB adapter.
type CacheRewardSource struct {
mu sync.RWMutex
stats map[ascache.PolicyType]*armStats
}
func NewCacheRewardSource(armNames []ascache.PolicyType) *CacheRewardSource {
crs := &CacheRewardSource{
stats: make(map[ascache.PolicyType]*armStats, len(armNames)),
}
for _, name := range armNames {
crs.stats[name] = &armStats{}
}
return crs
}
// GetRewards is the "Pull" method called by stitchfix/mab when it needs to
// make an arm-selection decision.
func (crs *CacheRewardSource) GetRewards(ctx context.Context, banditContext interface{}) ([]mab.Dist, error) {
crs.mu.RLock()
defer crs.mu.RUnlock()
distributions := make([]mab.Dist, len(crs.stats))
for i, arm := range crs.stats {
distributions[i] = mab.Beta(arm.Hits+1, arm.Misses+1)
}
return distributions, nil
}
// updateStats is the "Push" method called by the MAB adapter to record
// observed hits and misses for a given policy.
func (crs *CacheRewardSource) updateStats(policy ascache.PolicyType, hits, misses int64) {
crs.mu.Lock()
defer crs.mu.Unlock()
s, ok := crs.stats[policy]
if !ok {
return
}
s.Hits += float64(hits)
s.Misses += float64(misses)
}
//====================================================================
// 2. ADAPTER IMPLEMENTING THE `Bandit` INTERFACE
//====================================================================
// StitchFixBanditAdapter wraps the stitchfix bandit and implements the
// ascache.Bandit interface.
type StitchFixBanditAdapter struct {
bandit *mab.Bandit
rewardStore *CacheRewardSource
armNames []ascache.PolicyType
// unitID is a stable identifier for the single shared cache instance,
// used by stitchfix/mab for deterministic arm selection.
unitID string
}
// RecordStats implements the ascache.Bandit "Push" interface.
// It forwards shadow-cache statistics into the reward store.
func (s *StitchFixBanditAdapter) RecordStats(stats ascache.ShadowStats) {
s.rewardStore.updateStats(stats.Policy, stats.Hits, stats.Misses)
}
// SelectPolicy implements the ascache.Bandit "Pull" interface.
// It asks the stitchfix bandit to select the best arm by sampling from
// the Beta distributions stored in the reward source.
func (s *StitchFixBanditAdapter) SelectPolicy() ascache.PolicyType {
selectedArm, err := s.bandit.SelectArm(context.Background(), s.unitID, s.armNames)
if err != nil {
// Fall back to the first arm on any error.
return s.armNames[0]
}
return s.armNames[selectedArm.Arm]
}