Repository navigation
feat(ess): add study trial fields to work units - #1683
VLukiantsev wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces study trial fields (parameters and trial_id) to the WorkUnit message for STUDY experiments. It adds database columns and migrations for both PostgreSQL and Spanner, implements validation logic to verify trial parameters against the study configuration, and updates the RPC handlers and tests accordingly. The review feedback suggests a minor optimization in the batch validation logic to avoid storing empty trial IDs in the tracking map.
fcad1fb to
0c5b863
Compare
0c5b863 to
39165e8
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces study trial fields (parameters and trial_id) to WorkUnit and implements validation, storage, and retrieval of these fields across both Postgres and Spanner backends. The BatchCreateWorkUnits RPC now validates trial parameters against the study configuration, while CreateWorkUnit rejects them. Feedback on the changes highlights two validation issues in internal/validator/studytrial.go: a potential validation bypass when duplicate parameter specs exist in StudyConfig, and a potential nil-pointer panic if a parameter value is nil.
39165e8 to
d1b2471
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for study trial fields (parameters and trial_id) on WorkUnit objects. It updates the protobuf definitions, implements validation logic against StudyConfig parameters, adds database schema migrations and storage support for both PostgreSQL and Spanner, and updates the BatchCreateWorkUnits RPC to handle these fields. The review feedback focuses on defensive programming, suggesting the addition of nil checks for WorkUnit and ParameterSpec pointers in the validation and database mapping layers to prevent potential nil pointer dereference panics.
| return fmt.Errorf("work_unit.parameters is required on work units of STUDY experiments: %w", grpcerr.ErrInvalidArgument) | ||
| } | ||
| fields := params.GetFields() | ||
| for _, spec := range specs { |
There was a problem hiding this comment.
Defensive programming: specs can potentially contain nil elements if the study configuration is malformed or not fully validated. Adding a nil check for spec prevents a potential nil pointer dereference panic when calling spec.GetParameterId().
for _, spec := range specs {
if spec == nil {
continue
}There was a problem hiding this comment.
No panic is possible here. specs comes from the experiment's stored StudyConfig, which ESS validated at CreateExperiment time and which is immutable. Proto unmarshalling never produces nil elements in a repeated message field. Even for a nil *ParameterSpec, the generated getters (GetParameterId(), GetParameterValueSpec()) are nil-receiver-safe and return zero values. Such a spec would just never match a key, and the value would fail as a type mismatch (INVALID_ARGUMENT). Adding explicit nil branches would add dead code without changing behaviour.
| func validateParameterValue(spec *essmsgpb.ParameterSpec, value *structpb.Value) error { | ||
| switch s := spec.GetParameterValueSpec().(type) { |
There was a problem hiding this comment.
Defensive programming: Ensure spec is not nil before accessing its fields or calling GetParameterValueSpec() to prevent a potential nil pointer dereference panic.
func validateParameterValue(spec *essmsgpb.ParameterSpec, value *structpb.Value) error {
if spec == nil {
return fmt.Errorf("parameter spec is nil: %w", grpcerr.ErrInvalidArgument)
}
switch s := spec.GetParameterValueSpec().(type) {There was a problem hiding this comment.
No panic is possible here. specs comes from the experiment's stored StudyConfig, which ESS validated at CreateExperiment time and which is immutable. Proto unmarshalling never produces nil elements in a repeated message field. Even for a nil *ParameterSpec, the generated getters (GetParameterId(), GetParameterValueSpec()) are nil-receiver-safe and return zero values. Such a spec would just never match a key, and the value would fail as a type mismatch (INVALID_ARGUMENT). Adding explicit nil branches would add dead code without changing behaviour.
| // message is deterministic at linear cost. | ||
| func firstUnknownKey(fields map[string]*structpb.Value, specs []*essmsgpb.ParameterSpec) (string, bool) { | ||
| known := make(map[string]bool, len(specs)) | ||
| for _, spec := range specs { |
There was a problem hiding this comment.
No panic is possible here. specs comes from the experiment's stored StudyConfig, which ESS validated at CreateExperiment time and which is immutable. Proto unmarshalling never produces nil elements in a repeated message field. Even for a nil *ParameterSpec, the generated getters (GetParameterId(), GetParameterValueSpec()) are nil-receiver-safe and return zero values. Such a spec would just never match a key, and the value would fail as a type mismatch (INVALID_ARGUMENT). Adding explicit nil branches would add dead code without changing behaviour.
| func setTrialFields(wu *essmsgpb.WorkUnit, parameters []byte, trialID *string) error { | ||
| if parameters != nil { |
There was a problem hiding this comment.
Defensive programming: Add a nil check for wu to prevent a potential nil pointer dereference panic if a nil work unit is passed to setTrialFields.
func setTrialFields(wu *essmsgpb.WorkUnit, parameters []byte, trialID *string) error {
if wu == nil {
return fmt.Errorf("work unit is nil")
}
if parameters != nil {There was a problem hiding this comment.
Not adding a nil check: this is a private converter helper, and a nil work unit can't reach it.
- Row-to-proto converters (
WorkUnitRowToProtoand the paused-task converter): they call it on aWorkUnitthey have just allocated. WorkUnitProtoToRow: it first validateswu.GetCurrentWorkUnitExecution().GetName(). The getters are nil-safe, so a nil work unit fails that validation with an error beforetrialFieldsToColumnsis reached.
A nil check here would be dead code.
| func trialFieldsToColumns(wu *essmsgpb.WorkUnit) ([]byte, *string, error) { | ||
| parameters, err := MarshalNullableProto(wu.GetParameters()) |
There was a problem hiding this comment.
Defensive programming: Add a nil check for wu to prevent a potential nil pointer dereference panic if a nil work unit is passed to trialFieldsToColumns.
| func trialFieldsToColumns(wu *essmsgpb.WorkUnit) ([]byte, *string, error) { | |
| parameters, err := MarshalNullableProto(wu.GetParameters()) | |
| func trialFieldsToColumns(wu *essmsgpb.WorkUnit) ([]byte, *string, error) { | |
| if wu == nil { | |
| return nil, nil, fmt.Errorf("work unit is nil") | |
| } | |
| parameters, err := MarshalNullableProto(wu.GetParameters()) |
There was a problem hiding this comment.
Not adding a nil check: this is a private converter helper, and a nil work unit can't reach it.
- Row-to-proto converters (
WorkUnitRowToProtoand the paused-task converter): they call it on aWorkUnitthey have just allocated. WorkUnitProtoToRow: it first validateswu.GetCurrentWorkUnitExecution().GetName(). The getters are nil-safe, so a nil work unit fails that validation with an error beforetrialFieldsToColumnsis reached.
A nil check here would be dead code.
| func readTrialFields(row *spanner.Row, wu *pb.WorkUnit) error { | ||
| var parameters []byte |
There was a problem hiding this comment.
There was a problem hiding this comment.
Not adding a nil check: this is a private converter helper, and a nil work unit can't reach it.
- Row-to-proto converters (
WorkUnitRowToProtoand the paused-task converter): they call it on aWorkUnitthey have just allocated. WorkUnitProtoToRow: it first validateswu.GetCurrentWorkUnitExecution().GetName(). The getters are nil-safe, so a nil work unit fails that validation with an error beforetrialFieldsToColumnsis reached.
A nil check here would be dead code.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for study trial parameters and trial IDs by adding parameters (as google.protobuf.Struct) and trial_id (as string) fields to the WorkUnit proto. The changes include updating the storage schemas for both Postgres and Spanner, implementing validation logic in the experiment state server to ensure these fields are correctly handled during BatchCreateWorkUnits, and updating relevant tests and documentation. I have no feedback to provide.
Study trials must carry their hyperparameter values and the optimization engine's trial ID, and ESS must reject values outside the study's search space before they reach a workload. - API: immutable WorkUnit.parameters (google.protobuf.Struct) and WorkUnit.trial_id. - Storage: migration 020 adds nullable WorkUnits.Parameters (proto bytes, no proto bundle change) and TrialId on Spanner and PostgreSQL; converters write them and every read path returns them, including the paused-task joins. - Validation: BatchCreateWorkUnits requires trial_id (at most 128 printable bytes) and parameters with exactly one feasible value per study_config parameter, and rejects duplicate trial IDs within a batch; CreateWorkUnit rejects both fields. - Docs: ESS README STUDY section and the batch create flow. BUG=b/558652203 TAG=agy CONV=4b859498-5191-4c6a-9c79-00403f7cb8ae
d1b2471 to
b9340bd
Compare
Summary
Add the study trial fields
WorkUnit.parameters(field 11,google.protobuf.Struct, IMMUTABLE) andWorkUnit.trial_id(field 12, IMMUTABLE, set by the controller). They are persisted through migration 020 on both backends.BatchCreateWorkUnitsrequires both fields onSTUDYexperiments and validates the parameters against the study config; unaryCreateWorkUnitrejects them. This is layer 3 of a 4-PR stack.Motivation & Context
Each trial carries the optimization engine's suggested parameter values and the engine's trial ID. The actuator will inject the parameters into containers in the follow-up dispatch task. Validating them against
StudyConfigat the API boundary keeps malformed or out-of-range suggestions out of storage.Sub-slice: HPO M1 S1.3. First production caller: the upcoming controller trial-dispatch task.
Key Changes
parameters = 11andtrial_id = 12onWorkUnit, withstruct_protoadded to BUILD deps.TestSchemaVersionParitypasses):WorkUnits.Parameters BYTES(MAX)(proto-marshaled Struct) andTrialId STRING(MAX).BYTEAandTEXT.WorkUnitdescriptor, which backs theWorkUnitTypecolumn andOperations.Response, keeps the new fields as unknown fields.ess-schema.sql,workunitschema(selectable, not filterable) and converters on both backends handle NULL, empty and corrupt bytes. Every read path, including the paused-task joins, returns the new columns on both backends.internal/validator/studytrial.go):trial_idis required, printable, valid UTF-8, at most 128 bytes, and unique within the batch.parametersmust have exactly the study's parameter keys.CreateWorkUnitrejects them with "set only through BatchCreateWorkUnits on a STUDY experiment".trial_iddedup) and the work unit lifecycle doc.Assumptions
CREATE PROTO BUNDLEwith aWorkUnitdescriptor that referencesgoogle.protobuf.Structbehaves like the existingTimestampreferences — it was verified only on an upgraded live database and the emulator — if a fresh database rejects it, the bundle needs the Struct types or a schema split.trial_idis unique only within one batch — the controller owns cross-batch dedup — duplicates across ticks are possible if the controller misbehaves.TestSchemaVersionParityand the repository tests.Verification & Testing
./.githooks/pre-commit;bazel teston the ESS server,internal/workunit,internal/validator,internal/storage/...andinternal/experiment/...(76/76 pass);run_affected_tests.pyat the stack tip (155/155 pass).stable-2026bprofile, stack tip after the sync to main):ddl describeshowsParameters BYTES(MAX), TrialId STRING(MAX);SchemaMigrationsis version 20, not dirty.trialId/parameters, visible inGetWorkUnit,ListWorkUnitsand the Spanner rows.InvalidArgumentand nothing was created: missing key, unknown key, out of range, duplicatetrial_id.Related Issues / Bugs
BUG=b/558652203
TAG=agy
CONV=4b859498-5191-4c6a-9c79-00403f7cb8ae