Browse Source

update from prod

stashes
Fusl 2 years ago
parent
commit
a83778e713
1 changed files with 27 additions and 19 deletions
  1. +27
    -19
      main.go

+ 27
- 19
main.go View File

@@ -34,10 +34,9 @@ type ProjectRedisConfig struct {
} }


type ProjectOffloadConfig struct { type ProjectOffloadConfig struct {
WatermarkHigh int64 `json:"high"`
WatermarkMiddle int64 `json:"middle"`
WatermarkLow int64 `json:"low"`
BatchSize int64 `json:"batchsize"`
WatermarkHigh int64 `json:"high"`
WatermarkLow int64 `json:"low"`
BatchSize int64 `json:"batchsize"`
} }


type ProjectConfig struct { type ProjectConfig struct {
@@ -48,7 +47,6 @@ type ProjectConfig struct {
type Offloader struct { type Offloader struct {
RedisClient *redis.Client RedisClient *redis.Client
ProjectConfig ProjectConfig ProjectConfig ProjectConfig
OffloadConfig ProjectOffloadConfig
Context context.Context Context context.Context
Cancel context.CancelFunc Cancel context.CancelFunc
Done chan bool Done chan bool
@@ -72,32 +70,30 @@ func (that *Offloader) RedisConfigDiffers(new *ProjectRedisConfig) bool {
} }


func (that *Offloader) OffloadConfigDiffers(new ProjectOffloadConfig) bool { func (that *Offloader) OffloadConfigDiffers(new ProjectOffloadConfig) bool {
return that.OffloadConfig.WatermarkHigh != new.WatermarkHigh || that.OffloadConfig.WatermarkMiddle != new.WatermarkMiddle || that.OffloadConfig.WatermarkLow != new.WatermarkLow || that.OffloadConfig.BatchSize != new.BatchSize
return that.ProjectConfig.OffloadConfig.WatermarkHigh != new.WatermarkHigh || that.ProjectConfig.OffloadConfig.WatermarkLow != new.WatermarkLow || that.ProjectConfig.OffloadConfig.BatchSize != new.BatchSize
} }


func (that *Offloader) RefreshQueues() {
func (that *Offloader) RefreshQueues() error {
pipe := that.RedisClient.Pipeline() pipe := that.RedisClient.Pipeline()
prioritiesCmdRes := pipe.ZRange(that.Context, fmt.Sprintf("%s:priorities", that.Name), 0, -1) prioritiesCmdRes := pipe.ZRange(that.Context, fmt.Sprintf("%s:priorities", that.Name), 0, -1)
filtersCmdRes := pipe.SMembers(that.Context, fmt.Sprintf("%s:filters", that.Name)) filtersCmdRes := pipe.SMembers(that.Context, fmt.Sprintf("%s:filters", that.Name))
_, err := pipe.Exec(that.Context) _, err := pipe.Exec(that.Context)
if err != nil { if err != nil {
log.Printf("unable to refresh queues for offloader %s: %s", that.Name, err)
return
return err
} }
priorities, err := prioritiesCmdRes.Result() priorities, err := prioritiesCmdRes.Result()
if err != nil { if err != nil {
log.Printf("unable to refresh queues for offloader %s: %s", that.Name, err)
return
return err
} }
filters, err := filtersCmdRes.Result() filters, err := filtersCmdRes.Result()
if err != nil { if err != nil {
log.Printf("unable to refresh queues for offloader %s: %s", that.Name, err)
return
return err
} }
setQueueMap := map[string]string{ setQueueMap := map[string]string{
"todo": "todo", "todo": "todo",
"todo:secondary": "todo:secondary", "todo:secondary": "todo:secondary",
"todo:redo": "todo:redo", "todo:redo": "todo:redo",
"todo:backfeed": "todo:backfeed",
"done": "done", "done": "done",
"unretrievable": "unretrievable", "unretrievable": "unretrievable",
} }
@@ -112,7 +108,11 @@ func (that *Offloader) RefreshQueues() {
needQueueMap[queueName] = true needQueueMap[queueName] = true
if _, has := that.Queues[queueName]; !has { if _, has := that.Queues[queueName]; !has {
log.Printf("opening queue %s for %s:%s", queueName, that.Name, setName) log.Printf("opening queue %s for %s:%s", queueName, that.Name, setName)
that.Queues[queueName] = dq.New(fmt.Sprintf("%s:%s", that.Name, that.CleanName(queueName)), dataDir, 128*1024*1024, 0, 128*1024*1024, 1_000_000, 5*time.Second, l)
queue := dq.New(fmt.Sprintf("%s:%s", that.Name, that.CleanName(queueName)), dataDir, 128*1024*1024, 0, 128*1024*1024, 1_000_000, 5*time.Second, l)
if queue == nil {
return fmt.Errorf("unable to open disk queue %s:%s (dq.New()==nil)", that.Name, that.CleanName(queueName))
}
that.Queues[queueName] = queue
} }
that.Sets[setName] = queueName that.Sets[setName] = queueName
} }
@@ -122,6 +122,7 @@ func (that *Offloader) RefreshQueues() {
delete(that.Queues, k) delete(that.Queues, k)
} }
} }
return nil
} }


func (that *Offloader) CloseQueues() { func (that *Offloader) CloseQueues() {
@@ -163,23 +164,26 @@ func (that *Offloader) Do() {
refreshTicker := time.NewTicker(5 * time.Minute) refreshTicker := time.NewTicker(5 * time.Minute)
defer refreshTicker.Stop() defer refreshTicker.Stop()


that.RefreshQueues()
if err := that.RefreshQueues(); err != nil {
log.Printf("unable to refresh queues for %s: %s", that.Name, err)
return
}
that.UpdateStats() that.UpdateStats()


skipSleepChan := make(chan bool, 1) skipSleepChan := make(chan bool, 1)
defer close(skipSleepChan) defer close(skipSleepChan)


watermarkHigh := that.OffloadConfig.WatermarkHigh
watermarkHigh := that.ProjectConfig.OffloadConfig.WatermarkHigh
if watermarkHigh == 0 { if watermarkHigh == 0 {
watermarkHigh = DefaultWatermarkHigh watermarkHigh = DefaultWatermarkHigh
} }


watermarkLow := that.OffloadConfig.WatermarkLow
watermarkLow := that.ProjectConfig.OffloadConfig.WatermarkLow
if watermarkLow == 0 { if watermarkLow == 0 {
watermarkLow = DefaultWatermarkLow watermarkLow = DefaultWatermarkLow
} }


batchSize := that.OffloadConfig.BatchSize
batchSize := that.ProjectConfig.OffloadConfig.BatchSize
if batchSize == 0 { if batchSize == 0 {
batchSize = DefaultBatchSize batchSize = DefaultBatchSize
} }
@@ -334,7 +338,10 @@ func (that *Offloader) Do() {
case <-that.Context.Done(): case <-that.Context.Done():
return return
case <-refreshTicker.C: case <-refreshTicker.C:
that.RefreshQueues()
if err := that.RefreshQueues(); err != nil {
log.Printf("unable to refresh queues for %s: %s", that.Name, err)
return
}
that.UpdateStats() that.UpdateStats()
case <-ticker.C: case <-ticker.C:
that.UpdateStats() that.UpdateStats()
@@ -449,6 +456,7 @@ func main() {
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
ticker := time.NewTicker(1 * time.Minute) ticker := time.NewTicker(1 * time.Minute)
defer StopProjects()
for { for {
RefreshProjects(mainClient) RefreshProjects(mainClient)
select { select {


Loading…
Cancel
Save