Skip to content

Commit

Permalink
config: logDir add dyn support
Browse files Browse the repository at this point in the history
This will add the dyn logDir support based the dyn-config without
restarting the gluster-blockd service.

Fixes: gluster#199
Signed-off-by: Xiubo Li <[email protected]>
  • Loading branch information
lxbsz committed Apr 24, 2019
1 parent b512de2 commit 5328d58
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 30 deletions.
6 changes: 1 addition & 5 deletions daemon/gluster-blockd.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,13 @@ main (int argc, char **argv)
int wstatus;


if (pthread_mutex_init(&gbConf.lock, NULL) < 0) {
exit(EXIT_FAILURE);
}

if(initLogging()) {
exit(EXIT_FAILURE);
}

fetchGlfsVolServerFromEnv();

gbCfg = glusterBlockSetupConfig(NULL);
gbCfg = glusterBlockSetupConfig();
if (!gbCfg) {
LOG("mgmt", GB_LOG_ERROR, "glusterBlockSetupConfig() failed");
exit(EXIT_FAILURE);
Expand Down
5 changes: 4 additions & 1 deletion systemd/gluster-blockd.sysconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# least recently used object.
#GB_GLFS_LRU_COUNT=5


# Supported loglevels [ NONE, CRIT, ERROR, WARNING, INFO, DEBUG, TRACE ]
# And the default logging level is INFO, if you want to change the
# default level, uncomment it and set your level:
Expand All @@ -25,3 +24,7 @@

# Expert use only, just incase if we have any extra args to pass for daemon
#GB_EXTRA_ARGS=""

# Supported setting the log directory path from the sysconfig
# default path is /var/log/gluster-blockd, uncomment it and set your path:
#GB_LOG_DIR="/var/log/gluster-block"
2 changes: 1 addition & 1 deletion utils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ libgb_la_CFLAGS = $(GFAPI_CFLAGS) $(TIRPC_CFLAGS) \
-DCONFDIR=\"$(GLUSTER_BLOCKD_WORKDIR)\" \
-I$(top_builddir)/ -I$(top_builddir)/rpc/rpcl

libgb_la_LIBADD = $(GFAPI_LIBS) $(TIRPC_LIBS)
libgb_la_LIBADD = $(GFAPI_LIBS) $(TIRPC_LIBS) $(PTHREAD)

libgb_la_LDFLAGS = -lpthread

Expand Down
70 changes: 59 additions & 11 deletions utils/dyn-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,20 @@ glusterBlockGetOption(const char *key)
} while (0);

static void
glusterBlockConfSetOptions(gbConfig *cfg, bool reloading)
glusterBlockConfSetOptions(gbConfig *cfg, bool getLogDir)
{
unsigned int logLevel;


/* set logdir option */
GB_PARSE_CFG_STR(cfg, GB_LOG_DIR, GB_LOGDIR_DEF);
if (getLogDir) {
return;
}
if (cfg->GB_LOG_DIR) {
glusterBlockSetLogDir(cfg->GB_LOG_DIR);
}

/* set logLevel option */
GB_PARSE_CFG_STR(cfg, GB_LOG_LEVEL, "INFO");
if (cfg->GB_LOG_LEVEL) {
Expand Down Expand Up @@ -207,6 +216,7 @@ glusterBlockConfFreeStrKeys(gbConfig *cfg)
* For example:
* GB_FREE_CFG_STR_KEY(cfg, 'STR KEY');
*/
GB_FREE_CFG_STR_KEY(cfg, GB_LOG_DIR);
GB_FREE_CFG_STR_KEY(cfg, GB_LOG_LEVEL);
}

Expand Down Expand Up @@ -437,7 +447,7 @@ glusterBlockParseOption(char **cur, const char *end)
}

static void
glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool reloading)
glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool getLogDir)
{
char *cur = buf, *end = buf + len;

Expand All @@ -448,11 +458,11 @@ glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool reloading)
}

/* parse the options from gb_options[] to struct gbConfig */
glusterBlockConfSetOptions(cfg, reloading);
glusterBlockConfSetOptions(cfg, getLogDir);
}

