Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): specify machine type and concurrency info of worker through env vars #884

Merged
merged 5 commits into from
Feb 20, 2025

Conversation

pyshx
Copy link
Contributor

@pyshx pyshx commented Feb 19, 2025

Overview

What I've done

What I haven't done

How I tested

Screenshot

Which point I want you to review particularly

Memo

Summary by CodeRabbit

  • New Features
    • Introduced enhanced configuration options for batch processing, including new fields for boot disk size, boot disk type, and machine type.
    • Administrators can now adjust worker settings—such as boot disk size, boot disk type, machine type, maximum concurrency, and task count—via new environment variable configurations, offering more flexible control over job execution.

@pyshx pyshx requested a review from a team as a code owner February 19, 2025 20:00
Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Walkthrough

The changes introduce multiple new fields to the Config struct in the configuration file, specifically for worker settings, including Worker_BootDiskSizeGB, Worker_BootDiskType, Worker_MachineType, Worker_MaxConcurrency, and Worker_TaskCount. Additionally, existing fields have been updated to support environment variable configuration. The initBatch function and BatchConfig struct have been modified to incorporate these new fields, enhancing the flexibility of batch processing configurations and ensuring proper error handling for type conversions.

Changes

File Changes
server/api/.../config/config.go Added new fields Worker_BootDiskSizeGB, Worker_BootDiskType, Worker_MachineType, Worker_MaxConcurrency, and Worker_TaskCount to Config. Modified existing fields to include envconfig tags.
server/api/.../repo.go Updated initBatch function to initialize BootDiskSizeGB, BootDiskType, MachineType, and TaskCount fields in BatchConfig with error handling for conversions.
server/api/.../gcpbatch/batch.go Enhanced BatchConfig struct by adding BootDiskSizeGB, BootDiskType, MachineType, and modified TaskCount to integer; updated SubmitJob to utilize these new dynamic parameters.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant CFG as Config Loader
    participant IB as initBatch Function
    participant BC as BatchConfig
    participant BR as BatchRepo (SubmitJob)
    participant GCP as GCP Batch Service

    App->>CFG: Load configuration values
    CFG->>IB: Provide config (incl. worker fields)
    IB->>BC: Convert string values & initialize BatchConfig
    BC->>BR: Pass configuration for batch processing
    BR->>GCP: Submit job with BootDiskSizeGB, BootDiskType, MachineType, TaskCount
Loading

Possibly related PRs

  • chore(api): add support for env vars for flow worker #816: The changes in the main PR, which involve adding and modifying fields in the Config struct, are directly related to the updates in the retrieved PR that enhance the BatchConfig struct by adding similar fields for batch processing configuration.

Poem

I'm a hopping bunny in a field of code,
Skipping along each aligned node.
New worker fields make my heart beat fast,
As batch jobs leap and run at last.
With a joyful hop and a cheerful cheer,
I nibble sweet carrots of code so dear! 🐰


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1cee710 and 881b052.

📒 Files selected for processing (2)
  • server/api/internal/app/config/config.go (1 hunks)
  • server/api/internal/app/repo.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/api/internal/app/config/config.go
🔇 Additional comments (2)
server/api/internal/app/repo.go (2)

5-5: LGTM!

The strconv package is correctly imported for string-to-integer conversions.


118-123: LGTM!

The new fields are correctly added to the BatchConfig struct and follow proper field ordering.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Feb 19, 2025

Deploy Preview for reearth-flow canceled.

Name Link
🔨 Latest commit 881b052
🔍 Latest deploy log https://app.netlify.com/sites/reearth-flow/deploys/67b6f4b295c1340008164495

@pyshx pyshx changed the title [FLOW-BE-28] feat(api): specify machine type and concurrency info of worker through env vars feat(api): specify machine type and concurrency info of worker through env vars Feb 19, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
server/api/internal/app/repo.go (1)

113-119: Consider using a helper function for string to int conversion.

The conversion logic for MaxConcurrency is duplicated with TaskCount. Consider extracting this into a helper function to improve code maintainability and reduce duplication.

Apply this diff to extract the conversion logic:

