testckpt.c 12 KB

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