amfcluster.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /** @file amfcluster.c
  2. *
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  5. * - Refactoring of code into several AMF files
  6. * - Constructors/destructors
  7. * - Serializers/deserializers
  8. *
  9. * All rights reserved.
  10. *
  11. *
  12. * This software licensed under BSD license, the text of which follows:
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  36. * THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * AMF Cluster Class Implementation
  39. *
  40. * This file contains functions for handling the AMF cluster. It can be
  41. * viewed as the implementation of the AMF Cluster class
  42. * as described in SAI-Overview-B.02.01. The SA Forum specification
  43. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  44. * and is referred to as 'the spec' below.
  45. *
  46. * The functions in this file are responsible for:
  47. * - to start the cluster initially
  48. * - to handle the administrative operation support for the cluster (FUTURE)
  49. *
  50. * The cluster class contains the following state machines:
  51. * - administrative state machine (ADSM)
  52. * - availability control state machine (ACSM)
  53. *
  54. * The administrative state machine will be implemented in the future.
  55. *
  56. * ACSM handles initial start of the cluster. In the future it will also handle
  57. * administrative commands on the cluster as described in paragraph 7.4 of the
  58. * spec. ACSM includes two stable states (UNINSTANTIATED and STARTED) and a
  59. * number of states to control the transition between the stable states.
  60. *
  61. * The cluster is in state UNINSTANTIATED when the cluster starts. (In the
  62. * future this state will also be assumed after the LOCK_INSTANTIATION
  63. * administrative command.)
  64. *
  65. * State STARTED is assumed when the cluster has been initially started and
  66. * will in the future be re-assumed after the administrative command RESTART
  67. * have been executed.
  68. */
  69. #include <stdlib.h>
  70. #include <errno.h>
  71. #include "print.h"
  72. #include "amf.h"
  73. #include "util.h"
  74. #include "main.h"
  75. #include "service.h"
  76. /**
  77. * Determine if all applications are started
  78. * @param cluster
  79. *
  80. * @return int
  81. */
  82. static int all_applications_started (struct amf_cluster *cluster)
  83. {
  84. int all_started = 1;
  85. struct amf_application *app;
  86. struct amf_sg *sg;
  87. struct amf_su *su;
  88. for (app = cluster->application_head; app != NULL; app = app->next) {
  89. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  90. for (su = sg->su_head; su != NULL; su = su->next) {
  91. if (su->saAmfSUPresenceState != SA_AMF_PRESENCE_INSTANTIATED) {
  92. all_started = 0;
  93. goto done;
  94. }
  95. }
  96. }
  97. }
  98. done:
  99. return all_started;
  100. }
  101. static void timer_function_cluster_assign_workload_tmo (void *_cluster)
  102. {
  103. struct req_exec_amf_cluster_start_tmo req;
  104. struct iovec iovec;
  105. ENTER ("");
  106. req.header.size = sizeof (struct req_exec_amf_cluster_start_tmo);
  107. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  108. MESSAGE_REQ_EXEC_AMF_CLUSTER_START_TMO);
  109. iovec.iov_base = (char *)&req;
  110. iovec.iov_len = sizeof (req);
  111. assert (totempg_groups_mcast_joined (openais_group_handle,
  112. &iovec, 1, TOTEMPG_AGREED) == 0);
  113. }
  114. void amf_cluster_start (struct amf_cluster *cluster)
  115. {
  116. struct amf_application *app;
  117. log_printf(LOG_NOTICE, "Cluster: starting applications.");
  118. amf_cluster->state = CLUSTER_STARTING;
  119. for (app = cluster->application_head; app != NULL; app = app->next) {
  120. amf_application_start (app, NULL);
  121. }
  122. /* wait a while before assigning workload */
  123. poll_timer_add (aisexec_poll_handle, cluster->saAmfClusterStartupTimeout,
  124. cluster,
  125. timer_function_cluster_assign_workload_tmo,
  126. &cluster->timeout_handle);
  127. }
  128. void amf_cluster_init (void)
  129. {
  130. log_init ("AMF");
  131. }
  132. void amf_cluster_application_started (
  133. struct amf_cluster *cluster, struct amf_application *application)
  134. {
  135. ENTER ("application '%s' started", application->name.value);
  136. if (all_applications_started (cluster)) {
  137. log_printf(LOG_NOTICE,
  138. "Cluster: all applications started, assigning workload.");
  139. if (cluster->timeout_handle) {
  140. poll_timer_delete (aisexec_poll_handle, cluster->timeout_handle);
  141. cluster->timeout_handle = 0;
  142. }
  143. amf_cluster_assign_workload (cluster);
  144. }
  145. }
  146. struct amf_cluster *amf_cluster_new (void)
  147. {
  148. struct amf_cluster *cluster = calloc (1, sizeof (struct amf_cluster));
  149. if (cluster == NULL) {
  150. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  151. }
  152. cluster->saAmfClusterStartupTimeout = -1;
  153. cluster->saAmfClusterAdminState = SA_AMF_ADMIN_UNLOCKED;
  154. return cluster;
  155. }
  156. void amf_cluster_application_workload_assigned (
  157. struct amf_cluster *cluster, struct amf_application *app)
  158. {
  159. log_printf(LOG_NOTICE, "Cluster: all workload assigned.");
  160. amf_cluster->state = CLUSTER_STARTED;
  161. }
  162. void *amf_cluster_serialize (struct amf_cluster *cluster, int *len)
  163. {
  164. int objsz = sizeof (struct amf_cluster);
  165. struct amf_cluster *copy;
  166. copy = amf_malloc (objsz);
  167. memcpy (copy, cluster, objsz);
  168. *len = objsz;
  169. TRACE8 ("%s", copy->name.value);
  170. return copy;
  171. }
  172. struct amf_cluster *amf_cluster_deserialize (char *buf, int size)
  173. {
  174. int objsz = sizeof (struct amf_cluster);
  175. if (objsz > size) {
  176. return NULL;
  177. } else {
  178. struct amf_cluster *obj = amf_cluster_new ();
  179. if (obj == NULL) {
  180. return NULL;
  181. }
  182. memcpy (obj, buf, objsz);
  183. TRACE8 ("%s", obj->name.value);
  184. obj->node_head = NULL;
  185. obj->application_head = NULL;
  186. obj->timeout_handle = 0;
  187. return obj;
  188. }
  189. }
  190. void amf_cluster_assign_workload (struct amf_cluster *cluster)
  191. {
  192. struct amf_application *app;
  193. ENTER ("");
  194. if (cluster->timeout_handle) {
  195. poll_timer_delete (aisexec_poll_handle, cluster->timeout_handle);
  196. cluster->timeout_handle = 0;
  197. }
  198. for (app = cluster->application_head; app != NULL; app = app->next) {
  199. amf_application_assign_workload (app, this_amf_node);
  200. }
  201. }