testckpt.c 13 KB

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