Skip to content

Commit b23df7c

Browse files
chore: using configured stan modelId (#1237)
1 parent d959edc commit b23df7c

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

manifests/bucketeer/charts/batch/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ spec:
144144
value: "{{ .Values.env.nonPersistentRedis.poolMaxActive }}"
145145
- name: BUCKETEER_BATCH_EXPERIMENT_LOCK_TTL
146146
value: "{{ .Values.env.experimentLockTTL }}"
147+
- name: BUCKETEER_BATCH_STAN_MODEL_ID
148+
value: "{{ .Values.httpstan.modelId }}"
147149

148150
volumeMounts:
149151
- name: service-cert-secret

manifests/bucketeer/charts/batch/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ httpstan:
9797
pullPolicy: IfNotPresent
9898
config:
9999
port: 8080
100+
# The model ID is generated when the HTTP Stan model is compiled.
101+
# We must update the ID every time we compile the model.
102+
# Since we don't change the model so often, this ID won't change.
103+
modelId: y3qsnd7m
100104
healthPath: v1/health
101105
resources: {}
102106

manifests/bucketeer/values.dev.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ batch-server:
161161
addr: localenv-redis-headless.default.svc.cluster.local:6379
162162
poolMaxIdle: 25
163163
poolMaxActive: 25
164-
experimentLockTTL: 10m
164+
experimentLockTTL: 1m
165165

166166
tls:
167167
service:

pkg/batch/cmd/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type server struct {
8080
oauthIssuer *string
8181
stanHost *string
8282
stanPort *string
83+
stanModelID *string
8384
// MySQL
8485
mysqlUser *string
8586
mysqlPass *string
@@ -136,6 +137,7 @@ func RegisterCommand(r cli.CommandRegistry, p cli.ParentCommand) cli.Command {
136137
oauthIssuer: cmd.Flag("oauth-issuer", "The issuer url").Required().String(),
137138
stanHost: cmd.Flag("stan-host", "httpstan host.").Default("localhost").String(),
138139
stanPort: cmd.Flag("stan-port", "httpstan port.").Default("8080").String(),
140+
stanModelID: cmd.Flag("stan-model-id", "httpstan modelId.").Required().String(),
139141
mysqlUser: cmd.Flag("mysql-user", "MySQL user.").Required().String(),
140142
mysqlPass: cmd.Flag("mysql-pass", "MySQL password.").Required().String(),
141143
mysqlHost: cmd.Flag("mysql-host", "MySQL host.").Required().String(),
@@ -456,6 +458,7 @@ func (s *server) Run(ctx context.Context, metrics metrics.Metrics, logger *zap.L
456458
),
457459
calculator.NewExperimentCalculate(
458460
stan.NewStan(*s.stanHost, *s.stanPort),
461+
*s.stanModelID,
459462
environmentClient,
460463
experimentClient,
461464
eventCounterClient,

pkg/batch/jobs/calculator/experiment_calculate.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type experimentCalculate struct {
5252

5353
func NewExperimentCalculate(
5454
httpStan *stan.Stan,
55+
stanModelID string,
5556
environmentClient environmentclient.Client,
5657
experimentClient experimentclient.Client,
5758
ecClient ecclient.Client,
@@ -69,6 +70,7 @@ func NewExperimentCalculate(
6970
}
7071
calculator := experimentcalc.NewExperimentCalculator(
7172
httpStan,
73+
stanModelID,
7274
environmentClient,
7375
ecClient,
7476
experimentClient,
@@ -154,8 +156,9 @@ func (e *experimentCalculate) calculateExperimentWithLock(ctx context.Context,
154156
)
155157
return nil
156158
}
157-
158-
defer func() {
159+
if calcErr := e.calculateExperiment(ctx, env, experiment); calcErr != nil {
160+
// To prevent calculating the same experiment multiple times in a short time,
161+
// we set the TTL for the lock key and only unlock it when an error occurs so that it can retry.
159162
unlocked, unlockErr := e.experimentLock.Unlock(ctx, env.Id, experiment.Id, lockValue)
160163
if unlockErr != nil {
161164
e.logger.Error("Failed to release lock",
@@ -170,9 +173,9 @@ func (e *experimentCalculate) calculateExperimentWithLock(ctx context.Context,
170173
zap.String("experimentId", experiment.Id),
171174
)
172175
}
173-
}()
174-
175-
return e.calculateExperiment(ctx, env, experiment)
176+
return calcErr
177+
}
178+
return nil
176179
}
177180

178181
func (e *experimentCalculate) calculateExperiment(ctx context.Context,

pkg/experimentcalculator/experimentcalc/experiment_calculator.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const (
5050
)
5151

5252
type ExperimentCalculator struct {
53-
httpStan *stan.Stan
54-
modelID string
53+
httpStan *stan.Stan
54+
stanModelID string
5555

5656
environmentClient envclient.Client
5757
eventCounterClient ecclient.Client
@@ -65,6 +65,7 @@ type ExperimentCalculator struct {
6565

6666
func NewExperimentCalculator(
6767
httpStan *stan.Stan,
68+
stanModelID string,
6869
environmentClient envclient.Client,
6970
eventCounterClient ecclient.Client,
7071
experimentClient experimentclient.Client,
@@ -74,24 +75,9 @@ func NewExperimentCalculator(
7475
logger *zap.Logger,
7576
) *ExperimentCalculator {
7677
registerMetrics(metrics)
77-
var compiledModel stan.ModelCompileResp
78-
for i := 0; i < 3; i++ {
79-
resp, err := httpStan.CompileModel(context.TODO(), stan.ModelCode())
80-
if err != nil {
81-
logger.Warn("ExperimentCalculator failed to compile model, retrying...",
82-
zap.Error(err),
83-
)
84-
time.Sleep(1 * time.Second)
85-
} else {
86-
compiledModel = resp
87-
break
88-
}
89-
}
90-
modelID := compiledModel.Name[len("models/"):]
9178
return &ExperimentCalculator{
92-
httpStan: httpStan,
93-
modelID: modelID,
94-
79+
httpStan: httpStan,
80+
stanModelID: stanModelID,
9581
environmentClient: environmentClient,
9682
eventCounterClient: eventCounterClient,
9783
experimentClient: experimentClient,
@@ -462,11 +448,11 @@ func (e ExperimentCalculator) binomialModelSample(
462448
NumWarmup: 1000,
463449
RandomSeed: 1234,
464450
}
465-
fitResp, err := e.httpStan.CreateFit(ctx, e.modelID, req)
451+
fitResp, err := e.httpStan.CreateFit(ctx, e.stanModelID, req)
466452
if err != nil {
467453
e.logger.Error("Failed to create fit",
468454
log.FieldsFromImcomingContext(ctx).AddFields(
469-
zap.String("modelId", e.modelID),
455+
zap.String("modelId", e.stanModelID),
470456
zap.Error(err),
471457
)...,
472458
)
@@ -489,7 +475,7 @@ func (e ExperimentCalculator) binomialModelSample(
489475
}
490476
time.Sleep(50 * time.Millisecond)
491477
}
492-
result, err := e.httpStan.GetFitResult(ctx, e.modelID, fitId)
478+
result, err := e.httpStan.GetFitResult(ctx, e.stanModelID, fitId)
493479
if err != nil {
494480
e.logger.Error("Failed to get fit result",
495481
log.FieldsFromImcomingContext(ctx).AddFields(
@@ -500,7 +486,7 @@ func (e ExperimentCalculator) binomialModelSample(
500486
return
501487
}
502488
fit := e.httpStan.ExtractFromFitResult(ctx, result)
503-
constrainedNames, _ := e.httpStan.StanParams(ctx, e.modelID, req.Data)
489+
constrainedNames, _ := e.httpStan.StanParams(ctx, e.stanModelID, req.Data)
504490
samplesChan <- fit.Select(constrainedNames)
505491
}(i)
506492
}

pkg/experimentcalculator/experimentcalc/experiment_calculator_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ var (
3636
jpLocation = time.FixedZone("Asia/Tokyo", 9*60*60)
3737
)
3838

39+
const (
40+
stanModelID = "y3qsnd7m"
41+
)
42+
3943
func creatExperimentCalculator(mockController *gomock.Controller) *ExperimentCalculator {
4044
registerer := metricsmock.NewMockRegisterer(mockController)
4145
registerer.EXPECT().MustRegister(gomock.Any()).Return()
4246
return NewExperimentCalculator(
4347
stan.NewStan("localhost", "8080"),
48+
stanModelID,
4449
envclient.NewMockClient(mockController),
4550
ecclient.NewMockClient(mockController),
4651
experimentclient.NewMockClient(mockController),

0 commit comments

Comments
 (0)