4
0

qdevice-instance.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2015-2019 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. return (0);
  51. }
  52. int
  53. qdevice_instance_destroy(struct qdevice_instance *instance)
  54. {
  55. node_list_free(&instance->config_node_list);
  56. return (0);
  57. }
  58. int
  59. qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instance)
  60. {
  61. char *str;
  62. long long int lli;
  63. int i;
  64. int res;
  65. cs_error_t cs_err;
  66. cmap_iter_handle_t iter_handle;
  67. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  68. size_t value_len;
  69. cmap_value_types_t type;
  70. struct qdevice_heuristics_exec_list tmp_exec_list;
  71. struct qdevice_heuristics_exec_list *exec_list;
  72. char *command;
  73. char exec_name[CMAP_KEYNAME_MAXLEN + 1];
  74. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  75. size_t no_execs;
  76. int send_exec_list;
  77. instance->heuristics_instance.timeout = instance->heartbeat_interval / 2;
  78. if (cmap_get_string(instance->cmap_handle,
  79. "quorum.device.heuristics.timeout", &str) == CS_OK) {
  80. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  81. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  82. log(LOG_ERR, "heuristics.timeout must be valid number in "
  83. "range <%"PRIu32",%"PRIu32">",
  84. instance->advanced_settings->heuristics_min_timeout,
  85. instance->advanced_settings->heuristics_max_timeout);
  86. free(str);
  87. return (-1);
  88. } else {
  89. instance->heuristics_instance.timeout = lli;
  90. }
  91. free(str);
  92. }
  93. instance->heuristics_instance.sync_timeout = instance->sync_heartbeat_interval / 2;
  94. if (cmap_get_string(instance->cmap_handle,
  95. "quorum.device.heuristics.sync_timeout", &str) == CS_OK) {
  96. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
  97. instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
  98. log(LOG_ERR, "heuristics.sync_timeout must be valid number in "
  99. "range <%"PRIu32",%"PRIu32">",
  100. instance->advanced_settings->heuristics_min_timeout,
  101. instance->advanced_settings->heuristics_max_timeout);
  102. free(str);
  103. return (-1);
  104. } else {
  105. instance->heuristics_instance.sync_timeout = lli;
  106. }
  107. free(str);
  108. }
  109. instance->heuristics_instance.interval = instance->heartbeat_interval * 3;
  110. if (cmap_get_string(instance->cmap_handle,
  111. "quorum.device.heuristics.interval", &str) == CS_OK) {
  112. if (utils_strtonum(str, instance->advanced_settings->heuristics_min_interval,
  113. instance->advanced_settings->heuristics_max_interval, &lli) == -1) {
  114. log(LOG_ERR, "heuristics.interval must be valid number in "
  115. "range <%"PRIu32",%"PRIu32">",
  116. instance->advanced_settings->heuristics_min_interval,
  117. instance->advanced_settings->heuristics_max_interval);
  118. free(str);
  119. return (-1);
  120. } else {
  121. instance->heuristics_instance.interval = lli;
  122. }
  123. free(str);
  124. }
  125. instance->heuristics_instance.mode = QDEVICE_DEFAULT_HEURISTICS_MODE;
  126. if (cmap_get_string(instance->cmap_handle, "quorum.device.heuristics.mode", &str) == CS_OK) {
  127. if ((i = utils_parse_bool_str(str)) == -1) {
  128. if (strcasecmp(str, "sync") != 0) {
  129. log(LOG_ERR, "quorum.device.heuristics.mode value is not valid.");
  130. free(str);
  131. return (-1);
  132. } else {
  133. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_SYNC;
  134. }
  135. } else {
  136. if (i == 1) {
  137. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_ENABLED;
  138. } else {
  139. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  140. }
  141. }
  142. free(str);
  143. }
  144. send_exec_list = 0;
  145. exec_list = NULL;
  146. qdevice_heuristics_exec_list_init(&tmp_exec_list);
  147. if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_DISABLED) {
  148. exec_list = NULL;
  149. send_exec_list = 1;
  150. } else if (instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  151. instance->heuristics_instance.mode == QDEVICE_HEURISTICS_MODE_SYNC) {
  152. /*
  153. * Walk thru list of commands to exec
  154. */
  155. cs_err = cmap_iter_init(instance->cmap_handle, "quorum.device.heuristics.exec_", &iter_handle);
  156. if (cs_err != CS_OK) {
  157. log(LOG_ERR, "Can't iterate quorum.device.heuristics.exec_ keys. "
  158. "Error %s", cs_strerror(cs_err));
  159. return (-1);
  160. }
  161. while ((cs_err = cmap_iter_next(instance->cmap_handle, iter_handle, key_name,
  162. &value_len, &type)) == CS_OK) {
  163. if (type != CMAP_VALUETYPE_STRING) {
  164. log(LOG_WARNING, "%s key is not of string type. Ignoring", key_name);
  165. continue ;
  166. }
  167. res = sscanf(key_name, "quorum.device.heuristics.exec_%[^.]%s", exec_name, tmp_key);
  168. if (res != 1) {
  169. log(LOG_WARNING, "%s key is not correct heuristics exec name. Ignoring", key_name);
  170. continue ;
  171. }
  172. cs_err = cmap_get_string(instance->cmap_handle, key_name, &command);
  173. if (cs_err != CS_OK) {
  174. log(LOG_WARNING, "Can't get value of %s key. Ignoring", key_name);
  175. continue ;
  176. }
  177. if (qdevice_heuristics_exec_list_add(&tmp_exec_list, exec_name, command) == NULL) {
  178. log(LOG_WARNING, "Can't store value of %s key into list. Ignoring", key_name);
  179. }
  180. free(command);
  181. }
  182. no_execs = qdevice_heuristics_exec_list_size(&tmp_exec_list);
  183. if (no_execs == 0) {
  184. log(LOG_INFO, "No valid heuristics execs defined. Disabling heuristics.");
  185. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  186. exec_list = NULL;
  187. send_exec_list = 1;
  188. } else if (no_execs > instance->advanced_settings->heuristics_max_execs) {
  189. log(LOG_ERR, "Too much (%zu) heuristics execs defined (max is %zu)."
  190. " Disabling heuristics.", no_execs,
  191. instance->advanced_settings->heuristics_max_execs);
  192. instance->heuristics_instance.mode = QDEVICE_HEURISTICS_MODE_DISABLED;
  193. exec_list = NULL;
  194. send_exec_list = 1;
  195. } else if (qdevice_heuristics_exec_list_eq(&tmp_exec_list,
  196. &instance->heuristics_instance.exec_list) == 1) {
  197. log(LOG_DEBUG, "Heuristics list is unchanged");
  198. send_exec_list = 0;
  199. } else {
  200. log(LOG_DEBUG, "Heuristics list changed");
  201. exec_list = &tmp_exec_list;
  202. send_exec_list = 1;
  203. }
  204. } else {
  205. log(LOG_CRIT, "Undefined heuristics mode");
  206. exit(1);
  207. }
  208. if (send_exec_list) {
  209. if (qdevice_heuristics_change_exec_list(&instance->heuristics_instance,
  210. exec_list, instance->sync_in_progress) != 0) {
  211. return (-1);
  212. }
  213. }
  214. qdevice_heuristics_exec_list_free(&tmp_exec_list);
  215. return (0);
  216. }
  217. int
  218. qdevice_instance_configure_from_cmap(struct qdevice_instance *instance)
  219. {
  220. char *str;
  221. if (cmap_get_string(instance->cmap_handle, "quorum.device.model", &str) != CS_OK) {
  222. log(LOG_ERR, "Can't read quorum.device.model cmap key.");
  223. return (-1);
  224. }
  225. if (qdevice_model_str_to_type(str, &instance->model_type) != 0) {
  226. log(LOG_ERR, "Configured device model %s is not supported.", str);
  227. free(str);
  228. return (-1);
  229. }
  230. free(str);
  231. if (cmap_get_uint32(instance->cmap_handle, "runtime.votequorum.this_node_id",
  232. &instance->node_id) != CS_OK) {
  233. log(LOG_ERR, "Unable to retrieve this node nodeid.");
  234. return (-1);
  235. }
  236. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.timeout", &instance->heartbeat_interval) != CS_OK) {
  237. instance->heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  238. }
  239. if (cmap_get_uint32(instance->cmap_handle, "quorum.device.sync_timeout",
  240. &instance->sync_heartbeat_interval) != CS_OK) {
  241. instance->sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  242. }
  243. if (qdevice_instance_configure_from_cmap_heuristics(instance) != 0) {
  244. return (-1);
  245. }
  246. return (0);
  247. }