testckpt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. if (error != SA_OK) {
  156. error = saCkptSectionExpirationTimeSet (&checkpointHandle,
  157. &sectionId1,
  158. sectionCreationAttributes1.expirationTime);
  159. }
  160. printf ("create1 error is %d\n", error);
  161. printf ("Please wait, testing expiry of checkpoint sections.\n");
  162. do {
  163. error = saCkptCheckpointRead (&checkpointHandle,
  164. ReadVectorElements,
  165. 1,
  166. &erroroneousVectorIndex);
  167. } while (error != SA_ERR_NOT_EXIST);
  168. gettimeofday (&tv_end, NULL);
  169. timersub (&tv_end, &tv_start, &tv_elapsed);
  170. 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);
  171. error = saCkptCheckpointRetentionDurationSet (&checkpointHandle,
  172. 5000000000LL);
  173. printf ("RetentionDurationSet is %d\n", error);
  174. error = saCkptSectionCreate (&checkpointHandle,
  175. &sectionCreationAttributes2,
  176. "Initial Data #0",
  177. strlen ("Initial Data #0") + 1);
  178. printf ("create2 error is %d\n", error);
  179. printf ("saCkptSectionCreate result %d (should be 1)\n", error);
  180. #ifdef cmpout
  181. for (ckptinv = 0; ckptinv < 500000; ckptinv++) {
  182. printf ("Writing checkpoint loop %d\n", ckptinv);
  183. /*
  184. * Test checkpoint write
  185. */
  186. error = saCkptCheckpointWrite (&checkpointHandle,
  187. WriteVectorElements,
  188. 1,
  189. &erroroneousVectorIndex);
  190. if (error != SA_OK) {
  191. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  192. exit (1);
  193. }
  194. }
  195. exit (1);
  196. #endif
  197. error = saCkptCheckpointUnlink (&checkpointName);
  198. printf ("unlink result %d (should be 1)\n", error);
  199. error = saCkptCheckpointOpen (&checkpointName,
  200. &checkpointCreationAttributes,
  201. SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  202. 0,
  203. &checkpointHandle2);
  204. printf ("open after unlink result %d (should be 7)\n", error);
  205. error = saCkptCheckpointClose (&checkpointHandle);
  206. printf ("close result %d (should be 1)\n", error);
  207. error = saCkptCheckpointOpen (&checkpointName,
  208. &checkpointCreationAttributes,
  209. SA_CKPT_CHECKPOINT_READ,
  210. 0,
  211. &checkpointHandleRead);
  212. printf ("read only open result %d (should be 1)\n", error);
  213. error = saCkptCheckpointOpen (&checkpointName,
  214. &checkpointCreationAttributes,
  215. SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  216. 0,
  217. &checkpointHandle);
  218. printf ("open after unlink/close result %d (should be 1)\n", error);
  219. error = saCkptCheckpointRetentionDurationSet (&checkpointHandle,
  220. 5000000000LL);
  221. printf ("Retention duration set error is %d\n", error);
  222. printf ("set checkpoint retention duration result %d (should be 1)\n", error);
  223. error = saCkptCheckpointStatusGet (&checkpointHandle,
  224. &checkpointStatus);
  225. printf ("saCkptCheckpointStatusGet result %d (should be 1)\n", error);
  226. if (error == SA_OK) {
  227. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  228. (int)checkpointStatus.numberOfSections);
  229. }
  230. error = saCkptSectionCreate (&checkpointHandleRead,
  231. &sectionCreationAttributes1,
  232. "Initial Data #0",
  233. strlen ("Initial Data #0") + 1);
  234. printf ("saCkptSectionCreate result %d (should be 11)\n", error);
  235. error = saCkptSectionCreate (&checkpointHandle,
  236. &sectionCreationAttributes1,
  237. "Initial Data #0",
  238. strlen ("Initial Data #0") + 1);
  239. printf ("saCkptSectionCreate result %d (should be 1)\n", error);
  240. error = saCkptSectionCreate (&checkpointHandle,
  241. &sectionCreationAttributes1,
  242. "Initial Data #0",
  243. strlen ("Initial Data #0") + 1);
  244. printf ("saCkptSectionCreate result %d (should be 14)\n", error);
  245. #ifdef COMPILE_OUT
  246. error = saCkptSectionDelete (&checkpointHandle,
  247. &sectionId1);
  248. printf ("saCkptSectionDelete result %d (should be 1)\n", error);
  249. error = saCkptSectionCreate (&checkpointHandle,
  250. &sectionCreationAttributes1,
  251. "Initial Data #0",
  252. strlen ("Initial Data #0") + 1);
  253. printf ("saCkptSectionCreate result %d (should be 1)\n", error);
  254. #endif
  255. error = saCkptSectionExpirationTimeSet (&checkpointHandle,
  256. &sectionId2,
  257. SA_TIME_END);
  258. printf ("saCkptSectionExpirationTimeSet result %d (should be 1)\n", error);
  259. error = saCkptSectionOverwrite (&checkpointHandle,
  260. &sectionId1,
  261. "Overwrite Data #1",
  262. strlen ("Overwrite Data #1") + 1);
  263. printf ("saCkptSectionOverwrite result %d (should be 1)\n", error);
  264. /*
  265. * Test checkpoint read
  266. */
  267. memset (readBuffer1, 0, sizeof (readBuffer1));
  268. memset (readBuffer2, 0, sizeof (readBuffer2));
  269. error = saCkptSectionCreate (&checkpointHandle,
  270. &sectionCreationAttributes2,
  271. "Initial Data #2",
  272. strlen ("Initial Data #2") + 1);
  273. printf ("saCkptSectionCreate result %d (should be 1)\n", error);
  274. error = saCkptCheckpointRead (&checkpointHandle,
  275. ReadVectorElements,
  276. 2,
  277. &erroroneousVectorIndex);
  278. printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  279. printf ("Buffers after checkpoint read\n");
  280. printf (" buffer #1: '%s'\n", readBuffer1);
  281. printf (" buffer #2: '%s'\n", readBuffer2);
  282. //sleep (20);
  283. #ifdef COMPILE_OUT
  284. for (ckptinv = 0; ckptinv < 2000; ckptinv++) {
  285. /*
  286. * Test checkpoint write
  287. */
  288. error = saCkptCheckpointWrite (&checkpointHandle,
  289. WriteVectorElements,
  290. 2,
  291. &erroroneousVectorIndex);
  292. if (error != SA_OK) {
  293. printf ("Writing checkpoint loop %d\n", ckptinv);
  294. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  295. exit (1);
  296. }
  297. }
  298. exit (1);
  299. #endif
  300. error = saCkptCheckpointRead (&checkpointHandle,
  301. ReadVectorElements,
  302. 2,
  303. &erroroneousVectorIndex);
  304. // printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  305. // printf ("Buffers after checkpoint write are:\n");
  306. // printf (" buffer #1: '%s'\n", readBuffer1);
  307. // printf (" buffer #2: '%s'\n", readBuffer2);
  308. error = saCkptCheckpointStatusGet (&checkpointHandle,
  309. &checkpointStatus);
  310. printf ("saCkptCheckpointStatusGet result %d (should be 1)\n", error);
  311. if (error == SA_OK) {
  312. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  313. (int)checkpointStatus.numberOfSections);
  314. }
  315. error = saCkptSectionIteratorInitialize (&checkpointHandle,
  316. 0,
  317. 0,
  318. &sectionIterator);
  319. printf ("saCkptSectionIteratorInitialize result %d (should be 1)\n", error);
  320. /*
  321. * Iterate all sections
  322. */
  323. do {
  324. error = saCkptSectionIteratorNext (&sectionIterator,
  325. &sectionDescriptor);
  326. printf ("saCkptSectionIteratorNext result %d (should be 1)\n", error);
  327. if (error == SA_OK) {
  328. printf ("Section '%s' expires %llx size %d state %x update %llx\n",
  329. sectionDescriptor.sectionId.id,
  330. sectionDescriptor.expirationTime,
  331. sectionDescriptor.sectionSize,
  332. sectionDescriptor.sectionState,
  333. sectionDescriptor.lastUpdate);
  334. }
  335. } while (error == SA_OK);
  336. error = saCkptSectionIteratorFinalize (&sectionIterator);
  337. printf ("saCkptSectionIteratorFinalize result %d (should be 1)\n", error);
  338. return (0);
  339. }