amfsu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. amf_sg_su_state_changed (su->sg, su, SA_AMF_PRESENCE_STATE, presence_state);
  151. }
  152. static void su_operational_state_set (struct amf_su *su,
  153. SaAmfOperationalStateT oper_state)
  154. {
  155. struct amf_comp* comp;
  156. if (oper_state == su->saAmfSUOperState) {
  157. log_printf (LOG_INFO,
  158. "Not assigning service unit new operational state - same state\n");
  159. return;
  160. }
  161. su->saAmfSUOperState = oper_state;
  162. TRACE1 ("Setting SU '%s' operational state: %s\n",
  163. su->name.value, amf_op_state (oper_state));
  164. if (oper_state == SA_AMF_OPERATIONAL_ENABLED) {
  165. su_readiness_state_set (su, SA_AMF_READINESS_IN_SERVICE);
  166. for (comp = su->comp_head; comp; comp = comp->next) {
  167. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_IN_SERVICE);
  168. }
  169. // amf_sg_su_state_changed (su->sg, su, SA_AMF_OP_STATE, SA_AMF_OPERATIONAL_ENABLED);
  170. } else if (oper_state == SA_AMF_OPERATIONAL_DISABLED) {
  171. su_readiness_state_set (su, SA_AMF_READINESS_OUT_OF_SERVICE);
  172. }
  173. }
  174. static void comp_assign_csi (struct amf_comp *comp, struct amf_csi *csi,
  175. SaAmfHAStateT ha_state)
  176. {
  177. struct amf_csi_assignment *csi_assignment;
  178. dprintf (" Creating CSI '%s' to comp '%s' with hastate %s\n",
  179. getSaNameT (&csi->name), getSaNameT (&comp->name),
  180. amf_ha_state (ha_state));
  181. csi_assignment = malloc (sizeof (struct amf_csi_assignment));
  182. if (csi_assignment == NULL) {
  183. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  184. }
  185. csi_assignment->comp_next = comp->assigned_csis;
  186. comp->assigned_csis = csi_assignment;
  187. csi_assignment->csi_next = csi->csi_assignments;
  188. csi->csi_assignments = csi_assignment;
  189. setSaNameT (&csi_assignment->name, (char*)comp->name.value);
  190. csi_assignment->saAmfCSICompHAState = ha_state;
  191. csi_assignment->csi = csi;
  192. csi_assignment->comp = comp;
  193. csi_assignment->saAmfCSICompHAState = 0; /* undefined confirmed HA state */
  194. csi_assignment->requested_ha_state = ha_state;
  195. if (ha_state == SA_AMF_HA_ACTIVE)
  196. comp->saAmfCompNumCurrActiveCsi++;
  197. else if (ha_state == SA_AMF_HA_STANDBY)
  198. comp->saAmfCompNumCurrStandbyCsi++;
  199. else
  200. assert (0);
  201. }
  202. static void su_cleanup (struct amf_su *su)
  203. {
  204. struct amf_comp *comp;
  205. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  206. amf_comp_restart (comp);
  207. }
  208. }
  209. static void escalation_policy_cleanup (struct amf_comp *comp)
  210. {
  211. // escalation_timer_start (comp);
  212. switch (comp->su->escalation_level) {
  213. case ESCALATION_LEVEL_NO_ESCALATION:
  214. comp->saAmfCompRestartCount += 1;
  215. if (comp->saAmfCompRestartCount >= comp->su->sg->saAmfSGCompRestartMax) {
  216. comp->su->escalation_level = ESCALATION_LEVEL_ONE;
  217. escalation_policy_cleanup (comp);
  218. comp->saAmfCompRestartCount = 0;
  219. return;
  220. }
  221. dprintf ("Escalation level 0 - restart component\n");
  222. dprintf ("Cleaning up and restarting component.\n");
  223. amf_comp_restart (comp);
  224. break;
  225. case ESCALATION_LEVEL_ONE:
  226. comp->su->saAmfSURestartCount += 1;
  227. if (comp->su->saAmfSURestartCount >= comp->su->sg->saAmfSGSuRestartMax) {
  228. comp->su->escalation_level = ESCALATION_LEVEL_TWO;
  229. escalation_policy_cleanup (comp);
  230. comp->saAmfCompRestartCount = 0;
  231. comp->su->saAmfSURestartCount = 0;
  232. return;
  233. }
  234. dprintf ("Escalation level 1 - restart unit\n");
  235. dprintf ("Cleaning up and restarting unit.\n");
  236. su_cleanup (comp->su);
  237. break;
  238. case ESCALATION_LEVEL_TWO:
  239. dprintf ("Escalation level TWO\n");
  240. su_cleanup (comp->su);
  241. // unit_terminate_failover (comp);
  242. break;
  243. case ESCALATION_LEVEL_THREE:
  244. //TODO
  245. break;
  246. }
  247. }
  248. void amf_su_instantiate (struct amf_su *su)
  249. {
  250. struct amf_comp *comp;
  251. ENTER ("'%s'", su->name.value);
  252. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  253. amf_comp_instantiate (comp);
  254. }
  255. }
  256. void amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  257. SaAmfHAStateT ha_state)
  258. {
  259. struct amf_si_assignment *si_assignment;
  260. dprintf ("Creating SI '%s' to SU '%s' with hastate %s\n",
  261. getSaNameT (&si->name), getSaNameT (&su->name),
  262. amf_ha_state (ha_state));
  263. si_assignment = malloc (sizeof (struct amf_si_assignment));
  264. if (si_assignment == NULL) {
  265. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  266. }
  267. setSaNameT (&si_assignment->name, (char*)su->name.value);
  268. si_assignment->saAmfSISUHAState = 0; /* undefined confirmed HA state */
  269. si_assignment->requested_ha_state = ha_state;
  270. si_assignment->next = su->assigned_sis;
  271. su->assigned_sis = si_assignment;
  272. si_assignment->si = si;
  273. memcpy (&si_assignment->si->saAmfSIProtectedbySG,
  274. &su->sg->name, sizeof (SaNameT));
  275. if (ha_state == SA_AMF_HA_ACTIVE) {
  276. si->saAmfSINumCurrActiveAssignments++;
  277. su->saAmfSUNumCurrActiveSIs++;
  278. } else if (ha_state == SA_AMF_HA_STANDBY) {
  279. su->saAmfSUNumCurrStandbySIs++;
  280. si->saAmfSINumCurrStandbyAssignments++;
  281. } else
  282. assert(0);
  283. if ((si->saAmfSINumCurrActiveAssignments == si->saAmfSIPrefActiveAssignments) &&
  284. (si->saAmfSINumCurrStandbyAssignments == si->saAmfSIPrefStandbyAssignments)) {
  285. si->saAmfSIAssignmentState = SA_AMF_ASSIGNMENT_FULLY_ASSIGNED;
  286. } else if ((si->saAmfSINumCurrActiveAssignments < si->saAmfSIPrefActiveAssignments) ||
  287. (si->saAmfSINumCurrStandbyAssignments < si->saAmfSIPrefStandbyAssignments)) {
  288. si->saAmfSIAssignmentState = SA_AMF_ASSIGNMENT_PARTIALLY_ASSIGNED;
  289. }
  290. {
  291. struct amf_csi *csi;
  292. struct amf_comp *comp;
  293. SaNameT *cs_type;
  294. int i;
  295. /*
  296. ** for each component in SU, find a CSI in the SI with the same type
  297. */
  298. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  299. int no_of_cs_types = 0;
  300. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  301. cs_type = comp->saAmfCompCsTypes[i];
  302. no_of_cs_types++;
  303. int no_of_assignments = 0;
  304. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  305. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value, cs_type->length)) {
  306. comp_assign_csi (comp, csi, ha_state);
  307. no_of_assignments++;
  308. }
  309. }
  310. if (no_of_assignments == 0) {
  311. log_printf (LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  312. getSaNameT (cs_type));
  313. }
  314. }
  315. if (no_of_cs_types == 0) {
  316. log_printf (LOG_LEVEL_ERROR, "\t No CS types configured for comp %s ?!!\n",
  317. getSaNameT (&comp->name));
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Used by a component to report a state change event
  324. * @param su
  325. * @param comp
  326. * @param type type of state
  327. * @param state new state
  328. */
  329. void amf_su_comp_state_changed (
  330. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  331. {
  332. if (type == SA_AMF_PRESENCE_STATE) {
  333. /*
  334. * If all comp presence states are INSTANTIATED, then SU should
  335. * be instantated.
  336. */
  337. if (state == SA_AMF_PRESENCE_INSTANTIATED) {
  338. if (presence_state_all_comps_in_su_are_set (
  339. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  340. su_presence_state_set (comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  341. } else {
  342. /*
  343. * This state occurs when there is more then
  344. * one SU
  345. */
  346. return;
  347. }
  348. } else if (state == SA_AMF_PRESENCE_INSTANTIATING) {
  349. } else if (state == SA_AMF_PRESENCE_RESTARTING) {
  350. } else {
  351. assert (0);
  352. }
  353. } else if (type == SA_AMF_OP_STATE) {
  354. /*
  355. * If all component op states are ENABLED, then SU op
  356. * state should be ENABLED.
  357. */
  358. if (state == SA_AMF_OPERATIONAL_ENABLED) {
  359. struct amf_comp *comp_compare;
  360. int all_set = 1;
  361. for (comp_compare = comp->su->comp_head; comp_compare != NULL; comp_compare = comp_compare->next) {
  362. if (comp_compare->saAmfCompOperState != SA_AMF_OPERATIONAL_ENABLED) {
  363. all_set = 0;
  364. break;
  365. }
  366. }
  367. if (all_set) {
  368. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  369. } else {
  370. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  371. }
  372. } else {
  373. assert (0);
  374. }
  375. } else {
  376. assert (0);
  377. }
  378. }
  379. /**
  380. * Used by a component to report a change in HA state
  381. * @param su
  382. * @param comp
  383. * @param csi_assignment
  384. */
  385. void amf_su_comp_hastate_changed (
  386. struct amf_su *su, struct amf_comp *comp,
  387. struct amf_csi_assignment *csi_assignment)
  388. {
  389. ENTER("'%s' '%s'", comp->name.value, csi_assignment->csi->name.value);
  390. }
  391. /**
  392. * Determine if the SU is hosted on the local node.
  393. * @param su
  394. *
  395. * @return int
  396. */
  397. int amf_su_is_local (struct amf_su *su)
  398. {
  399. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  400. return 1;
  401. } else {
  402. return 0;
  403. }
  404. }
  405. /**
  406. * Called by a component to report a suspected error on a component
  407. * @param su
  408. * @param comp
  409. * @param recommended_recovery
  410. */
  411. void amf_su_comp_error_suspected (
  412. struct amf_su *su,
  413. struct amf_comp *comp,
  414. SaAmfRecommendedRecoveryT recommended_recovery)
  415. {
  416. escalation_policy_cleanup (comp);
  417. }
  418. void amf_su_init (void)
  419. {
  420. log_init ("AMF");
  421. }