config.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package vacuum
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/admin/config"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/worker_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/worker/tasks/base"
  8. )
  9. // Config extends BaseConfig with vacuum-specific settings
  10. type Config struct {
  11. base.BaseConfig
  12. GarbageThreshold float64 `json:"garbage_threshold"`
  13. MinVolumeAgeSeconds int `json:"min_volume_age_seconds"`
  14. MinIntervalSeconds int `json:"min_interval_seconds"`
  15. }
  16. // NewDefaultConfig creates a new default vacuum configuration
  17. func NewDefaultConfig() *Config {
  18. return &Config{
  19. BaseConfig: base.BaseConfig{
  20. Enabled: true,
  21. ScanIntervalSeconds: 2 * 60 * 60, // 2 hours
  22. MaxConcurrent: 2,
  23. },
  24. GarbageThreshold: 0.3, // 30%
  25. MinVolumeAgeSeconds: 24 * 60 * 60, // 24 hours
  26. MinIntervalSeconds: 7 * 24 * 60 * 60, // 7 days
  27. }
  28. }
  29. // ToTaskPolicy converts configuration to a TaskPolicy protobuf message
  30. func (c *Config) ToTaskPolicy() *worker_pb.TaskPolicy {
  31. return &worker_pb.TaskPolicy{
  32. Enabled: c.Enabled,
  33. MaxConcurrent: int32(c.MaxConcurrent),
  34. RepeatIntervalSeconds: int32(c.ScanIntervalSeconds),
  35. CheckIntervalSeconds: int32(c.ScanIntervalSeconds),
  36. TaskConfig: &worker_pb.TaskPolicy_VacuumConfig{
  37. VacuumConfig: &worker_pb.VacuumTaskConfig{
  38. GarbageThreshold: float64(c.GarbageThreshold),
  39. MinVolumeAgeHours: int32(c.MinVolumeAgeSeconds / 3600), // Convert seconds to hours
  40. MinIntervalSeconds: int32(c.MinIntervalSeconds),
  41. },
  42. },
  43. }
  44. }
  45. // FromTaskPolicy loads configuration from a TaskPolicy protobuf message
  46. func (c *Config) FromTaskPolicy(policy *worker_pb.TaskPolicy) error {
  47. if policy == nil {
  48. return fmt.Errorf("policy is nil")
  49. }
  50. // Set general TaskPolicy fields
  51. c.Enabled = policy.Enabled
  52. c.MaxConcurrent = int(policy.MaxConcurrent)
  53. c.ScanIntervalSeconds = int(policy.RepeatIntervalSeconds) // Direct seconds-to-seconds mapping
  54. // Set vacuum-specific fields from the task config
  55. if vacuumConfig := policy.GetVacuumConfig(); vacuumConfig != nil {
  56. c.GarbageThreshold = float64(vacuumConfig.GarbageThreshold)
  57. c.MinVolumeAgeSeconds = int(vacuumConfig.MinVolumeAgeHours * 3600) // Convert hours to seconds
  58. c.MinIntervalSeconds = int(vacuumConfig.MinIntervalSeconds)
  59. }
  60. return nil
  61. }
  62. // LoadConfigFromPersistence loads configuration from the persistence layer if available
  63. func LoadConfigFromPersistence(configPersistence interface{}) *Config {
  64. config := NewDefaultConfig()
  65. // Try to load from persistence if available
  66. if persistence, ok := configPersistence.(interface {
  67. LoadVacuumTaskPolicy() (*worker_pb.TaskPolicy, error)
  68. }); ok {
  69. if policy, err := persistence.LoadVacuumTaskPolicy(); err == nil && policy != nil {
  70. if err := config.FromTaskPolicy(policy); err == nil {
  71. glog.V(1).Infof("Loaded vacuum configuration from persistence")
  72. return config
  73. }
  74. }
  75. }
  76. glog.V(1).Infof("Using default vacuum configuration")
  77. return config
  78. }
  79. // GetConfigSpec returns the configuration schema for vacuum tasks
  80. func GetConfigSpec() base.ConfigSpec {
  81. return base.ConfigSpec{
  82. Fields: []*config.Field{
  83. {
  84. Name: "enabled",
  85. JSONName: "enabled",
  86. Type: config.FieldTypeBool,
  87. DefaultValue: true,
  88. Required: false,
  89. DisplayName: "Enable Vacuum Tasks",
  90. Description: "Whether vacuum tasks should be automatically created",
  91. HelpText: "Toggle this to enable or disable automatic vacuum task generation",
  92. InputType: "checkbox",
  93. CSSClasses: "form-check-input",
  94. },
  95. {
  96. Name: "scan_interval_seconds",
  97. JSONName: "scan_interval_seconds",
  98. Type: config.FieldTypeInterval,
  99. DefaultValue: 2 * 60 * 60,
  100. MinValue: 10 * 60,
  101. MaxValue: 24 * 60 * 60,
  102. Required: true,
  103. DisplayName: "Scan Interval",
  104. Description: "How often to scan for volumes needing vacuum",
  105. HelpText: "The system will check for volumes that need vacuuming at this interval",
  106. Placeholder: "2",
  107. Unit: config.UnitHours,
  108. InputType: "interval",
  109. CSSClasses: "form-control",
  110. },
  111. {
  112. Name: "max_concurrent",
  113. JSONName: "max_concurrent",
  114. Type: config.FieldTypeInt,
  115. DefaultValue: 2,
  116. MinValue: 1,
  117. MaxValue: 10,
  118. Required: true,
  119. DisplayName: "Max Concurrent Tasks",
  120. Description: "Maximum number of vacuum tasks that can run simultaneously",
  121. HelpText: "Limits the number of vacuum operations running at the same time to control system load",
  122. Placeholder: "2 (default)",
  123. Unit: config.UnitCount,
  124. InputType: "number",
  125. CSSClasses: "form-control",
  126. },
  127. {
  128. Name: "garbage_threshold",
  129. JSONName: "garbage_threshold",
  130. Type: config.FieldTypeFloat,
  131. DefaultValue: 0.3,
  132. MinValue: 0.0,
  133. MaxValue: 1.0,
  134. Required: true,
  135. DisplayName: "Garbage Percentage Threshold",
  136. Description: "Trigger vacuum when garbage ratio exceeds this percentage",
  137. HelpText: "Volumes with more deleted content than this threshold will be vacuumed",
  138. Placeholder: "0.30 (30%)",
  139. Unit: config.UnitNone,
  140. InputType: "number",
  141. CSSClasses: "form-control",
  142. },
  143. {
  144. Name: "min_volume_age_seconds",
  145. JSONName: "min_volume_age_seconds",
  146. Type: config.FieldTypeInterval,
  147. DefaultValue: 24 * 60 * 60,
  148. MinValue: 1 * 60 * 60,
  149. MaxValue: 7 * 24 * 60 * 60,
  150. Required: true,
  151. DisplayName: "Minimum Volume Age",
  152. Description: "Only vacuum volumes older than this duration",
  153. HelpText: "Prevents vacuuming of recently created volumes that may still be actively written to",
  154. Placeholder: "24",
  155. Unit: config.UnitHours,
  156. InputType: "interval",
  157. CSSClasses: "form-control",
  158. },
  159. {
  160. Name: "min_interval_seconds",
  161. JSONName: "min_interval_seconds",
  162. Type: config.FieldTypeInterval,
  163. DefaultValue: 7 * 24 * 60 * 60,
  164. MinValue: 1 * 24 * 60 * 60,
  165. MaxValue: 30 * 24 * 60 * 60,
  166. Required: true,
  167. DisplayName: "Minimum Interval",
  168. Description: "Minimum time between vacuum operations on the same volume",
  169. HelpText: "Prevents excessive vacuuming of the same volume by enforcing a minimum wait time",
  170. Placeholder: "7",
  171. Unit: config.UnitDays,
  172. InputType: "interval",
  173. CSSClasses: "form-control",
  174. },
  175. },
  176. }
  177. }