testckpt.c 16 KB

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