amfsg.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /** @file amfsg.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 Group Class Implementation
  46. *
  47. * This file contains functions for handling AMF-service groups(SGs). It can be
  48. * viewed as the implementation of the AMF Service Group class (called SG)
  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. * -on request start the service group by instantiating the contained SUs
  55. * -on request assign the service instances it protects to the in-service
  56. * service units it contains respecting as many as possible of the configured
  57. * requirements for the group
  58. * -create and delete an SI-assignment object for each relation between
  59. * an SI and an SU
  60. * -order each contained SU to create and delete CSI-assignments
  61. * -request the Service Instance class (SI) to execute the transfer of the
  62. * HA-state set/remove requests to each component involved
  63. * -fully control the execution of component failover and SU failover
  64. * -on request control the execution of the initial steps of node switchover
  65. * and node failover
  66. * -fully handle the auto adjust procedure
  67. *
  68. * Currently only the 'n+m' redundancy model is implemented. It is the
  69. * ambition to identify n+m specific variables and functions and add the suffix
  70. * '_nplusm' to them so that they can be easily recognized.
  71. *
  72. * When SG is requested to assign workload to all SUs or all SUs hosted on
  73. * a specific node, a procedure containing several steps is executed:
  74. * <1> An algorithm is executed which assigns SIs to SUs respecting the rules
  75. * that has been configured for SG. The algorithm also has to consider
  76. * if assignments between som SIs and SUs already exist. The scope of this
  77. * algorithm is to create SI-assignments and set up requested HA-state for
  78. * each assignment but not to transfer those HA-states to the components.
  79. * <2> All SI-assignments with a requested HA state == ACTIVE are transferred
  80. * to the components concerned before any STANDBY assignments are
  81. * transferred. All components have to acknowledge the setting of the
  82. * ACTIVE HA state before the transfer of any STANDBY assignment is
  83. * initiated.
  84. * <3> All active assignments can not be transferred at the same time to the
  85. * different components because the rules for dependencies between SI and
  86. * SI application wide and CSI and CSI within one SI, has to be respected.
  87. *
  88. * SG is fully responsible for step <1> but not fully responsible for handling
  89. * step <2> and <3>. However, SG uses an attribute called 'dependency level'
  90. * when requsted to assign workload. This parameter refers to an integer that
  91. * has been calculated initially for each SI. The 'dependency level' indicates
  92. * to which extent an SI depends on other SIs such that an SI that depends on
  93. * no other SI is on dependecy_level == 1, an SI that depends only on an SI on
  94. * dependency_level == 1 is on dependency-level == 2.
  95. * An SI that depends on several SIs gets a
  96. * dependency_level that is one unit higher than the SI with the highest
  97. * dependency_level it depends on. When SG is requested to assign the workload
  98. * on a certain dependency level, it requests all SI objects on that level to
  99. * activate (all) SI-assignments that during step <1> has been requested to
  100. * assume the active HA state.
  101. *
  102. * SG contains the following state machines:
  103. * - administrative state machine (ADSM) (NOT IN THIS RELEASE)
  104. * - availability control state machine (ACSM)
  105. *
  106. * The availability control state machine contains two states and one of them
  107. * is composite. Being a composite state means that it contains substates.
  108. * The states are:
  109. * - IDLE (non composite state)
  110. * - MANAGING_SG (composite state)
  111. * MANAGING_SG is entered at several different events which has in common
  112. * the need to set up or change the assignment of SIs to SUs. Only one such
  113. * event can be handled at the time. If new events occur while one event is
  114. * being handled then the new event is saved and will be handled after the
  115. * handling of the first event is ready (return to IDLE state has been done).
  116. * MANAGING_SG handles the following events:
  117. * - start (requests SG to order SU to instantiate all SUs in SG and waits
  118. * for SU to indicate presence state change reports from the SUs and
  119. * finally responds 'started' to the requester)
  120. * - assign (requests SG to assign SIs to SUs according to pre-configured
  121. * rules (if not already done) and transfer the HA state of
  122. * the SIs on the requested SI dependency level. Then SG waits for
  123. * confirmation that the HA state has been succesfully set and
  124. * finally responds 'assigned' to the reqeuster)
  125. * - auto_adjust (this event indicates that the auto-adjust probation timer has
  126. * expired and that SG should evaluate current assignments of
  127. * SIs to SUs and if needed remove current assignments and
  128. * create new according to what is specified in paragraph
  129. * 3.7.1.2)
  130. * - failover_comp (requests SG to failover a specific component according to
  131. * the procedure described in paragraph 3.12.1.3)
  132. * - failover_su (requests SG to failover a specific SU according to the
  133. * procedure described in paragraph 3.12.1.3 and 3.12.1.4)
  134. * - switchover_node (requests SG to execute the recovery actions described
  135. * in 3.12.1.3 and respond to the requester when recovery
  136. * is completed)
  137. * - failover_node (requests SG to execute the recovery actions described
  138. * in 3.12.1.3 and respond to the requester when recovery is
  139. * completed)
  140. *
  141. */
  142. #include <stdlib.h>
  143. #include <errno.h>
  144. #include "amf.h"
  145. #include "print.h"
  146. #include "main.h"
  147. #include "util.h"
  148. /**
  149. * Delete all SI assignments and all CSI assignments
  150. * by requesting all contained components.
  151. * @param su
  152. */
  153. static void delete_si_assignments (struct amf_su *su)
  154. {
  155. struct amf_csi *csi;
  156. struct amf_si *si;
  157. struct amf_si_assignment *si_assignment;
  158. ENTER ("'%s'", su->name.value);
  159. for (si = su->sg->application->si_head; si != NULL; si = si->next) {
  160. if (!name_match (&si->saAmfSIProtectedbySG, &su->sg->name)) {
  161. continue;
  162. }
  163. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  164. amf_csi_delete_assignments (csi, su);
  165. }
  166. /*
  167. * TODO: this only works for n+m where each SI list has only two
  168. * assignments, one active and one standby.
  169. * TODO: use DN instead
  170. */
  171. if (si->assigned_sis->su == su) {
  172. si_assignment = si->assigned_sis;
  173. si->assigned_sis = si_assignment->next;
  174. dprintf ("first");
  175. } else {
  176. si_assignment = si->assigned_sis->next;
  177. si->assigned_sis->next = NULL;
  178. dprintf ("second");
  179. }
  180. dprintf ("%p, %d, %d",
  181. si_assignment, si_assignment->name.length,
  182. si->assigned_sis->name.length);
  183. assert (si_assignment != NULL);
  184. free (si_assignment);
  185. }
  186. }
  187. static int all_si_has_hastate (struct amf_su *su, SaAmfHAStateT hastate)
  188. {
  189. struct amf_si_assignment *si_assignment;
  190. int all_confirmed = 1;
  191. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  192. while (si_assignment != NULL) {
  193. if (si_assignment->saAmfSISUHAState != hastate) {
  194. all_confirmed = 0;
  195. break;
  196. }
  197. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  198. }
  199. return all_confirmed;
  200. }
  201. /**
  202. * Callback function used by SI when an SI has been deactivated.
  203. * @param si_assignment
  204. * @param result
  205. */
  206. static void failover_su_si_deactivated_cbfn (
  207. struct amf_si_assignment *si_assignment, int result)
  208. {
  209. ENTER ("'%s', %d", si_assignment->si->name.value, result);
  210. /*
  211. * If all SI assignments for the SU are quiesced, goto next
  212. * state (TerminatingSuspected).
  213. */
  214. if (all_si_has_hastate (si_assignment->su, SA_AMF_HA_QUIESCED)) {
  215. si_assignment->su->sg->avail_state = SG_AC_TerminatingSuspected;
  216. /*
  217. * Terminate suspected SU(s)
  218. */
  219. amf_su_terminate (si_assignment->su);
  220. }
  221. }
  222. static int su_instantiated_count (struct amf_sg *sg)
  223. {
  224. int cnt = 0;
  225. struct amf_su *su;
  226. for (su = sg->su_head; su != NULL; su = su->next) {
  227. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATED)
  228. cnt++;
  229. }
  230. return cnt;
  231. }
  232. static void standby_su_activated_cbfn (
  233. struct amf_si_assignment *si_assignment, int result)
  234. {
  235. struct amf_su *su;
  236. ENTER ("'%s', %d", si_assignment->si->name.value, result);
  237. /*
  238. * TODO: create SI assignment to spare and assign them
  239. */
  240. si_assignment->su->sg->avail_state = SG_AC_AssigningStandbyToSpare;
  241. si_assignment->su->sg->avail_state = SG_AC_ReparingSu;
  242. if (all_si_has_hastate (si_assignment->su, SA_AMF_HA_ACTIVE)) {
  243. for (su = si_assignment->su->sg->su_head; su != NULL; su = su->next) {
  244. if ((su->saAmfSUPresenceState == SA_AMF_PRESENCE_UNINSTANTIATED) &&
  245. (su_instantiated_count (si_assignment->su->sg) <
  246. si_assignment->su->sg->saAmfSGNumPrefInserviceSUs)) {
  247. amf_su_instantiate (su);
  248. }
  249. }
  250. }
  251. }
  252. static void assign_si_assumed_cbfn (
  253. struct amf_si_assignment *si_assignment, int result)
  254. {
  255. struct amf_si_assignment *tmp_si_assignment;
  256. struct amf_si *si;
  257. struct amf_sg *sg = si_assignment->su->sg;
  258. int si_assignment_cnt = 0;
  259. int confirmed_assignments = 0;
  260. ENTER ("'%s', %d", si_assignment->si->name.value, result);
  261. /*
  262. * Report to application when all SIs that this SG protects
  263. * has been assigned or go back to idle state if not cluster
  264. * start.
  265. */
  266. for (si = sg->application->si_head; si != NULL; si = si->next) {
  267. if (name_match (&si->saAmfSIProtectedbySG, &sg->name)) {
  268. for (tmp_si_assignment = si->assigned_sis;
  269. tmp_si_assignment != NULL;
  270. tmp_si_assignment = tmp_si_assignment->next) {
  271. si_assignment_cnt++;
  272. if (tmp_si_assignment->requested_ha_state ==
  273. tmp_si_assignment->saAmfSISUHAState) {
  274. confirmed_assignments++;
  275. }
  276. }
  277. }
  278. }
  279. assert (confirmed_assignments != 0);
  280. switch (sg->avail_state) {
  281. case SG_AC_AssigningOnRequest:
  282. if (si_assignment_cnt == confirmed_assignments) {
  283. sg->avail_state = SG_AC_Idle;
  284. amf_application_sg_assigned (sg->application, sg);
  285. } else {
  286. dprintf ("%d, %d", si_assignment_cnt, confirmed_assignments);
  287. }
  288. break;
  289. case SG_AC_AssigningStandBy:
  290. {
  291. SaNameT dn;
  292. if (si_assignment_cnt == confirmed_assignments) {
  293. sg->avail_state = SG_AC_Idle;
  294. amf_su_dn_make (si_assignment->su, &dn);
  295. sg->avail_state = SG_AC_Idle;
  296. log_printf (
  297. LOG_NOTICE, "'%s' failover recovery action finished",
  298. dn.value);
  299. }
  300. break;
  301. }
  302. default:
  303. dprintf ("%d, %d, %d", sg->avail_state, si_assignment_cnt,
  304. confirmed_assignments);
  305. amf_runtime_attributes_print (amf_cluster);
  306. assert (0);
  307. }
  308. }
  309. /**
  310. * Find an SU assigned with standby workload and activate it.
  311. * @param su
  312. */
  313. static void standby_su_activate (struct amf_su *su)
  314. {
  315. struct amf_si_assignment *su_si_assignment;
  316. struct amf_si_assignment *si_assignment;
  317. ENTER ("Old SU '%s'", su->name.value);
  318. su->sg->avail_state = SG_AC_ActivatingStandby;
  319. /*
  320. * For each (active) SI assignment on the old SU, find a standby
  321. * SI assignment and activate it.
  322. */
  323. su_si_assignment = amf_su_get_next_si_assignment (su, NULL);
  324. while (su_si_assignment != NULL) {
  325. for (si_assignment = su_si_assignment->si->assigned_sis;
  326. si_assignment != NULL;
  327. si_assignment = si_assignment->next) {
  328. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  329. si_assignment->requested_ha_state = SA_AMF_HA_ACTIVE;
  330. amf_si_ha_state_assume (
  331. si_assignment, standby_su_activated_cbfn);
  332. break; /* one standby is enough */
  333. }
  334. }
  335. su_si_assignment = amf_su_get_next_si_assignment (su, su_si_assignment);
  336. }
  337. delete_si_assignments (su);
  338. LEAVE ("");
  339. }
  340. static inline int div_round (int a, int b)
  341. {
  342. int res;
  343. res = a / b;
  344. if ((a % b) != 0)
  345. res++;
  346. return res;
  347. }
  348. static int all_su_has_presence_state(
  349. struct amf_sg *sg, SaAmfPresenceStateT state)
  350. {
  351. struct amf_su *su;
  352. int all_set = 1;
  353. for (su = sg->su_head; su != NULL; su = su->next) {
  354. if (su->saAmfSUPresenceState != state) {
  355. all_set = 0;
  356. break;
  357. }
  358. }
  359. return all_set;
  360. }
  361. /**
  362. * Get number of SIs protected by the specified SG.
  363. * @param sg
  364. *
  365. * @return int
  366. */
  367. static int sg_si_count_get (struct amf_sg *sg)
  368. {
  369. struct amf_si *si;
  370. int cnt = 0;
  371. for (si = sg->application->si_head; si != NULL; si = si->next) {
  372. if (name_match (&si->saAmfSIProtectedbySG, &sg->name)) {
  373. cnt += 1;
  374. }
  375. }
  376. return (cnt);
  377. }
  378. static void sg_assign_nm_active (struct amf_sg *sg, int su_active_assign)
  379. {
  380. struct amf_su *su;
  381. struct amf_si *si;
  382. int assigned = 0;
  383. int assign_per_su = 0;
  384. int total_assigned = 0;
  385. int si_cnt;
  386. ENTER ("'%s'", sg->name.value);
  387. si_cnt = sg_si_count_get (sg);
  388. assign_per_su = div_round (si_cnt, su_active_assign);
  389. if (assign_per_su > sg->saAmfSGMaxActiveSIsperSUs) {
  390. assign_per_su = sg->saAmfSGMaxActiveSIsperSUs;
  391. }
  392. si = sg->application->si_head;
  393. su = sg->su_head;
  394. while (su != NULL) {
  395. if (amf_su_get_saAmfSUReadinessState (su) !=
  396. SA_AMF_READINESS_IN_SERVICE ||
  397. amf_su_get_saAmfSUNumCurrActiveSIs (su) ==
  398. sg->saAmfSGMaxActiveSIsperSUs ||
  399. amf_su_get_saAmfSUNumCurrStandbySIs (su) > 0) {
  400. su = su->next;
  401. continue; /* Not in service */
  402. }
  403. assigned = 0;
  404. while (si != NULL &&
  405. assigned < assign_per_su &&
  406. total_assigned < si_cnt) {
  407. if (amf_si_get_saAmfSINumCurrActiveAssignments (si) == 0) {
  408. assigned += 1;
  409. total_assigned += 1;
  410. amf_su_assign_si (su, si, SA_AMF_HA_ACTIVE);
  411. }
  412. si = si->next;
  413. }
  414. su = su->next;
  415. }
  416. if (total_assigned == 0) {
  417. dprintf ("Info: No SIs assigned!");
  418. }
  419. }
  420. static void sg_assign_nm_standby (struct amf_sg *sg, int su_standby_assign)
  421. {
  422. struct amf_su *su;
  423. struct amf_si *si;
  424. int assigned = 0;
  425. int assign_per_su = 0;
  426. int total_assigned = 0;
  427. int si_cnt;
  428. ENTER ("'%s'", sg->name.value);
  429. if (su_standby_assign == 0) {
  430. return;
  431. }
  432. si_cnt = sg_si_count_get (sg);
  433. assign_per_su = div_round (si_cnt, su_standby_assign);
  434. if (assign_per_su > sg->saAmfSGMaxStandbySIsperSUs) {
  435. assign_per_su = sg->saAmfSGMaxStandbySIsperSUs;
  436. }
  437. si = sg->application->si_head;
  438. su = sg->su_head;
  439. while (su != NULL) {
  440. if (amf_su_get_saAmfSUReadinessState (su) !=
  441. SA_AMF_READINESS_IN_SERVICE ||
  442. amf_su_get_saAmfSUNumCurrActiveSIs (su) > 0 ||
  443. amf_su_get_saAmfSUNumCurrStandbySIs (su) ==
  444. sg->saAmfSGMaxStandbySIsperSUs) {
  445. su = su->next;
  446. continue; /* Not available for assignment */
  447. }
  448. assigned = 0;
  449. while (si != NULL && assigned < assign_per_su) {
  450. if (amf_si_get_saAmfSINumCurrStandbyAssignments (si) == 0) {
  451. assigned += 1;
  452. total_assigned += 1;
  453. amf_su_assign_si (su, si, SA_AMF_HA_STANDBY);
  454. }
  455. si = si->next;
  456. }
  457. su = su->next;
  458. }
  459. if (total_assigned == 0) {
  460. dprintf ("Info: No SIs assigned!");
  461. }
  462. }
  463. static int su_inservice_count_get (struct amf_sg *sg)
  464. {
  465. struct amf_su *su;
  466. int answer = 0;
  467. for (su = sg->su_head; su != NULL; su = su->next) {
  468. if (amf_su_get_saAmfSUReadinessState (su) ==
  469. SA_AMF_READINESS_IN_SERVICE) {
  470. answer += 1;
  471. }
  472. }
  473. return (answer);
  474. }
  475. /**
  476. * TODO: dependency_level not used, hard coded
  477. * @param sg
  478. * @param dependency_level
  479. */
  480. void amf_sg_assign_si (struct amf_sg *sg, int dependency_level)
  481. {
  482. int active_sus_needed;
  483. int standby_sus_needed;
  484. int inservice_count;
  485. int units_for_standby;
  486. int units_for_active;
  487. int ii_spare;
  488. int su_active_assign;
  489. int su_standby_assign;
  490. int su_spare_assign;
  491. ENTER ("'%s'", sg->name.value);
  492. sg->avail_state = SG_AC_AssigningOnRequest;
  493. /**
  494. * Phase 1: Calculate assignments and create all runtime objects in
  495. * information model. Do not do the actual assignment, done in
  496. * phase 2.
  497. */
  498. /**
  499. * Calculate number of SUs to assign to active or standby state
  500. */
  501. inservice_count = (float)su_inservice_count_get (sg);
  502. active_sus_needed = div_round (sg_si_count_get (sg),
  503. sg->saAmfSGMaxActiveSIsperSUs);
  504. standby_sus_needed = div_round (sg_si_count_get (sg),
  505. sg->saAmfSGMaxStandbySIsperSUs);
  506. units_for_active = inservice_count - sg->saAmfSGNumPrefStandbySUs;
  507. if (units_for_active < 0) {
  508. units_for_active = 0;
  509. }
  510. units_for_standby = inservice_count - sg->saAmfSGNumPrefActiveSUs;
  511. if (units_for_standby < 0) {
  512. units_for_standby = 0;
  513. }
  514. ii_spare = inservice_count - sg->saAmfSGNumPrefActiveSUs -
  515. sg->saAmfSGNumPrefStandbySUs;
  516. if (ii_spare < 0) {
  517. ii_spare = 0;
  518. }
  519. /**
  520. * Determine number of active and standby service units
  521. * to assign based upon reduction procedure
  522. */
  523. if ((inservice_count - active_sus_needed) < 0) {
  524. dprintf ("assignment VI - partial assignment with SIs drop outs\n");
  525. su_active_assign = active_sus_needed;
  526. su_standby_assign = 0;
  527. su_spare_assign = 0;
  528. } else
  529. if ((inservice_count - active_sus_needed - standby_sus_needed) < 0) {
  530. dprintf ("assignment V - partial assignment with reduction "
  531. "of standby units\n");
  532. su_active_assign = active_sus_needed;
  533. if (standby_sus_needed > units_for_standby) {
  534. su_standby_assign = units_for_standby;
  535. } else {
  536. su_standby_assign = standby_sus_needed;
  537. }
  538. su_spare_assign = 0;
  539. } else
  540. if ((sg->saAmfSGMaxStandbySIsperSUs * units_for_standby) <=
  541. sg_si_count_get (sg)) {
  542. dprintf ("IV: full assignment with reduction of active service units\n");
  543. su_active_assign = inservice_count - standby_sus_needed;
  544. su_standby_assign = standby_sus_needed;
  545. su_spare_assign = 0;
  546. } else
  547. if ((sg->saAmfSGMaxActiveSIsperSUs * units_for_active) <=
  548. sg_si_count_get (sg)) {
  549. dprintf ("III: full assignment with reduction of standby "
  550. "service units\n");
  551. su_active_assign = sg->saAmfSGNumPrefActiveSUs;
  552. su_standby_assign = units_for_standby;
  553. su_spare_assign = 0;
  554. } else
  555. if (ii_spare == 0) {
  556. dprintf ("II: full assignment with spare reduction\n");
  557. su_active_assign = sg->saAmfSGNumPrefActiveSUs;
  558. su_standby_assign = sg->saAmfSGNumPrefStandbySUs;
  559. su_spare_assign = 0;
  560. } else {
  561. dprintf ("I: full assignment with spares\n");
  562. su_active_assign = sg->saAmfSGNumPrefActiveSUs;
  563. su_standby_assign = sg->saAmfSGNumPrefStandbySUs;
  564. su_spare_assign = ii_spare;
  565. }
  566. dprintf ("(inservice=%d) (assigning active=%d) (assigning standby=%d)"
  567. " (assigning spares=%d)\n",
  568. inservice_count, su_active_assign, su_standby_assign, su_spare_assign);
  569. sg_assign_nm_active (sg, su_active_assign);
  570. sg_assign_nm_standby (sg, su_standby_assign);
  571. sg->saAmfSGNumCurrAssignedSUs = inservice_count;
  572. /**
  573. * Phase 2: do the actual assignment to the component
  574. * TODO: first do active, then standby
  575. */
  576. {
  577. struct amf_si *si;
  578. struct amf_si_assignment *si_assignment;
  579. for (si = sg->application->si_head; si != NULL; si = si->next) {
  580. if (name_match (&si->saAmfSIProtectedbySG, &sg->name)) {
  581. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  582. si_assignment = si_assignment->next) {
  583. if (si_assignment->requested_ha_state !=
  584. si_assignment->saAmfSISUHAState) {
  585. amf_si_ha_state_assume (
  586. si_assignment, assign_si_assumed_cbfn);
  587. }
  588. }
  589. }
  590. }
  591. }
  592. LEAVE ("'%s'", sg->name.value);
  593. }
  594. void amf_sg_start (struct amf_sg *sg, struct amf_node *node)
  595. {
  596. struct amf_su *su;
  597. ENTER ("'%s'", sg->name.value);
  598. sg->avail_state = SG_AC_InstantiatingServiceUnits;
  599. for (su = sg->su_head; su != NULL; su = su->next) {
  600. if (node == NULL) {
  601. /* Cluster start */
  602. amf_su_instantiate (su);
  603. } else {
  604. /* Node start, match if SU is hosted on the specified node */
  605. if (name_match (&node->name, &su->saAmfSUHostedByNode)) {
  606. amf_su_instantiate (su);
  607. }
  608. }
  609. }
  610. }
  611. void amf_sg_su_state_changed (
  612. struct amf_sg *sg, struct amf_su *su, SaAmfStateT type, int state)
  613. {
  614. ENTER ("'%s' SU '%s' state %s",
  615. sg->name.value, su->name.value, amf_presence_state(state));
  616. if (type == SA_AMF_PRESENCE_STATE) {
  617. if (state == SA_AMF_PRESENCE_INSTANTIATED) {
  618. if (all_su_has_presence_state(su->sg,
  619. SA_AMF_PRESENCE_INSTANTIATED)) {
  620. if (sg->avail_state == SG_AC_InstantiatingServiceUnits) {
  621. su->sg->avail_state = SG_AC_Idle;
  622. amf_application_sg_started (
  623. sg->application, sg, this_amf_node);
  624. } else if (sg->avail_state == SG_AC_ReparingSu) {
  625. su->sg->avail_state = SG_AC_AssigningStandBy;
  626. amf_sg_assign_si (sg, 0);
  627. } else {
  628. assert (0);
  629. }
  630. }
  631. } else if (state == SA_AMF_PRESENCE_UNINSTANTIATED) {
  632. if (sg->avail_state == SG_AC_TerminatingSuspected) {
  633. standby_su_activate (su);
  634. } else {
  635. assert (0);
  636. }
  637. } else {
  638. assert (0);
  639. }
  640. } else {
  641. assert (0);
  642. }
  643. }
  644. void amf_sg_init (void)
  645. {
  646. log_init ("AMF");
  647. }
  648. void amf_sg_failover_su_req (
  649. struct amf_sg *sg, struct amf_su *su, struct amf_node *node)
  650. {
  651. struct amf_si_assignment *si_assignment;
  652. ENTER ("");
  653. sg->avail_state = SG_AC_DeactivatingDependantWorkload;
  654. /*
  655. * Deactivate workload for SU
  656. */
  657. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  658. while (si_assignment != NULL) {
  659. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  660. si_assignment->requested_ha_state = SA_AMF_HA_QUIESCED;
  661. amf_si_ha_state_assume (
  662. si_assignment, failover_su_si_deactivated_cbfn);
  663. }
  664. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  665. }
  666. }
  667. /**
  668. * Constructor for SG objects. Adds SG to the list owned by
  669. * the specified application. Always returns a valid SG
  670. * object, out-of-memory problems are handled here. Default
  671. * values are initialized.
  672. * @param sg
  673. * @param name
  674. *
  675. * @return struct amf_sg*
  676. */
  677. struct amf_sg *amf_sg_new (struct amf_application *app, char *name)
  678. {
  679. struct amf_sg *sg = calloc (1, sizeof (struct amf_sg));
  680. if (sg == NULL) {
  681. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  682. }
  683. sg->next = app->sg_head;
  684. app->sg_head = sg;
  685. sg->saAmfSGAdminState = SA_AMF_ADMIN_UNLOCKED;
  686. sg->saAmfSGNumPrefActiveSUs = 1;
  687. sg->saAmfSGNumPrefStandbySUs = 1;
  688. sg->saAmfSGNumPrefInserviceSUs = ~0;
  689. sg->saAmfSGCompRestartProb = -1;
  690. sg->saAmfSGCompRestartMax = ~0;
  691. sg->saAmfSGSuRestartProb = -1;
  692. sg->saAmfSGSuRestartMax = ~0;
  693. sg->saAmfSGAutoAdjustProb = -1;
  694. sg->saAmfSGAutoRepair = SA_TRUE;
  695. sg->application = app;
  696. setSaNameT (&sg->name, name);
  697. return sg;
  698. }
  699. void amf_sg_delete (struct amf_sg *sg)
  700. {
  701. struct amf_su *su;
  702. for (su = sg->su_head; su != NULL;) {
  703. struct amf_su *tmp = su;
  704. su = su->next;
  705. amf_su_delete (tmp);
  706. }
  707. free (sg);
  708. }
  709. void *amf_sg_serialize (struct amf_sg *sg, int *len)
  710. {
  711. int objsz = sizeof (struct amf_sg);
  712. struct amf_sg *copy;
  713. copy = amf_malloc (objsz);
  714. memcpy (copy, sg, objsz);
  715. *len = objsz;
  716. TRACE8 ("%s", copy->name.value);
  717. return copy;
  718. }
  719. struct amf_sg *amf_sg_deserialize (
  720. struct amf_application *app, char *buf, int size)
  721. {
  722. int objsz = sizeof (struct amf_sg);
  723. if (objsz > size) {
  724. return NULL;
  725. } else {
  726. /*
  727. * TODO: use amf_sg_new
  728. */
  729. struct amf_sg *sg = calloc (1, sizeof (struct amf_sg));
  730. if (sg == NULL) {
  731. return NULL;
  732. }
  733. memcpy (sg, buf, objsz);
  734. TRACE8 ("%s", sg->name.value);
  735. sg->application = app;
  736. sg->su_head = NULL;
  737. sg->next = app->sg_head;
  738. app->sg_head = sg;
  739. return sg;
  740. }
  741. }
  742. struct amf_sg *amf_sg_find (struct amf_application *app, char *name)
  743. {
  744. struct amf_sg *sg;
  745. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  746. if (strncmp (name, (char*)sg->name.value, sg->name.length) == 0) {
  747. break;
  748. }
  749. }
  750. return sg;
  751. }