Skip to content

Commit

Permalink
config: dynamic res_head for dir works
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorz authored and alaaeddineelamri committed Aug 4, 2022
1 parent 7a2ce28 commit 3f71bfa
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 55 deletions.
22 changes: 9 additions & 13 deletions core/src/dird/dird.cc
Expand Up @@ -508,10 +508,11 @@ bool DoReloadConfig()

DbSqlPoolFlush();

BareosResource** saved_res_table = my_config->CopyResourceTable();

my_config->BackupResourceTable();
my_config->ClearResourceTables();
Dmsg0(100, "Reloading config file\n");


my_config->err_type_ = M_ERROR;
my_config->ClearWarnings();
bool ok = my_config->ParseConfig();
Expand All @@ -528,27 +529,22 @@ bool DoReloadConfig()

// remove our reference to current config so it will be freed when last job
// owning it finishes
my_config->ResetResHeadContainer();
free(saved_res_table);
StartStatisticsThread();
my_config->ResetResHeadContainerPrevious();

StartStatisticsThread();

} else { // parse config failed
Jmsg(nullptr, M_ERROR, 0, _("Please correct the configuration in %s\n"),
my_config->get_base_config_path().c_str());
Jmsg(nullptr, M_ERROR, 0, _("Resetting to previous configuration.\n"));

int num_rcodes = my_config->r_num_;
for (int i = 0; i < num_rcodes; i++) {
// restore original config
my_config->res_head_container_->res_head_[i] = saved_res_table[i];
// my_config->res_head_[i] = res_table[i];
}
free(saved_res_table);

my_config->RestoreResourceTable();
// me is changed above by CheckResources()
me = (DirectorResource*)my_config->GetNextRes(R_DIRECTOR, nullptr);
assert(me);
my_config->own_resource_ = me;

StartStatisticsThread();
}

UnlockRes(my_config);
Expand Down
28 changes: 20 additions & 8 deletions core/src/dird/dird_conf.cc
Expand Up @@ -77,8 +77,8 @@ extern struct s_kw RunFields[];
* types. Note, these should be unique for each
* daemon though not a requirement.
*/
static BareosResource* sres_head[R_NUM];
static BareosResource** res_head = sres_head;
/* static BareosResource* sres_head[R_NUM]; */
/* static BareosResource** res_head = sres_head; */
static PoolMem* configure_usage_string = NULL;

extern void StoreInc(LEX* lc, ResourceItem* item, int index, int pass);
Expand Down Expand Up @@ -2529,7 +2529,11 @@ static void StoreActiononpurge(LEX* lc, ResourceItem* item, int index, int pass)
* first reference. The details of the resource are obtained
* later from the SD.
*/
static void StoreDevice(LEX* lc, ResourceItem* item, int index, int pass)
static void StoreDevice(LEX* lc,
ResourceItem* item,
int index,
int pass,
BareosResource** res_head)
{
int rindex = R_DEVICE;

Expand Down Expand Up @@ -2572,7 +2576,11 @@ static void StoreDevice(LEX* lc, ResourceItem* item, int index, int pass)
}

