amfsu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. int amf_su_presence_state_all_comps_in_su_are_set (struct amf_su *su,
  128. SaAmfPresenceStateT state)
  129. {
  130. int all_set = 1;
  131. struct amf_comp *comp;
  132. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  133. if (comp->saAmfCompPresenceState != state) {
  134. all_set = 0;
  135. }
  136. }
  137. return all_set;
  138. }
  139. /**
  140. * This function only logs since the readiness state is runtime
  141. * calculated.
  142. * @param su
  143. * @param amf_readiness_state
  144. */
  145. static void su_readiness_state_set (struct amf_su *su,
  146. SaAmfReadinessStateT readiness_state)
  147. {
  148. log_printf (LOG_NOTICE, "Setting SU '%s' readiness state: %s\n",
  149. su->name.value, amf_readiness_state (readiness_state));
  150. }
  151. static void clear_ha_state (
  152. struct amf_su *su, struct amf_si_assignment *si_assignment)
  153. {
  154. ENTER ("");
  155. si_assignment->saAmfSISUHAState = 0;
  156. }
  157. static void su_presence_state_set (struct amf_su *su,
  158. SaAmfPresenceStateT presence_state)
  159. {
  160. /*
  161. * Set all SI's confirmed HA state to unknown if uninstantiated
  162. */
  163. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_UNINSTANTIATED) {
  164. amf_su_foreach_si_assignment (su, clear_ha_state);
  165. }
  166. su->saAmfSUPresenceState = presence_state;
  167. log_printf (LOG_NOTICE, "Setting SU '%s' presence state: %s\n",
  168. su->name.value, amf_presence_state (presence_state));
  169. if (su->restart_control_state != SU_RC_RESTART_SU_SETTING) {
  170. amf_sg_su_state_changed (
  171. su->sg, su, SA_AMF_PRESENCE_STATE, presence_state);
  172. }
  173. }
  174. static void su_operational_state_set (struct amf_su *su,
  175. SaAmfOperationalStateT oper_state)
  176. {
  177. struct amf_comp* comp;
  178. su->saAmfSUOperState = oper_state;
  179. log_printf (LOG_NOTICE, "Setting SU '%s' operational state: %s\n",
  180. su->name.value, amf_op_state (oper_state));
  181. if (oper_state == SA_AMF_OPERATIONAL_ENABLED) {
  182. su_readiness_state_set (su, SA_AMF_READINESS_IN_SERVICE);
  183. for (comp = su->comp_head; comp; comp = comp->next) {
  184. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_IN_SERVICE);
  185. }
  186. } else if (oper_state == SA_AMF_OPERATIONAL_DISABLED) {
  187. su_readiness_state_set (su, SA_AMF_READINESS_OUT_OF_SERVICE);
  188. for (comp = su->comp_head; comp; comp = comp->next) {
  189. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_OUT_OF_SERVICE);
  190. }
  191. }
  192. }
  193. static void comp_assign_csi (struct amf_comp *comp, struct amf_csi *csi,
  194. struct amf_si_assignment *si_assignment, SaAmfHAStateT ha_state)
  195. {
  196. struct amf_csi_assignment *csi_assignment;
  197. dprintf (" Creating CSI '%s' to comp '%s' with hastate %s\n",
  198. getSaNameT (&csi->name), getSaNameT (&comp->name),
  199. amf_ha_state (ha_state));
  200. csi_assignment = amf_malloc (sizeof (struct amf_csi_assignment));
  201. csi_assignment->next = csi->assigned_csis;
  202. csi->assigned_csis = csi_assignment;
  203. amf_comp_dn_make (comp, &csi_assignment->name);
  204. csi_assignment->comp = comp;
  205. csi_assignment->csi = csi;
  206. csi_assignment->saAmfCSICompHAState = 0; /* undefined confirmed HA state */
  207. csi_assignment->requested_ha_state = ha_state;
  208. csi_assignment->si_assignment = si_assignment;
  209. }
  210. static void su_restart (struct amf_su *su)
  211. {
  212. struct amf_comp *comp;
  213. SaNameT dn;
  214. ENTER ("'%s'", su->name.value);
  215. amf_su_dn_make (su, &dn);
  216. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  217. "action: SU restart", dn.value);
  218. su->restart_control_state = SU_RC_RESTART_SU_DEACTIVATING;
  219. su->restart_control_state = SU_RC_RESTART_SU_INSTANTIATING;
  220. su->escalation_level_history_state =
  221. SU_RC_ESCALATION_LEVEL_2;
  222. su->saAmfSURestartCount += 1;
  223. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  224. amf_comp_restart (comp);
  225. }
  226. }
  227. static void comp_restart (struct amf_comp *comp)
  228. {
  229. SaNameT dn;
  230. ENTER ("'%s'", comp->name.value);
  231. amf_comp_dn_make (comp, &dn);
  232. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  233. "action: Component restart", dn.value);
  234. comp->su->restart_control_state = SU_RC_RESTART_COMP_DEACTIVATING;
  235. comp->su->restart_control_state = SU_RC_RESTART_COMP_RESTARTING;
  236. comp->su->escalation_level_history_state = SU_RC_ESCALATION_LEVEL_1;
  237. amf_comp_restart (comp);
  238. }
  239. void amf_su_instantiate (struct amf_su *su)
  240. {
  241. struct amf_comp *comp;
  242. ENTER ("'%s'", su->name.value);
  243. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  244. amf_comp_instantiate (comp);
  245. }
  246. }
  247. void amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  248. SaAmfHAStateT ha_state)
  249. {
  250. struct amf_si_assignment *si_assignment;
  251. dprintf ("Creating SI '%s' to SU '%s' with hastate %s\n",
  252. getSaNameT (&si->name), getSaNameT (&su->name),
  253. amf_ha_state (ha_state));
  254. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  255. amf_su_dn_make (su, &si_assignment->name);
  256. si_assignment->saAmfSISUHAState = 0; /* undefined confirmed HA state */
  257. si_assignment->requested_ha_state = ha_state;
  258. si_assignment->next = si->assigned_sis;
  259. si->assigned_sis = si_assignment;
  260. si_assignment->si = si;
  261. si_assignment->su = su;
  262. {
  263. struct amf_csi *csi;
  264. struct amf_comp *comp;
  265. SaNameT *cs_type;
  266. int i;
  267. /*
  268. ** for each component in SU, find a CSI in the SI with the same type
  269. */
  270. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  271. int no_of_cs_types = 0;
  272. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  273. cs_type = comp->saAmfCompCsTypes[i];
  274. no_of_cs_types++;
  275. int no_of_assignments = 0;
  276. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  277. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value,
  278. cs_type->length)) {
  279. comp_assign_csi (comp, csi, si_assignment, ha_state);
  280. no_of_assignments++;
  281. }
  282. }
  283. if (no_of_assignments == 0) {
  284. log_printf (
  285. LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  286. getSaNameT (cs_type));
  287. }
  288. }
  289. if (no_of_cs_types == 0) {
  290. log_printf (LOG_LEVEL_ERROR,
  291. "\t No CS types configured for comp %s ?!!\n",
  292. getSaNameT (&comp->name));
  293. }
  294. }
  295. }
  296. }
  297. static void si_ha_state_assumed_cbfn (
  298. struct amf_si_assignment *si_assignment, int result)
  299. {
  300. struct amf_si_assignment *tmp_si_assignment;
  301. struct amf_comp *comp;
  302. struct amf_csi_assignment *csi_assignment;
  303. int all_confirmed = 1;
  304. ENTER ("");
  305. tmp_si_assignment = amf_su_get_next_si_assignment(si_assignment->su, NULL);
  306. while (tmp_si_assignment != NULL) {
  307. for (comp = tmp_si_assignment->su->comp_head; comp != NULL;
  308. comp = comp->next) {
  309. csi_assignment = amf_comp_get_next_csi_assignment(comp, NULL);
  310. while (csi_assignment != NULL) {
  311. if (csi_assignment->requested_ha_state !=
  312. csi_assignment->saAmfCSICompHAState) {
  313. all_confirmed = 0;
  314. }
  315. csi_assignment = amf_comp_get_next_csi_assignment(
  316. comp, csi_assignment);
  317. }
  318. }
  319. tmp_si_assignment = amf_su_get_next_si_assignment(
  320. si_assignment->su, tmp_si_assignment);
  321. }
  322. if (all_confirmed) {
  323. switch (si_assignment->su->restart_control_state) {
  324. case SU_RC_RESTART_COMP_SETTING:
  325. log_printf (LOG_NOTICE, "Component restart recovery finished");
  326. break;
  327. case SU_RC_RESTART_SU_SETTING:
  328. log_printf (LOG_NOTICE, "SU restart recovery finished");
  329. break;
  330. default:
  331. assert (0);
  332. }
  333. si_assignment->su->restart_control_state =
  334. si_assignment->su->escalation_level_history_state;
  335. }
  336. }
  337. static void reassign_sis(struct amf_su *su)
  338. {
  339. struct amf_si_assignment *si_assignment;
  340. ENTER ("");
  341. si_assignment = amf_su_get_next_si_assignment(su, NULL);
  342. while (si_assignment != NULL) {
  343. si_assignment->saAmfSISUHAState = 0; /* unknown */
  344. amf_si_ha_state_assume (si_assignment, si_ha_state_assumed_cbfn);
  345. si_assignment = amf_su_get_next_si_assignment(su, si_assignment);
  346. }
  347. }
  348. static void su_comp_presence_state_changed (
  349. struct amf_su *su, struct amf_comp *comp, int state)
  350. {
  351. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  352. switch (state) {
  353. case SA_AMF_PRESENCE_INSTANTIATED:
  354. switch (su->restart_control_state) {
  355. case SU_RC_ESCALATION_LEVEL_2:
  356. /*
  357. * TODO: send to node
  358. */
  359. case SU_RC_ESCALATION_LEVEL_0:
  360. if (amf_su_presence_state_all_comps_in_su_are_set (
  361. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  362. su_presence_state_set (
  363. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  364. }
  365. break;
  366. case SU_RC_RESTART_COMP_RESTARTING:
  367. su->restart_control_state = SU_RC_RESTART_COMP_SETTING;
  368. reassign_sis (comp->su);
  369. break;
  370. case SU_RC_RESTART_SU_INSTANTIATING:
  371. if (amf_su_presence_state_all_comps_in_su_are_set (
  372. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  373. su->restart_control_state = SU_RC_RESTART_SU_SETTING;
  374. su_presence_state_set (
  375. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  376. reassign_sis (comp->su);
  377. }
  378. break;
  379. default:
  380. dprintf ("state %d", su->restart_control_state);
  381. assert (0);
  382. }
  383. break;
  384. case SA_AMF_PRESENCE_UNINSTANTIATED:
  385. if (amf_su_presence_state_all_comps_in_su_are_set (
  386. su, SA_AMF_PRESENCE_UNINSTANTIATED)) {
  387. su_presence_state_set (comp->su,
  388. SA_AMF_PRESENCE_UNINSTANTIATED);
  389. }
  390. break;
  391. case SA_AMF_PRESENCE_INSTANTIATING:
  392. break;
  393. case SA_AMF_PRESENCE_RESTARTING:
  394. break;
  395. case SA_AMF_PRESENCE_TERMINATING:
  396. break;
  397. default:
  398. assert (0);
  399. }
  400. }
  401. static void su_comp_op_state_changed (
  402. struct amf_su *su, struct amf_comp *comp, int state)
  403. {
  404. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  405. switch (state) {
  406. case SA_AMF_OPERATIONAL_ENABLED:
  407. {
  408. struct amf_comp *comp_compare;
  409. int all_set = 1;
  410. for (comp_compare = comp->su->comp_head;
  411. comp_compare != NULL; comp_compare = comp_compare->next) {
  412. if (comp_compare->saAmfCompOperState !=
  413. SA_AMF_OPERATIONAL_ENABLED) {
  414. all_set = 0;
  415. break;
  416. }
  417. }
  418. if (all_set) {
  419. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  420. } else {
  421. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  422. }
  423. break;
  424. }
  425. case SA_AMF_OPERATIONAL_DISABLED:
  426. break;
  427. default:
  428. assert (0);
  429. }
  430. }
  431. /**
  432. * Used by a component to report a state change event
  433. * @param su
  434. * @param comp
  435. * @param type type of state
  436. * @param state new state
  437. */
  438. void amf_su_comp_state_changed (
  439. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  440. {
  441. switch (type) {
  442. case SA_AMF_PRESENCE_STATE:
  443. su_comp_presence_state_changed (su, comp, state);
  444. break;
  445. case SA_AMF_OP_STATE:
  446. su_comp_op_state_changed (su, comp, state);
  447. break;
  448. default:
  449. assert (0);
  450. }
  451. }
  452. /**
  453. * Determine if the SU is hosted on the local node.
  454. * @param su
  455. *
  456. * @return int
  457. */
  458. int amf_su_is_local (struct amf_su *su)
  459. {
  460. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  461. return 1;
  462. } else {
  463. return 0;
  464. }
  465. }
  466. /**
  467. * Called by a component to report a suspected error on a component
  468. * @param su
  469. * @param comp
  470. * @param recommended_recovery
  471. */
  472. void amf_su_comp_error_suspected (
  473. struct amf_su *su,
  474. struct amf_comp *comp,
  475. SaAmfRecommendedRecoveryT recommended_recovery)
  476. {
  477. ENTER ("Comp '%s', SU '%s'", comp->name.value, su->name.value);
  478. switch (su->restart_control_state) {
  479. case SU_RC_ESCALATION_LEVEL_0:
  480. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  481. su->restart_control_state = SU_RC_ESCALATION_LEVEL_1;
  482. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  483. } else {
  484. comp_restart (comp);
  485. }
  486. break;
  487. case SU_RC_ESCALATION_LEVEL_1:
  488. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  489. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  490. su->restart_control_state = SU_RC_ESCALATION_LEVEL_2;
  491. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  492. } else {
  493. su_restart (comp->su);
  494. }
  495. } else {
  496. comp_restart (comp);
  497. }
  498. break;
  499. case SU_RC_ESCALATION_LEVEL_2:
  500. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  501. /*
  502. * TODO: delegate to node
  503. */
  504. SaNameT dn;
  505. su_operational_state_set (su, SA_AMF_OPERATIONAL_DISABLED);
  506. amf_comp_operational_state_set (
  507. comp, SA_AMF_OPERATIONAL_DISABLED);
  508. amf_comp_dn_make (comp, &dn);
  509. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  510. "action:\n\t\tSU failover", dn.value);
  511. amf_sg_failover_su_req (comp->su->sg, comp->su, this_amf_node);
  512. return;
  513. } else {
  514. su_restart (comp->su);
  515. }
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. void amf_su_init (void)
  522. {
  523. log_init ("AMF");
  524. }
  525. void amf_su_terminate (struct amf_su *su)
  526. {
  527. struct amf_comp *comp;
  528. ENTER ("'%s'", su->name.value);
  529. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  530. /*
  531. * Terminate all components in SU abruptly
  532. */
  533. comp->error_suspected = 1;
  534. amf_comp_terminate (comp);
  535. }
  536. }
  537. char *amf_su_dn_make (struct amf_su *su, SaNameT *name)
  538. {
  539. int i;
  540. assert (su != NULL);
  541. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  542. "safSu=%s,safSg=%s,safApp=%s",
  543. su->name.value, su->sg->name.value, su->sg->application->name.value);
  544. assert (i <= SA_MAX_NAME_LENGTH);
  545. name->length = i;
  546. return (char *)name->value;
  547. }
  548. struct amf_si_assignment *amf_su_get_next_si_assignment (
  549. struct amf_su *su, const struct amf_si_assignment *si_assignment)
  550. {
  551. struct amf_si *si;
  552. struct amf_si_assignment *tmp_si_assignment;
  553. SaNameT dn;
  554. amf_su_dn_make (su, &dn);
  555. if (si_assignment == NULL) {
  556. assert (su->sg);
  557. assert (su->sg->application);
  558. assert (su->sg->application->si_head);
  559. si = su->sg->application->si_head;
  560. tmp_si_assignment = si->assigned_sis;
  561. } else {
  562. tmp_si_assignment = si_assignment->next;
  563. if (tmp_si_assignment == NULL) {
  564. si = si_assignment->si->next;
  565. if (si == NULL) {
  566. return NULL;
  567. } else {
  568. tmp_si_assignment = si->assigned_sis;
  569. }
  570. } else {
  571. si = tmp_si_assignment->si;
  572. }
  573. }
  574. for (; si != NULL; si = si->next) {
  575. if (tmp_si_assignment == NULL && si != NULL) {
  576. tmp_si_assignment = si->assigned_sis;
  577. }
  578. for (; tmp_si_assignment != NULL;
  579. tmp_si_assignment = tmp_si_assignment->next) {
  580. if (name_match (&tmp_si_assignment->name, &dn)) {
  581. return tmp_si_assignment;
  582. }
  583. }
  584. }
  585. return NULL;
  586. }
  587. void amf_su_foreach_si_assignment (
  588. struct amf_su *su,
  589. void (*foreach_fn)(struct amf_su *su,
  590. struct amf_si_assignment *si_assignment))
  591. {
  592. struct amf_si_assignment *si_assignment;
  593. assert (foreach_fn != NULL);
  594. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  595. while (si_assignment != NULL) {
  596. foreach_fn (su, si_assignment);
  597. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  598. }
  599. }
  600. int amf_su_get_saAmfSUNumCurrActiveSIs(struct amf_su *su)
  601. {
  602. int cnt = 0;
  603. struct amf_si_assignment *si_assignment;
  604. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  605. while (si_assignment != NULL) {
  606. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  607. si_assignment->requested_ha_state == SA_AMF_HA_ACTIVE) {
  608. cnt++;
  609. } else {
  610. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  611. cnt++;
  612. }
  613. }
  614. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  615. }
  616. return cnt;
  617. }
  618. int amf_su_get_saAmfSUNumCurrStandbySIs(struct amf_su *su)
  619. {
  620. int cnt = 0;
  621. struct amf_si_assignment *si_assignment;
  622. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  623. while (si_assignment != NULL) {
  624. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  625. si_assignment->requested_ha_state == SA_AMF_HA_STANDBY) {
  626. cnt++;
  627. } else {
  628. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  629. cnt++;
  630. }
  631. }
  632. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  633. }
  634. return cnt;
  635. }
  636. SaAmfReadinessStateT amf_su_get_saAmfSUReadinessState (struct amf_su *su)
  637. {
  638. if ((su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) &&
  639. ((su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATED) ||
  640. (su->saAmfSUPresenceState == SA_AMF_PRESENCE_RESTARTING))) {
  641. return SA_AMF_READINESS_IN_SERVICE;
  642. } else if (su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) {
  643. return SA_AMF_READINESS_STOPPING;
  644. } else {
  645. return SA_AMF_READINESS_OUT_OF_SERVICE;
  646. }
  647. }
  648. /**
  649. * Constructor for SU objects. Adds SU last in the ordered
  650. * list owned by the specified SG. Always returns a
  651. * valid SU object, out-of-memory problems are handled here.
  652. * Default values are initialized.
  653. * @param sg
  654. * @param name
  655. *
  656. * @return struct amf_su*
  657. */
  658. struct amf_su *amf_su_new (struct amf_sg *sg, char *name)
  659. {
  660. struct amf_su *tail = sg->su_head;
  661. struct amf_su *su = calloc (1, sizeof (struct amf_su));
  662. if (su == NULL) {
  663. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  664. }
  665. while (tail != NULL) {
  666. if (tail->next == NULL) {
  667. break;
  668. }
  669. tail = tail->next;
  670. }
  671. if (tail == NULL) {
  672. sg->su_head = su;
  673. } else {
  674. tail->next = su;
  675. }
  676. su->sg = sg;
  677. /* setup default values from spec. */
  678. su->saAmfSURank = 0;
  679. su->saAmfSUIsExternal = 0;
  680. su->saAmfSUFailover = 1;
  681. su->saAmfSUAdminState = SA_AMF_ADMIN_UNLOCKED;
  682. su->saAmfSUOperState = SA_AMF_OPERATIONAL_DISABLED;
  683. su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  684. su->restart_control_state = SU_RC_ESCALATION_LEVEL_0;
  685. setSaNameT (&su->name, name);
  686. return su;
  687. }
  688. void amf_su_delete (struct amf_su *su)
  689. {
  690. struct amf_comp *comp;
  691. for (comp = su->comp_head; comp != NULL;) {
  692. struct amf_comp *tmp = comp;
  693. comp = comp->next;
  694. amf_comp_delete (tmp);
  695. }
  696. free (su);
  697. }
  698. void *amf_su_serialize (struct amf_su *su, int *len)
  699. {
  700. char *buf = NULL;
  701. int offset = 0, size = 0;
  702. TRACE8 ("%s", su->name.value);
  703. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->name);
  704. buf = amf_serialize_SaUint32T (buf, &size, &offset, su->saAmfSURank);
  705. buf = amf_serialize_SaUint32T (
  706. buf, &size, &offset, su->saAmfSUNumComponents);
  707. buf = amf_serialize_SaUint32T (
  708. buf, &size, &offset, su->saAmfSUIsExternal);
  709. buf = amf_serialize_SaUint32T (
  710. buf, &size, &offset, su->saAmfSUFailover);
  711. buf = amf_serialize_SaUint32T (
  712. buf, &size, &offset, su->saAmfSUPreInstantiable);
  713. buf = amf_serialize_SaUint32T (
  714. buf, &size, &offset, su->saAmfSUOperState);
  715. buf = amf_serialize_SaUint32T (
  716. buf, &size, &offset, su->saAmfSUAdminState);
  717. buf = amf_serialize_SaUint32T (
  718. buf, &size, &offset, su->saAmfSUPresenceState);
  719. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->saAmfSUHostedByNode);
  720. buf = amf_serialize_SaUint32T (
  721. buf, &size, &offset, su->saAmfSURestartCount);
  722. buf = amf_serialize_SaUint32T (
  723. buf, &size, &offset, su->restart_control_state);
  724. buf = amf_serialize_SaUint32T (
  725. buf, &size, &offset, su->escalation_level_history_state);
  726. buf = amf_serialize_SaStringT (
  727. buf, &size, &offset, su->clccli_path);
  728. buf = amf_serialize_SaUint32T (
  729. buf, &size, &offset, su->su_failover_cnt);
  730. *len = offset;
  731. return buf;
  732. }
  733. struct amf_su *amf_su_deserialize (struct amf_sg *sg, char *buf, int size)
  734. {
  735. char *tmp = buf;
  736. struct amf_su *su;
  737. su = amf_su_new (sg, "");
  738. tmp = amf_deserialize_SaNameT (tmp, &su->name);
  739. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURank);
  740. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUNumComponents);
  741. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUIsExternal);
  742. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUFailover);
  743. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPreInstantiable);
  744. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUOperState);
  745. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUAdminState);
  746. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPresenceState);
  747. tmp = amf_deserialize_SaNameT (tmp, &su->saAmfSUHostedByNode);
  748. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURestartCount);
  749. tmp = amf_deserialize_SaUint32T (tmp, &su->restart_control_state);
  750. tmp = amf_deserialize_SaUint32T (tmp, &su->escalation_level_history_state);
  751. tmp = amf_deserialize_SaStringT (tmp, &su->clccli_path);
  752. tmp = amf_deserialize_SaUint32T (tmp, &su->su_failover_cnt);
  753. return su;
  754. }
  755. struct amf_su *amf_su_find (struct amf_cluster *cluster, SaNameT *name)
  756. {
  757. struct amf_application *app;
  758. struct amf_sg *sg;
  759. struct amf_su *su = NULL;
  760. char *app_name;
  761. char *sg_name;
  762. char *su_name;
  763. char *ptrptr;
  764. char *buf;
  765. assert (cluster != NULL && name != NULL);
  766. /* malloc new buffer since strtok_r writes to its first argument */
  767. buf = amf_malloc (name->length + 1);
  768. memcpy (buf, name->value, name->length + 1);
  769. su_name = strtok_r(buf, ",", &ptrptr);
  770. sg_name = strtok_r(NULL, ",", &ptrptr);
  771. app_name = strtok_r(NULL, ",", &ptrptr);
  772. if (su_name == NULL || sg_name == NULL || app_name == NULL) {
  773. goto end;
  774. }
  775. su_name += 6;
  776. sg_name += 6;
  777. app_name += 7;
  778. app = amf_application_find (cluster, app_name);
  779. if (app == NULL) {
  780. goto end;
  781. }
  782. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  783. if (strncmp (sg_name, (char*)sg->name.value,
  784. sg->name.length) == 0) {
  785. for (su = sg->su_head; su != NULL; su = su->next) {
  786. if (su->name.length == strlen(su_name) &&
  787. strncmp (su_name, (char*)su->name.value,
  788. su->name.length) == 0) {
  789. goto end;
  790. }
  791. }
  792. }
  793. }
  794. end:
  795. free (buf);
  796. return su;
  797. }