testamf1.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sched.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include "saAis.h"
  47. #include "saAmf.h"
  48. SaAmfHandleT handle;
  49. SaAmfHealthcheckKeyT key0 = {
  50. .key = "key1",
  51. .keyLen = 4
  52. };
  53. SaNameT compNameGlobal;
  54. int good_health = 0;
  55. int good_health_limit = 0;
  56. void printSaNameT (SaNameT *name)
  57. {
  58. int i;
  59. for (i = 0; i < name->length; i++) {
  60. printf ("%c", name->value[i]);
  61. }
  62. }
  63. void setSanameT (SaNameT *name, char *str) {
  64. name->length = strlen (str);
  65. memcpy (name->value, str, name->length);
  66. }
  67. static unsigned int healthcheck_no = 0;
  68. int stop = 0;
  69. void HealthcheckCallback (SaInvocationT invocation,
  70. const SaNameT *compName,
  71. SaAmfHealthcheckKeyT *healthcheckKey)
  72. {
  73. SaAisErrorT res;
  74. if( !good_health && healthcheck_no++);
  75. /*
  76. printf ("Healthcheck %u for key '%s' for component ",
  77. healthcheck_no, healthcheckKey->key);
  78. printSaNameT ((SaNameT *)compName);
  79. printf ("\n");
  80. */
  81. if (healthcheck_no == good_health_limit ) {
  82. printf ("COMPONENT REPORTING ERROR %s\n", compNameGlobal.value);
  83. saAmfComponentErrorReport (handle, compName, 0, SA_AMF_COMPONENT_RESTART, 0);
  84. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  85. printf ("COMPONENT DONE REPORTING ERROR\n");
  86. } else {
  87. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  88. }
  89. /*
  90. if (healthcheck_no < good_health_limit) {
  91. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  92. }
  93. */
  94. /*
  95. if (healthcheck_no == good_health_limit) {
  96. res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
  97. stop = 1;
  98. }
  99. */
  100. }
  101. void ComponentTerminateCallback (
  102. SaInvocationT invocation,
  103. const SaNameT *compName)
  104. {
  105. printf ("ComponentTerminateCallback\n");
  106. saAmfResponse (handle, invocation, SA_AIS_OK);
  107. exit (0);
  108. }
  109. #if 1
  110. #include <sys/time.h>
  111. #define TRU "%d"
  112. #define TRS "%s"
  113. #define TR(format,x) do { \
  114. struct timeval t;\
  115. gettimeofday(&t,NULL); \
  116. printf("%s:%d: %s : %d : %u: %u :%s : " format "\n",\
  117. __FILE__,__LINE__,__FUNCTION__, \
  118. getpid(),(int)t.tv_sec, (int)t.tv_usec,#x,x); \
  119. }while(0)
  120. #else
  121. #define TRU "%d"
  122. #define TRS "%s"
  123. #define TR(format,x)
  124. #endif
  125. void CSISetCallback (
  126. SaInvocationT invocation,
  127. const SaNameT *compName,
  128. SaAmfHAStateT haState,
  129. SaAmfCSIDescriptorT *csiDescriptor)
  130. {
  131. int res;
  132. switch (haState) {
  133. case SA_AMF_HA_ACTIVE:
  134. printf ("CSISetCallback:");
  135. printf ("for CSI '");
  136. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  137. printf ("' for component ");
  138. printSaNameT ((SaNameT *)compName);
  139. printf ("'");
  140. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  141. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  142. int i;
  143. TR(TRU, csiDescriptor->csiAttr.number);
  144. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  145. if( strcmp(csiDescriptor->csiAttr.attr[i].attrName, "good_health_limit") == 0){
  146. good_health = strcmp(csiDescriptor->csiAttr.attr[i].attrValue, "0") ? 0 : 1;
  147. good_health_limit = atoi(csiDescriptor->csiAttr.attr[i].attrValue);
  148. }
  149. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  150. TR(TRS, csiDescriptor->csiAttr.attr[i].attrValue);
  151. }
  152. TR(TRU, csiDescriptor->csiFlags);
  153. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.activeDescriptor.activeCompName);
  154. TR(TRU, csiDescriptor->csiStateDescriptor.activeDescriptor.transitionDescriptor);
  155. break;
  156. case SA_AMF_HA_STANDBY:
  157. printf ("CSISetCallback:");
  158. printf ("for CSI '");
  159. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  160. printf ("' for component ");
  161. printSaNameT ((SaNameT *)compName);
  162. printf ("'");
  163. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  164. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  165. TR(TRU,csiDescriptor->csiAttr.number);
  166. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  167. if(!strcmp(csiDescriptor->csiAttr.attr[i].attrName, "good_health") &&
  168. !strcmp(csiDescriptor->csiAttr.attr[i].attrValue, "true")){
  169. good_health = 1;
  170. }
  171. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  172. TR(TRS,csiDescriptor->csiAttr.attr[i].attrValue);
  173. }
  174. printf("%s:%d:%s:%d\n",__FILE__,__LINE__,__FUNCTION__,res);
  175. TR(TRU,csiDescriptor->csiFlags);
  176. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.standbyDescriptor.activeCompName);
  177. TR(TRU,csiDescriptor->csiStateDescriptor.standbyDescriptor.standbyRank);
  178. break;
  179. case SA_AMF_HA_QUIESCED:
  180. break;
  181. case SA_AMF_HA_QUIESCING:
  182. break;
  183. }
  184. }
  185. void CSIRemoveCallback (
  186. SaInvocationT invocation,
  187. const SaNameT *compName,
  188. const SaNameT *csiName,
  189. SaAmfCSIFlagsT csiFlags)
  190. {
  191. int res;
  192. printf ("CSIRemoveCallback for component '");
  193. printSaNameT ((SaNameT *)compName);
  194. printf ("' in CSI '");
  195. printSaNameT ((SaNameT *)csiName);
  196. printf ("'\n");
  197. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  198. }
  199. #ifdef COMPILE_OUT
  200. void ProtectionGroupTrackCallback (
  201. const SaNameT *csiName,
  202. SaAmfProtectionGroupNotificationT *notificationBuffer,
  203. SaUint32T numberOfItems,
  204. SaUint32T numberOfMembers,
  205. SaAisErrorT error)
  206. {
  207. int i;
  208. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  209. printf ("buffer is %p\n", notificationBuffer);
  210. for (i = 0; i < numberOfItems; i++) {
  211. printf ("component name");
  212. printSaNameT (&notificationBuffer[i].member.compName);
  213. printf ("\n");
  214. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  215. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  216. printf ("\tchange is %d\n", notificationBuffer[i].change);
  217. }
  218. }
  219. #endif
  220. SaAmfCallbacksT amfCallbacks = {
  221. .saAmfHealthcheckCallback = HealthcheckCallback,
  222. .saAmfComponentTerminateCallback = ComponentTerminateCallback,
  223. .saAmfCSISetCallback = CSISetCallback,
  224. .saAmfCSIRemoveCallback = CSIRemoveCallback,
  225. };
  226. SaAmfCallbacksT amfCallbacks;
  227. SaVersionT version = { 'B', 1, 1 };
  228. #if defined(OPENAIS_BSD) || defined(OPENAIS_LINUX)
  229. static struct sched_param sched_param = {
  230. sched_priority: 99
  231. };
  232. #endif
  233. void sigintr_handler (int signum) {
  234. exit (0);
  235. }
  236. void write_pid (void) {
  237. char pid[256];
  238. char filename[256];
  239. int fd;
  240. int res;
  241. sprintf (filename, "/var/run/openais_cleanup_%s", compNameGlobal.value);
  242. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  243. if (fd == -1) {
  244. printf("Failed using /var/run for pid file, using /tmp\n");
  245. sprintf (filename, "/tmp/openais_cleanup_%s", compNameGlobal.value);
  246. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  247. }
  248. sprintf (pid, "%d", getpid());
  249. res = write (fd, pid, strlen (pid));
  250. close (fd);
  251. }
  252. int main (int argc, char **argv) {
  253. int result;
  254. SaSelectionObjectT select_fd;
  255. fd_set read_fds;
  256. extern char *optarg;
  257. extern int optind;
  258. signal (SIGINT, sigintr_handler);
  259. #if defined(OPENAIS_BSD) || defined(OPENAIS_LINUX)
  260. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  261. if (result == -1) {
  262. printf ("couldn't set sched priority\n");
  263. }
  264. #endif
  265. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  266. if (result != SA_AIS_OK) {
  267. printf ("initialize result is %d\n", result);
  268. exit (1);
  269. }
  270. FD_ZERO (&read_fds);
  271. saAmfSelectionObjectGet (handle, &select_fd);
  272. FD_SET (select_fd, &read_fds);
  273. saAmfComponentNameGet (handle, &compNameGlobal);
  274. write_pid ();
  275. result = saAmfHealthcheckStart (handle,
  276. &compNameGlobal,
  277. &key0,
  278. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  279. SA_AMF_COMPONENT_FAILOVER);
  280. printf ("healthcheck start result %d (should be 1)\n", result);
  281. /*
  282. * Test already started healthcheck
  283. */
  284. result = saAmfHealthcheckStart (handle,
  285. &compNameGlobal,
  286. &key0,
  287. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  288. SA_AMF_COMPONENT_FAILOVER);
  289. printf ("healthcheck start result %d (should be 14)\n", result);
  290. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  291. printf ("register result is %d (should be 1)\n", result);
  292. do {
  293. select (select_fd + 1, &read_fds, 0, 0, 0);
  294. saAmfDispatch (handle, SA_DISPATCH_ALL);
  295. } while (result && stop == 0);
  296. printf ("healthchecks stopped for 5 seconds\n");
  297. sleep (5);
  298. result = saAmfHealthcheckStart (handle,
  299. &compNameGlobal,
  300. &key0,
  301. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  302. SA_AMF_COMPONENT_FAILOVER);
  303. do {
  304. select (select_fd + 1, &read_fds, 0, 0, 0);
  305. saAmfDispatch (handle, SA_DISPATCH_ALL);
  306. } while (result);
  307. saAmfFinalize (handle);
  308. exit (0);
  309. }