4
0

qdevice-instance.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2015-2026 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 "log-corosync.h"
  38. #include "qdevice-config.h"
  39. #include "qdevice-instance.h"
  40. #include "qdevice-heuristics-exec-list.h"
  41. #include "qdevice-model.h"
  42. #include "utils.h"
  43. int
  44. qdevice_instance_init(struct qdevice_instance *instance,
  45. const struct qdevice_advanced_settings *advanced_settings)
  46. {
  47. memset(instance, 0, sizeof(*instance));
  48. node_list_init(&instance->config_node_list);
  49. instance->vq_last_poll = ((time_t) -1);
  50. instance->advanced_settings = advanced_settings;
  51. pr_poll_loop_init(&instance->main_poll_loop);
  52. return (0);
  53. }
  54. int
  55. qdevice_instance_destroy(struct qdevice_instance *instance)
  56. {
  57. node_list_free(&instance->config_node_list);
  58. pr_poll_loop_destroy(&instance->main_poll_loop);
  59. return (0);
  60. }
  61. int
  62. qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instance)
  63. {
  64. char *str;
  65. long long int lli;
  66. int i;
  67. int res;
  68. cs_error_t cs_err;
  69. cmap_iter_handle_t iter_handle;
  70. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  71. size_t value_len;
  72. cmap_value_types_t type;
  73. struct qdevice_heuristics_exec_list tmp_exec_list;
  74. struct qdevice_heuristics_exec_list *exec_list;
  75. char *command;
  76. char exec_name[CMAP_KEYNAME_MAXLEN + 1];
  77. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  78. size_t no_execs;
  79. int send_exec_list;
  80. instance->heuristics_instance.timeout = instance->heartbeat_interval / 2;
  81. if (cmap_get_string(instance->cmap_handle,
  82. "quorum.device.heuristics.timeout", &str) == CS_OK) {
  83. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  84. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  85. log(LOG_ERR, "heuristics.timeout must be valid number in "
  86. "range <%"PRIu32",%"PRIu32">",
  87. instance->advanced_settings->heuristics_min_timeout,
  88. instance->advanced_settings->heuristics_max_timeout);
  89. free(str);
  90. return (-1);
  91. } else {
  92. instance->heuristics_instance.timeout = lli;
  93. }
  94. free(str);
  95. }
  96. instance->heuristics_instance.sync_timeout = instance->sync_heartbeat_interval / 2;
  97. if (cmap_get_string(instance->cmap_handle,
  98. "quorum.device.heuristics.sync_timeout", &str) == CS_OK) {
  99. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  100. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  101. log(LOG_ERR, "heuristics.sync_timeout must be valid number in "
  102. "range <%"PRIu32",%"PRIu32">",
  103. instance->advanced_settings->heuristics_min_timeout,
  104. instance->advanced_settings->heuristics_max_timeout);
  105. free(str);
  106. return (-1);
  107. } else {
  108. instance->heuristics_instance.sync_timeout = lli;
  109. }
  110. free(str);
  111. }
  112. instance->heuristics_instance.interval = instance->heartbeat_interval * 3;
  113. if (cmap_get_string(instance->cmap_handle,
  114. "quorum.device.heuristics.interval", &str) == CS_OK) {
  115. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_interval,
  116. instance->advanced_settings->heuristics_max_interval, &lli) == -1) {
  117. log(LOG_ERR, "heuristics.interval must be valid number in "
  118. "range <%"PRIu32",%"PRIu32">",
  119. instance->advanced_settings->heuristics_min_interval,
  120. instance->advanced_settings->heuristics_max_interval);
  121. free(str);
  122. return (-1);
  123. } else {
  124. instance->heuristics_instance.interval = lli;
  125. }
  126. free(str);
  127. }
  128. instance->heuristics_instance.mode = QDEVICE_DEFAULT_HEURISTICS_MODE;
  129. if (cmap_get_string(instance->cmap_handle, "quorum.device.heuristics.mode", &str) == CS_OK) {
  130. if ((i = utils_parse_bool_str(str)) == -1) {
  131. if (strcasecmp(str, "sync") != 0) {
  132. log(LOG_ERR, "quorum.device.heuristics.mode value is not valid.");
  133. free(str);
  134. return (-1);
  135. } else {
  136. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_SYNC;
  137. }
  138. } else {
  139. if (i == 1) {
  140. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_ENABLED;
  141. } else {
  142. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  143. }
  144. }
  145. free(str);
  146. }
  147. send_exec_list = 0;
  148. exec_list = NULL;
  149. qdevice_heuristics_exec_list_init(&tmp_exec_list);
  150. if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_DISABLED) {
  151. exec_list = NULL;
  152. send_exec_list = 1;
  153. } else if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  154. instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_SYNC) {
  155. /*
  156. * Walk thru list of commands to exec
  157. */
  158. cs_err = cmap_iter_init(instance->cmap_handle, "quorum.device.heuristics.exec_", &iter_handle);
  159. if (cs_err != CS_OK) {
  160. log_corosync(LOG_ERR, cs_err, "Can't iterate quorum.device.heuristics.exec_ keys");
  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. cs_error_t cs_err;
  224. if ((cs_err = cmap_get_string(instance->cmap_handle, "quorum.device.model", &str)) != CS_OK) {
  225. log_corosync(LOG_ERR, cs_err, "Can't read quorum.device.model cmap key");
  226. return (-1);
  227. }
  228. if (qdevice_model_str_to_type(str, &instance->model_type) != 0) {
  229. log(LOG_ERR, "Configured device model %s is not supported.", str);
  230. free(str);
  231. return (-1);
  232. }
  233. free(str);
  234. if ((cs_err = cmap_get_uint32(instance->cmap_handle, "runtime.votequorum.this_node_id",
  235. &instance->node_id)) != CS_OK) {
  236. log_corosync(LOG_ERR, cs_err, "Unable to retrieve this node nodeid");
  237. return (-1);
  238. }
  239. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.timeout", &instance->heartbeat_interval) != CS_OK) {
  240. instance->heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  241. }
  242. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.sync_timeout",
  243. &instance->sync_heartbeat_interval) != CS_OK) {
  244. instance->sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  245. }
  246. if (qdevice_instance_configure_from_cmap_heuristics(instance) != 0) {
  247. return (-1);
  248. }
  249. return (0);
  250. }