Skip to content

[#557] Workers: Use Deque - #596

Closed
dancingactor wants to merge 1 commit into
pgmoneta:mainfrom
dancingactor:test4
Closed

dancingactor wants to merge 1 commit into
pgmoneta:mainfrom
dancingactor:test4

Conversation

@dancingactor

@dancingactor dancingactor commented Apr 14, 2025 •

Copy link
Copy Markdown
Contributor

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

  1. change semaphore value from binary to counting -> This prevents a situation where worker threads go to sleep even though tasks are still available, which can happen if tasks are added faster than they’re polled from the deque.
  2. seperate the worker_task and worker_common to avoid freeing them both when we want to free the task
  3. add pgmoneta_workers_wait to pgmoneta_workers_destroy

@dancingactor dancingactor mentioned this pull request Apr 15, 2025
@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label Apr 15, 2025
Comment thread src/include/workers.h Outdated
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 */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment alignment

Comment thread src/libpgmoneta/workers.c Outdated
w->outcome = true;

if (queue_init(&w->queue))
if (pgmoneta_deque_create(true, &w->queue)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting - run uncrustify

Comment thread src/include/workers.h Outdated
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 ? */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use deque_size ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this mean

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that comments are at the same position like

   /**< X */
   /**< Y */

Not

 /**< X */
   /**< Y */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use deque_size ?
Sorry, I meant to ask: what does deque_size mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean replace semaphore with pgmoneta_deque_size?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if it is possible

@dancingactor dancingactor Apr 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can be changed, the semaphore(has_tasks) in the workers struct is essential for dispatching tasks to workers

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expand the documentation then

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, let me do that

Comment thread src/libpgmoneta/workers.c Outdated

if (workers != NULL)
{
pgmoneta_workers_wait(workers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _destroy calls _wait then we can remove _wait from the public API, and make it a static function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But other code like restore.c and wf_hot_standby.c will still use _wait independently(not with _destroy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so you have to port to the new API

@dancingactor dancingactor Apr 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, lets keep them separate then.

However, the code in wf_hot_standby.c is buggy so that need to be fixed

@dancingactor dancingactor Apr 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should always be safe to call _destroy even if workers is NULL, so yes - but also the success path...

@dancingactor dancingactor Apr 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary to call _wait inside _destroy if it is part of the public API

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, so I sperate them

@jesperpedersen

Copy link
Copy Markdown
Member

Maybe add some TRACE logging when adding and finishing a task - and showing how many tasks are running

@dancingactor
dancingactor force-pushed the test4 branch 3 times, most recently from 791faa6 to e3b59fa Compare April 18, 2025 10:26
Comment thread src/libpgmoneta/wf_gzip.c

if (number_of_workers > 0)
{
pgmoneta_workers_wait(workers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this then

Comment thread src/libpgmoneta/wf_hot_standby.c Outdated
free(old_manifest);
free(new_manifest);

if (number_of_workers > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need the if

Comment thread src/libpgmoneta/wf_lz4.c

if (number_of_workers > 0)
{
pgmoneta_workers_wait(workers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep

Comment thread src/libpgmoneta/workers.c Outdated
w->has_tasks = (struct semaphore*)malloc(sizeof(struct semaphore));
if (w->has_tasks == NULL)
{
pgmoneta_log_error("Could not allocate memory for task semaphore");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task -> workers

Comment thread src/libpgmoneta/workers.c Outdated
if (semaphore_init(w->has_tasks, 0))
{
pgmoneta_log_error("Could not allocate memory for queue");
pgmoneta_log_error("Could not initialize task semaphore");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task -> workers

Comment thread src/libpgmoneta/workers.c Outdated
struct value_config config = {0};
config.destroy_data = destroy_task_wrapper;

pgmoneta_log_trace("Adding task to queue");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more information about what was added ?

Comment thread src/libpgmoneta/workers.c Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use > 0 to make it explicit

Comment thread src/libpgmoneta/workers.c Outdated
{
task->function(task->wc);
free(task);
pgmoneta_log_trace("Finished task");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move up a bit and add a bit more information

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if I move up this log, how can I make sure it logs after finishing the task

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the TRACE

Comment thread src/libpgmoneta/workers.c Outdated

if (workers != NULL)
{
pgmoneta_workers_wait(workers);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary to call _wait inside _destroy if it is part of the public API

Comment thread src/libpgmoneta/workers.c Outdated
{
pthread_mutex_lock(&semaphore->mutex);
while (semaphore->value != 1)
while (semaphore->value == 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@dancingactor

dancingactor commented Apr 18, 2025 •

Copy link
Copy Markdown
Contributor Author

@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.

@dancingactor
dancingactor force-pushed the test4 branch 2 times, most recently from 10a96a1 to a143fca Compare April 18, 2025 17:08
Comment thread src/include/workers.h Outdated
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 */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove const

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, could you explain the reason. Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like const

Comment thread src/include/workers.h Outdated
*/
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same const

Comment thread src/libpgmoneta/workers.c Outdated

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove const

Comment thread src/libpgmoneta/workers.c Outdated
struct value_config config = {0};
config.destroy_data = destroy_task_wrapper;

pgmoneta_log_trace("Adding task to queue: %s", function_name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the task pointer too so %s/%p

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the TRACE

Comment thread src/libpgmoneta/workers.c Outdated
if (task)
{
task->function(task->wc);
pgmoneta_log_trace("Finished task: %s", task->name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here %s/%p

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the TRACE

Comment thread src/libpgmoneta/workers.c Outdated
struct value_config config = {0};
config.destroy_data = destroy_task_wrapper;

pgmoneta_log_trace("Adding task to queue: %s (%p)", function_name, function);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove - it doesn't add anything really

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean I should remove the log?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But didn't you just tell me to add the log when adding and finishing the tasks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it didn't add anything useful in the end - so we can remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

Comment thread src/libpgmoneta/workers.c Outdated
if (task)
{
task->function(task->wc);
pgmoneta_log_trace("Finished task: %s (%p)", task->name, task->function);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove - it doesn't really add anything

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the TRACE

Comment thread src/libpgmoneta/workers.c Outdated

static int
queue_init(struct queue* queue)
semaphore_init(struct semaphore* semaphore, int count)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove int count and init to 0

@jesperpedersen

Copy link
Copy Markdown
Member

Please, look at pgmoneta_workers_destroy - it need A LOT of work

Comment thread src/libpgmoneta/workers.c Outdated
task->name = function_name;

queue_push(&workers->queue, t);
struct value_config config = {0};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to the beginning of the function

@jesperpedersen

Copy link
Copy Markdown
Member

Try and see if you can make workers->number_of_alive an atomic, then in some (not all) can remove the mutex locking

@jesperpedersen

Copy link
Copy Markdown
Member

And, workers->number_of_working

@jesperpedersen

jesperpedersen commented Apr 19, 2025 •

Copy link
Copy Markdown
Member

Make semaphore_init use an output parameter

@dancingactor

Copy link
Copy Markdown
Contributor Author

Try and see if you can make workers->number_of_alive an atomic, then in some (not all) can remove the mutex locking

Sure. but could I ask the reason, I am curious, Thanks

@jesperpedersen

Copy link
Copy Markdown
Member

with atomic we don't need to take/release the mutex -- but it isn't super critical if you can't make it work

@dancingactor

Copy link
Copy Markdown
Contributor Author

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

@jesperpedersen

Copy link
Copy Markdown
Member

Of course we can remove function_name too since we don't need it

@jesperpedersen

Copy link
Copy Markdown
Member

Maybe forward declare struct workers instead of struct worker_common because then struct worker_common can be the first struct worker_X defined

@jesperpedersen

jesperpedersen commented Apr 20, 2025 •

Copy link
Copy Markdown
Member

We should do the standard function signature for all functions too, e.g.

int
pgmoneta_XYZ(input, output);

Since _destroy() should handle NULL it will always return 0 - well, the deque should be empty when called, if there is/are entries in it then it is a question

@jesperpedersen

Copy link
Copy Markdown
Member

To make it easier just use atomic_ all over

@dancingactor
dancingactor force-pushed the test4 branch 5 times, most recently from 55aa68e to 57ed02c Compare April 20, 2025 10:35
@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor Thanks, I'll work on it for a bit

@dancingactor

Copy link
Copy Markdown
Contributor Author

Sorry, I haven’t finished it yet. I’ll re-request the review again once I complete

@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor No worries, I'm working on your patch since we need it for 0.16

@dancingactor

Copy link
Copy Markdown
Contributor Author

@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

@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor I got it

@dancingactor

Copy link
Copy Markdown
Contributor Author

If there's anything I can help with, I am happy to do anytime!

@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor Feel free to find a task, and start on it

@dancingactor

Copy link
Copy Markdown
Contributor Author

Thanks! Now I am working on another issue #523

@jesperpedersen jesperpedersen self-assigned this Apr 20, 2025
@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor Don't spend time on this - I have completely changed the API on my branch

@dancingactor

Copy link
Copy Markdown
Contributor Author

Oh okay, thanks for the heads-up!

@jesperpedersen

Copy link
Copy Markdown
Member

@dancingactor We are going with mostly your patch for now.

We can make improvements later

@jesperpedersen

Copy link
Copy Markdown
Member

Merged.

Thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants