testckpt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <errno.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/select.h>
  42. #include <sys/un.h>
  43. #include <sys/time.h>
  44. #include "saAis.h"
  45. #include "saCkpt.h"
  46. #include "sa_error.h"
  47. #define SECONDS_TO_EXPIRE 4
  48. int ckptinv;
  49. SaInvocationT open_invocation = 16;
  50. void printSaNameT (SaNameT *name)
  51. {
  52. int i;
  53. for (i = 0; i < name->length; i++) {
  54. printf ("%c", name->value[i]);
  55. }
  56. }
  57. SaVersionT version = { 'B', 1, 1 };
  58. SaNameT checkpointName = { 5, "abra\0" };
  59. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  60. SA_CKPT_WR_ALL_REPLICAS,
  61. 100000,
  62. 5000000000LL,
  63. 5,
  64. 20000,
  65. 10
  66. };
  67. SaCkptSectionIdT sectionId1 = {
  68. 14,
  69. (SaUint8T *) "section ID #1"
  70. };
  71. SaCkptSectionIdT sectionId2 = {
  72. 14,
  73. (SaUint8T *) "section ID #2"
  74. };
  75. SaCkptSectionCreationAttributesT sectionCreationAttributes1 = {
  76. &sectionId1,
  77. SA_TIME_END
  78. };
  79. SaCkptSectionCreationAttributesT sectionCreationAttributes2 = {
  80. &sectionId2,
  81. SA_TIME_END
  82. };
  83. char readBuffer1[1025];
  84. char readBuffer2[1025];
  85. char default_read_buffer[1025];
  86. SaCkptIOVectorElementT ReadVectorElements[] = {
  87. {
  88. {
  89. 14,
  90. (SaUint8T *) "section ID #1"
  91. },
  92. readBuffer1,
  93. sizeof (readBuffer1),
  94. 0,
  95. 0
  96. },
  97. {
  98. {
  99. 14,
  100. (SaUint8T *) "section ID #2"
  101. },
  102. readBuffer2,
  103. sizeof (readBuffer2),
  104. 0,
  105. 0
  106. }
  107. };
  108. SaCkptIOVectorElementT default_read_vector[] = {
  109. {
  110. SA_CKPT_DEFAULT_SECTION_ID,
  111. default_read_buffer, /*"written data #1, this should extend past end of old section data", */
  112. sizeof(default_read_buffer), /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  113. 0, //5,
  114. 0
  115. }
  116. };
  117. #define DATASIZE 127000
  118. char data1[DATASIZE];
  119. char data2[DATASIZE];
  120. char default_write_data[56];
  121. SaCkptIOVectorElementT WriteVectorElements[] = {
  122. {
  123. {
  124. 14,
  125. (SaUint8T *) "section ID #1"
  126. },
  127. data1, /*"written data #1, this should extend past end of old section data", */
  128. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  129. 0, //5,
  130. 0
  131. },
  132. {
  133. {
  134. 14,
  135. (SaUint8T *) "section ID #2",
  136. },
  137. data2, /*"written data #2, this should extend past end of old section data" */
  138. DATASIZE, /*sizeof ("written data #2, this should extend past end of old section data") + 1, */
  139. 0, //3,
  140. 0
  141. }
  142. };
  143. SaCkptIOVectorElementT default_write_vector[] = {
  144. {
  145. SA_CKPT_DEFAULT_SECTION_ID,
  146. default_write_data, /*"written data #1, this should extend past end of old section data", */
  147. 56, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  148. 0, //5,
  149. 0
  150. }
  151. };
  152. SaCkptCheckpointHandleT checkpointHandle;
  153. void OpenCallBack (
  154. SaInvocationT invocation,
  155. const SaCkptCheckpointHandleT chckpointHandle,
  156. SaAisErrorT error) {
  157. printf ("%s: This is a call back for open for invocation = %d\n",
  158. get_test_output (error, SA_AIS_OK), (int)invocation);
  159. checkpointHandle = chckpointHandle;
  160. }
  161. SaCkptCallbacksT callbacks = {
  162. &OpenCallBack,
  163. 0
  164. };
  165. int main (void) {
  166. SaCkptHandleT ckptHandle;
  167. SaCkptCheckpointHandleT checkpointHandle2;
  168. SaCkptCheckpointHandleT checkpointHandleRead;
  169. SaCkptCheckpointDescriptorT checkpointStatus;
  170. SaCkptSectionIterationHandleT sectionIterator;
  171. SaCkptSectionDescriptorT sectionDescriptor;
  172. SaUint32T erroroneousVectorIndex = 0;
  173. SaErrorT error;
  174. struct timeval tv_start;
  175. struct timeval tv_end;
  176. struct timeval tv_elapsed;
  177. SaSelectionObjectT sel_fd;
  178. fd_set read_set;
  179. error = saCkptInitialize (&ckptHandle, &callbacks, &version);
  180. error = saCkptCheckpointOpenAsync (ckptHandle,
  181. open_invocation,
  182. &checkpointName,
  183. &checkpointCreationAttributes,
  184. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE);
  185. printf ("%s: initial asynchronous open of checkpoint\n",
  186. get_test_output (error, SA_AIS_OK));
  187. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  188. printf ("%s: Retrieve selection object %lld\n",
  189. get_test_output (error, SA_AIS_OK), sel_fd);
  190. FD_SET (sel_fd, &read_set);
  191. select (sel_fd + 1, &read_set, 0, 0, 0);
  192. error = saCkptDispatch (ckptHandle, SA_DISPATCH_ALL);
  193. printf ("%s: Dispatch response for open async of checkpoint\n",
  194. get_test_output (error, SA_AIS_OK));
  195. error = saCkptCheckpointClose (checkpointHandle);
  196. printf ("%s: Closing checkpoint\n", get_test_output (error, SA_AIS_OK));
  197. error = saCkptCheckpointOpen (ckptHandle,
  198. &checkpointName,
  199. &checkpointCreationAttributes,
  200. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  201. 0,
  202. &checkpointHandle);
  203. printf ("%s: initial open of checkpoint\n",
  204. get_test_output (error, SA_AIS_OK));
  205. error = saCkptCheckpointRead (checkpointHandle,
  206. default_read_vector,
  207. 1,
  208. &erroroneousVectorIndex);
  209. printf ("%s: Reading default checkpoint section before update\n",
  210. get_test_output (error, SA_AIS_OK));
  211. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  212. memset (default_read_buffer, 0, sizeof (default_read_buffer));
  213. memcpy(default_write_data, "This is an update to the default section date, update#1", 56);
  214. error = saCkptCheckpointWrite (checkpointHandle,
  215. default_write_vector,
  216. 1,
  217. &erroroneousVectorIndex);
  218. printf ("%s: Writing default checkpoint section with data '%s' \n",get_test_output (error, SA_AIS_OK), default_write_data);
  219. error = saCkptCheckpointRead (checkpointHandle,
  220. default_read_vector,
  221. 1,
  222. &erroroneousVectorIndex);
  223. printf ("%s: Reading default checkpoint section \n",
  224. get_test_output (error, SA_AIS_OK));
  225. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  226. error = saCkptSectionCreate (checkpointHandle,
  227. &sectionCreationAttributes1,
  228. "Initial Data #0",
  229. strlen ("Initial Data #0") + 1);
  230. printf ("%s: checkpoint section create\n",
  231. get_test_output (error, SA_AIS_OK));
  232. gettimeofday (&tv_start, 0);
  233. 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));
  234. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  235. &sectionId1,
  236. sectionCreationAttributes1.expirationTime);
  237. printf ("%s: checkpoint section expiration set\n",
  238. get_test_output (error, SA_AIS_OK));
  239. printf ("Please wait, testing expiry of checkpoint sections.\n");
  240. do {
  241. error = saCkptCheckpointRead (checkpointHandle,
  242. ReadVectorElements,
  243. 1,
  244. &erroroneousVectorIndex);
  245. } while (error != SA_ERR_NOT_EXIST);
  246. gettimeofday (&tv_end, NULL);
  247. timersub (&tv_end, &tv_start, &tv_elapsed);
  248. 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);
  249. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  250. 5000000000LL);
  251. printf ("%s: RetentionDurationSet\n",
  252. get_test_output (error, SA_AIS_OK));
  253. error = saCkptSectionCreate (checkpointHandle,
  254. &sectionCreationAttributes2,
  255. "Initial Data #0",
  256. strlen ("Initial Data #0") + 1);
  257. printf ("%s: Section creation\n",
  258. get_test_output (error, SA_AIS_OK));
  259. error = saCkptCheckpointUnlink (ckptHandle, &checkpointName);
  260. printf ("%s: Unlinking checkpoint\n",
  261. get_test_output (error, SA_AIS_OK));
  262. error = saCkptCheckpointOpen (ckptHandle,
  263. &checkpointName,
  264. &checkpointCreationAttributes,
  265. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  266. 0,
  267. &checkpointHandle2);
  268. printf ("%s: Opening unlinked checkpoint\n",
  269. get_test_output (error, SA_AIS_OK));
  270. error = saCkptCheckpointClose (checkpointHandle);
  271. printf ("%s: Closing checkpoint\n",
  272. get_test_output (error, SA_AIS_OK));
  273. error = saCkptCheckpointOpen (ckptHandle,
  274. &checkpointName,
  275. &checkpointCreationAttributes,
  276. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ,
  277. 0,
  278. &checkpointHandleRead);
  279. printf ("%s: Open checkpoint read only\n",
  280. get_test_output (error, SA_AIS_OK));
  281. error = saCkptCheckpointOpen (ckptHandle,
  282. &checkpointName,
  283. &checkpointCreationAttributes,
  284. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  285. 0,
  286. &checkpointHandle);
  287. printf ("%s: open after unlink/close\n",
  288. get_test_output (error, SA_AIS_OK));
  289. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  290. 5000000000LL);
  291. printf ("%s: set checkpoint retention duration\n",
  292. get_test_output (error, SA_AIS_OK));
  293. error = saCkptCheckpointStatusGet (checkpointHandle,
  294. &checkpointStatus);
  295. printf ("%s: Get checkpoint status\n",
  296. get_test_output (error, SA_AIS_OK));
  297. if (error == SA_OK) {
  298. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  299. (int)checkpointStatus.numberOfSections);
  300. }
  301. error = saCkptSectionCreate (checkpointHandleRead,
  302. &sectionCreationAttributes1,
  303. "Initial Data #0",
  304. strlen ("Initial Data #0") + 1);
  305. printf ("%s: Create checkpoint section on read only checkpoint\n",
  306. get_test_output (error, SA_AIS_ERR_ACCESS));
  307. error = saCkptSectionCreate (checkpointHandle,
  308. &sectionCreationAttributes1,
  309. "Initial Data #0",
  310. strlen ("Initial Data #0") + 1);
  311. printf ("%s: Create checkpoint section on writeable checkpoint\n",
  312. get_test_output (error, SA_AIS_OK));
  313. error = saCkptSectionCreate (checkpointHandle,
  314. &sectionCreationAttributes1,
  315. "Initial Data #0",
  316. strlen ("Initial Data #0") + 1);
  317. printf ("%s: Create checkpoint section when one already exists\n",
  318. get_test_output (error, 14));
  319. error = saCkptSectionDelete (checkpointHandle,
  320. &sectionId1);
  321. printf ("%s: deleting checkpoint handle\n",
  322. get_test_output (error, SA_AIS_OK));
  323. error = saCkptSectionCreate (checkpointHandle,
  324. &sectionCreationAttributes1,
  325. "Initial Data #0",
  326. strlen ("Initial Data #0") + 1);
  327. printf ("%s: replacing deleted checkpoint section\n",
  328. get_test_output (error, SA_AIS_OK));
  329. error = saCkptSectionCreate (checkpointHandle,
  330. &sectionCreationAttributes2,
  331. "Initial Data #2",
  332. strlen ("Initial Data #2") + 1);
  333. printf ("%s: creating section 2 \n",
  334. get_test_output (error, SA_AIS_ERR_EXIST));
  335. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  336. &sectionId2,
  337. SA_TIME_END);
  338. printf ("%s: Setting expiration time for section 2\n",
  339. get_test_output (error, SA_AIS_OK));
  340. error = saCkptSectionOverwrite (checkpointHandle,
  341. &sectionId1,
  342. "Overwrite Data #1",
  343. strlen ("Overwrite Data #1") + 1);
  344. printf ("%s: overwriting checkpoint section 1\n",
  345. get_test_output (error, SA_AIS_OK));
  346. /*
  347. * Test checkpoint read
  348. */
  349. memset (readBuffer1, 0, sizeof (readBuffer1));
  350. memset (readBuffer2, 0, sizeof (readBuffer2));
  351. error = saCkptCheckpointRead (checkpointHandle,
  352. ReadVectorElements,
  353. 2,
  354. &erroroneousVectorIndex);
  355. printf ("%s: checkpoint read operation",
  356. get_test_output (error, SA_AIS_OK));
  357. printf ("Buffers after checkpoint read\n");
  358. printf (" buffer #1: '%s'\n", readBuffer1);
  359. printf (" buffer #2: '%s'\n", readBuffer2);
  360. for (ckptinv = 0; ckptinv < 10; ckptinv++) {
  361. /*
  362. * Test checkpoint write
  363. */
  364. error = saCkptCheckpointWrite (checkpointHandle,
  365. WriteVectorElements,
  366. 2,
  367. &erroroneousVectorIndex);
  368. if (error != SA_OK) {
  369. printf ("Writing checkpoint loop %d\n", ckptinv);
  370. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  371. }
  372. }
  373. printf ("%s: Testing checkpoint writes\n",
  374. get_test_output (error, SA_AIS_OK));
  375. error = saCkptCheckpointRead (checkpointHandle,
  376. ReadVectorElements,
  377. 2,
  378. &erroroneousVectorIndex);
  379. // printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  380. // printf ("Buffers after checkpoint write are:\n");
  381. // printf (" buffer #1: '%s'\n", readBuffer1);
  382. // printf (" buffer #2: '%s'\n", readBuffer2);
  383. error = saCkptCheckpointStatusGet (checkpointHandle,
  384. &checkpointStatus);
  385. printf ("%s: get checkpoint status\n",
  386. get_test_output (error, SA_AIS_OK));
  387. if (error == SA_OK) {
  388. printf ("Memory used %d in %d sections.\n",
  389. (int)checkpointStatus.memoryUsed,
  390. (int)checkpointStatus.numberOfSections);
  391. }
  392. error = saCkptSectionIterationInitialize (checkpointHandle,
  393. 0,
  394. 0,
  395. &sectionIterator);
  396. printf ("%s: initialize section iterator\n",
  397. get_test_output (error, SA_AIS_OK));
  398. /*
  399. * Iterate all sections
  400. */
  401. do {
  402. error = saCkptSectionIterationNext (sectionIterator,
  403. &sectionDescriptor);
  404. printf ("%s: Get next section in iteartion\n",
  405. get_test_output (error, SA_AIS_OK));
  406. if (error == SA_OK) {
  407. printf ("Section '%s' expires %llx size %lld state %x update %llx\n",
  408. sectionDescriptor.sectionId.id,
  409. sectionDescriptor.expirationTime,
  410. sectionDescriptor.sectionSize,
  411. sectionDescriptor.sectionState,
  412. sectionDescriptor.lastUpdate);
  413. }
  414. } while (error == SA_OK);
  415. printf ("The last iteration should fail\n");
  416. error = saCkptSectionIterationFinalize (sectionIterator);
  417. printf ("%s: Finalize iteration\n",
  418. get_test_output (error, SA_AIS_OK));
  419. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  420. error = saCkptFinalize (ckptHandle);
  421. printf ("%s: Finalize checkpoint\n",
  422. get_test_output (error, SA_AIS_OK));
  423. return (0);
  424. }