Repository navigation
[#557] Workers: Use Deque - #596
dancingactor wants to merge 1 commit into
Conversation
| pthread_cond_t worker_all_idle; /**< Are workers idle */ | ||
| bool outcome; /**< Outcome of the workers */ | ||
| struct queue queue; /**< The queue */ | ||
| struct deque* queue; /**< The task queue using deque */ |
| w->outcome = true; | ||
|
|
||
| if (queue_init(&w->queue)) | ||
| if (pgmoneta_deque_create(true, &w->queue)) { |
There was a problem hiding this comment.
Formatting - run uncrustify
| bool outcome; /**< Outcome of the workers */ | ||
| struct queue queue; /**< The queue */ | ||
| struct deque* queue; /**< The task queue using deque */ | ||
| struct semaphore* has_tasks; /**< Are there any tasks ? */ |
There was a problem hiding this comment.
what does this mean
There was a problem hiding this comment.
that comments are at the same position like
/**< X */
/**< Y */
Not
/**< X */
/**< Y */
There was a problem hiding this comment.
Can we use deque_size ?
Sorry, I meant to ask: what does deque_size mean?
There was a problem hiding this comment.
There was a problem hiding this comment.
Do you mean replace semaphore with pgmoneta_deque_size?
There was a problem hiding this comment.
I don't think it can be changed, the semaphore(has_tasks) in the workers struct is essential for dispatching tasks to workers
There was a problem hiding this comment.
Maybe expand the documentation then
There was a problem hiding this comment.
You're right, let me do that
|
|
||
| if (workers != NULL) | ||
| { | ||
| pgmoneta_workers_wait(workers); |
There was a problem hiding this comment.
If _destroy calls _wait then we can remove _wait from the public API, and make it a static function
There was a problem hiding this comment.
But other code like restore.c and wf_hot_standby.c will still use _wait independently(not with _destroy)
There was a problem hiding this comment.
Yes, so you have to port to the new API
There was a problem hiding this comment.
But _destroy and _wait serve different purposes, so I think we should keep both public API
For example, wf_hot_standby.c only uses _wait to make sure all tasks are completed before checking the outcome
There was a problem hiding this comment.
Ok, lets keep them separate then.
However, the code in wf_hot_standby.c is buggy so that need to be fixed
There was a problem hiding this comment.
However, the code in wf_hot_standby.c is buggy so that need to be fixed
Is the bug that _destroy is not called in the error: section?
There was a problem hiding this comment.
It should always be safe to call _destroy even if workers is NULL, so yes - but also the success path...
There was a problem hiding this comment.
I think the success path doesn't have a bug
The snippet at wf_hot_standby.c doesn't call _destroy because workers is still needed for the upcoming pgmoneta_copy_directory() at line 322. The call to _destroy is correctly placed later at line 335.
There was a problem hiding this comment.
It shouldn't be necessary to call _wait inside _destroy if it is part of the public API
There was a problem hiding this comment.
got it, so I sperate them
|
Maybe add some TRACE logging when adding and finishing a task - and showing how many tasks are running |
791faa6 to
e3b59fa
Compare
|
|
||
| if (number_of_workers > 0) | ||
| { | ||
| pgmoneta_workers_wait(workers); |
There was a problem hiding this comment.
We should keep this then
| free(old_manifest); | ||
| free(new_manifest); | ||
|
|
||
| if (number_of_workers > 0) { |
There was a problem hiding this comment.
We shouldn't need the if
|
|
||
| if (number_of_workers > 0) | ||
| { | ||
| pgmoneta_workers_wait(workers); |
| w->has_tasks = (struct semaphore*)malloc(sizeof(struct semaphore)); | ||
| if (w->has_tasks == NULL) | ||
| { | ||
| pgmoneta_log_error("Could not allocate memory for task semaphore"); |
| if (semaphore_init(w->has_tasks, 0)) | ||
| { | ||
| pgmoneta_log_error("Could not allocate memory for queue"); | ||
| pgmoneta_log_error("Could not initialize task semaphore"); |
| struct value_config config = {0}; | ||
| config.destroy_data = destroy_task_wrapper; | ||
|
|
||
| pgmoneta_log_trace("Adding task to queue"); |
There was a problem hiding this comment.
Can we add more information about what was added ?
| pthread_mutex_lock(&workers->worker_lock); | ||
|
|
||
| while (workers->queue.number_of_tasks || workers->number_of_working) | ||
| while (pgmoneta_deque_size(workers->queue) || workers->number_of_working) |
There was a problem hiding this comment.
Use > 0 to make it explicit
| { | ||
| task->function(task->wc); | ||
| free(task); | ||
| pgmoneta_log_trace("Finished task"); |
There was a problem hiding this comment.
Move up a bit and add a bit more information
There was a problem hiding this comment.
But if I move up this log, how can I make sure it logs after finishing the task
|
|
||
| if (workers != NULL) | ||
| { | ||
| pgmoneta_workers_wait(workers); |
There was a problem hiding this comment.
It shouldn't be necessary to call _wait inside _destroy if it is part of the public API
| { | ||
| pthread_mutex_lock(&semaphore->mutex); | ||
| while (semaphore->value != 1) | ||
| while (semaphore->value == 0) |
There was a problem hiding this comment.
Yes, this is correct. Because semaphore->value is changing from binary(0 or 1) to counting(1, 2, 3, 4....). It will only keep waiting when the number of tasks is zero.
But I think value should be renamed to count, as it better reflects its current usage, should I do that?
|
@jesperpedersen By the way, I really appreciate your time and feeback. you literally make my code better with your valuable time, and I have learned so much from it. I really appreciate your help, especially knowing you’re doing this with no pay and just to support the community. |
10a96a1 to
a143fca
Compare
| int number_of_tasks; /**< The number of tasks */ | ||
| void (*function)(struct worker_common*); /**< The task function */ | ||
| struct worker_common* wc; /**< Pointer to the common data */ | ||
| const char* name; /**< The name of the function */ |
There was a problem hiding this comment.
got it, could you explain the reason. Thanks
| */ | ||
| int | ||
| pgmoneta_workers_add(struct workers* workers, void (*function)(struct worker_common*), struct worker_common* wi); | ||
| pgmoneta_workers_add(struct workers* workers, void (*function)(struct worker_common*), struct worker_common* wc, const char* function_name); |
|
|
||
| int | ||
| pgmoneta_workers_add(struct workers* workers, void (*function)(struct worker_common*), struct worker_common* wc) | ||
| pgmoneta_workers_add(struct workers* workers, void (*function)(struct worker_common*), struct worker_common* wc, const char* function_name) |
| struct value_config config = {0}; | ||
| config.destroy_data = destroy_task_wrapper; | ||
|
|
||
| pgmoneta_log_trace("Adding task to queue: %s", function_name); |
There was a problem hiding this comment.
Maybe add the task pointer too so %s/%p
| if (task) | ||
| { | ||
| task->function(task->wc); | ||
| pgmoneta_log_trace("Finished task: %s", task->name); |
| struct value_config config = {0}; | ||
| config.destroy_data = destroy_task_wrapper; | ||
|
|
||
| pgmoneta_log_trace("Adding task to queue: %s (%p)", function_name, function); |
There was a problem hiding this comment.
Remove - it doesn't add anything really
There was a problem hiding this comment.
Do you mean I should remove the log?
There was a problem hiding this comment.
But didn't you just tell me to add the log when adding and finishing the tasks
There was a problem hiding this comment.
There was a problem hiding this comment.
Yes, but it didn't add anything useful in the end - so we can remove it
| if (task) | ||
| { | ||
| task->function(task->wc); | ||
| pgmoneta_log_trace("Finished task: %s (%p)", task->name, task->function); |
There was a problem hiding this comment.
Remove - it doesn't really add anything
|
|
||
| static int | ||
| queue_init(struct queue* queue) | ||
| semaphore_init(struct semaphore* semaphore, int count) |
There was a problem hiding this comment.
Remove int count and init to 0
|
Please, look at |
| task->name = function_name; | ||
|
|
||
| queue_push(&workers->queue, t); | ||
| struct value_config config = {0}; |
There was a problem hiding this comment.
Move to the beginning of the function
|
Try and see if you can make |
|
And, |
|
Make |
Sure. but could I ask the reason, I am curious, Thanks |
|
with atomic we don't need to take/release the mutex -- but it isn't super critical if you can't make it work |
Got it, I will try it out |
|
Of course we can remove |
|
Maybe forward declare |
|
We should do the standard function signature for all functions too, e.g. Since |
|
To make it easier just use |
55aa68e to
57ed02c
Compare
|
@dancingactor Thanks, I'll work on it for a bit |
|
Sorry, I haven’t finished it yet. I’ll re-request the review again once I complete |
|
@dancingactor No worries, I'm working on your patch since we need it for 0.16 |
got it, so do I need to keep fixing the code |
|
@dancingactor I got it |
|
If there's anything I can help with, I am happy to do anytime! |
|
@dancingactor Feel free to find a task, and start on it |
|
Thanks! Now I am working on another issue #523 |
|
@dancingactor Don't spend time on this - I have completely changed the API on my branch |
|
Oh okay, thanks for the heads-up! |
|
@dancingactor We are going with mostly your patch for now. We can make improvements later |
|
Merged. Thanks for your contribution ! |
implement #557
fix PR #565
This PR replace the custom queue in workers.c with the existing deque in deque.c implementation. Backup successfully with workers = 8
Updates
Comparing to the original code, I’ve made several updates
worker_taskandworker_commonto avoid freeing them both when we want to free the taskpgmoneta_workers_waittopgmoneta_workers_destroy