4
0

qdevice-instance.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include "log.h"
  37. #include "qdevice-config.h"
  38. #include "qdevice-instance.h"
  39. #include "qdevice-heuristics-exec-list.h"
  40. #include "qdevice-model.h"
  41. #include "utils.h"
  42. int
  43. qdevice_instance_init(struct qdevice_instance *instance,
  44. const struct qdevice_advanced_settings *advanced_settings)
  45. {
  46. memset(instance, 0, sizeof(*instance));
  47. node_list_init(&instance->config_node_list);
  48. instance->vq_last_poll = ((time_t) -1);
  49. instance->advanced_settings = advanced_settings;
  50. pr_poll_loop_init(&instance->main_poll_loop);
  51. return (0);
  52. }
  53. int
  54. qdevice_instance_destroy(struct qdevice_instance *instance)
  55. {
  56. node_list_free(&instance->config_node_list);
  57. pr_poll_loop_destroy(&instance->main_poll_loop);
  58. return (0);
  59. }
  60. int
  61. qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instance)
  62. {
  63. char *str;
  64. long long int lli;
  65. int i;
  66. int res;
  67. cs_error_t cs_err;
  68. cmap_iter_handle_t iter_handle;
  69. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  70. size_t value_len;
  71. cmap_value_types_t type;
  72. struct qdevice_heuristics_exec_list tmp_exec_list;
  73. struct qdevice_heuristics_exec_list *exec_list;
  74. char *command;
  75. char exec_name[CMAP_KEYNAME_MAXLEN + 1];
  76. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  77. size_t no_execs;
  78. int send_exec_list;
  79. instance->heuristics_instance.timeout = instance->heartbeat_interval / 2;
  80. if (cmap_get_string(instance->cmap_handle,
  81. "quorum.device.heuristics.timeout", &str) == CS_OK) {
  82. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  83. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  84. log(LOG_ERR, "heuristics.timeout must be valid number in "
  85. "range <%"PRIu32",%"PRIu32">",
  86. instance->advanced_settings->heuristics_min_timeout,
  87. instance->advanced_settings->heuristics_max_timeout);
  88. free(str);
  89. return (-1);
  90. } else {
  91. instance->heuristics_instance.timeout = lli;
  92. }
  93. free(str);
  94. }
  95. instance->heuristics_instance.sync_timeout = instance->sync_heartbeat_interval / 2;
  96. if (cmap_get_string(instance->cmap_handle,
  97. "quorum.device.heuristics.sync_timeout", &str) == CS_OK) {
  98. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  99. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  100. log(LOG_ERR, "heuristics.sync_timeout must be valid number in "
  101. "range <%"PRIu32",%"PRIu32">",
  102. instance->advanced_settings->heuristics_min_timeout,
  103. instance->advanced_settings->heuristics_max_timeout);
  104. free(str);
  105. return (-1);
  106. } else {
  107. instance->heuristics_instance.sync_timeout = lli;
  108. }
  109. free(str);
  110. }
  111. instance->heuristics_instance.interval = instance->heartbeat_interval * 3;
  112. if (cmap_get_string(instance->cmap_handle,
  113. "quorum.device.heuristics.interval", &str) == CS_OK) {
  114. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_interval,
  115. instance->advanced_settings->heuristics_max_interval, &lli) == -1) {
  116. log(LOG_ERR, "heuristics.interval must be valid number in "
  117. "range <%"PRIu32",%"PRIu32">",
  118. instance->advanced_settings->heuristics_min_interval,
  119. instance->advanced_settings->heuristics_max_interval);
  120. free(str);
  121. return (-1);
  122. } else {
  123. instance->heuristics_instance.interval = lli;
  124. }
  125. free(str);
  126. }
  127. instance->heuristics_instance.mode = QDEVICE_DEFAULT_HEURISTICS_MODE;
  128. if (cmap_get_string(instance->cmap_handle, "quorum.device.heuristics.mode", &str) == CS_OK) {
  129. if ((i = utils_parse_bool_str(str)) == -1) {
  130. if (strcasecmp(str, "sync") != 0) {
  131. log(LOG_ERR, "quorum.device.heuristics.mode value is not valid.");
  132. free(str);
  133. return (-1);
  134. } else {
  135. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_SYNC;
  136. }
  137. } else {
  138. if (i == 1) {
  139. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_ENABLED;
  140. } else {
  141. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  142. }
  143. }
  144. free(str);
  145. }
  146. send_exec_list = 0;
  147. exec_list = NULL;
  148. qdevice_heuristics_exec_list_init(&tmp_exec_list);
  149. if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_DISABLED) {
  150. exec_list = NULL;
  151. send_exec_list = 1;
  152. } else if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  153. instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_SYNC) {
  154. /*
  155. * Walk thru list of commands to exec
  156. */
  157. cs_err = cmap_iter_init(instance->cmap_handle, "quorum.device.heuristics.exec_", &iter_handle);
  158. if (cs_err != CS_OK) {
  159. log(LOG_ERR, "Can't iterate quorum.device.heuristics.exec_ keys. "
  160. "Error %s", cs_strerror(cs_err));
  161. return (-1);
  162. }
  163. while ((cs_err = cmap_iter_next(instance->cmap_handle, iter_handle, key_name,
  164. &value_len, &type)) == CS_OK) {
  165. if (type != CMAP_VALUETYPE_STRING) {
  166. log(LOG_WARNING, "%s key is not of string type. Ignoring", key_name);
  167. continue ;
  168. }
  169. res = sscanf(key_name, "quorum.device.heuristics.exec_%[^.]%s", exec_name, tmp_key);
  170. if (res != 1) {
  171. log(LOG_WARNING, "%s key is not correct heuristics exec name. Ignoring", key_name);
  172. continue ;
  173. }
  174. cs_err = cmap_get_string(instance->cmap_handle, key_name, &command);
  175. if (cs_err != CS_OK) {
  176. log(LOG_WARNING, "Can't get value of %s key. Ignoring", key_name);
  177. continue ;
  178. }
  179. if (qdevice_heuristics_exec_list_add(&tmp_exec_list, exec_name, command) == NULL) {
  180. log(LOG_WARNING, "Can't store value of %s key into list. Ignoring", key_name);
  181. }
  182. free(command);
  183. }
  184. no_execs = qdevice_heuristics_exec_list_size(&tmp_exec_list);
  185. if (no_execs == 0) {
  186. log(LOG_INFO, "No valid heuristics execs defined. Disabling heuristics.");
  187. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  188. exec_list = NULL;
  189. send_exec_list = 1;
  190. } else if (no_execs > instance->advanced_settings->heuristics_max_execs) {
  191. log(LOG_ERR, "Too much (%zu) heuristics execs defined (max is %zu)."
  192. " Disabling heuristics.", no_execs,
  193. instance->advanced_settings->heuristics_max_execs);
  194. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  195. exec_list = NULL;
  196. send_exec_list = 1;
  197. } else if (qdevice_heuristics_exec_list_eq(&tmp_exec_list,
  198. &instance->heuristics_instance.exec_list) == 1) {
  199. log(LOG_DEBUG, "Heuristics list is unchanged");
  200. send_exec_list = 0;
  201. } else {
  202. log(LOG_DEBUG, "Heuristics list changed");
  203. exec_list = &tmp_exec_list;
  204. send_exec_list = 1;
  205. }
  206. } else {
  207. log(LOG_CRIT, "Undefined heuristics mode");
  208. exit(EXIT_FAILURE);
  209. }
  210. if (send_exec_list) {
  211. if (qdevice_heuristics_change_exec_list(&instance->heuristics_instance,
  212. exec_list, instance->sync_in_progress) != 0) {
  213. return (-1);
  214. }
  215. }
  216. qdevice_heuristics_exec_list_free(&tmp_exec_list);
  217. return (0);
  218. }
  219. int
  220. qdevice_instance_configure_from_cmap(struct qdevice_instance *instance)
  221. {
  222. char *str;
  223. if (cmap_get_string(instance->cmap_handle, "quorum.device.model", &str) != CS_OK) {
  224. log(LOG_ERR, "Can't read quorum.device.model cmap key.");
  225. return (-1);
  226. }
  227. if (qdevice_model_str_to_type(str, &instance->model_type) != 0) {
  228. log(LOG_ERR, "Configured device model %s is not supported.", str);
  229. free(str);
  230. return (-1);
  231. }
  232. free(str);
  233. if (cmap_get_uint32(instance->cmap_handle, "runtime.votequorum.this_node_id",
  234. &instance->node_id) != CS_OK) {
  235. log(LOG_ERR, "Unable to retrieve this node nodeid.");
  236. return (-1);
  237. }
  238. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.timeout", &instance->heartbeat_interval) != CS_OK) {
  239. instance->heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  240. }
  241. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.sync_timeout",
  242. &instance->sync_heartbeat_interval) != CS_OK) {
  243. instance->sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  244. }
  245. if (qdevice_instance_configure_from_cmap_heuristics(instance) != 0) {
  246. return (-1);
  247. }
  248. return (0);
  249. }