|
| 1 | +// Copyright 2021-2022, Offchain Labs, Inc. |
| 2 | +// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md |
| 3 | + |
| 4 | +package arbnode |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/andybalholm/brotli" |
| 12 | + "github.com/knadh/koanf" |
| 13 | + "github.com/knadh/koanf/providers/confmap" |
| 14 | + |
| 15 | + "github.com/offchainlabs/nitro/util/arbmath" |
| 16 | +) |
| 17 | + |
| 18 | +// CompressionLevelStep defines compression levels to use at a given backlog threshold. |
| 19 | +type CompressionLevelStep struct { |
| 20 | + Backlog uint64 `koanf:"backlog" json:"backlog"` |
| 21 | + Level int `koanf:"level" json:"level"` |
| 22 | + RecompressionLevel int `koanf:"recompression-level" json:"recompression-level"` |
| 23 | +} |
| 24 | + |
| 25 | +// CompressionLevelStepList is a list of compression level steps for configuring |
| 26 | +// adaptive compression based on batch backlog. |
| 27 | +type CompressionLevelStepList []CompressionLevelStep |
| 28 | + |
| 29 | +func (l *CompressionLevelStepList) Set(jsonStr string) error { |
| 30 | + return l.UnmarshalJSON([]byte(jsonStr)) |
| 31 | +} |
| 32 | + |
| 33 | +func (l *CompressionLevelStepList) String() string { |
| 34 | + b, _ := json.Marshal(l) |
| 35 | + return string(b) |
| 36 | +} |
| 37 | + |
| 38 | +func (l *CompressionLevelStepList) UnmarshalJSON(data []byte) error { |
| 39 | + var tmp []CompressionLevelStep |
| 40 | + if err := json.Unmarshal(data, &tmp); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + *l = tmp |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (_ *CompressionLevelStepList) Type() string { |
| 48 | + return "CompressionLevelStepList" |
| 49 | +} |
| 50 | + |
| 51 | +// Validate checks that the compression level steps are valid: |
| 52 | +// - Must have at least one entry |
| 53 | +// - First entry must have backlog: 0 |
| 54 | +// - Backlog thresholds must be strictly ascending |
| 55 | +// - Level and RecompressionLevel must be weakly descending (non-increasing) |
| 56 | +// - RecompressionLevel must be >= Level within each entry |
| 57 | +// - All levels must be in valid range: 0-11 |
| 58 | +func (l CompressionLevelStepList) Validate() error { |
| 59 | + if len(l) == 0 { |
| 60 | + return errors.New("compression-levels must have at least one entry") |
| 61 | + } |
| 62 | + if l[0].Backlog != 0 { |
| 63 | + return errors.New("first compression-levels entry must have backlog: 0") |
| 64 | + } |
| 65 | + for i, step := range l { |
| 66 | + if step.Level < 0 || step.Level > 11 { |
| 67 | + return fmt.Errorf("compression-levels[%d].level must be 0-11, got %d", i, step.Level) |
| 68 | + } |
| 69 | + if step.RecompressionLevel < 0 || step.RecompressionLevel > 11 { |
| 70 | + return fmt.Errorf("compression-levels[%d].recompression-level must be 0-11, got %d", i, step.RecompressionLevel) |
| 71 | + } |
| 72 | + if step.RecompressionLevel < step.Level { |
| 73 | + return fmt.Errorf("compression-levels[%d].recompression-level (%d) must be >= level (%d)", i, step.RecompressionLevel, step.Level) |
| 74 | + } |
| 75 | + if i > 0 { |
| 76 | + if step.Backlog <= l[i-1].Backlog { |
| 77 | + return fmt.Errorf("compression-levels[%d].backlog must be > compression-levels[%d].backlog", i, i-1) |
| 78 | + } |
| 79 | + if step.Level > l[i-1].Level { |
| 80 | + return fmt.Errorf("compression-levels[%d].level must be <= compression-levels[%d].level (weakly descending)", i, i-1) |
| 81 | + } |
| 82 | + if step.RecompressionLevel > l[i-1].RecompressionLevel { |
| 83 | + return fmt.Errorf("compression-levels[%d].recompression-level must be <= compression-levels[%d].recompression-level (weakly descending)", i, i-1) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +var parsedCompressionLevelsConf CompressionLevelStepList |
| 91 | + |
| 92 | +// FixCompressionLevelsCLIParsing decode compression-levels json CLI ARG |
| 93 | +func FixCompressionLevelsCLIParsing(path string, k *koanf.Koanf) error { |
| 94 | + raw := k.Get(path) |
| 95 | + if jsonStr, ok := raw.(string); ok { |
| 96 | + if err := parsedCompressionLevelsConf.Set(jsonStr); err != nil { |
| 97 | + |
| 98 | + return err |
| 99 | + } |
| 100 | + tempMap := map[string]interface{}{path: parsedCompressionLevelsConf} |
| 101 | + if err := k.Load(confmap.Provider(tempMap, "."), nil); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + } |
| 105 | + return fmt.Errorf("CompressionLevels config not found in %s", path) |
| 106 | +} |
| 107 | + |
| 108 | +// DefaultCompressionLevels replicates the previous hardcoded adaptive compression behavior: |
| 109 | +var DefaultCompressionLevels = CompressionLevelStepList{ |
| 110 | + {Backlog: 0, Level: brotli.BestCompression, RecompressionLevel: brotli.BestCompression}, |
| 111 | + {Backlog: 21, Level: brotli.DefaultCompression, RecompressionLevel: brotli.BestCompression}, |
| 112 | + {Backlog: 41, Level: brotli.DefaultCompression, RecompressionLevel: brotli.DefaultCompression}, |
| 113 | + {Backlog: 61, Level: 4, RecompressionLevel: brotli.DefaultCompression}, |
| 114 | +} |
| 115 | + |
| 116 | +// ResolveCompressionLevels resolves the compression configuration from deprecated and new fields. |
| 117 | +// Returns error if both are set. Converts deprecated format to new format if needed. |
| 118 | +func ResolveCompressionLevels(compressionLevel int, compressionLevels CompressionLevelStepList) (CompressionLevelStepList, error) { |
| 119 | + // Check for conflict: both deprecated and new config set |
| 120 | + if compressionLevel > 0 && len(compressionLevels) > 0 { |
| 121 | + return nil, errors.New("cannot specify both compression-level (deprecated) and compression-levels; use only compression-levels") |
| 122 | + } |
| 123 | + |
| 124 | + // Return DefaultCompressionLevels if both (compressionLevel and compressionLevels ) are not set |
| 125 | + if len(compressionLevels) == 0 && compressionLevel == 0 { |
| 126 | + return DefaultCompressionLevels, nil |
| 127 | + } |
| 128 | + |
| 129 | + // If new config is set, validate and return it |
| 130 | + if len(compressionLevels) > 0 { |
| 131 | + if err := compressionLevels.Validate(); err != nil { |
| 132 | + return nil, fmt.Errorf("invalid compression-levels: %w", err) |
| 133 | + } |
| 134 | + return compressionLevels, nil |
| 135 | + } |
| 136 | + |
| 137 | + // Convert deprecated `compressionLevel` config to new format of compressionLevels |
| 138 | + resolved := CompressionLevelStepList{ |
| 139 | + {Backlog: 0, Level: compressionLevel, RecompressionLevel: compressionLevel}, |
| 140 | + {Backlog: 21, Level: arbmath.MinInt(compressionLevel, brotli.DefaultCompression), RecompressionLevel: compressionLevel}, |
| 141 | + {Backlog: 41, Level: arbmath.MinInt(compressionLevel, brotli.DefaultCompression), RecompressionLevel: arbmath.MinInt(compressionLevel, brotli.DefaultCompression)}, |
| 142 | + {Backlog: 61, Level: arbmath.MinInt(compressionLevel, 4), RecompressionLevel: arbmath.MinInt(compressionLevel, brotli.DefaultCompression)}, |
| 143 | + } |
| 144 | + |
| 145 | + if err := resolved.Validate(); err != nil { |
| 146 | + return nil, fmt.Errorf("invalid compression-levels derived from compression-level: %w", err) |
| 147 | + } |
| 148 | + return resolved, nil |
| 149 | +} |
0 commit comments