testckpt.c 15 KB

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