4
0

qdevice-instance.c 9.3 KB

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