You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.8 KiB

Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
4 years ago
Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
4 years ago
Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
4 years ago
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package queue
  5. import (
  6. "context"
  7. "io/ioutil"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/modules/util"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestPersistableChannelQueue(t *testing.T) {
  14. handleChan := make(chan *testData)
  15. handle := func(data ...Data) {
  16. assert.True(t, len(data) == 2)
  17. for _, datum := range data {
  18. testDatum := datum.(*testData)
  19. handleChan <- testDatum
  20. }
  21. }
  22. queueShutdown := []func(){}
  23. queueTerminate := []func(){}
  24. tmpDir, err := ioutil.TempDir("", "persistable-channel-queue-test-data")
  25. assert.NoError(t, err)
  26. defer util.RemoveAll(tmpDir)
  27. queue, err := NewPersistableChannelQueue(handle, PersistableChannelQueueConfiguration{
  28. DataDir: tmpDir,
  29. BatchLength: 2,
  30. QueueLength: 20,
  31. Workers: 1,
  32. MaxWorkers: 10,
  33. }, &testData{})
  34. assert.NoError(t, err)
  35. go queue.Run(func(_ context.Context, shutdown func()) {
  36. queueShutdown = append(queueShutdown, shutdown)
  37. }, func(_ context.Context, terminate func()) {
  38. queueTerminate = append(queueTerminate, terminate)
  39. })
  40. test1 := testData{"A", 1}
  41. test2 := testData{"B", 2}
  42. err = queue.Push(&test1)
  43. assert.NoError(t, err)
  44. go func() {
  45. err = queue.Push(&test2)
  46. assert.NoError(t, err)
  47. }()
  48. result1 := <-handleChan
  49. assert.Equal(t, test1.TestString, result1.TestString)
  50. assert.Equal(t, test1.TestInt, result1.TestInt)
  51. result2 := <-handleChan
  52. assert.Equal(t, test2.TestString, result2.TestString)
  53. assert.Equal(t, test2.TestInt, result2.TestInt)
  54. err = queue.Push(test1)
  55. assert.Error(t, err)
  56. for _, callback := range queueShutdown {
  57. callback()
  58. }
  59. time.Sleep(200 * time.Millisecond)
  60. err = queue.Push(&test1)
  61. assert.NoError(t, err)
  62. err = queue.Push(&test2)
  63. assert.NoError(t, err)
  64. select {
  65. case <-handleChan:
  66. assert.Fail(t, "Handler processing should have stopped")
  67. default:
  68. }
  69. for _, callback := range queueTerminate {
  70. callback()
  71. }
  72. // Reopen queue
  73. queue, err = NewPersistableChannelQueue(handle, PersistableChannelQueueConfiguration{
  74. DataDir: tmpDir,
  75. BatchLength: 2,
  76. QueueLength: 20,
  77. Workers: 1,
  78. MaxWorkers: 10,
  79. }, &testData{})
  80. assert.NoError(t, err)
  81. go queue.Run(func(_ context.Context, shutdown func()) {
  82. queueShutdown = append(queueShutdown, shutdown)
  83. }, func(_ context.Context, terminate func()) {
  84. queueTerminate = append(queueTerminate, terminate)
  85. })
  86. result3 := <-handleChan
  87. assert.Equal(t, test1.TestString, result3.TestString)
  88. assert.Equal(t, test1.TestInt, result3.TestInt)
  89. result4 := <-handleChan
  90. assert.Equal(t, test2.TestString, result4.TestString)
  91. assert.Equal(t, test2.TestInt, result4.TestInt)
  92. for _, callback := range queueShutdown {
  93. callback()
  94. }
  95. for _, callback := range queueTerminate {
  96. callback()
  97. }
  98. }