int
glusterBlockLoadConfig(gbConfig *cfg, bool reloading)
glusterBlockLoadConfig(gbConfig *cfg, bool getLogDir)
{
ssize_t len = 0;
char *buf;
Expand All @@ -465,12 +475,53 @@ glusterBlockLoadConfig(gbConfig *cfg, bool reloading)
return -1;
}

glusterBlockParseOptions(cfg, buf, len, reloading);
glusterBlockParseOptions(cfg, buf, len, getLogDir);

GB_FREE(buf);
return 0;
}


char *
glusterBlockDynConfigGetLogDir(void)
{
gbConfig *cfg = NULL;
char *logDir = NULL;
char *configPath = GB_DEF_CONFIGPATH;
int ret;


if (GB_ALLOC(cfg) < 0) {
MSG(stderr, "Alloc GB config failed for configPath: %s!\n", configPath);
return NULL;
}

if (GB_STRDUP(cfg->configPath, configPath) < 0) {
MSG(stderr, "failed to copy configPath: %s\n", configPath);
goto freeConfig;
}

if (glusterBlockLoadConfig(cfg, true)) {
MSG(stderr, "Loading GB config failed for configPath: %s!\n", configPath);
goto freeConfigPath;
}

if (cfg->GB_LOG_DIR) {
if (GB_STRDUP(logDir, cfg->GB_LOG_DIR) < 0) {
MSG(stderr, "failed to copy logDir: %s\n", cfg->GB_LOG_DIR);
logDir = NULL;
}
}

freeConfigPath:
GB_FREE(cfg->configPath);
freeConfig:
GB_FREE(cfg);

return logDir;
}


static void *
glusterBlockDynConfigStart(void *arg)
{
Expand Down Expand Up @@ -538,7 +589,7 @@ glusterBlockDynConfigStart(void *arg)

/* Try to reload the config file */
if (event->mask & IN_MODIFY) {
glusterBlockLoadConfig(cfg, true);
glusterBlockLoadConfig(cfg, false);
}
}
}
Expand All @@ -548,16 +599,13 @@ glusterBlockDynConfigStart(void *arg)
}

