gemini-code-assist[bot] commented on code in PR #37974:
URL: https://github.com/apache/beam/pull/37974#discussion_r3184835466


##########
sdks/go/pkg/beam/artifact/options.go:
##########
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package artifact
+
+import (
+       "context"
+
+       structpb "google.golang.org/protobuf/types/known/structpb"
+)
+
+type contextKey string
+
+const pipelineOptionsKey contextKey = "pipeline_options"
+
+// WithPipelineOptions returns a new context carrying the full pipeline 
options struct.
+func WithPipelineOptions(ctx context.Context, options *structpb.Struct) 
context.Context {
+       return context.WithValue(ctx, pipelineOptionsKey, options)
+}
+
+// PipelineOptions returns the pipeline options from the context if present.
+func PipelineOptions(ctx context.Context) *structpb.Struct {
+       options, _ := ctx.Value(pipelineOptionsKey).(*structpb.Struct)
+       return options
+}
+
+// GetExperiments extracts a list of experiments from the pipeline options.
+func GetExperiments(options *structpb.Struct) []string {
+       if options == nil {
+               return nil
+       }
+
+       // Try legacy style first
+       var exps []string
+       for _, v := range 
options.GetFields()["options"].GetStructValue().GetFields()["experiments"].GetListValue().GetValues()
 {
+               exps = append(exps, v.GetStringValue())
+       }
+       if len(exps) > 0 {
+               return exps
+       }
+
+       // Try URN style
+       for _, v := range 
options.GetFields()["beam:option:experiments:v1"].GetListValue().GetValues() {
+               exps = append(exps, v.GetStringValue())
+       }
+       return exps
+}
+
+// HasExperiment checks if a specific experiment is enabled in the pipeline 
options.
+func HasExperiment(options *structpb.Struct, experiment string) bool {
+       for _, exp := range GetExperiments(options) {
+               if exp == experiment {
+                       return true
+               }
+       }
+       return false

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   This function is called for every artifact during materialization. Since 
`GetExperiments` traverses the proto structure and allocates a new slice on 
every call, this introduces unnecessary overhead. Consider caching the 
experiments list or the result of the integrity check (e.g., in the context or 
a local variable) since pipeline options do not change during the boot process.



##########
sdks/go/pkg/beam/artifact/options.go:
##########
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package artifact
+
+import (
+       "context"
+
+       structpb "google.golang.org/protobuf/types/known/structpb"
+)
+
+type contextKey string
+
+const pipelineOptionsKey contextKey = "pipeline_options"
+
+// WithPipelineOptions returns a new context carrying the full pipeline 
options struct.
+func WithPipelineOptions(ctx context.Context, options *structpb.Struct) 
context.Context {
+       return context.WithValue(ctx, pipelineOptionsKey, options)
+}
+
+// PipelineOptions returns the pipeline options from the context if present.
+func PipelineOptions(ctx context.Context) *structpb.Struct {
+       options, _ := ctx.Value(pipelineOptionsKey).(*structpb.Struct)
+       return options
+}
+
+// GetExperiments extracts a list of experiments from the pipeline options.
+func GetExperiments(options *structpb.Struct) []string {
+       if options == nil {
+               return nil
+       }
+
+       // Try legacy style first
+       var exps []string
+       for _, v := range 
options.GetFields()["options"].GetStructValue().GetFields()["experiments"].GetListValue().GetValues()
 {
+               exps = append(exps, v.GetStringValue())
+       }
+       if len(exps) > 0 {
+               return exps
+       }
+
+       // Try URN style
+       for _, v := range 
options.GetFields()["beam:option:experiments:v1"].GetListValue().GetValues() {
+               exps = append(exps, v.GetStringValue())
+       }
+       return exps

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The current implementation returns early if experiments are found in the 
legacy `options` field, which means any experiments provided in the URN-based 
`beam:option:experiments:v1` field will be ignored. It is safer to collect and 
combine experiments from both locations. Additionally, using explicit checks 
improves readability over the long chain of calls.
   
   ```go
        if optVal, ok := options.GetFields()["options"]; ok {
                if experimentsVal, ok := 
optVal.GetStructValue().GetFields()["experiments"]; ok {
                        for _, v := range 
experimentsVal.GetListValue().GetValues() {
                                exps = append(exps, v.GetStringValue())
                        }
                }
        }
   
        // Try URN style
        if urnVal, ok := options.GetFields()["beam:option:experiments:v1"]; ok {
                for _, v := range urnVal.GetListValue().GetValues() {
                        exps = append(exps, v.GetStringValue())
                }
        }
        return exps
   ```



##########
sdks/go/pkg/beam/artifact/materialize.go:
##########
@@ -231,7 +233,18 @@ func (a artifact) retrieve(ctx context.Context, dest 
string) error {
        stat, _ := fd.Stat()
        log.Printf("Downloaded: %v (sha256: %v, size: %v)", filename, 
sha256Hash, stat.Size())
 
-       return fd.Close()
+       if err := fd.Close(); err != nil {
+               return err
+       }
+
+       if isArtifactValidationEnabled(ctx) {
+               if a.expectedSha256 != "" && sha256Hash != a.expectedSha256 {
+                       return errors.Errorf("bad SHA256 for %v: %v, want %v", 
filename, sha256Hash, a.expectedSha256)
+               }
+               log.Printf("Sha256 validation done for file: %v, with sha256 
%v", filename, a.expectedSha256)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Logging a success message for every artifact validation can be very noisy 
for jobs with a large number of staged files. Consider removing this log or 
reducing its verbosity level, as it provides little value during normal 
successful operation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to