"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/xlators/mgmt/glusterd/src/glusterd-gfproxyd-svc.c" (16 Sep 2020, 14083 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "glusterd-gfproxyd-svc.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2    Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com>
    3    This file is part of GlusterFS.
    4 
    5    This file is licensed to you under your choice of the GNU Lesser
    6    General Public License, version 3 or any later version (LGPLv3 or
    7    later), or the GNU General Public License, version 2 (GPLv2), in all
    8    cases as published by the Free Software Foundation.
    9 */
   10 
   11 #include <glusterfs/globals.h>
   12 #include <glusterfs/run.h>
   13 #include "glusterd.h"
   14 #include "glusterd-utils.h"
   15 #include "glusterd-volgen.h"
   16 #include "glusterd-gfproxyd-svc.h"
   17 #include "glusterd-messages.h"
   18 #include "glusterd-svc-helper.h"
   19 #include "glusterd-svc-mgmt.h"
   20 #include "glusterd-gfproxyd-svc-helper.h"
   21 #include <glusterfs/syscall.h>
   22 
   23 void
   24 glusterd_gfproxydsvc_build(glusterd_svc_t *svc)
   25 {
   26     svc->manager = glusterd_gfproxydsvc_manager;
   27     svc->start = glusterd_gfproxydsvc_start;
   28     svc->stop = glusterd_gfproxydsvc_stop;
   29     svc->reconfigure = glusterd_gfproxydsvc_reconfigure;
   30 }
   31 
   32 int
   33 glusterd_gfproxydsvc_stop(glusterd_svc_t *svc, int sig)
   34 {
   35     glusterd_volinfo_t *volinfo = NULL;
   36     int ret = 0;
   37 
   38     ret = glusterd_svc_stop(svc, sig);
   39     if (ret)
   40         goto out;
   41 
   42     volinfo = glusterd_gfproxyd_volinfo_from_svc(svc);
   43     volinfo->gfproxyd.port = 0;
   44 
   45 out:
   46     return ret;
   47 }
   48 
   49 int
   50 glusterd_gfproxydsvc_init(glusterd_volinfo_t *volinfo)
   51 {
   52     int ret = -1;
   53     char rundir[PATH_MAX] = {
   54         0,
   55     };
   56     char sockpath[PATH_MAX] = {
   57         0,
   58     };
   59     char pidfile[PATH_MAX] = {
   60         0,
   61     };
   62     char volfile[PATH_MAX] = {
   63         0,
   64     };
   65     char logdir[PATH_MAX] = {
   66         0,
   67     };
   68     char logfile[PATH_MAX] = {
   69         0,
   70     };
   71     char volfileid[256] = {0};
   72     glusterd_svc_t *svc = NULL;
   73     glusterd_conf_t *priv = NULL;
   74     glusterd_conn_notify_t notify = NULL;
   75     xlator_t *this = NULL;
   76     char *volfileserver = NULL;
   77     int32_t len = 0;
   78 
   79     this = THIS;
   80     GF_VALIDATE_OR_GOTO("glusterd", this, out);
   81 
   82     priv = this->private;
   83     GF_VALIDATE_OR_GOTO(this->name, priv, out);
   84 
   85     svc = &(volinfo->gfproxyd.svc);
   86 
   87     ret = snprintf(svc->name, sizeof(svc->name), "%s", gfproxyd_svc_name);
   88     if (ret < 0)
   89         goto out;
   90 
   91     notify = glusterd_svc_common_rpc_notify;
   92 
   93     glusterd_svc_build_gfproxyd_rundir(volinfo, rundir, sizeof(rundir));
   94     glusterd_svc_create_rundir(rundir);
   95 
   96     /* Initialize the connection mgmt */
   97     glusterd_svc_build_gfproxyd_socket_filepath(volinfo, sockpath,
   98                                                 sizeof(sockpath));
   99     ret = glusterd_conn_init(&(svc->conn), sockpath, 600, notify);
  100     if (ret)
  101         goto out;
  102 
  103     /* Initialize the process mgmt */
  104     glusterd_svc_build_gfproxyd_pidfile(volinfo, pidfile, sizeof(pidfile));
  105     glusterd_svc_build_gfproxyd_volfile_path(volinfo, volfile, sizeof(volfile));
  106     glusterd_svc_build_gfproxyd_logdir(logdir, volinfo->volname,
  107                                        sizeof(logdir));
  108     ret = mkdir_p(logdir, 0755, _gf_true);
  109     if ((ret == -1) && (EEXIST != errno)) {
  110         gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_CREATE_DIR_FAILED,
  111                "Unable to create logdir %s", logdir);
  112         goto out;
  113     }
  114     glusterd_svc_build_gfproxyd_logfile(logfile, logdir, sizeof(logfile));
  115     len = snprintf(volfileid, sizeof(volfileid), "gfproxyd/%s",
  116                    volinfo->volname);
  117     if ((len < 0) || (len >= sizeof(volfileid))) {
  118         ret = -1;
  119         goto out;
  120     }
  121 
  122     if (dict_get_strn(this->options, "transport.socket.bind-address",
  123                       SLEN("transport.socket.bind-address"),
  124                       &volfileserver) != 0) {
  125         volfileserver = "localhost";
  126     }
  127     ret = glusterd_proc_init(&(svc->proc), gfproxyd_svc_name, pidfile, logdir,
  128                              logfile, volfile, volfileid, volfileserver);
  129     if (ret)
  130         goto out;
  131 
  132 out:
  133     gf_msg_debug(this ? this->name : "glusterd", 0, "Returning %d", ret);
  134     return ret;
  135 }
  136 
  137 static int
  138 glusterd_gfproxydsvc_create_volfile(glusterd_volinfo_t *volinfo)
  139 {
  140     int ret = -1;
  141     xlator_t *this = NULL;
  142 
  143     this = THIS;
  144     GF_VALIDATE_OR_GOTO("glusterd", this, out);
  145 
  146     ret = glusterd_generate_gfproxyd_volfile(volinfo);
  147     if (ret) {
  148         gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLFILE_CREATE_FAIL,
  149                "Failed to create volfile");
  150         goto out;
  151     }
  152 
  153 out:
  154     gf_msg_debug(this ? this->name : "glusterd", 0, "Returning %d", ret);
  155 
  156     return ret;
  157 }
  158 
  159 int
  160 glusterd_gfproxydsvc_manager(glusterd_svc_t *svc, void *data, int flags)
  161 {
  162     int ret = -1;
  163     glusterd_volinfo_t *volinfo = NULL;
  164     xlator_t *this = NULL;
  165 
  166     this = THIS;
  167     GF_VALIDATE_OR_GOTO("glusterd", this, out);
  168 
  169     volinfo = data;
  170     GF_VALIDATE_OR_GOTO(this->name, data, out);
  171 
  172     if (!svc->inited) {
  173         ret = glusterd_gfproxydsvc_init(volinfo);
  174         if (ret) {
  175             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_FAILED_INIT_QUOTASVC,
  176                    "Failed to init "
  177                    "gfproxyd service");
  178             goto out;
  179         } else {
  180             svc->inited = _gf_true;
  181             gf_msg_debug(this->name, 0,
  182                          "gfproxyd service "
  183                          "initialized");
  184         }
  185     }
  186 
  187     ret = glusterd_is_gfproxyd_enabled(volinfo);
  188     if (ret == -1) {
  189         gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
  190                "Failed to read volume "
  191                "options");
  192         goto out;
  193     }
  194 
  195     if (ret) {
  196         if (!glusterd_is_volume_started(volinfo)) {
  197             if (glusterd_proc_is_running(&svc->proc)) {
  198                 ret = svc->stop(svc, SIGTERM);
  199                 if (ret)
  200                     gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_STOP_FAIL,
  201                            "Couldn't stop gfproxyd for "
  202                            "volume: %s",
  203                            volinfo->volname);
  204             } else {
  205                 /* Since gfproxyd is not running set ret to 0 */
  206                 ret = 0;
  207             }
  208             goto out;
  209         }
  210 
  211         ret = glusterd_gfproxydsvc_create_volfile(volinfo);
  212         if (ret) {
  213             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_CREATE_FAIL,
  214                    "Couldn't create "
  215                    "gfroxyd volfile for volume: %s",
  216                    volinfo->volname);
  217             goto out;
  218         }
  219         ret = svc->stop(svc, SIGTERM);
  220         if (ret) {
  221             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_START_FAIL,
  222                    "Couldn't stop "
  223                    "gfproxyd for volume: %s",
  224                    volinfo->volname);
  225             goto out;
  226         }
  227 
  228         ret = svc->start(svc, flags);
  229         if (ret) {
  230             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_START_FAIL,
  231                    "Couldn't start "
  232                    "gfproxyd for volume: %s",
  233                    volinfo->volname);
  234             goto out;
  235         }
  236 
  237         glusterd_volinfo_ref(volinfo);
  238         ret = glusterd_conn_connect(&(svc->conn));
  239         if (ret) {
  240             glusterd_volinfo_unref(volinfo);
  241             volinfo = NULL;
  242             goto out;
  243         }
  244 
  245     } else if (glusterd_proc_is_running(&svc->proc)) {
  246         ret = svc->stop(svc, SIGTERM);
  247         if (ret) {
  248             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_STOP_FAIL,
  249                    "Couldn't stop gfproxyd for volume: %s", volinfo->volname);
  250             goto out;
  251         }
  252     }
  253 
  254 out:
  255     if (ret) {
  256         if (volinfo) {
  257             gf_event(EVENT_SVC_MANAGER_FAILED, "volume=%s;svc_name=%s",
  258                      volinfo->volname, svc->name);
  259         }
  260     }
  261 
  262     gf_msg_debug("glusterd", 0, "Returning %d", ret);
  263 
  264     return ret;
  265 }
  266 
  267 int
  268 glusterd_gfproxydsvc_start(glusterd_svc_t *svc, int flags)
  269 {
  270     int ret = -1;
  271     runner_t runner = {
  272         0,
  273     };
  274     glusterd_conf_t *priv = NULL;
  275     xlator_t *this = NULL;
  276     char valgrind_logfile[PATH_MAX] = {0};
  277     int gfproxyd_port = 0;
  278     char msg[1024] = {
  279         0,
  280     };
  281     char gfproxyd_id[PATH_MAX] = {
  282         0,
  283     };
  284     glusterd_volinfo_t *volinfo = NULL;
  285     char *localtime_logging = NULL;
  286     int32_t len = 0;
  287 
  288     this = THIS;
  289     GF_VALIDATE_OR_GOTO("glusterd", this, out);
  290 
  291     priv = this->private;
  292     GF_VALIDATE_OR_GOTO(this->name, priv, out);
  293 
  294     volinfo = glusterd_gfproxyd_volinfo_from_svc(svc);
  295     if (!volinfo)
  296         goto out;
  297 
  298     ret = sys_access(svc->proc.volfile, F_OK);
  299     if (ret) {
  300         gf_msg(this->name, GF_LOG_DEBUG, 0, GD_MSG_VOLINFO_GET_FAIL,
  301                "gfproxyd Volfile %s is not present", svc->proc.volfile);
  302         ret = glusterd_gfproxydsvc_create_volfile(volinfo);
  303         if (ret) {
  304             gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLFILE_CREATE_FAIL,
  305                    "Couldn't create "
  306                    "gfproxyd volfile for volume: %s",
  307                    volinfo->volname);
  308             goto out;
  309         }
  310     }
  311     runinit(&runner);
  312 
  313     if (this->ctx->cmd_args.valgrind) {
  314         len = snprintf(valgrind_logfile, PATH_MAX, "%s/valgrind-%s",
  315                        svc->proc.logdir, svc->proc.logfile);
  316         if ((len < 0) || (len >= PATH_MAX)) {
  317             ret = -1;
  318             goto out;
  319         }
  320 
  321         runner_add_args(&runner, "valgrind", "--leak-check=full",
  322                         "--trace-children=yes", "--track-origins=yes", NULL);
  323         runner_argprintf(&runner, "--log-file=%s", valgrind_logfile);
  324     }
  325 
  326     snprintf(gfproxyd_id, sizeof(gfproxyd_id), "gfproxyd-%s", volinfo->volname);
  327     runner_add_args(&runner, SBIN_DIR "/glusterfsd", "-s",
  328                     svc->proc.volfileserver, "--volfile-id",
  329                     svc->proc.volfileid, "-p", svc->proc.pidfile, "-l",
  330                     svc->proc.logfile, "--brick-name", gfproxyd_id, "-S",
  331                     svc->conn.sockpath, NULL);
  332 
  333     if (volinfo->memory_accounting)
  334         runner_add_arg(&runner, "--mem-accounting");
  335     if (dict_get_strn(priv->opts, GLUSTERD_LOCALTIME_LOGGING_KEY,
  336                       SLEN(GLUSTERD_LOCALTIME_LOGGING_KEY),
  337                       &localtime_logging) == 0) {
  338         if (strcmp(localtime_logging, "enable") == 0)
  339             runner_add_arg(&runner, "--localtime-logging");
  340     }
  341 
  342     gfproxyd_port = pmap_assign_port(this, volinfo->gfproxyd.port, gfproxyd_id);
  343     volinfo->gfproxyd.port = gfproxyd_port;
  344 
  345     runner_add_arg(&runner, "--brick-port");
  346     runner_argprintf(&runner, "%d", gfproxyd_port);
  347     runner_add_arg(&runner, "--xlator-option");
  348     runner_argprintf(&runner, "%s-server.listen-port=%d", volinfo->volname,
  349                      gfproxyd_port);
  350 
  351     snprintf(msg, sizeof(msg), "Starting the gfproxyd service for volume %s",
  352              volinfo->volname);
  353     runner_log(&runner, this->name, GF_LOG_DEBUG, msg);
  354 
  355     if (flags == PROC_START_NO_WAIT) {
  356         ret = runner_run_nowait(&runner);
  357     } else {
  358         synclock_unlock(&priv->big_lock);
  359         {
  360             ret = runner_run(&runner);
  361         }
  362         synclock_lock(&priv->big_lock);
  363     }
  364 
  365 out:
  366     return ret;
  367 }
  368 
  369 int
  370 glusterd_gfproxydsvc_restart()
  371 {
  372     glusterd_volinfo_t *volinfo = NULL;
  373     glusterd_volinfo_t *tmp = NULL;
  374     int ret = -1;
  375     xlator_t *this = THIS;
  376     glusterd_conf_t *conf = NULL;
  377     glusterd_svc_t *svc = NULL;
  378 
  379     GF_VALIDATE_OR_GOTO("glusterd", this, out);
  380 
  381     conf = this->private;
  382     GF_VALIDATE_OR_GOTO(this->name, conf, out);
  383 
  384     cds_list_for_each_entry_safe(volinfo, tmp, &conf->volumes, vol_list)
  385     {
  386         /* Start per volume gfproxyd svc */
  387         if (volinfo->status == GLUSTERD_STATUS_STARTED) {
  388             svc = &(volinfo->gfproxyd.svc);
  389             ret = svc->manager(svc, volinfo, PROC_START_NO_WAIT);
  390             if (ret) {
  391                 gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPD_START_FAIL,
  392                        "Couldn't resolve gfproxyd for "
  393                        "vol: %s on restart",
  394                        volinfo->volname);
  395                 gf_event(EVENT_SVC_MANAGER_FAILED, "volume=%s;svc_name=%s",
  396                          volinfo->volname, svc->name);
  397                 goto out;
  398             }
  399         }
  400     }
  401 out:
  402     return ret;
  403 }
  404 
  405 int
  406 glusterd_gfproxydsvc_reconfigure(void *data)
  407 {
  408     int ret = -1;
  409     xlator_t *this = NULL;
  410     gf_boolean_t identical = _gf_false;
  411     glusterd_volinfo_t *volinfo = NULL;
  412 
  413     volinfo = data;
  414 
  415     this = THIS;
  416     GF_VALIDATE_OR_GOTO("glusterd", this, out);
  417 
  418     if (!volinfo->gfproxyd.svc.inited)
  419         goto manager;
  420 
  421     if (!glusterd_is_gfproxyd_enabled(volinfo))
  422         goto manager;
  423     else if (!glusterd_proc_is_running(&volinfo->gfproxyd.svc.proc))
  424         goto manager;
  425 
  426     /*
  427      * Check both OLD and NEW volfiles, if they are SAME by size
  428      * and cksum i.e. "character-by-character". If YES, then
  429      * NOTHING has been changed, just return.
  430      */
  431     ret = glusterd_svc_check_gfproxyd_volfile_identical(
  432         volinfo->gfproxyd.svc.name, volinfo, &identical);
  433     if (ret)
  434         goto out;
  435 
  436     if (identical) {
  437         ret = 0;
  438         goto out;
  439     }
  440 
  441     /*
  442      * They are not identical. Find out if the topology is changed
  443      * OR just the volume options. If just the options which got
  444      * changed, then inform the xlator to reconfigure the options.
  445      */
  446     identical = _gf_false; /* RESET the FLAG */
  447     ret = glusterd_svc_check_gfproxyd_topology_identical(
  448         volinfo->gfproxyd.svc.name, volinfo, &identical);
  449     if (ret)
  450         goto out;
  451 
  452     /* Topology is not changed, but just the options. But write the
  453      * options to gfproxyd volfile, so that gfproxyd will be reconfigured.
  454      */
  455     if (identical) {
  456         ret = glusterd_gfproxydsvc_create_volfile(volinfo);
  457         if (ret == 0) { /* Only if above PASSES */
  458             ret = glusterd_fetchspec_notify(this);
  459         }
  460         goto out;
  461     }
  462 manager:
  463     /*
  464      * gfproxyd volfile's topology has been changed. gfproxyd server needs
  465      * to be RESTARTED to ACT on the changed volfile.
  466      */
  467     ret = volinfo->gfproxyd.svc.manager(&(volinfo->gfproxyd.svc), volinfo,
  468                                         PROC_START_NO_WAIT);
  469 
  470 out:
  471     gf_msg_debug("glusterd", 0, "Returning %d", ret);
  472     return ret;
  473 }