gbConfig *
glusterBlockSetupConfig(const char *configPath)
glusterBlockSetupConfig(void)
{
gbConfig *cfg = NULL;
char *configPath = GB_DEF_CONFIGPATH;
int ret;


if (!configPath) {
configPath = GB_DEF_CONFIGPATH;
}

if (GB_ALLOC(cfg) < 0) {
LOG("mgmt", GB_LOG_ERROR, "Alloc GB config failed for configPath: %s!", configPath);
return NULL;
Expand Down
36 changes: 35 additions & 1 deletion utils/lru.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
cases as published by the Free Software Foundation.
*/

# include <pthread.h>
# include "lru.h"
# include "utils.h"


static struct list_head Cache;
static LIST_HEAD(Cache);
static int lruCount;
static pthread_mutex_t lru_lock = PTHREAD_MUTEX_INITIALIZER;

typedef struct Entry {
char volume[255];
Expand All @@ -22,6 +24,31 @@ typedef struct Entry {
struct list_head list;
} Entry;


void
glusterBlockUpdateLruLogdir(const char *logPath)
{
struct list_head *pos, *q;
Entry *tmp;


LOCK(lru_lock);
if (!logPath || list_empty(&Cache)) {
UNLOCK(lru_lock);
return;
}

list_for_each_safe(pos, q, &Cache){
tmp = list_entry(pos, Entry, list);
if (glfs_set_logging(tmp->glfs, logPath, GFAPI_LOG_LEVEL)) {
LOG("mgmt", GB_LOG_WARNING, "glfs_set_logging(%s, %d) on %s failed[%s]",
logPath, GFAPI_LOG_LEVEL, tmp->volume, strerror(errno));
}
}
UNLOCK(lru_lock);
}


int
glusterBlockSetLruCount(const size_t lruCount)
{
Expand Down Expand Up @@ -52,6 +79,7 @@ releaseColdEntry(void)
struct list_head *pos, *q = &Cache;


LOCK(lru_lock);
list_for_each_prev(pos, q) {
tmp = list_entry(pos, Entry, list);
list_del(pos);
Expand All @@ -62,6 +90,7 @@ releaseColdEntry(void)

break;
}
UNLOCK(lru_lock);
}


Expand All @@ -83,7 +112,9 @@ appendNewEntry(const char *volname, glfs_t *fs)
GB_STRCPYSTATIC(tmp->volume, volname);
tmp->glfs = fs;

LOCK(lru_lock);
list_add(&(tmp->list), &Cache);
UNLOCK(lru_lock);

lruCount++;
UNLOCK(gbConf.lock);
Expand Down Expand Up @@ -117,13 +148,16 @@ queryCache(const char *volname)
struct list_head *pos, *q, *r = &Cache;


LOCK(lru_lock);
list_for_each_safe(pos, q, r){
tmp = list_entry(pos, Entry, list);
if (!strcmp(tmp->volume, volname)) {
boostEntryWarmness(volname);
UNLOCK(lru_lock);
return tmp->glfs;
}
}
UNLOCK(lru_lock);

return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions utils/lru.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ appendNewEntry(const char *volname, glfs_t *glfs);
int
glusterBlockSetLruCount(const size_t lruCount);

void
glusterBlockUpdateLruLogdir(const char *logDir);

# endif /* _LRU_H */
60 changes: 51 additions & 9 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
# include <stdio.h>
# include <dirent.h>
# include <sys/stat.h>
# include <pthread.h>

# include "utils.h"
# include "lru.h"
# include "config.h"

struct gbConf gbConf = {
.lock = PTHREAD_MUTEX_INITIALIZER,
.glfsLruCount = LRU_COUNT_DEF,
.logLevel = GB_LOG_INFO,
.logDir = GB_LOGDIR,
.logDir = GB_LOGDIR_DEF,
.cliTimeout = CLI_TIMEOUT_DEF
};

Expand Down Expand Up @@ -278,24 +280,43 @@ void fetchGlfsVolServerFromEnv()
LOG("mgmt", GB_LOG_INFO, "Block Hosting Volfile Server Set to: %s", gbConf.volServer);
}


int
initLogging(void)
static int
initLogDirAndFiles(char *newLogDir)
{
char *logDir = NULL;
char *tmpLogDir = NULL;
int ret = 0;


/*
* The priority of the logdir setting is:
* 1, /etc/sysconfig/gluster-blockd config file
* 2, "GB_LOGDIR" from the ENV setting
* 3, default as GB_LOGDIR_DEF
*/
if (newLogDir) {
logDir = newLogDir;
} else {
logDir = getenv("GB_LOGDIR");

tmpLogDir = glusterBlockDynConfigGetLogDir();
if (tmpLogDir) {
logDir = tmpLogDir;
}

logDir = getenv("GB_LOGDIR");
if (!logDir) {
logDir = GB_LOGDIR;
if (!logDir) {
logDir = GB_LOGDIR_DEF;
}
}

if (strlen(logDir) > PATH_MAX - GB_MAX_LOGFILENAME) {
fprintf(stderr, "strlen of logDir Path > PATH_MAX: %s", logDir);
return EXIT_FAILURE;
goto out;
}

/* set logfile paths */
LOCK(gbConf.lock);
snprintf(gbConf.logDir, PATH_MAX,
"%s", logDir);
snprintf(gbConf.daemonLogFile, PATH_MAX,
Expand All @@ -310,10 +331,31 @@ initLogging(void)
"%s/cmd_history.log", logDir);

if(!glusterBlockLogdirCreate()) {
return EXIT_FAILURE;
ret = EXIT_FAILURE;
goto unlock;
}

return 0;
glusterBlockUpdateLruLogdir(gbConf.gfapiLogFile);

unlock:
UNLOCK(gbConf.lock);
out:
GB_FREE(tmpLogDir);
return ret;
}


int
initLogging(void)
{
return initLogDirAndFiles(NULL);
}


bool
glusterBlockSetLogDir(char *logDir)
{
return initLogDirAndFiles(logDir);
}


Expand Down
Loading

0 comments on commit 5328d58

Please sign in to comment.