testckpt.c 11 KB

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