testckpt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. error = saCkptInitialize (&ckptHandle, &callbacks, &version);
  181. error = saCkptCheckpointOpenAsync (ckptHandle,
  182. open_invocation,
  183. &checkpointName,
  184. &checkpointCreationAttributes,
  185. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE);
  186. printf ("%s: initial asynchronous open of checkpoint\n",
  187. get_test_output (error, SA_AIS_OK));
  188. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  189. printf ("%s: Retrieve selection object %llu\n",
  190. get_test_output (error, SA_AIS_OK), (unsigned long long)sel_fd);
  191. FD_SET (sel_fd, &read_set);
  192. select (sel_fd + 1, &read_set, 0, 0, 0);
  193. error = saCkptDispatch (ckptHandle, SA_DISPATCH_ALL);
  194. printf ("%s: Dispatch response for open async of checkpoint\n",
  195. get_test_output (error, SA_AIS_OK));
  196. error = saCkptCheckpointClose (checkpointHandle);
  197. printf ("%s: Closing checkpoint\n", get_test_output (error, SA_AIS_OK));
  198. error = saCkptCheckpointOpen (ckptHandle,
  199. &checkpointName,
  200. &checkpointCreationAttributes,
  201. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  202. 0,
  203. &checkpointHandle);
  204. printf ("%s: initial open of checkpoint\n",
  205. get_test_output (error, SA_AIS_OK));
  206. error = saCkptCheckpointRead (checkpointHandle,
  207. default_read_vector,
  208. 1,
  209. &erroroneousVectorIndex);
  210. printf ("%s: Reading default checkpoint section before update\n",
  211. get_test_output (error, SA_AIS_OK));
  212. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  213. memset (default_read_buffer, 0, sizeof (default_read_buffer));
  214. memcpy(default_write_data, "This is an update to the default section date, update#1", 56);
  215. error = saCkptCheckpointWrite (checkpointHandle,
  216. default_write_vector,
  217. 1,
  218. &erroroneousVectorIndex);
  219. printf ("%s: Writing default checkpoint section with data '%s' \n",get_test_output (error, SA_AIS_OK), default_write_data);
  220. error = saCkptCheckpointRead (checkpointHandle,
  221. default_read_vector,
  222. 1,
  223. &erroroneousVectorIndex);
  224. printf ("%s: Reading default checkpoint section \n",
  225. get_test_output (error, SA_AIS_OK));
  226. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  227. error = saCkptSectionCreate (checkpointHandle,
  228. &sectionCreationAttributes1,
  229. "Initial Data #0",
  230. strlen ("Initial Data #0") + 1);
  231. printf ("%s: checkpoint section create\n",
  232. get_test_output (error, SA_AIS_OK));
  233. gettimeofday (&tv_start, 0);
  234. 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));
  235. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  236. &sectionId1,
  237. sectionCreationAttributes1.expirationTime);
  238. printf ("%s: checkpoint section expiration set\n",
  239. get_test_output (error, SA_AIS_OK));
  240. printf ("Please wait, testing expiry of checkpoint sections.\n");
  241. do {
  242. error = saCkptCheckpointRead (checkpointHandle,
  243. ReadVectorElements,
  244. 1,
  245. &erroroneousVectorIndex);
  246. } while (error != SA_AIS_ERR_NOT_EXIST);
  247. gettimeofday (&tv_end, NULL);
  248. timersub (&tv_end, &tv_start, &tv_elapsed);
  249. printf ("Elapsed Time to expiry is %ld.%ld (should be about %d seconds)\n", tv_elapsed.tv_sec, tv_elapsed.tv_usec, SECONDS_TO_EXPIRE);
  250. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  251. 5000000000LL);
  252. printf ("%s: RetentionDurationSet\n",
  253. get_test_output (error, SA_AIS_OK));
  254. error = saCkptSectionCreate (checkpointHandle,
  255. &sectionCreationAttributes2,
  256. "Initial Data #0",
  257. strlen ("Initial Data #0") + 1);
  258. printf ("%s: Section creation\n",
  259. get_test_output (error, SA_AIS_OK));
  260. error = saCkptCheckpointUnlink (ckptHandle, &checkpointName);
  261. printf ("%s: Unlinking checkpoint\n",
  262. get_test_output (error, SA_AIS_OK));
  263. error = saCkptCheckpointOpen (ckptHandle,
  264. &checkpointName,
  265. &checkpointCreationAttributes,
  266. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  267. 0,
  268. &checkpointHandle2);
  269. printf ("%s: Opening unlinked checkpoint\n",
  270. get_test_output (error, SA_AIS_OK));
  271. error = saCkptCheckpointClose (checkpointHandle);
  272. printf ("%s: Closing checkpoint\n",
  273. get_test_output (error, SA_AIS_OK));
  274. error = saCkptCheckpointOpen (ckptHandle,
  275. &checkpointName,
  276. &checkpointCreationAttributes,
  277. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ,
  278. 0,
  279. &checkpointHandleRead);
  280. printf ("%s: Open checkpoint read only\n",
  281. get_test_output (error, SA_AIS_OK));
  282. error = saCkptCheckpointOpen (ckptHandle,
  283. &checkpointName,
  284. &checkpointCreationAttributes,
  285. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  286. 0,
  287. &checkpointHandle);
  288. printf ("%s: open after unlink/close\n",
  289. get_test_output (error, SA_AIS_OK));
  290. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  291. 5000000000LL);
  292. printf ("%s: set checkpoint retention duration\n",
  293. get_test_output (error, SA_AIS_OK));
  294. error = saCkptCheckpointStatusGet (checkpointHandle,
  295. &checkpointStatus);
  296. printf ("%s: Get checkpoint status\n",
  297. get_test_output (error, SA_AIS_OK));
  298. if (error == SA_AIS_OK) {
  299. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  300. (int)checkpointStatus.numberOfSections);
  301. }
  302. error = saCkptSectionCreate (checkpointHandleRead,
  303. &sectionCreationAttributes1,
  304. "Initial Data #0",
  305. strlen ("Initial Data #0") + 1);
  306. printf ("%s: Create checkpoint section on read only checkpoint\n",
  307. get_test_output (error, SA_AIS_ERR_ACCESS));
  308. error = saCkptSectionCreate (checkpointHandle,
  309. &sectionCreationAttributes1,
  310. "Initial Data #0",
  311. strlen ("Initial Data #0") + 1);
  312. printf ("%s: Create checkpoint section on writeable checkpoint\n",
  313. get_test_output (error, SA_AIS_OK));
  314. error = saCkptSectionCreate (checkpointHandle,
  315. &sectionCreationAttributes1,
  316. "Initial Data #0",
  317. strlen ("Initial Data #0") + 1);
  318. printf ("%s: Create checkpoint section when one already exists\n",
  319. get_test_output (error, 14));
  320. error = saCkptSectionDelete (checkpointHandle,
  321. &sectionId1);
  322. printf ("%s: deleting checkpoint handle\n",
  323. get_test_output (error, SA_AIS_OK));
  324. error = saCkptSectionCreate (checkpointHandle,
  325. &sectionCreationAttributes1,
  326. "Initial Data #0",
  327. strlen ("Initial Data #0") + 1);
  328. printf ("%s: replacing deleted checkpoint section\n",
  329. get_test_output (error, SA_AIS_OK));
  330. error = saCkptSectionCreate (checkpointHandle,
  331. &sectionCreationAttributes2,
  332. "Initial Data #2",
  333. strlen ("Initial Data #2") + 1);
  334. printf ("%s: creating section 2 \n",
  335. get_test_output (error, SA_AIS_ERR_EXIST));
  336. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  337. &sectionId2,
  338. SA_TIME_END);
  339. printf ("%s: Setting expiration time for section 2\n",
  340. get_test_output (error, SA_AIS_OK));
  341. error = saCkptSectionOverwrite (checkpointHandle,
  342. &sectionId1,
  343. "Overwrite Data #1",
  344. strlen ("Overwrite Data #1") + 1);
  345. printf ("%s: overwriting checkpoint section 1\n",
  346. get_test_output (error, SA_AIS_OK));
  347. /*
  348. * Test checkpoint read
  349. */
  350. memset (readBuffer1, 0, sizeof (readBuffer1));
  351. memset (readBuffer2, 0, sizeof (readBuffer2));
  352. error = saCkptCheckpointRead (checkpointHandle,
  353. ReadVectorElements,
  354. 2,
  355. &erroroneousVectorIndex);
  356. printf ("%s: checkpoint read operation",
  357. get_test_output (error, SA_AIS_OK));
  358. printf ("Buffers after checkpoint read\n");
  359. printf (" buffer #1: '%s'\n", readBuffer1);
  360. printf (" buffer #2: '%s'\n", readBuffer2);
  361. for (ckptinv = 0; ckptinv < 10; ckptinv++) {
  362. /*
  363. * Test checkpoint write
  364. */
  365. error = saCkptCheckpointWrite (checkpointHandle,
  366. WriteVectorElements,
  367. 2,
  368. &erroroneousVectorIndex);
  369. if (error != SA_AIS_OK) {
  370. printf ("Writing checkpoint loop %d\n", ckptinv);
  371. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  372. }
  373. }
  374. printf ("%s: Testing checkpoint writes\n",
  375. get_test_output (error, SA_AIS_OK));
  376. error = saCkptCheckpointRead (checkpointHandle,
  377. ReadVectorElements,
  378. 2,
  379. &erroroneousVectorIndex);
  380. // printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  381. // printf ("Buffers after checkpoint write are:\n");
  382. // printf (" buffer #1: '%s'\n", readBuffer1);
  383. // printf (" buffer #2: '%s'\n", readBuffer2);
  384. error = saCkptCheckpointStatusGet (checkpointHandle,
  385. &checkpointStatus);
  386. printf ("%s: get checkpoint status\n",
  387. get_test_output (error, SA_AIS_OK));
  388. if (error == SA_AIS_OK) {
  389. printf ("Memory used %d in %d sections.\n",
  390. (int)checkpointStatus.memoryUsed,
  391. (int)checkpointStatus.numberOfSections);
  392. }
  393. error = saCkptSectionIterationInitialize (checkpointHandle,
  394. SA_CKPT_SECTIONS_ANY,
  395. 0,
  396. &sectionIterator);
  397. printf ("%s: initialize section iterator\n",
  398. get_test_output (error, SA_AIS_OK));
  399. /*
  400. * Iterate all sections
  401. */
  402. do {
  403. error = saCkptSectionIterationNext (sectionIterator,
  404. &sectionDescriptor);
  405. printf ("%s: Get next section in iteartion\n",
  406. get_test_output (error, SA_AIS_OK));
  407. if (error == SA_AIS_OK) {
  408. printf ("Section '%s' expires %llx size %llu state %x update %llx\n",
  409. sectionDescriptor.sectionId.id,
  410. (unsigned long long)sectionDescriptor.expirationTime,
  411. (unsigned long long)sectionDescriptor.sectionSize,
  412. sectionDescriptor.sectionState,
  413. (unsigned long long)sectionDescriptor.lastUpdate);
  414. }
  415. } while (error == SA_AIS_OK);
  416. printf ("The last iteration should fail\n");
  417. error = saCkptSectionIterationFinalize (sectionIterator);
  418. printf ("%s: Finalize iteration\n",
  419. get_test_output (error, SA_AIS_OK));
  420. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  421. error = saCkptFinalize (ckptHandle);
  422. printf ("%s: Finalize checkpoint\n",
  423. get_test_output (error, SA_AIS_OK));
  424. return (0);
  425. }