// Store Migration/Copy type
static void StoreMigtype(LEX* lc, ResourceItem* item, int index, int pass)
static void StoreMigtype(LEX* lc,
ResourceItem* item,
int index,
int pass,
BareosResource** res_head)
{
LexGetToken(lc, BCT_NAME);
// Store the type both in pass 1 and pass 2
Expand Down Expand Up @@ -3204,7 +3212,11 @@ static void InitResourceCb(ResourceItem* item, int pass)
* callback function for parse_config
* See ../lib/parse_conf.c, function ParseConfig, for more generic handling.
*/
static void ParseConfigCb(LEX* lc, ResourceItem* item, int index, int pass)
static void ParseConfigCb(LEX* lc,
ResourceItem* item,
int index,
int pass,
BareosResource** res_head)
{
switch (item->type) {
case CFG_TYPE_AUTOPASSWORD:
Expand All @@ -3223,7 +3235,7 @@ static void ParseConfigCb(LEX* lc, ResourceItem* item, int index, int pass)
StoreAuthtype(lc, item, index, pass);
break;
case CFG_TYPE_DEVICE:
StoreDevice(lc, item, index, pass);
StoreDevice(lc, item, index, pass, res_head);
break;
case CFG_TYPE_JOBTYPE:
StoreJobtype(lc, item, index, pass);
Expand All @@ -3244,7 +3256,7 @@ static void ParseConfigCb(LEX* lc, ResourceItem* item, int index, int pass)
StoreRunscript(lc, item, index, pass);
break;
case CFG_TYPE_MIGTYPE:
StoreMigtype(lc, item, index, pass);
StoreMigtype(lc, item, index, pass, res_head);
break;
case CFG_TYPE_INCEXC:
StoreInc(lc, item, index, pass);
Expand Down Expand Up @@ -3681,7 +3693,7 @@ ConfigurationParser* InitDirConfig(const char* configfile, int exit_code)
{
ConfigurationParser* config = new ConfigurationParser(
configfile, nullptr, nullptr, InitResourceCb, ParseConfigCb,
PrintConfigCb, exit_code, R_NUM, resources, res_head,
PrintConfigCb, exit_code, R_NUM, resources,
default_config_filename.c_str(), "bareos-dir.d", ConfigBeforeCallback,
ConfigReadyCallback, SaveResource, DumpResource, FreeResource);
if (config) { config->r_own_ = R_DIRECTOR; }
Expand Down
49 changes: 32 additions & 17 deletions core/src/lib/parse_conf.cc
Expand Up @@ -96,7 +96,8 @@ ConfigurationParser::ConfigurationParser()
, r_own_(0)
, own_resource_(nullptr)
, resource_definitions_(0)
, res_head_(nullptr)
/* , res_head_(nullptr) */
/* , res_head_backup_(nullptr) */
, SaveResourceCb_(nullptr)
, DumpResourceCb_(nullptr)
, FreeResourceCb_(nullptr)
Expand All @@ -117,7 +118,6 @@ ConfigurationParser::ConfigurationParser(
int32_t err_type,
int32_t r_num,
ResourceTable* resource_definitions,
BareosResource** res_head,
const char* config_default_filename,
const char* config_include_dir,
void (*ParseConfigBeforeCb)(ConfigurationParser&),
Expand All @@ -138,7 +138,7 @@ ConfigurationParser::ConfigurationParser(
err_type_ = err_type;
r_num_ = r_num;
resource_definitions_ = resource_definitions;
res_head_ = res_head;
res_head_container_.reset(new ResHeadContainer(this));
config_default_filename_
= config_default_filename == nullptr ? "" : config_default_filename;
config_include_dir_ = config_include_dir == nullptr ? "" : config_include_dir;
Expand All @@ -150,14 +150,21 @@ ConfigurationParser::ConfigurationParser(
SaveResourceCb_ = SaveResourceCb;
DumpResourceCb_ = DumpResourceCb;
FreeResourceCb_ = FreeResourceCb;
ResetResHeadContainer();
}

void ConfigurationParser::ResetResHeadContainer()
// remove our shared pointer to previous configuration so that it will be freed
// when last job owning is done
void ConfigurationParser::ResetResHeadContainerPrevious()
{
res_head_container_.reset(new ResHeadContainer(this, res_head_));
// res_head_container_previous_ = nullptr;
}

void ConfigurationParser::RestorePreviousConfig()
{
// res_head_container_ = std::move(res_head_container_previous_);
}


ConfigurationParser::~ConfigurationParser()
{
#if 0
Expand Down Expand Up @@ -214,6 +221,10 @@ bool ConfigurationParser::ParseConfig()
bool success = ParseConfigFile(config_path.c_str(), nullptr, scan_error_,
scan_warning_);
if (success && ParseConfigReadyCb_) { ParseConfigReadyCb_(*this); }
/* if (success) { */
/* res_head_container_previous_ = std::move(res_head_container_); */
/* res_head_container_.reset(new ResHeadContainer(this)); */
/* } */
return success;
}

Expand Down Expand Up @@ -521,19 +532,23 @@ bool ConfigurationParser::FindConfigPath(PoolMem& full_path)
return found;
}

BareosResource** ConfigurationParser::CopyResourceTable()

void ConfigurationParser::ClearResourceTables()
{
int num = r_num_;
BareosResource** res
= (BareosResource**)malloc(num * sizeof(BareosResource*));

for (int i = 0; i < num; i++) {
res[i] = res_head_container_->res_head_[i];
res_head_container_->res_head_[i] = nullptr;
res_head_[i] = nullptr;
}
/* int num = r_num_; */
/* for (int i = 0; i < num; i++) { */
/* res_head_container_->res_head_[i] = nullptr; */
/* res_head_[i] = nullptr; */
/* } */
}

return res;
// restore the previously saved res_head_backup_ to res_head_
bool ConfigurationParser::RestoreResourceTable() { return true; }
// copy the current resource table to res_head_backup_
bool ConfigurationParser::BackupResourceTable()
{
// res_head_container_previous_.reset(res_head_container_.get());
return true;
}

bool ConfigurationParser::RemoveResource(int rcode, const char* name)
Expand Down
24 changes: 15 additions & 9 deletions core/src/lib/parse_conf.h
Expand Up @@ -184,7 +184,8 @@ typedef void(INIT_RES_HANDLER)(ResourceItem* item, int pass);
typedef void(STORE_RES_HANDLER)(LEX* lc,
ResourceItem* item,
int index,
int pass);
int pass,
BareosResource** res_head);
typedef void(PRINT_RES_HANDLER)(ResourceItem& item,
OutputFormatterResource& send,
bool hide_sensitive_data,
Expand Down Expand Up @@ -218,10 +219,8 @@ class ConfigurationParser {
BareosResource* own_resource_; /* Pointer to own resource */
ResourceTable*
resource_definitions_; /* Pointer to table of permitted resources */
private:
BareosResource** res_head_; /* Pointer to defined resources */
public:
std::shared_ptr<ResHeadContainer> res_head_container_;
// std::shared_ptr<ResHeadContainer> res_head_container_previous_;
mutable brwlock_t res_lock_; /* Resource lock */

SaveResourceCb_t SaveResourceCb_;
Expand All @@ -238,7 +237,6 @@ class ConfigurationParser {
int32_t err_type,
int32_t r_num,
ResourceTable* resources,
BareosResource** res_head,
const char* config_default_filename,
const char* config_include_dir,
void (*ParseConfigBeforeCb)(ConfigurationParser&),
Expand All @@ -248,7 +246,9 @@ class ConfigurationParser {
FreeResourceCb_t FreeResourceCb);

~ConfigurationParser();
void ResetResHeadContainer();
void ResetResHeadContainerPrevious();
void RestorePreviousConfig();
void ClearResourceTables();

bool IsUsingConfigIncludeDir() const { return use_config_include_dir_; }
bool ParseConfig();
Expand All @@ -258,7 +258,8 @@ class ConfigurationParser {
LEX_WARNING_HANDLER* scan_warning = nullptr);
const std::string& get_base_config_path() const { return used_config_path_; }
void FreeResources();
BareosResource** CopyResourceTable();
bool BackupResourceTable();
bool RestoreResourceTable();
void InitResource(int rcode,
ResourceItem items[],
int pass,
Expand Down Expand Up @@ -420,10 +421,14 @@ class ConfigurationParser {
struct ResHeadContainer {
BareosResource** res_head_;
ConfigurationParser* config_;
ResHeadContainer(ConfigurationParser* config, BareosResource** res_head)

ResHeadContainer(ConfigurationParser* config)
{
res_head_ = res_head;
config_ = config;
int num = config_->r_num_;
res_head_ = (BareosResource**)malloc(num * sizeof(BareosResource*));

for (int i = 0; i < num; i++) { res_head_[i] = nullptr; }
Dmsg1(100, "ResHeadContainer::ResHeadContainer : res_head_ is at %p\n",
res_head_);
}
Expand All @@ -437,6 +442,7 @@ struct ResHeadContainer {
}
Dmsg1(100, "ResHeadContainer::~ResHeadContainer : freed restable at %p\n",
res_head_);
free(res_head_);
}
};

Expand Down
8 changes: 5 additions & 3 deletions core/src/lib/parse_conf_state_machine.cc
Expand Up @@ -144,7 +144,8 @@ ConfigParserStateMachine::ScanResource(int token)
parser_pass_number_)) {
if (my_config_.store_res_) {
my_config_.store_res_(lexical_parser_, item, resource_item_index,
parser_pass_number_);
parser_pass_number_,
my_config_.res_head_container_->res_head_);
}
}
} else {
Expand Down Expand Up @@ -280,8 +281,9 @@ void ConfigParserStateMachine::DumpResourcesAfterSecondPass()
{
if (debug_level >= 900 && parser_pass_number_ == 2) {
for (int i = 0; i <= my_config_.r_num_ - 1; i++) {
my_config_.DumpResourceCb_(i, my_config_.res_head_[i], PrintMessage,
nullptr, false, false);
my_config_.DumpResourceCb_(i,
my_config_.res_head_container_->res_head_[i],
PrintMessage, nullptr, false, false);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/res.cc
Expand Up @@ -96,7 +96,7 @@ BareosResource* ConfigurationParser::GetResWithName(int rcode,

if (lock) { LockRes(this); }

res = res_head_[rindex];
res = res_head_container_->res_head_[rindex];
while (res) {
if (bstrcmp(res->resource_name_, name)) { break; }
res = res->next_;
Expand All @@ -119,7 +119,7 @@ BareosResource* ConfigurationParser::GetNextRes(int rcode,
int rindex = rcode;

if (res == NULL) {
nres = res_head_[rindex];
nres = res_head_container_->res_head_[rindex];
} else {
nres = res->next_;
}
Expand Down
27 changes: 24 additions & 3 deletions core/src/tests/test_config_parser_dir.cc
Expand Up @@ -40,11 +40,32 @@ TEST(ConfigParser_Dir, bareos_configparser_tests)
RELATIVE_PROJECT_SOURCE_DIR "/configs/bareos-configparser-tests");
my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM);
my_config->ParseConfig();
my_config->DumpResources(PrintMessage, NULL);

ASSERT_EQ(true, my_config->BackupResourceTable());
my_config->ParseConfig();
me = (DirectorResource*)my_config->GetNextRes(R_DIRECTOR, nullptr);
my_config->own_resource_ = me;

ASSERT_NE(nullptr, me);
my_config->DumpResources(PrintMessage, NULL);
ASSERT_NE(nullptr, me);

ASSERT_EQ(true, my_config->RestoreResourceTable());
ASSERT_NE(nullptr, me);
my_config->ParseConfig();
me = (DirectorResource*)my_config->GetNextRes(R_DIRECTOR, nullptr);
my_config->own_resource_ = me;
assert(me);

ASSERT_NE(nullptr, me);
my_config->DumpResources(PrintMessage, NULL);
ASSERT_NE(nullptr, me);

delete my_config;
}

#if 0
TEST(ConfigParser_Dir, runscript_test)
{
OSDependentInit();
Expand Down Expand Up @@ -138,9 +159,9 @@ void test_CFG_TYPE_STR_VECTOR_OF_DIRS(DirectorResource* me)
* but this is later overwritten in the Director Daemon with ".".
* Therefore we skip this test.
*/
#if !defined(HAVE_WIN32)
# if !defined(HAVE_WIN32)
EXPECT_EQ(me->backend_directories.at(0), PATH_BAREOS_BACKENDDIR);
#endif
# endif
}

TEST(ConfigParser_Dir, CFG_TYPE_STR_VECTOR_OF_DIRS)
Expand Down Expand Up @@ -232,5 +253,5 @@ TEST(ConfigParser_Dir, CFG_TYPE_TIME)
{
test_config_directive_type(test_CFG_TYPE_TIME);
}

#endif
} // namespace directordaemon

0 comments on commit 3f71bfa

Please sign in to comment.