Skip to content

dird: add prev and new jobid variables#1499

Merged
BareosBot merged 12 commits intobareos:masterfrom
SamuelBoerlin:migration-prev-new-jobid-vars
Aug 5, 2023
Merged

dird: add prev and new jobid variables#1499
BareosBot merged 12 commits intobareos:masterfrom
SamuelBoerlin:migration-prev-new-jobid-vars

Conversation

@SamuelBoerlin
Copy link
Contributor

@SamuelBoerlin SamuelBoerlin commented Jun 30, 2023

Thank you for contributing to the Bareos Project!

This PR adds two new RunScript variables %O and %N which contain the previous and new jobid of a migration/copy job.
Currently there is no convenient way to retrieve this information in RunScripts. This would be useful to e.g. easily archive copied or migrated jobs (especially with always incremental and in combination with #1372):

  Run Script {
        console = "update jobid=%N jobtype=A"
        Runs When = After
        Runs On Client = No
        Runs On Failure = No
  }

Please check

  • Short description and the purpose of this PR is present above this paragraph
  • Your name is present in the AUTHORS file (optional)

If you have any questions or problems, please give a comment in the PR.

Helpful documentation and best practices

Checklist for the reviewer of the PR (will be processed by the Bareos team)

Make sure you check/merge the PR using devtools/pr-tool to have some simple automated checks run and a proper changelog record added.

General
  • Is the PR title usable as CHANGELOG entry?
  • Purpose of the PR is understood
  • Commit descriptions are understandable and well formatted
  • Check backport line
  • Required backport PRs have been created
Source code quality
  • Source code changes are understandable
  • Variable and function names are meaningful
  • Code comments are correct (logically and spelling)
  • Required documentation changes are present and part of the PR
Tests
  • Decision taken that a test is required (if not, then remove this paragraph)
  • The choice of the type of test (unit test or systemtest) is reasonable
  • Testname matches exactly what is being tested
  • On a fail, output of the test leads quickly to the origin of the fault

static char yes[] = "yes";
static char no[] = "no";

static char str[50];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is using a static array to store and return the result okay? I copied that from another similar callback, so I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

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

I don't really like it, because it won't be thread-safe. i.e. if the threads for multiple jobs enter job_code_callback() around the same time, there's a possible data-race on str which may lead to the wrong value being returned.

However, there's no great way to work around this inside the function right now. I guess the best way to proceed would be to remove the requirement for extern "C" and return a std::string instead of a char*.
I'll see if I can make that change easily.

Copy link
Contributor Author

@SamuelBoerlin SamuelBoerlin Jul 3, 2023

Choose a reason for hiding this comment

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

Alright, thanks for your feedback. I already have a branch where I've refactored this callback to return an std::optional<std::string>, should I merge that into this PR or do you want that somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've only refactored it up to util.cc#edit_job_codes though (SamuelBoerlin@0b7498a#diff-edeceb51c3de23d07b2d5d9e679d111e1238b74cc870a492143e5283e4386ee9R937-R943), after which point it gets more complicated.

Copy link
Member

Choose a reason for hiding this comment

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

That looks promising! You can just add it to this PR as it is the refactoring required to implement the feature you're working on.

You could

  • drop the extern "C" everywhere, having C-linkage if you need a std::string probably won't help much
  • return the plain char*/const char*, compiler will take care of conversion on return

Your solution for edit_job_codes() is probably as good as it gets without refactoring the whole thing. However, for a plain char* snprintf() is better suited than Bsnprintf(), as the latter expects a resizable POOLMEM*.

@SamuelBoerlin
Copy link
Contributor Author

SamuelBoerlin commented Jul 3, 2023

Just realized there seems to already exist a %m variable here: https://github.com/bareos/bareos/blob/master/core/src/filed/fileset.cc#L68-L69
but I couldn't find anything about this in the documentation. Technically this shouldn't be a problem because that %m and my proposed %m should never conflict, but it might be confusing from a configuration standpoint.

Copy link
Member

@arogge arogge left a comment

Choose a reason for hiding this comment

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

btw. I cannot thank you enough for adding the test for your new feature!

static char yes[] = "yes";
static char no[] = "no";

static char str[50];
Copy link
Member

Choose a reason for hiding this comment

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

That looks promising! You can just add it to this PR as it is the refactoring required to implement the feature you're working on.

You could

  • drop the extern "C" everywhere, having C-linkage if you need a std::string probably won't help much
  • return the plain char*/const char*, compiler will take care of conversion on return

Your solution for edit_job_codes() is probably as good as it gets without refactoring the whole thing. However, for a plain char* snprintf() is better suited than Bsnprintf(), as the latter expects a resizable POOLMEM*.

@SamuelBoerlin SamuelBoerlin marked this pull request as ready for review July 4, 2023 10:00
@SamuelBoerlin SamuelBoerlin requested a review from arogge July 4, 2023 10:00
@arogge
Copy link
Member

arogge commented Jul 4, 2023

That looks really really good. The only issue I see is the letters you chose.
We already have %m on the FD and it is hard enough to understand without duplicate letters.
As the set of letters to select from is agkoquyzAEGHIJKLMNOQRSTUWXYZ my proposal is to go with %O for the original jobid and %N for new jobid.
But I'm happy with whatever other non-duplicate letters you come up with.

@SamuelBoerlin
Copy link
Contributor Author

Thanks! Okay, makes sense. I'll go with %O and %N then.

@SamuelBoerlin
Copy link
Contributor Author

I've also added %m to the docs because that one was undocumented as far as I can tell.

@arogge arogge changed the title dird: add prev and new jobid variables %m %M dird: add prev and new jobid variables Jul 4, 2023
@SamuelBoerlin SamuelBoerlin force-pushed the migration-prev-new-jobid-vars branch from b7a715f to 5883b45 Compare July 6, 2023 19:06
@arogge
Copy link
Member

arogge commented Jul 13, 2023

Sorry for keeping you waiting so long.
That changes look good (I'm going to finish the review today). You can (and probably should) drop the CHANGELOG commit. We have a Bot that adds a ChangeLog record based on the PR title automatically.

Copy link
Member

@arogge arogge left a comment

Choose a reason for hiding this comment

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

These changes look great. Thank you for the effort you put into this!
You can fix the CHANGELOG.md conflict by just removing your change in that file, the Bot will take care of that.
Do you have a need to see this in Bareos 22? I think we could backport these changes if you have a need for them to become available in the next Bareos 22 release...

@SamuelBoerlin SamuelBoerlin force-pushed the migration-prev-new-jobid-vars branch from 5883b45 to 3d0ada2 Compare July 13, 2023 09:38
@SamuelBoerlin
Copy link
Contributor Author

SamuelBoerlin commented Jul 13, 2023

Great! I've removed the changelog changes again.
Yes, it would be nice to have this in the next Bareos 22 release. Currently I get the new jobid by just adding +1 to the copy/migrate control jobid, but that seems unsafe and prone to breaking, so it'd be nice to have this new variable instead.

@arogge arogge force-pushed the migration-prev-new-jobid-vars branch from 3d0ada2 to f5a9472 Compare August 5, 2023 11:06
@BareosBot BareosBot merged commit 98dfabd into bareos:master Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants