testckpt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #define _BSD_SOURCE
  2. /*
  3. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include <sys/time.h>
  46. #include "saAis.h"
  47. #include "saCkpt.h"
  48. #include "sa_error.h"
  49. #ifdef OPENAIS_SOLARIS
  50. #define timersub(a, b, result) \
  51. do { \
  52. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  53. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  54. if ((result)->tv_usec < 0) { \
  55. --(result)->tv_sec; \
  56. (result)->tv_usec += 1000000; \
  57. } \
  58. } while (0)
  59. #endif
  60. #define SECONDS_TO_EXPIRE 4
  61. int ckptinv;
  62. SaInvocationT open_invocation = 16;
  63. void printSaNameT (SaNameT *name)
  64. {
  65. int i;
  66. for (i = 0; i < name->length; i++) {
  67. printf ("%c", name->value[i]);
  68. }
  69. }
  70. SaVersionT version = { 'B', 1, 1 };
  71. SaNameT checkpointName = { 5, "abra\0" };
  72. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  73. SA_CKPT_WR_ALL_REPLICAS,
  74. 100000,
  75. 5000000000LL,
  76. 10,
  77. 200000,
  78. 10
  79. };
  80. SaCkptSectionIdT sectionId1 = {
  81. 14,
  82. (SaUint8T *) "section ID #1"
  83. };
  84. SaCkptSectionIdT sectionId2 = {
  85. 14,
  86. (SaUint8T *) "section ID #2"
  87. };
  88. SaCkptSectionCreationAttributesT sectionCreationAttributes1 = {
  89. &sectionId1,
  90. SA_TIME_END
  91. };
  92. SaCkptSectionCreationAttributesT sectionCreationAttributes2 = {
  93. &sectionId2,
  94. SA_TIME_END
  95. };
  96. char readBuffer1[1025];
  97. char readBuffer2[1025];
  98. char default_read_buffer[1025];
  99. SaCkptIOVectorElementT ReadVectorElements[] = {
  100. {
  101. {
  102. 14,
  103. (SaUint8T *) "section ID #1"
  104. },
  105. readBuffer1,
  106. sizeof (readBuffer1),
  107. 0,
  108. 0
  109. },
  110. {
  111. {
  112. 14,
  113. (SaUint8T *) "section ID #2"
  114. },
  115. readBuffer2,
  116. sizeof (readBuffer2),
  117. 0,
  118. 0
  119. }
  120. };
  121. SaCkptIOVectorElementT default_read_vector[] = {
  122. {
  123. SA_CKPT_DEFAULT_SECTION_ID,
  124. default_read_buffer, /*"written data #1, this should extend past end of old section data", */
  125. sizeof(default_read_buffer), /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  126. 0, //5,
  127. 0
  128. }
  129. };
  130. #define DATASIZE 127000
  131. char data1[DATASIZE];
  132. char data2[DATASIZE];
  133. char default_write_data[56];
  134. SaCkptIOVectorElementT WriteVectorElements[] = {
  135. {
  136. {
  137. 14,
  138. (SaUint8T *) "section ID #1"
  139. },
  140. data1, /*"written data #1, this should extend past end of old section data", */
  141. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  142. 0, //5,
  143. 0
  144. },
  145. {
  146. {
  147. 14,
  148. (SaUint8T *) "section ID #2",
  149. },
  150. data2, /*"written data #2, this should extend past end of old section data" */
  151. DATASIZE, /*sizeof ("written data #2, this should extend past end of old section data") + 1, */
  152. 0, //3,
  153. 0
  154. }
  155. };
  156. SaCkptIOVectorElementT default_write_vector[] = {
  157. {
  158. SA_CKPT_DEFAULT_SECTION_ID,
  159. default_write_data, /*"written data #1, this should extend past end of old section data", */
  160. 56, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  161. 0, //5,
  162. 0
  163. }
  164. };
  165. SaCkptCheckpointHandleT checkpointHandle;
  166. void OpenCallBack (
  167. SaInvocationT invocation,
  168. const SaCkptCheckpointHandleT chckpointHandle,
  169. SaAisErrorT error) {
  170. printf ("%s: This is a call back for open for invocation = %d\n",
  171. get_test_output (error, SA_AIS_OK), (int)invocation);
  172. checkpointHandle = chckpointHandle;
  173. }
  174. SaCkptCallbacksT callbacks = {
  175. &OpenCallBack,
  176. 0
  177. };
  178. int main (void) {
  179. SaCkptHandleT ckptHandle;
  180. SaCkptCheckpointHandleT checkpointHandle2;
  181. SaCkptCheckpointHandleT checkpointHandleRead;
  182. SaCkptCheckpointDescriptorT checkpointStatus;
  183. SaCkptSectionIterationHandleT sectionIterator;
  184. SaCkptSectionDescriptorT sectionDescriptor;
  185. SaUint32T erroroneousVectorIndex = 0;
  186. SaAisErrorT error;
  187. struct timeval tv_start;
  188. struct timeval tv_end;
  189. struct timeval tv_elapsed;
  190. SaSelectionObjectT sel_fd;
  191. fd_set read_set;
  192. int i;
  193. error = saCkptInitialize (&ckptHandle, &callbacks, &version);
  194. error = saCkptCheckpointOpenAsync (ckptHandle,
  195. open_invocation,
  196. &checkpointName,
  197. &checkpointCreationAttributes,
  198. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE);
  199. printf ("%s: initial asynchronous open of checkpoint\n",
  200. get_test_output (error, SA_AIS_OK));
  201. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  202. printf ("%s: Retrieve selection object %llu\n",
  203. get_test_output (error, SA_AIS_OK), (unsigned long long)sel_fd);
  204. FD_SET (sel_fd, &read_set);
  205. select (sel_fd + 1, &read_set, 0, 0, 0);
  206. error = saCkptDispatch (ckptHandle, SA_DISPATCH_ALL);
  207. printf ("%s: Dispatch response for open async of checkpoint\n",
  208. get_test_output (error, SA_AIS_OK));
  209. error = saCkptCheckpointClose (checkpointHandle);
  210. printf ("%s: Closing checkpoint\n", get_test_output (error, SA_AIS_OK));
  211. error = saCkptCheckpointOpen (ckptHandle,
  212. &checkpointName,
  213. &checkpointCreationAttributes,
  214. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  215. 0,
  216. &checkpointHandle);
  217. printf ("%s: initial open of checkpoint\n",
  218. get_test_output (error, SA_AIS_OK));
  219. error = saCkptCheckpointRead (checkpointHandle,
  220. default_read_vector,
  221. 1,
  222. &erroroneousVectorIndex);
  223. printf ("%s: Reading default checkpoint section before update\n",
  224. get_test_output (error, SA_AIS_OK));
  225. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  226. memset (default_read_buffer, 0, sizeof (default_read_buffer));
  227. memcpy(default_write_data, "This is an update to the default section date, update#1", 56);
  228. error = saCkptCheckpointWrite (checkpointHandle,
  229. default_write_vector,
  230. 1,
  231. &erroroneousVectorIndex);
  232. printf ("%s: Writing default checkpoint section with data '%s' \n",get_test_output (error, SA_AIS_OK), default_write_data);
  233. error = saCkptCheckpointRead (checkpointHandle,
  234. default_read_vector,
  235. 1,
  236. &erroroneousVectorIndex);
  237. printf ("%s: Reading default checkpoint section \n",
  238. get_test_output (error, SA_AIS_OK));
  239. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  240. error = saCkptSectionCreate (checkpointHandle,
  241. &sectionCreationAttributes1,
  242. "Initial Data #0",
  243. strlen ("Initial Data #0") + 1);
  244. printf ("%s: checkpoint section create\n",
  245. get_test_output (error, SA_AIS_OK));
  246. gettimeofday (&tv_start, 0);
  247. sectionCreationAttributes1.expirationTime = ((unsigned long long)(tv_start.tv_sec + SECONDS_TO_EXPIRE)) * ((unsigned long long)1000000000) + ((unsigned long long)(tv_start.tv_usec) * ((unsigned long long)1000));
  248. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  249. &sectionId1,
  250. sectionCreationAttributes1.expirationTime);
  251. printf ("%s: checkpoint section expiration set\n",
  252. get_test_output (error, SA_AIS_OK));
  253. printf ("Please wait, testing expiry of checkpoint sections.\n");
  254. do {
  255. error = saCkptCheckpointRead (checkpointHandle,
  256. ReadVectorElements,
  257. 1,
  258. &erroroneousVectorIndex);
  259. } while (error != SA_AIS_ERR_NOT_EXIST);
  260. gettimeofday (&tv_end, NULL);
  261. timersub (&tv_end, &tv_start, &tv_elapsed);
  262. printf ("Elapsed Time to expiry is %ld.%ld (should be about %d seconds)\n", tv_elapsed.tv_sec, (unsigned long)tv_elapsed.tv_usec, SECONDS_TO_EXPIRE);
  263. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  264. 5000000000LL);
  265. printf ("%s: RetentionDurationSet\n",
  266. get_test_output (error, SA_AIS_OK));
  267. error = saCkptSectionCreate (checkpointHandle,
  268. &sectionCreationAttributes2,
  269. "Initial Data #0",
  270. strlen ("Initial Data #0") + 1);
  271. printf ("%s: Section creation\n",
  272. get_test_output (error, SA_AIS_OK));
  273. error = saCkptCheckpointUnlink (ckptHandle, &checkpointName);
  274. printf ("%s: Unlinking checkpoint\n",
  275. get_test_output (error, SA_AIS_OK));
  276. error = saCkptCheckpointOpen (ckptHandle,
  277. &checkpointName,
  278. &checkpointCreationAttributes,
  279. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  280. 0,
  281. &checkpointHandle2);
  282. printf ("%s: Opening unlinked checkpoint\n",
  283. get_test_output (error, SA_AIS_OK));
  284. error = saCkptCheckpointClose (checkpointHandle);
  285. printf ("%s: Closing checkpoint\n",
  286. get_test_output (error, SA_AIS_OK));
  287. error = saCkptCheckpointOpen (ckptHandle,
  288. &checkpointName,
  289. &checkpointCreationAttributes,
  290. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ,
  291. 0,
  292. &checkpointHandleRead);
  293. printf ("%s: Open checkpoint read only\n",
  294. get_test_output (error, SA_AIS_OK));
  295. error = saCkptCheckpointOpen (ckptHandle,
  296. &checkpointName,
  297. &checkpointCreationAttributes,
  298. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  299. 0,
  300. &checkpointHandle);
  301. printf ("%s: open after unlink/close\n",
  302. get_test_output (error, SA_AIS_OK));
  303. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  304. 5000000000LL);
  305. printf ("%s: set checkpoint retention duration\n",
  306. get_test_output (error, SA_AIS_OK));
  307. error = saCkptCheckpointStatusGet (checkpointHandle,
  308. &checkpointStatus);
  309. printf ("%s: Get checkpoint status\n",
  310. get_test_output (error, SA_AIS_OK));
  311. if (error == SA_AIS_OK) {
  312. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  313. (int)checkpointStatus.numberOfSections);
  314. }
  315. error = saCkptSectionCreate (checkpointHandleRead,
  316. &sectionCreationAttributes1,
  317. "Initial Data #0",
  318. strlen ("Initial Data #0") + 1);
  319. printf ("%s: Create checkpoint section on read only checkpoint\n",
  320. get_test_output (error, SA_AIS_ERR_ACCESS));
  321. error = saCkptSectionCreate (checkpointHandle,
  322. &sectionCreationAttributes1,
  323. "Initial Data #0",
  324. strlen ("Initial Data #0") + 1);
  325. printf ("%s: Create checkpoint section on writeable checkpoint\n",
  326. get_test_output (error, SA_AIS_OK));
  327. error = saCkptSectionCreate (checkpointHandle,
  328. &sectionCreationAttributes1,
  329. "Initial Data #0",
  330. strlen ("Initial Data #0") + 1);
  331. printf ("%s: Create checkpoint section when one already exists\n",
  332. get_test_output (error, 14));
  333. error = saCkptSectionDelete (checkpointHandle,
  334. &sectionId1);
  335. printf ("%s: deleting section handle\n",
  336. get_test_output (error, SA_AIS_OK));
  337. error = saCkptSectionCreate (checkpointHandle,
  338. &sectionCreationAttributes1,
  339. "Initial Data #0",
  340. strlen ("Initial Data #0") + 1);
  341. printf ("%s: replacing deleted checkpoint section\n",
  342. get_test_output (error, SA_AIS_OK));
  343. error = saCkptSectionCreate (checkpointHandle,
  344. &sectionCreationAttributes2,
  345. "Initial Data #2",
  346. strlen ("Initial Data #2") + 1);
  347. printf ("%s: creating section 2 for first time\n",
  348. get_test_output (error, SA_AIS_OK));
  349. error = saCkptSectionCreate (checkpointHandle,
  350. &sectionCreationAttributes2,
  351. "Initial Data #2",
  352. strlen ("Initial Data #2") + 1);
  353. printf ("%s: creating section 2 for second time\n",
  354. get_test_output (error, SA_AIS_ERR_EXIST));
  355. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  356. &sectionId2,
  357. SA_TIME_END);
  358. printf ("%s: Setting expiration time for section 2\n",
  359. get_test_output (error, SA_AIS_OK));
  360. error = saCkptSectionOverwrite (checkpointHandle,
  361. &sectionId1,
  362. "Overwrite Data #1",
  363. strlen ("Overwrite Data #1") + 1);
  364. printf ("%s: overwriting checkpoint section 1\n",
  365. get_test_output (error, SA_AIS_OK));
  366. /*
  367. * Test checkpoint read
  368. */
  369. memset (readBuffer1, 0, sizeof (readBuffer1));
  370. memset (readBuffer2, 0, sizeof (readBuffer2));
  371. error = saCkptCheckpointRead (checkpointHandle,
  372. ReadVectorElements,
  373. 2,
  374. &erroroneousVectorIndex);
  375. printf ("%s: checkpoint read operation",
  376. get_test_output (error, SA_AIS_OK));
  377. printf ("Buffers after checkpoint read\n");
  378. printf (" buffer #1: '%s'\n", readBuffer1);
  379. printf (" buffer #2: '%s'\n", readBuffer2);
  380. for (ckptinv = 0; ckptinv < 10; ckptinv++) {
  381. /*
  382. * Test checkpoint write
  383. */
  384. error = saCkptCheckpointWrite (checkpointHandle,
  385. WriteVectorElements,
  386. 2,
  387. &erroroneousVectorIndex);
  388. if (error != SA_AIS_OK) {
  389. printf ("Writing checkpoint loop %d\n", ckptinv);
  390. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  391. }
  392. }
  393. printf ("%s: Testing checkpoint writes\n",
  394. get_test_output (error, SA_AIS_OK));
  395. error = saCkptCheckpointRead (checkpointHandle,
  396. ReadVectorElements,
  397. 2,
  398. &erroroneousVectorIndex);
  399. // printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  400. // printf ("Buffers after checkpoint write are:\n");
  401. // printf (" buffer #1: '%s'\n", readBuffer1);
  402. // printf (" buffer #2: '%s'\n", readBuffer2);
  403. error = saCkptCheckpointStatusGet (checkpointHandle,
  404. &checkpointStatus);
  405. printf ("%s: get checkpoint status\n",
  406. get_test_output (error, SA_AIS_OK));
  407. if (error == SA_AIS_OK) {
  408. printf ("Memory used %d in %d sections.\n",
  409. (int)checkpointStatus.memoryUsed,
  410. (int)checkpointStatus.numberOfSections);
  411. }
  412. printf ("iterating all sections 5 times\n");
  413. /*
  414. * iterate all sections 5 times
  415. */
  416. for (i = 0; i < 5; i++) {
  417. error = saCkptSectionIterationInitialize (checkpointHandle,
  418. SA_CKPT_SECTIONS_ANY,
  419. 0,
  420. &sectionIterator);
  421. printf ("%s: initialize section iterator\n",
  422. get_test_output (error, SA_AIS_OK));
  423. /*
  424. * Iterate all sections
  425. */
  426. do {
  427. printf ("Starting iteration\n");
  428. error = saCkptSectionIterationNext (sectionIterator,
  429. &sectionDescriptor);
  430. if (error == SA_AIS_ERR_NO_SECTIONS) {
  431. printf ("No more sections to iterate\n");
  432. } else {
  433. printf ("%s: Get next section in iteration\n",
  434. get_test_output (error, SA_AIS_OK));
  435. }
  436. if (error == SA_AIS_OK) {
  437. printf ("Section '%s' expires %llx size %llu state %x update %llx\n",
  438. sectionDescriptor.sectionId.id,
  439. (unsigned long long)sectionDescriptor.expirationTime,
  440. (unsigned long long)sectionDescriptor.sectionSize,
  441. sectionDescriptor.sectionState,
  442. (unsigned long long)sectionDescriptor.lastUpdate);
  443. }
  444. } while (error == SA_AIS_OK);
  445. error = saCkptSectionIterationFinalize (sectionIterator);
  446. printf ("%s: Finalize iteration\n",
  447. get_test_output (error, SA_AIS_OK));
  448. }
  449. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  450. error = saCkptFinalize (ckptHandle);
  451. printf ("%s: Finalize checkpoint\n",
  452. get_test_output (error, SA_AIS_OK));
  453. return (0);
  454. }