+func convertToInt(value string, fieldName string) int {
+    result, err := strconv.Atoi(value)
+    if err != nil {
+        log.Fatalf("Failed to convert %s: %v", fieldName, err)
+    }
+    return result
+}

 func initBatch(ctx context.Context, conf *config.Config) (batchRepo gateway.Batch) {
     var err error
     if conf.Worker_ImageURL != "" {
         config := gcpbatch.BatchConfig{
             BinaryPath:     conf.Worker_BinaryPath,
             ImageURI:       conf.Worker_ImageURL,
             MachineType:    conf.Worker_MachineType,
-            MaxConcurrency: func() int {
-                mc, err := strconv.Atoi(conf.Worker_MaxConcurrency)
-                if err != nil {
-                    log.Fatalf("Failed to convert MaxConcurrency: %v", err)
-                }
-                return mc
-            }(),
+            MaxConcurrency: convertToInt(conf.Worker_MaxConcurrency, "MaxConcurrency"),
             ProjectID:      conf.GCPProject,
             Region:         conf.GCPRegion,
             SAEmail:        conf.Worker_BatchSAEmail,
-            TaskCount: func() int {
-                tc, err := strconv.Atoi(conf.Worker_TaskCount)
-                if err != nil {
-                    log.Fatalf("Failed to convert TaskCount: %v", err)
-                }
-                return tc
-            }(),
+            TaskCount: convertToInt(conf.Worker_TaskCount, "TaskCount"),
         }
server/api/internal/app/config/config.go (1)

75-77: Consider using integer types for numeric configurations.

The Worker_MaxConcurrency and Worker_TaskCount fields are defined as strings but represent numeric values. Consider using integer types with string tags for better type safety and to avoid runtime conversion errors.

Apply this diff to use integer types:

-    Worker_MaxConcurrency string `envconfig:"WORKER_MAX_CONCURRENCY" default:"4" pp:",omitempty"`
-    Worker_TaskCount      string `envconfig:"WORKER_TASK_COUNT" default:"1" pp:",omitempty"`
+    Worker_MaxConcurrency int    `envconfig:"WORKER_MAX_CONCURRENCY" default:"4" pp:",omitempty"`
+    Worker_TaskCount      int    `envconfig:"WORKER_TASK_COUNT" default:"1" pp:",omitempty"`

This change would require updating the initBatch function to remove the string to int conversion:

// In server/api/internal/app/repo.go
     config := gcpbatch.BatchConfig{
         BinaryPath:     conf.Worker_BinaryPath,
         ImageURI:       conf.Worker_ImageURL,
         MachineType:    conf.Worker_MachineType,
-        MaxConcurrency: convertToInt(conf.Worker_MaxConcurrency, "MaxConcurrency"),
+        MaxConcurrency: conf.Worker_MaxConcurrency,
         ProjectID:      conf.GCPProject,
         Region:         conf.GCPRegion,
         SAEmail:        conf.Worker_BatchSAEmail,
-        TaskCount:      convertToInt(conf.Worker_TaskCount, "TaskCount"),
+        TaskCount:      conf.Worker_TaskCount,
     }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 09d0a8f and 403661a.

⛔ Files ignored due to path filters (1)
  • go.work.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • server/api/internal/app/config/config.go (3 hunks)
  • server/api/internal/app/repo.go (2 hunks)
  • server/api/internal/infrastructure/gcpbatch/batch.go (3 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
server/api/internal/app/repo.go

110-110: File is not goimports-ed

(goimports)

server/api/internal/app/config/config.go

35-35: File is not goimports-ed

(goimports)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: ci-api / ci-api-test

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
server/api/internal/infrastructure/gcpbatch/batch.go (1)

21-31: 🛠️ Refactor suggestion

Add field validation in BatchConfig.

The struct fields lack validation for their values.

+func (c *BatchConfig) Validate() error {
+    if c.BootDiskSizeGB <= 0 {
+        return fmt.Errorf("BootDiskSizeGB must be greater than 0")
+    }
+    if c.TaskCount <= 0 {
+        return fmt.Errorf("TaskCount must be greater than 0")
+    }
+    if !regexp.MustCompile(`^[a-z][a-z0-9]*-[a-z0-9-]*$`).MatchString(c.MachineType) {
+        return fmt.Errorf("invalid machine type format")
+    }
+    return nil
+}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 403661a and 1cee710.

📒 Files selected for processing (3)
  • server/api/internal/app/config/config.go (3 hunks)
  • server/api/internal/app/repo.go (2 hunks)
  • server/api/internal/infrastructure/gcpbatch/batch.go (2 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
server/api/internal/app/repo.go

110-110: File is not goimports-ed

(goimports)

server/api/internal/app/config/config.go

35-35: File is not goimports-ed

(goimports)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: ci-api / ci-api-test
🔇 Additional comments (1)
server/api/internal/infrastructure/gcpbatch/batch.go (1)

137-146: LGTM! Well-structured boot disk configuration.

The implementation of boot disk configuration is clean and properly uses the new configuration fields.

@pyshx pyshx merged commit 9c2660d into main Feb 20, 2025
21 checks passed
@pyshx pyshx deleted the feat/FLOW-BE-28 branch February 20, 2025 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant