amfsu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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, Anders Eriksson, Lars Holm
  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. * - Component/SU restart, SU failover
  13. * - Constructors/destructors
  14. * - Serializers/deserializers
  15. *
  16. * All rights reserved.
  17. *
  18. *
  19. * This software licensed under BSD license, the text of which follows:
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright notice,
  25. * this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  30. * contributors may be used to endorse or promote products derived from this
  31. * software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  37. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  38. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  43. * THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * AMF Service Unit Class Implementation
  46. *
  47. * This file contains functions for handling AMF-service units(SUs). It can be
  48. * viewed as the implementation of the AMF Service Unit class (called SU)
  49. * as described in SAI-Overview-B.02.01. The SA Forum specification
  50. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  51. * and is referred to as 'the spec' below.
  52. *
  53. * The functions in this file are responsible for:
  54. * - instantiating and terminating service units on request
  55. * (considering the dependencies between components described in paragraph
  56. * 3.9.2)
  57. * - creating and deleting CSI-assignment objects between its components and
  58. * CSI-objects upon request
  59. * - receiving error reports from its components and forwarding them to
  60. * appropriate handler (SU or SG or node or cluster)
  61. * - implementing restart of itself and its components (paragraph 3.12.1.2)
  62. * - implementing error escallation level 1 (paragraph 3.12.2.2 in the spec)
  63. * - handling all run time attributes of the AMF SU; all cached
  64. * attributes are stored as variables and sent to the IMM service
  65. * upon the changes described in the specification.
  66. *
  67. * SU contains the following state machines:
  68. * - presence state machine (PRSM)
  69. * - administrative state machine (ADSM) (NOT IN THIS RELEASE)
  70. * - operational state machine (OPSM)
  71. * - readiness state machine (RESM)
  72. * - ha state per service instance (SI)
  73. * - restart control state machine (RCSM)
  74. *
  75. * The presence state machine orders intantiation of its components on request.
  76. * It fully respects the dependency rules between components at instantiation
  77. * such that it orders instantiation simultaneously only of components on the
  78. * same instantiation level. The presence state machine is implemented with
  79. * the states described in the spec and the state transitions are trigged by
  80. * reported state transitions from its contained components according to
  81. * paragraph 3.3.1.1.
  82. *
  83. * The operational state machine is not responsible for any control function.
  84. * It assumes the DISABLED state if an incoming operational state change report
  85. * from a component indicates the component has assumed the DISABLED state.
  86. * Operational state changes are reported to IMM.
  87. *
  88. * The readiness state machine is not used for any control but is updated and
  89. * reported to IMM when it is changed.
  90. *
  91. * The restart control state machine (RCSM) is used to implement level 1 of
  92. * the error escallation polycy described in chapter 3.12.2 of the spec. It
  93. * also implements component restart and service unit restart as described in
  94. * paragraph 3.12.1.2 and 3.12.1.3.
  95. * RCSM contains three composite states.
  96. * Being a composite state means that the state contains substates.
  97. * RCSM composite states are:
  98. * - ESCALLATION_LEVEL (LEVEL_0, LEVEL_1 and LEVEL_2)
  99. * - RESTARTING_COMPONENT (DEACTIVATING, RESTARTING, SETTING and ACTIVATING)
  100. * - RESTARTING_SERVICE_UNIT (DEACTIVATING, TERMINATING, INSTANTIATING,
  101. * and ACTIVATING)
  102. *
  103. * ESCALLATION_LEVEL is a kind of idle state where no actions are performed
  104. * and used only to remember the escallation level. Substate LEVEL_0 indicates
  105. * no escallation. LEVEL_1 indicates that a component restart has been
  106. * executed recently and the escallation timer is still running. At this level
  107. * component restart requests will transition to RESTARTING_COMPONENT but
  108. * if there are too many restart requests before the probation timer expires
  109. * then a transition will be made to LEVEL_2 and the restart request will
  110. * be forwarded to the node instance hosting this component.
  111. * State RESTARTING_SERVICE_UNIT will only be assumed if the node explicitly
  112. * requests the SU to execute a restart of itself (after having evaluated its
  113. * part of the error escallation policy).
  114. *
  115. */
  116. /*
  117. *
  118. */
  119. #include <stdlib.h>
  120. #include <assert.h>
  121. #include <string.h>
  122. #include <errno.h>
  123. #include "amf.h"
  124. #include "util.h"
  125. #include "print.h"
  126. #include "main.h"
  127. /**
  128. * This function only logs since the readiness state is runtime
  129. * calculated.
  130. * @param su
  131. * @param amf_readiness_state
  132. */
  133. static void su_readiness_state_set (struct amf_su *su,
  134. SaAmfReadinessStateT readiness_state)
  135. {
  136. log_printf (LOG_NOTICE, "Setting SU '%s' readiness state: %s\n",
  137. su->name.value, amf_readiness_state (readiness_state));
  138. }
  139. static void clear_ha_state (
  140. struct amf_su *su, struct amf_si_assignment *si_assignment)
  141. {
  142. ENTER ("");
  143. si_assignment->saAmfSISUHAState = 0;
  144. }
  145. static void su_presence_state_set (struct amf_su *su,
  146. SaAmfPresenceStateT presence_state)
  147. {
  148. /*
  149. * Set all SI's confirmed HA state to unknown if uninstantiated
  150. */
  151. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_UNINSTANTIATED) {
  152. amf_su_foreach_si_assignment (su, clear_ha_state);
  153. }
  154. su->saAmfSUPresenceState = presence_state;
  155. log_printf (LOG_NOTICE, "Setting SU '%s' presence state: %s\n",
  156. su->name.value, amf_presence_state (presence_state));
  157. if (su->restart_control_state != SU_RC_RESTART_SU_SETTING) {
  158. amf_sg_su_state_changed (
  159. su->sg, su, SA_AMF_PRESENCE_STATE, presence_state);
  160. }
  161. }
  162. static void su_operational_state_set (struct amf_su *su,
  163. SaAmfOperationalStateT oper_state)
  164. {
  165. struct amf_comp* comp;
  166. su->saAmfSUOperState = oper_state;
  167. log_printf (LOG_NOTICE, "Setting SU '%s' operational state: %s\n",
  168. su->name.value, amf_op_state (oper_state));
  169. if (oper_state == SA_AMF_OPERATIONAL_ENABLED) {
  170. su_readiness_state_set (su, SA_AMF_READINESS_IN_SERVICE);
  171. for (comp = su->comp_head; comp; comp = comp->next) {
  172. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_IN_SERVICE);
  173. }
  174. } else if (oper_state == SA_AMF_OPERATIONAL_DISABLED) {
  175. su_readiness_state_set (su, SA_AMF_READINESS_OUT_OF_SERVICE);
  176. for (comp = su->comp_head; comp; comp = comp->next) {
  177. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_OUT_OF_SERVICE);
  178. }
  179. }
  180. }
  181. static void comp_assign_csi (struct amf_comp *comp, struct amf_csi *csi,
  182. struct amf_si_assignment *si_assignment, SaAmfHAStateT ha_state)
  183. {
  184. struct amf_csi_assignment *csi_assignment;
  185. dprintf (" Creating CSI '%s' to comp '%s' with hastate %s\n",
  186. getSaNameT (&csi->name), getSaNameT (&comp->name),
  187. amf_ha_state (ha_state));
  188. csi_assignment = amf_malloc (sizeof (struct amf_csi_assignment));
  189. csi_assignment->next = csi->assigned_csis;
  190. csi->assigned_csis = csi_assignment;
  191. amf_comp_dn_make (comp, &csi_assignment->name);
  192. csi_assignment->comp = comp;
  193. csi_assignment->csi = csi;
  194. csi_assignment->saAmfCSICompHAState = 0; /* undefined confirmed HA state */
  195. csi_assignment->requested_ha_state = ha_state;
  196. csi_assignment->si_assignment = si_assignment;
  197. }
  198. static void su_restart (struct amf_su *su)
  199. {
  200. struct amf_comp *comp;
  201. SaNameT dn;
  202. ENTER ("'%s'", su->name.value);
  203. amf_su_dn_make (su, &dn);
  204. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  205. "action: SU restart", dn.value);
  206. su->restart_control_state = SU_RC_RESTART_SU_DEACTIVATING;
  207. su->restart_control_state = SU_RC_RESTART_SU_INSTANTIATING;
  208. su->escalation_level_history_state =
  209. SU_RC_ESCALATION_LEVEL_2;
  210. su->saAmfSURestartCount += 1;
  211. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  212. amf_comp_restart (comp);
  213. }
  214. }
  215. static void comp_restart (struct amf_comp *comp)
  216. {
  217. SaNameT dn;
  218. ENTER ("'%s'", comp->name.value);
  219. amf_comp_dn_make (comp, &dn);
  220. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  221. "action: Component restart", dn.value);
  222. comp->su->restart_control_state = SU_RC_RESTART_COMP_DEACTIVATING;
  223. comp->su->restart_control_state = SU_RC_RESTART_COMP_RESTARTING;
  224. comp->su->escalation_level_history_state = SU_RC_ESCALATION_LEVEL_1;
  225. amf_comp_restart (comp);
  226. }
  227. static void si_ha_state_assumed_cbfn (
  228. struct amf_si_assignment *si_assignment, int result)
  229. {
  230. struct amf_si_assignment *tmp_si_assignment;
  231. struct amf_comp *comp;
  232. struct amf_csi_assignment *csi_assignment;
  233. int all_confirmed = 1;
  234. ENTER ("");
  235. tmp_si_assignment = amf_su_get_next_si_assignment(si_assignment->su, NULL);
  236. while (tmp_si_assignment != NULL) {
  237. for (comp = tmp_si_assignment->su->comp_head; comp != NULL;
  238. comp = comp->next) {
  239. csi_assignment = amf_comp_get_next_csi_assignment(comp, NULL);
  240. while (csi_assignment != NULL) {
  241. if (csi_assignment->requested_ha_state !=
  242. csi_assignment->saAmfCSICompHAState) {
  243. all_confirmed = 0;
  244. }
  245. csi_assignment = amf_comp_get_next_csi_assignment(
  246. comp, csi_assignment);
  247. }
  248. }
  249. tmp_si_assignment = amf_su_get_next_si_assignment(
  250. si_assignment->su, tmp_si_assignment);
  251. }
  252. if (all_confirmed) {
  253. switch (si_assignment->su->restart_control_state) {
  254. case SU_RC_RESTART_COMP_SETTING:
  255. log_printf (LOG_NOTICE, "Component restart recovery finished");
  256. break;
  257. case SU_RC_RESTART_SU_SETTING:
  258. log_printf (LOG_NOTICE, "SU restart recovery finished");
  259. break;
  260. default:
  261. assert (0);
  262. break;
  263. }
  264. si_assignment->su->restart_control_state =
  265. si_assignment->su->escalation_level_history_state;
  266. }
  267. }
  268. static void reassign_sis(struct amf_su *su)
  269. {
  270. struct amf_si_assignment *si_assignment;
  271. ENTER ("");
  272. si_assignment = amf_su_get_next_si_assignment(su, NULL);
  273. while (si_assignment != NULL) {
  274. si_assignment->saAmfSISUHAState = 0; /* unknown */
  275. amf_si_ha_state_assume (si_assignment, si_ha_state_assumed_cbfn);
  276. si_assignment = amf_su_get_next_si_assignment(su, si_assignment);
  277. }
  278. }
  279. static void su_comp_presence_state_changed (
  280. struct amf_su *su, struct amf_comp *comp, int state)
  281. {
  282. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  283. switch (state) {
  284. case SA_AMF_PRESENCE_INSTANTIATED:
  285. switch (su->restart_control_state) {
  286. case SU_RC_ESCALATION_LEVEL_2:
  287. /*
  288. * TODO: send to node
  289. */
  290. case SU_RC_ESCALATION_LEVEL_0:
  291. if (amf_su_presence_state_all_comps_in_su_are_set (
  292. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  293. su_presence_state_set (
  294. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  295. }
  296. break;
  297. case SU_RC_RESTART_COMP_RESTARTING:
  298. su->restart_control_state = SU_RC_RESTART_COMP_SETTING;
  299. reassign_sis (comp->su);
  300. break;
  301. case SU_RC_RESTART_SU_INSTANTIATING:
  302. if (amf_su_presence_state_all_comps_in_su_are_set (
  303. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  304. su->restart_control_state = SU_RC_RESTART_SU_SETTING;
  305. su_presence_state_set (
  306. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  307. reassign_sis (comp->su);
  308. }
  309. break;
  310. default:
  311. dprintf ("state %d", su->restart_control_state);
  312. assert (0);
  313. break;
  314. }
  315. break;
  316. case SA_AMF_PRESENCE_UNINSTANTIATED:
  317. if (amf_su_presence_state_all_comps_in_su_are_set (
  318. su, SA_AMF_PRESENCE_UNINSTANTIATED)) {
  319. su_presence_state_set (comp->su,SA_AMF_PRESENCE_UNINSTANTIATED);
  320. }
  321. break;
  322. case SA_AMF_PRESENCE_INSTANTIATING:
  323. su_presence_state_set (comp->su,SA_AMF_PRESENCE_INSTANTIATING);
  324. break;
  325. case SA_AMF_PRESENCE_RESTARTING:
  326. break;
  327. case SA_AMF_PRESENCE_TERMINATING:
  328. break;
  329. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  330. su_presence_state_set (
  331. comp->su, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  332. break;
  333. default:
  334. assert (0);
  335. break;
  336. }
  337. }
  338. static void su_comp_op_state_changed (
  339. struct amf_su *su, struct amf_comp *comp, int state)
  340. {
  341. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  342. switch (state) {
  343. case SA_AMF_OPERATIONAL_ENABLED:
  344. {
  345. struct amf_comp *comp_compare;
  346. int all_set = 1;
  347. for (comp_compare = comp->su->comp_head;
  348. comp_compare != NULL; comp_compare = comp_compare->next) {
  349. if (comp_compare->saAmfCompOperState !=
  350. SA_AMF_OPERATIONAL_ENABLED) {
  351. all_set = 0;
  352. break;
  353. }
  354. }
  355. if (all_set) {
  356. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  357. } else {
  358. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  359. }
  360. break;
  361. }
  362. case SA_AMF_OPERATIONAL_DISABLED:
  363. break;
  364. default:
  365. assert (0);
  366. break;
  367. }
  368. }
  369. int amf_su_presence_state_all_comps_in_su_are_set (struct amf_su *su,
  370. SaAmfPresenceStateT state)
  371. {
  372. int all_set = 1;
  373. struct amf_comp *comp;
  374. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  375. if (comp->saAmfCompPresenceState != state) {
  376. all_set = 0;
  377. }
  378. }
  379. return all_set;
  380. }
  381. void amf_su_instantiate (struct amf_su *su)
  382. {
  383. struct amf_comp *comp;
  384. ENTER ("'%s'", su->name.value);
  385. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  386. amf_comp_instantiate (comp);
  387. }
  388. }
  389. amf_si_assignment_t *amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  390. SaAmfHAStateT ha_state)
  391. {
  392. struct amf_si_assignment *si_assignment;
  393. dprintf ("Creating SI '%s' to SU '%s' with hastate %s\n",
  394. getSaNameT (&si->name), getSaNameT (&su->name),
  395. amf_ha_state (ha_state));
  396. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  397. amf_su_dn_make (su, &si_assignment->name);
  398. si_assignment->saAmfSISUHAState = 0; /* undefined confirmed HA state */
  399. si_assignment->requested_ha_state = ha_state;
  400. si_assignment->next = si->assigned_sis;
  401. si->assigned_sis = si_assignment;
  402. si_assignment->si = si;
  403. si_assignment->su = su;
  404. {
  405. struct amf_csi *csi;
  406. struct amf_comp *comp;
  407. SaNameT *cs_type;
  408. int i;
  409. /*
  410. ** for each component in SU, find a CSI in the SI with the same type
  411. */
  412. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  413. int no_of_cs_types = 0;
  414. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  415. cs_type = comp->saAmfCompCsTypes[i];
  416. no_of_cs_types++;
  417. int no_of_assignments = 0;
  418. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  419. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value,
  420. cs_type->length)) {
  421. comp_assign_csi (comp, csi, si_assignment, ha_state);
  422. no_of_assignments++;
  423. }
  424. }
  425. if (no_of_assignments == 0) {
  426. log_printf (
  427. LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  428. getSaNameT (cs_type));
  429. }
  430. }
  431. if (no_of_cs_types == 0) {
  432. log_printf (LOG_LEVEL_ERROR,
  433. "\t No CS types configured for comp %s ?!!\n",
  434. getSaNameT (&comp->name));
  435. }
  436. }
  437. }
  438. return si_assignment;
  439. }
  440. /**
  441. * Used by a component to report a state change event
  442. * @param su
  443. * @param comp
  444. * @param type type of state
  445. * @param state new state
  446. */
  447. void amf_su_comp_state_changed (
  448. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  449. {
  450. switch (type) {
  451. case SA_AMF_PRESENCE_STATE:
  452. su_comp_presence_state_changed (su, comp, state);
  453. break;
  454. case SA_AMF_OP_STATE:
  455. su_comp_op_state_changed (su, comp, state);
  456. break;
  457. default:
  458. assert (0);
  459. break;
  460. }
  461. }
  462. /**
  463. * Determine if the SU is hosted on the local node.
  464. * @param su
  465. *
  466. * @return int
  467. */
  468. int amf_su_is_local (struct amf_su *su)
  469. {
  470. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  471. return 1;
  472. } else {
  473. return 0;
  474. }
  475. }
  476. /**
  477. * Called by a component to report a suspected error on a component
  478. * @param su
  479. * @param comp
  480. * @param recommended_recovery
  481. */
  482. void amf_su_comp_error_suspected (
  483. struct amf_su *su,
  484. struct amf_comp *comp,
  485. SaAmfRecommendedRecoveryT recommended_recovery)
  486. {
  487. ENTER ("Comp '%s', SU '%s'", comp->name.value, su->name.value);
  488. switch (su->restart_control_state) {
  489. case SU_RC_ESCALATION_LEVEL_0:
  490. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  491. su->restart_control_state = SU_RC_ESCALATION_LEVEL_1;
  492. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  493. } else {
  494. comp_restart (comp);
  495. }
  496. break;
  497. case SU_RC_ESCALATION_LEVEL_1:
  498. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  499. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  500. su->restart_control_state = SU_RC_ESCALATION_LEVEL_2;
  501. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  502. } else {
  503. su_restart (comp->su);
  504. }
  505. } else {
  506. comp_restart (comp);
  507. }
  508. break;
  509. case SU_RC_ESCALATION_LEVEL_2:
  510. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  511. /*
  512. * TODO: delegate to node
  513. */
  514. SaNameT dn;
  515. su_operational_state_set (su, SA_AMF_OPERATIONAL_DISABLED);
  516. amf_comp_operational_state_set (
  517. comp, SA_AMF_OPERATIONAL_DISABLED);
  518. amf_comp_dn_make (comp, &dn);
  519. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  520. "action:\n\t\tSU failover", dn.value);
  521. amf_sg_failover_su_req (comp->su->sg, comp->su, this_amf_node);
  522. return;
  523. } else {
  524. su_restart (comp->su);
  525. }
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. void amf_su_init (void)
  532. {
  533. log_init ("AMF");
  534. }
  535. void amf_su_terminate (struct amf_su *su)
  536. {
  537. struct amf_comp *comp;
  538. ENTER ("'%s'", su->name.value);
  539. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  540. /*
  541. * Terminate all components in SU abruptly
  542. */
  543. comp->error_suspected = 1;
  544. amf_comp_terminate (comp);
  545. }
  546. }
  547. char *amf_su_dn_make (struct amf_su *su, SaNameT *name)
  548. {
  549. int i;
  550. assert (su != NULL);
  551. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  552. "safSu=%s,safSg=%s,safApp=%s",
  553. su->name.value, su->sg->name.value, su->sg->application->name.value);
  554. assert (i <= SA_MAX_NAME_LENGTH);
  555. name->length = i;
  556. return (char *)name->value;
  557. }
  558. struct amf_si_assignment *amf_su_get_next_si_assignment (
  559. struct amf_su *su, const struct amf_si_assignment *si_assignment)
  560. {
  561. struct amf_si *si;
  562. struct amf_si_assignment *tmp_si_assignment;
  563. SaNameT dn;
  564. amf_su_dn_make (su, &dn);
  565. if (si_assignment == NULL) {
  566. assert (su->sg);
  567. assert (su->sg->application);
  568. assert (su->sg->application->si_head);
  569. si = su->sg->application->si_head;
  570. tmp_si_assignment = si->assigned_sis;
  571. } else {
  572. tmp_si_assignment = si_assignment->next;
  573. if (tmp_si_assignment == NULL) {
  574. si = si_assignment->si->next;
  575. if (si == NULL) {
  576. return NULL;
  577. } else {
  578. tmp_si_assignment = si->assigned_sis;
  579. }
  580. } else {
  581. si = tmp_si_assignment->si;
  582. }
  583. }
  584. for (; si != NULL; si = si->next) {
  585. if (tmp_si_assignment == NULL && si != NULL) {
  586. tmp_si_assignment = si->assigned_sis;
  587. }
  588. for (; tmp_si_assignment != NULL;
  589. tmp_si_assignment = tmp_si_assignment->next) {
  590. if (name_match (&tmp_si_assignment->name, &dn)) {
  591. return tmp_si_assignment;
  592. }
  593. }
  594. }
  595. return NULL;
  596. }
  597. void amf_su_foreach_si_assignment (
  598. struct amf_su *su,
  599. void (*foreach_fn)(struct amf_su *su,
  600. struct amf_si_assignment *si_assignment))
  601. {
  602. struct amf_si_assignment *si_assignment;
  603. assert (foreach_fn != NULL);
  604. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  605. while (si_assignment != NULL) {
  606. foreach_fn (su, si_assignment);
  607. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  608. }
  609. }
  610. int amf_su_get_saAmfSUNumCurrActiveSIs(struct amf_su *su)
  611. {
  612. int cnt = 0;
  613. struct amf_si_assignment *si_assignment;
  614. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  615. while (si_assignment != NULL) {
  616. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  617. si_assignment->requested_ha_state == SA_AMF_HA_ACTIVE) {
  618. cnt++;
  619. } else {
  620. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  621. cnt++;
  622. }
  623. }
  624. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  625. }
  626. return cnt;
  627. }
  628. int amf_su_get_saAmfSUNumCurrStandbySIs(struct amf_su *su)
  629. {
  630. int cnt = 0;
  631. struct amf_si_assignment *si_assignment;
  632. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  633. while (si_assignment != NULL) {
  634. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  635. si_assignment->requested_ha_state == SA_AMF_HA_STANDBY) {
  636. cnt++;
  637. } else {
  638. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  639. cnt++;
  640. }
  641. }
  642. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  643. }
  644. return cnt;
  645. }
  646. SaAmfReadinessStateT amf_su_get_saAmfSUReadinessState (struct amf_su *su)
  647. {
  648. if ((su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) &&
  649. ((su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATED) ||
  650. (su->saAmfSUPresenceState == SA_AMF_PRESENCE_RESTARTING))) {
  651. return SA_AMF_READINESS_IN_SERVICE;
  652. } else if (su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) {
  653. return SA_AMF_READINESS_STOPPING;
  654. } else {
  655. return SA_AMF_READINESS_OUT_OF_SERVICE;
  656. }
  657. }
  658. /**
  659. * Constructor for SU objects. Adds SU last in the ordered
  660. * list owned by the specified SG. Always returns a
  661. * valid SU object, out-of-memory problems are handled here.
  662. * Default values are initialized.
  663. * @param sg
  664. * @param name
  665. *
  666. * @return struct amf_su*
  667. */
  668. struct amf_su *amf_su_new (struct amf_sg *sg, char *name)
  669. {
  670. struct amf_su *tail = sg->su_head;
  671. struct amf_su *su = amf_calloc (1, sizeof (struct amf_su));
  672. while (tail != NULL) {
  673. if (tail->next == NULL) {
  674. break;
  675. }
  676. tail = tail->next;
  677. }
  678. if (tail == NULL) {
  679. sg->su_head = su;
  680. } else {
  681. tail->next = su;
  682. }
  683. su->sg = sg;
  684. /* setup default values from spec. */
  685. su->saAmfSURank = 0;
  686. su->saAmfSUIsExternal = 0;
  687. su->saAmfSUFailover = 1;
  688. su->saAmfSUAdminState = SA_AMF_ADMIN_UNLOCKED;
  689. su->saAmfSUOperState = SA_AMF_OPERATIONAL_DISABLED;
  690. su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  691. su->restart_control_state = SU_RC_ESCALATION_LEVEL_0;
  692. setSaNameT (&su->name, name);
  693. return su;
  694. }
  695. void amf_su_delete (struct amf_su *su)
  696. {
  697. struct amf_comp *comp;
  698. for (comp = su->comp_head; comp != NULL;) {
  699. struct amf_comp *tmp = comp;
  700. comp = comp->next;
  701. amf_comp_delete (tmp);
  702. }
  703. free (su);
  704. }
  705. void *amf_su_serialize (struct amf_su *su, int *len)
  706. {
  707. char *buf = NULL;
  708. int offset = 0, size = 0;
  709. TRACE8 ("%s", su->name.value);
  710. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->name);
  711. buf = amf_serialize_SaUint32T (buf, &size, &offset, su->saAmfSURank);
  712. buf = amf_serialize_SaUint32T (
  713. buf, &size, &offset, su->saAmfSUNumComponents);
  714. buf = amf_serialize_SaUint32T (
  715. buf, &size, &offset, su->saAmfSUIsExternal);
  716. buf = amf_serialize_SaUint32T (
  717. buf, &size, &offset, su->saAmfSUFailover);
  718. buf = amf_serialize_SaUint32T (
  719. buf, &size, &offset, su->saAmfSUPreInstantiable);
  720. buf = amf_serialize_SaUint32T (
  721. buf, &size, &offset, su->saAmfSUOperState);
  722. buf = amf_serialize_SaUint32T (
  723. buf, &size, &offset, su->saAmfSUAdminState);
  724. buf = amf_serialize_SaUint32T (
  725. buf, &size, &offset, su->saAmfSUPresenceState);
  726. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->saAmfSUHostedByNode);
  727. buf = amf_serialize_SaUint32T (
  728. buf, &size, &offset, su->saAmfSURestartCount);
  729. buf = amf_serialize_SaUint32T (
  730. buf, &size, &offset, su->restart_control_state);
  731. buf = amf_serialize_SaUint32T (
  732. buf, &size, &offset, su->escalation_level_history_state);
  733. buf = amf_serialize_SaStringT (
  734. buf, &size, &offset, su->clccli_path);
  735. buf = amf_serialize_SaUint32T (
  736. buf, &size, &offset, su->su_failover_cnt);
  737. *len = offset;
  738. return buf;
  739. }
  740. struct amf_su *amf_su_deserialize (struct amf_sg *sg, char *buf)
  741. {
  742. char *tmp = buf;
  743. struct amf_su *su = amf_su_new (sg, "");
  744. tmp = amf_deserialize_SaNameT (tmp, &su->name);
  745. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURank);
  746. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUNumComponents);
  747. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUIsExternal);
  748. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUFailover);
  749. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPreInstantiable);
  750. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUOperState);
  751. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUAdminState);
  752. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPresenceState);
  753. tmp = amf_deserialize_SaNameT (tmp, &su->saAmfSUHostedByNode);
  754. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURestartCount);
  755. tmp = amf_deserialize_SaUint32T (tmp, &su->restart_control_state);
  756. tmp = amf_deserialize_SaUint32T (tmp, &su->escalation_level_history_state);
  757. tmp = amf_deserialize_SaStringT (tmp, &su->clccli_path);
  758. tmp = amf_deserialize_SaUint32T (tmp, &su->su_failover_cnt);
  759. return su;
  760. }
  761. struct amf_su *amf_su_find (struct amf_cluster *cluster, SaNameT *name)
  762. {
  763. struct amf_application *app;
  764. struct amf_sg *sg;
  765. struct amf_su *su = NULL;
  766. char *app_name;
  767. char *sg_name;
  768. char *su_name;
  769. char *ptrptr;
  770. char *buf;
  771. assert (cluster != NULL && name != NULL);
  772. /* malloc new buffer since strtok_r writes to its first argument */
  773. buf = amf_malloc (name->length + 1);
  774. memcpy (buf, name->value, name->length + 1);
  775. su_name = strtok_r(buf, ",", &ptrptr);
  776. sg_name = strtok_r(NULL, ",", &ptrptr);
  777. app_name = strtok_r(NULL, ",", &ptrptr);
  778. if (su_name == NULL || sg_name == NULL || app_name == NULL) {
  779. goto end;
  780. }
  781. su_name += 6;
  782. sg_name += 6;
  783. app_name += 7;
  784. app = amf_application_find (cluster, app_name);
  785. if (app == NULL) {
  786. goto end;
  787. }
  788. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  789. if (strncmp (sg_name, (char*)sg->name.value,
  790. sg->name.length) == 0) {
  791. for (su = sg->su_head; su != NULL; su = su->next) {
  792. if (su->name.length == strlen(su_name) &&
  793. strncmp (su_name, (char*)su->name.value,
  794. su->name.length) == 0) {
  795. goto end;
  796. }
  797. }
  798. }
  799. }
  800. end:
  801. free (buf);
  802. return su;
  803. }