- 
                Notifications
    You must be signed in to change notification settings 
- Fork 180
Improve the StorageLocal struct #1327
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
          
     Open
      
      
            djeebus
  wants to merge
  15
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
improve-storage-local
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a39e711
              
                Improve the StorageLocal struct
              
              
                djeebus ca7856b
              
                Merge commit 'a8d150d44809db43c1c36655410ea0159bf3c3f5' into improve-…
              
              
                djeebus 2a471b5
              
                add some metrics
              
              
                djeebus 9b9d473
              
                move more fields to env vars
              
              
                djeebus 3696ec5
              
                Merge remote-tracking branch 'origin/main' into improve-storage-local
              
              
                djeebus 5b5503f
              
                use files to lock network slot usage
              
              
                djeebus e800b36
              
                linting
              
              
                djeebus 2548dc4
              
                Merge branch 'main' into improve-storage-local
              
              
                djeebus 64f39bd
              
                clean up
              
              
                djeebus 5a80535
              
                oops.
              
              
                djeebus 24bb8d3
              
                Don't be clever, just call the function
              
              
                djeebus d4ffd93
              
                good catch cursor!
              
              
                djeebus 0346508
              
                fix the tests
              
              
                djeebus 83c4f00
              
                Merge remote-tracking branch 'origin/main' into improve-storage-local
              
              
                djeebus 1e76a51
              
                linting
              
              
                djeebus File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package network | ||
|  | ||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "reflect" | ||
|  | ||
| "github.com/caarlos0/env/v11" | ||
| ) | ||
|  | ||
| type Config struct { | ||
| // Using reserver IPv4 in range that is used for experiments and documentation | ||
| // https://en.wikipedia.org/wiki/Reserved_IP_addresses | ||
| HyperloopIPAddress string `env:"SANDBOX_HYPERLOOP_IP" envDefault:"192.0.2.1"` | ||
| HyperloopProxyPort uint16 `env:"SANDBOX_HYPERLOOP_PROXY_PORT" envDefault:"5010"` | ||
| UseLocalNamespaceStorage bool `env:"USE_LOCAL_NAMESPACE_STORAGE"` | ||
| LocalNamespaceStorageDir string `env:"LOCAL_NAMESPACE_STORAGE_DIR" envDefault:"/run/orchestrator/netns"` | ||
|  | ||
| SandboxesHostNetworkCIDR *net.IPNet `env:"SANDBOXES_HOST_NETWORK_CIDR" envDefault:"10.11.0.0/16"` | ||
| SandboxesVirtualNetworkCIDR *net.IPNet `env:"SANDBOXES_VIRTUAL_NETWORK_CIDR" envDefault:"10.12.0.0/16"` | ||
| } | ||
|  | ||
| func (c Config) GetVirtualSlotsSize() int { | ||
| ones, _ := c.SandboxesVirtualNetworkCIDR.Mask.Size() | ||
|  | ||
| // total IPs in the CIDR block | ||
| totalIPs := 1 << (32 - ones) | ||
|  | ||
| // total slots that we can allocate | ||
| // we need to divide total IPs by number of addresses per slot (vpeer and veth) | ||
| // then we subtract the number of addresses so it will not overflow, because we are adding them incrementally by slot index | ||
| totalSlots := (totalIPs / vrtAddressPerSlot) - vrtAddressPerSlot | ||
|  | ||
| return totalSlots | ||
| } | ||
|  | ||
| func ParseConfig() (Config, error) { | ||
| return env.ParseAsWithOptions[Config](env.Options{ | ||
| FuncMap: map[reflect.Type]env.ParserFunc{ | ||
| reflect.TypeOf(net.IPNet{}): ParseIPNet, | ||
| }, | ||
| }) | ||
| } | ||
|  | ||
| func ParseIPNet(cidr string) (any, error) { | ||
| _, subnet, err := net.ParseCIDR(cidr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse network CIDR %s: %w", cidr, err) | ||
| } | ||
|  | ||
| return *subnet, nil | ||
| } | ||
        
          
  
    
      
          
            32 changes: 32 additions & 0 deletions
          
          32 
        
  packages/orchestrator/internal/sandbox/network/config_test.go
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package network | ||
|  | ||
| import ( | ||
| "net" | ||
| "reflect" | ||
| "testing" | ||
|  | ||
| "github.com/caarlos0/env/v11" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|  | ||
| func TestParseIPNet(t *testing.T) { | ||
| t.Setenv("CIDR_OVERRIDE_DEFAULT", "5.0.0.0/8") | ||
|  | ||
| var testConfig struct { | ||
| HasDefault *net.IPNet `env:"CIDR_USE_DEFAULT" envDefault:"10.10.0.0/16"` | ||
| OverrideDefault *net.IPNet `env:"CIDR_OVERRIDE_DEFAULT" envDefault:"1.2.3.4/16"` | ||
| NilBecauseNonePassed *net.IPNet `env:"CIDR_NIL_BECAUSE_NONE_PASSED"` | ||
| } | ||
|  | ||
| err := env.ParseWithOptions(&testConfig, env.Options{ | ||
| FuncMap: map[reflect.Type]env.ParserFunc{ | ||
| reflect.TypeOf(net.IPNet{}): ParseIPNet, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|  | ||
| assert.Equal(t, "10.10.0.0/16", testConfig.HasDefault.String()) | ||
| assert.Equal(t, "5.0.0.0/8", testConfig.OverrideDefault.String()) | ||
| assert.Nil(t, testConfig.NilBecauseNonePassed) | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Value Receiver Prevents Caching in Method
The
GetVirtualSlotsSize()method uses a value receiver, so the assignment toc.virtualSlotSizemodifies a copy of the struct. This prevents the calculated value from being cached, causing the expensive calculation to repeat on every call.