amfsu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /** @file exec/amfsu.c
  2. *
  3. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  4. * Author: Steven Dake (sdake@mvista.com)
  5. *
  6. * Copyright (c) 2006 Ericsson AB.
  7. * Author: Hans Feldt
  8. * - Introduced AMF B.02 information model
  9. * - Use DN in API and multicast messages
  10. * - (Re-)Introduction of event based multicast messages
  11. * - Refactoring of code into several AMF files
  12. * Author: Anders Eriksson
  13. *
  14. * All rights reserved.
  15. *
  16. *
  17. * This software licensed under BSD license, the text of which follows:
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are met:
  21. *
  22. * - Redistributions of source code must retain the above copyright notice,
  23. * this list of conditions and the following disclaimer.
  24. * - Redistributions in binary form must reproduce the above copyright notice,
  25. * this list of conditions and the following disclaimer in the documentation
  26. * and/or other materials provided with the distribution.
  27. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived from this
  29. * software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  41. * THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * AMF Service Unit Class Implementation
  44. *
  45. * This file contains functions for handling AMF-service units(SUs). It can be
  46. * viewed as the implementation of the AMF Service Unit class (called SU)
  47. * as described in SAI-Overview-B.02.01. The SA Forum specification
  48. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  49. * and is referred to as 'the spec' below.
  50. *
  51. * The functions in this file are responsible for:
  52. * - instantiating and terminating service units on request
  53. * (considering the dependencies between components described in paragraph
  54. * 3.9.2)
  55. * - creating and deleting CSI-assignment objects between its components and
  56. * CSI-objects upon request
  57. * - receiving error reports from its components and forwarding them to
  58. * appropriate handler (SU or SG or node or cluster)
  59. * - implementing restart of itself and its components (paragraph 3.12.1.2)
  60. * - implementing error escallation level 1 (paragraph 3.12.2.2 in the spec)
  61. * - handling all run time attributes of the AMF SU; all cached
  62. * attributes are stored as variables and sent to the IMM service
  63. * upon the changes described in the specification.
  64. *
  65. * SU contains the following state machines:
  66. * - presence state machine (PRSM)
  67. * - administrative state machine (ADSM) (NOT IN THIS RELEASE)
  68. * - operational state machine (OPSM)
  69. * - readiness state machine (RESM)
  70. * - ha state per service instance (SI)
  71. * - restart control state machine (RCSM)
  72. *
  73. * The presence state machine orders intantiation of its components on request.
  74. * It fully respects the dependency rules between components at instantiation
  75. * such that it orders instantiation simultaneously only of components on the
  76. * same instantiation level. The presence state machine is implemented with
  77. * the states described in the spec and the state transitions are trigged by
  78. * reported state transitions from its contained components according to
  79. * paragraph 3.3.1.1.
  80. *
  81. * The operational state machine is not responsible for any control function.
  82. * It assumes the DISABLED state if an incoming operational state change report
  83. * from a component indicates the component has assumed the DISABLED state.
  84. * Operational state changes are reported to IMM.
  85. *
  86. * The readiness state machine is not used for any control but is updated and
  87. * reported to IMM when it is changed.
  88. *
  89. * The restart control state machine (RCSM) is used to implement level 1 of
  90. * the error escallation polycy described in chapter 3.12.2 of the spec. It
  91. * also implements component restart and service unit restart as described in
  92. * paragraph 3.12.1.2 and 3.12.1.3.
  93. * RCSM contains three composite states.
  94. * Being a composite state means that the state contains substates.
  95. * RCSM composite states are:
  96. * - ESCALLATION_LEVEL (LEVEL_0, LEVEL_1 and LEVEL_2)
  97. * - RESTARTING_COMPONENT (DEACTIVATING, RESTARTING, SETTING and ACTIVATING)
  98. * - RESTARTING_SERVICE_UNIT (DEACTIVATING, TERMINATING, INSTANTIATING,
  99. * and ACTIVATING)
  100. *
  101. * ESCALLATION_LEVEL is a kind of idle state where no actions are performed
  102. * and used only to remember the escallation level. Substate LEVEL_0 indicates
  103. * no escallation. LEVEL_1 indicates that a component restart has been
  104. * executed recently and the escallation timer is still running. At this level
  105. * component restart requests will transition to RESTARTING_COMPONENT but
  106. * if there are too many restart requests before the probation timer expires
  107. * then a transition will be made to LEVEL_2 and the restart request will
  108. * be forwarded to the node instance hosting this component.
  109. * State RESTARTING_SERVICE_UNIT will only be assumed if the node explicitly
  110. * requests the SU to execute a restart of itself (after having evaluated its
  111. * part of the error escallation policy).
  112. *
  113. */
  114. /*
  115. *
  116. */
  117. #include <stdlib.h>
  118. #include <assert.h>
  119. #include <string.h>
  120. #include <errno.h>
  121. #include "amf.h"
  122. #include "util.h"
  123. #include "print.h"
  124. #include "main.h"
  125. static int presence_state_all_comps_in_su_are_set (struct amf_su *su,
  126. SaAmfPresenceStateT state)
  127. {
  128. int all_set = 1;
  129. struct amf_comp *comp;
  130. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  131. if (comp->saAmfCompPresenceState != state) {
  132. all_set = 0;
  133. }
  134. }
  135. return all_set;
  136. }
  137. static void su_readiness_state_set (struct amf_su *su,
  138. SaAmfReadinessStateT readiness_state)
  139. {
  140. su->saAmfSUReadinessState = readiness_state;
  141. TRACE1 ("Setting SU '%s' readiness state: %s\n",
  142. &su->name.value, amf_readiness_state(readiness_state));
  143. }
  144. static void su_presence_state_set (struct amf_su *su,
  145. SaAmfPresenceStateT presence_state)
  146. {
  147. su->saAmfSUPresenceState = presence_state;
  148. TRACE1 ("Setting SU '%s' presence state: %s\n",
  149. su->name.value, amf_presence_state (presence_state));
  150. }
  151. static void su_operational_state_set (struct amf_su *su,
  152. SaAmfOperationalStateT oper_state)
  153. {
  154. struct amf_comp* comp;
  155. if (oper_state == su->saAmfSUOperState) {
  156. log_printf (LOG_INFO,
  157. "Not assigning service unit new operational state - same state\n");
  158. return;
  159. }
  160. su->saAmfSUOperState = oper_state;
  161. TRACE1 ("Setting SU '%s' operational state: %s\n",
  162. su->name.value, amf_op_state (oper_state));
  163. if (oper_state == SA_AMF_OPERATIONAL_ENABLED) {
  164. su_readiness_state_set (su, SA_AMF_READINESS_IN_SERVICE);
  165. for (comp = su->comp_head; comp; comp = comp->next) {
  166. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_IN_SERVICE);
  167. }
  168. amf_sg_su_state_changed (su->sg, su, SA_AMF_OP_STATE, SA_AMF_OPERATIONAL_ENABLED);
  169. } else if (oper_state == SA_AMF_OPERATIONAL_DISABLED) {
  170. su_readiness_state_set (su, SA_AMF_READINESS_OUT_OF_SERVICE);
  171. }
  172. }
  173. static void comp_assign_csi (struct amf_comp *comp, struct amf_csi *csi,
  174. SaAmfHAStateT ha_state)
  175. {
  176. struct amf_csi_assignment *csi_assignment;
  177. dprintf (" Assigning CSI '%s' to comp '%s' with hastate %s\n",
  178. getSaNameT (&csi->name), getSaNameT (&comp->name),
  179. amf_ha_state (ha_state));
  180. csi_assignment = malloc (sizeof (struct amf_csi_assignment));
  181. if (csi_assignment == NULL) {
  182. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  183. }
  184. csi_assignment->comp_next = comp->assigned_csis;
  185. comp->assigned_csis = csi_assignment;
  186. setSaNameT (&csi_assignment->name, (char*)comp->name.value);
  187. csi_assignment->saAmfCSICompHAState = ha_state;
  188. csi_assignment->csi = csi;
  189. csi_assignment->comp = comp;
  190. csi_assignment->saAmfCSICompHAState = ha_state;
  191. if (ha_state == SA_AMF_HA_ACTIVE)
  192. comp->saAmfCompNumCurrActiveCsi++;
  193. else if (ha_state == SA_AMF_HA_STANDBY)
  194. comp->saAmfCompNumCurrStandbyCsi++;
  195. else
  196. assert (0);
  197. amf_comp_hastate_set (comp, csi_assignment, ha_state);
  198. }
  199. static void su_cleanup (struct amf_su *su)
  200. {
  201. struct amf_comp *comp;
  202. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  203. amf_comp_restart (comp);
  204. }
  205. }
  206. static void escalation_policy_cleanup (struct amf_comp *comp)
  207. {
  208. // escalation_timer_start (comp);
  209. switch (comp->su->escalation_level) {
  210. case ESCALATION_LEVEL_NO_ESCALATION:
  211. comp->saAmfCompRestartCount += 1;
  212. if (comp->saAmfCompRestartCount >= comp->su->sg->saAmfSGCompRestartMax) {
  213. comp->su->escalation_level = ESCALATION_LEVEL_ONE;
  214. escalation_policy_cleanup (comp);
  215. comp->saAmfCompRestartCount = 0;
  216. return;
  217. }
  218. dprintf ("Escalation level 0 - restart component\n");
  219. dprintf ("Cleaning up and restarting component.\n");
  220. amf_comp_restart (comp);
  221. break;
  222. case ESCALATION_LEVEL_ONE:
  223. comp->su->saAmfSURestartCount += 1;
  224. if (comp->su->saAmfSURestartCount >= comp->su->sg->saAmfSGSuRestartMax) {
  225. comp->su->escalation_level = ESCALATION_LEVEL_TWO;
  226. escalation_policy_cleanup (comp);
  227. comp->saAmfCompRestartCount = 0;
  228. comp->su->saAmfSURestartCount = 0;
  229. return;
  230. }
  231. dprintf ("Escalation level 1 - restart unit\n");
  232. dprintf ("Cleaning up and restarting unit.\n");
  233. su_cleanup (comp->su);
  234. break;
  235. case ESCALATION_LEVEL_TWO:
  236. dprintf ("Escalation level TWO\n");
  237. su_cleanup (comp->su);
  238. // unit_terminate_failover (comp);
  239. break;
  240. case ESCALATION_LEVEL_THREE:
  241. //TODO
  242. break;
  243. }
  244. }
  245. void amf_su_instantiate (struct amf_su *su)
  246. {
  247. struct amf_comp *comp;
  248. ENTER ("'%s'", su->name.value);
  249. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  250. amf_comp_instantiate (comp);
  251. }
  252. }
  253. void amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  254. SaAmfHAStateT ha_state)
  255. {
  256. struct amf_si_assignment *si_assignment;
  257. dprintf ("Assigning SI '%s' to SU '%s' with hastate %s\n",
  258. getSaNameT (&si->name), getSaNameT (&su->name),
  259. amf_ha_state (ha_state));
  260. si_assignment = malloc (sizeof (struct amf_si_assignment));
  261. if (si_assignment == NULL) {
  262. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  263. }
  264. setSaNameT (&si_assignment->name, (char*)su->name.value);
  265. si_assignment->saAmfSISUHAState = ha_state;
  266. si_assignment->next = su->assigned_sis;
  267. su->assigned_sis = si_assignment;
  268. si_assignment->si = si;
  269. if (ha_state == SA_AMF_HA_ACTIVE) {
  270. si->saAmfSINumCurrActiveAssignments++;
  271. su->saAmfSUNumCurrActiveSIs++;
  272. } else if (ha_state == SA_AMF_HA_STANDBY) {
  273. su->saAmfSUNumCurrStandbySIs++;
  274. si->saAmfSINumCurrStandbyAssignments++;
  275. } else
  276. assert(0);
  277. if ((si->saAmfSINumCurrActiveAssignments == si->saAmfSIPrefActiveAssignments) &&
  278. (si->saAmfSINumCurrStandbyAssignments == si->saAmfSIPrefStandbyAssignments)) {
  279. si->saAmfSIAssignmentState = SA_AMF_ASSIGNMENT_FULLY_ASSIGNED;
  280. } else if ((si->saAmfSINumCurrActiveAssignments < si->saAmfSIPrefActiveAssignments) ||
  281. (si->saAmfSINumCurrStandbyAssignments < si->saAmfSIPrefStandbyAssignments)) {
  282. si->saAmfSIAssignmentState = SA_AMF_ASSIGNMENT_PARTIALLY_ASSIGNED;
  283. }
  284. {
  285. struct amf_csi *csi;
  286. struct amf_comp *comp;
  287. SaNameT *cs_type;
  288. int i;
  289. /*
  290. ** for each component in SU, find a CSI in the SI with the same type
  291. */
  292. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  293. int no_of_cs_types = 0;
  294. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  295. cs_type = comp->saAmfCompCsTypes[i];
  296. no_of_cs_types++;
  297. int no_of_assignments = 0;
  298. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  299. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value, cs_type->length)) {
  300. comp_assign_csi (comp, csi, ha_state);
  301. no_of_assignments++;
  302. }
  303. }
  304. if (no_of_assignments == 0) {
  305. log_printf (LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  306. getSaNameT (cs_type));
  307. }
  308. }
  309. if (no_of_cs_types == 0) {
  310. log_printf (LOG_LEVEL_ERROR, "\t No CS types configured for comp %s ?!!\n",
  311. getSaNameT (&comp->name));
  312. }
  313. }
  314. }
  315. }
  316. /**
  317. * Used by a component to report a state change event
  318. * @param su
  319. * @param comp
  320. * @param type type of state
  321. * @param state new state
  322. */
  323. void amf_su_comp_state_changed (
  324. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  325. {
  326. if (type == SA_AMF_PRESENCE_STATE) {
  327. /*
  328. * If all comp presence states are INSTANTIATED, then SU should
  329. * be instantated.
  330. */
  331. if (state == SA_AMF_PRESENCE_INSTANTIATED) {
  332. if (presence_state_all_comps_in_su_are_set (
  333. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  334. su_presence_state_set (comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  335. } else {
  336. assert (0);
  337. }
  338. } else if (state == SA_AMF_PRESENCE_INSTANTIATING) {
  339. } else if (state == SA_AMF_PRESENCE_RESTARTING) {
  340. } else {
  341. assert (0);
  342. }
  343. } else if (type == SA_AMF_OP_STATE) {
  344. /*
  345. * If all component op states are ENABLED, then SU op
  346. * state should be ENABLED.
  347. */
  348. if (state == SA_AMF_OPERATIONAL_ENABLED) {
  349. struct amf_comp *comp_compare;
  350. int all_set = 1;
  351. for (comp_compare = comp->su->comp_head; comp_compare != NULL; comp_compare = comp->next) {
  352. if (comp_compare->saAmfCompOperState != SA_AMF_OPERATIONAL_ENABLED) {
  353. all_set = 0;
  354. break;
  355. }
  356. }
  357. if (all_set) {
  358. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  359. } else {
  360. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  361. }
  362. } else {
  363. assert (0);
  364. }
  365. } else {
  366. assert (0);
  367. }
  368. }
  369. /**
  370. * Used by a component to report a change in HA state
  371. * @param su
  372. * @param comp
  373. * @param csi_assignment
  374. */
  375. void amf_su_comp_hastate_changed (
  376. struct amf_su *su, struct amf_comp *comp,
  377. struct amf_csi_assignment *csi_assignment)
  378. {
  379. ENTER("'%s' '%s'", comp->name.value, csi_assignment->csi->name.value);
  380. }
  381. /**
  382. * Determine if the SU is hosted on the local node.
  383. * @param su
  384. *
  385. * @return int
  386. */
  387. int amf_su_is_local (struct amf_su *su)
  388. {
  389. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  390. return 1;
  391. } else {
  392. return 0;
  393. }
  394. }
  395. /**
  396. * Called by a component to report a suspected error on a component
  397. * @param su
  398. * @param comp
  399. * @param recommended_recovery
  400. */
  401. void amf_su_comp_error_suspected (
  402. struct amf_su *su,
  403. struct amf_comp *comp,
  404. SaAmfRecommendedRecoveryT recommended_recovery)
  405. {
  406. escalation_policy_cleanup (comp);
  407. }
  408. void amf_su_init (void)
  409. {
  410. log_init ("AMF");
  411. }