testckpt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #define _BSD_SOURCE
  2. /*
  3. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include <sys/time.h>
  46. #include "saAis.h"
  47. #include "saCkpt.h"
  48. #include "sa_error.h"
  49. #ifdef OPENAIS_SOLARIS
  50. #define timersub(a, b, result) \
  51. do { \
  52. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  53. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  54. if ((result)->tv_usec < 0) { \
  55. --(result)->tv_sec; \
  56. (result)->tv_usec += 1000000; \
  57. } \
  58. } while (0)
  59. #endif
  60. #define SECONDS_TO_EXPIRE 4
  61. int ckptinv;
  62. SaInvocationT open_invocation = 16;
  63. void printSaNameT (SaNameT *name)
  64. {
  65. int i;
  66. for (i = 0; i < name->length; i++) {
  67. printf ("%c", name->value[i]);
  68. }
  69. }
  70. SaVersionT version = { 'B', 1, 1 };
  71. SaNameT checkpointName = { 5, "abra\0" };
  72. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  73. SA_CKPT_WR_ALL_REPLICAS,
  74. 100000,
  75. 5000000000LL,
  76. 10,
  77. 200000,
  78. 10
  79. };
  80. SaCkptSectionIdT sectionId1 = {
  81. 14,
  82. (SaUint8T *) "section ID #1"
  83. };
  84. SaCkptSectionIdT sectionId2 = {
  85. 14,
  86. (SaUint8T *) "section ID #2"
  87. };
  88. SaCkptSectionCreationAttributesT sectionCreationAttributes1 = {
  89. &sectionId1,
  90. SA_TIME_END
  91. };
  92. SaCkptSectionCreationAttributesT sectionCreationAttributes2 = {
  93. &sectionId2,
  94. SA_TIME_END
  95. };
  96. char readBuffer1[1025];
  97. char readBuffer2[1025];
  98. char default_read_buffer[1025];
  99. SaCkptIOVectorElementT ReadVectorElements[] = {
  100. {
  101. {
  102. 14,
  103. (SaUint8T *) "section ID #1"
  104. },
  105. readBuffer1,
  106. sizeof (readBuffer1),
  107. 0,
  108. 0
  109. },
  110. {
  111. {
  112. 14,
  113. (SaUint8T *) "section ID #2"
  114. },
  115. readBuffer2,
  116. sizeof (readBuffer2),
  117. 0,
  118. 0
  119. }
  120. };
  121. SaCkptIOVectorElementT default_read_vector[] = {
  122. {
  123. SA_CKPT_DEFAULT_SECTION_ID,
  124. default_read_buffer, /*"written data #1, this should extend past end of old section data", */
  125. sizeof(default_read_buffer), /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  126. 0, //5,
  127. 0
  128. }
  129. };
  130. #define DATASIZE 127000
  131. char data1[DATASIZE];
  132. char data2[DATASIZE];
  133. char default_write_data[56];
  134. SaCkptIOVectorElementT WriteVectorElements[] = {
  135. {
  136. {
  137. 14,
  138. (SaUint8T *) "section ID #1"
  139. },
  140. data1, /*"written data #1, this should extend past end of old section data", */
  141. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  142. 0, //5,
  143. 0
  144. },
  145. {
  146. {
  147. 14,
  148. (SaUint8T *) "section ID #2",
  149. },
  150. data2, /*"written data #2, this should extend past end of old section data" */
  151. DATASIZE, /*sizeof ("written data #2, this should extend past end of old section data") + 1, */
  152. 0, //3,
  153. 0
  154. }
  155. };
  156. SaCkptIOVectorElementT default_write_vector[] = {
  157. {
  158. SA_CKPT_DEFAULT_SECTION_ID,
  159. default_write_data, /*"written data #1, this should extend past end of old section data", */
  160. 56, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  161. 0, //5,
  162. 0
  163. }
  164. };
  165. SaCkptCheckpointHandleT checkpointHandle;
  166. void OpenCallBack (
  167. SaInvocationT invocation,
  168. const SaCkptCheckpointHandleT chckpointHandle,
  169. SaAisErrorT error) {
  170. printf ("%s: This is a call back for open for invocation = %d\n",
  171. get_test_output (error, SA_AIS_OK), (int)invocation);
  172. checkpointHandle = chckpointHandle;
  173. }
  174. SaCkptCallbacksT callbacks = {
  175. &OpenCallBack,
  176. 0
  177. };
  178. int main (void) {
  179. SaCkptHandleT ckptHandle;
  180. SaCkptCheckpointHandleT checkpointHandle2;
  181. SaCkptCheckpointHandleT checkpointHandleRead;
  182. SaCkptCheckpointDescriptorT checkpointStatus;
  183. SaCkptSectionIterationHandleT sectionIterator;
  184. SaCkptSectionDescriptorT sectionDescriptor;
  185. SaUint32T erroroneousVectorIndex = 0;
  186. SaAisErrorT error;
  187. struct timeval tv_start;
  188. struct timeval tv_end;
  189. struct timeval tv_elapsed;
  190. SaSelectionObjectT sel_fd;
  191. fd_set read_set;
  192. int i;
  193. error = saCkptInitialize (&ckptHandle, &callbacks, &version);
  194. printf ("%s: checkpoint initialize\n",
  195. get_test_output (error, SA_AIS_OK));
  196. error = saCkptCheckpointOpenAsync (ckptHandle,
  197. open_invocation,
  198. &checkpointName,
  199. &checkpointCreationAttributes,
  200. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE);
  201. printf ("%s: initial asynchronous open of checkpoint\n",
  202. get_test_output (error, SA_AIS_OK));
  203. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  204. printf ("%s: Retrieve selection object %llu\n",
  205. get_test_output (error, SA_AIS_OK), (unsigned long long)sel_fd);
  206. FD_SET (sel_fd, &read_set);
  207. select (sel_fd + 1, &read_set, 0, 0, 0);
  208. error = saCkptDispatch (ckptHandle, SA_DISPATCH_ALL);
  209. printf ("%s: Dispatch response for open async of checkpoint\n",
  210. get_test_output (error, SA_AIS_OK));
  211. error = saCkptCheckpointClose (checkpointHandle);
  212. printf ("%s: Closing checkpoint\n", get_test_output (error, SA_AIS_OK));
  213. error = saCkptCheckpointOpen (ckptHandle,
  214. &checkpointName,
  215. &checkpointCreationAttributes,
  216. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  217. 0,
  218. &checkpointHandle);
  219. printf ("%s: initial open of checkpoint\n",
  220. get_test_output (error, SA_AIS_OK));
  221. error = saCkptCheckpointRead (checkpointHandle,
  222. default_read_vector,
  223. 1,
  224. &erroroneousVectorIndex);
  225. printf ("%s: Reading default checkpoint section before update\n",
  226. get_test_output (error, SA_AIS_OK));
  227. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  228. memset (default_read_buffer, 0, sizeof (default_read_buffer));
  229. memcpy(default_write_data, "This is an update to the default section date, update#1", 56);
  230. error = saCkptCheckpointWrite (checkpointHandle,
  231. default_write_vector,
  232. 1,
  233. &erroroneousVectorIndex);
  234. printf ("%s: Writing default checkpoint section with data '%s' \n",get_test_output (error, SA_AIS_OK), default_write_data);
  235. error = saCkptCheckpointRead (checkpointHandle,
  236. default_read_vector,
  237. 1,
  238. &erroroneousVectorIndex);
  239. printf ("%s: Reading default checkpoint section \n",
  240. get_test_output (error, SA_AIS_OK));
  241. printf (" default_read_buffer:'%s'\n", default_read_buffer);
  242. error = saCkptSectionCreate (checkpointHandle,
  243. &sectionCreationAttributes1,
  244. "Initial Data #0",
  245. strlen ("Initial Data #0") + 1);
  246. printf ("%s: checkpoint section create\n",
  247. get_test_output (error, SA_AIS_OK));
  248. gettimeofday (&tv_start, 0);
  249. 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));
  250. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  251. &sectionId1,
  252. sectionCreationAttributes1.expirationTime);
  253. printf ("%s: checkpoint section expiration set\n",
  254. get_test_output (error, SA_AIS_OK));
  255. printf ("Please wait, testing expiry of checkpoint sections.\n");
  256. do {
  257. error = saCkptCheckpointRead (checkpointHandle,
  258. ReadVectorElements,
  259. 1,
  260. &erroroneousVectorIndex);
  261. } while (error != SA_AIS_ERR_NOT_EXIST);
  262. gettimeofday (&tv_end, NULL);
  263. timersub (&tv_end, &tv_start, &tv_elapsed);
  264. printf ("Elapsed Time to expiry is %ld.%ld (should be about %d seconds)\n", tv_elapsed.tv_sec, (unsigned long)tv_elapsed.tv_usec, SECONDS_TO_EXPIRE);
  265. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  266. 5000000000LL);
  267. printf ("%s: RetentionDurationSet\n",
  268. get_test_output (error, SA_AIS_OK));
  269. error = saCkptSectionCreate (checkpointHandle,
  270. &sectionCreationAttributes2,
  271. "Initial Data #0",
  272. strlen ("Initial Data #0") + 1);
  273. printf ("%s: Section creation\n",
  274. get_test_output (error, SA_AIS_OK));
  275. error = saCkptCheckpointUnlink (ckptHandle, &checkpointName);
  276. printf ("%s: Unlinking checkpoint\n",
  277. get_test_output (error, SA_AIS_OK));
  278. error = saCkptCheckpointOpen (ckptHandle,
  279. &checkpointName,
  280. &checkpointCreationAttributes,
  281. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  282. 0,
  283. &checkpointHandle2);
  284. printf ("%s: Opening unlinked checkpoint\n",
  285. get_test_output (error, SA_AIS_OK));
  286. error = saCkptCheckpointClose (checkpointHandle);
  287. printf ("%s: Closing checkpoint\n",
  288. get_test_output (error, SA_AIS_OK));
  289. error = saCkptCheckpointOpen (ckptHandle,
  290. &checkpointName,
  291. &checkpointCreationAttributes,
  292. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ,
  293. 0,
  294. &checkpointHandleRead);
  295. printf ("%s: Open checkpoint read only\n",
  296. get_test_output (error, SA_AIS_OK));
  297. error = saCkptCheckpointOpen (ckptHandle,
  298. &checkpointName,
  299. &checkpointCreationAttributes,
  300. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  301. 0,
  302. &checkpointHandle);
  303. printf ("%s: open after unlink/close\n",
  304. get_test_output (error, SA_AIS_OK));
  305. error = saCkptCheckpointRetentionDurationSet (checkpointHandle,
  306. 5000000000LL);
  307. printf ("%s: set checkpoint retention duration\n",
  308. get_test_output (error, SA_AIS_OK));
  309. error = saCkptCheckpointStatusGet (checkpointHandle,
  310. &checkpointStatus);
  311. printf ("%s: Get checkpoint status\n",
  312. get_test_output (error, SA_AIS_OK));
  313. if (error == SA_AIS_OK) {
  314. printf ("Memory used %d in %d sections.\n", (int)checkpointStatus.memoryUsed,
  315. (int)checkpointStatus.numberOfSections);
  316. }
  317. error = saCkptSectionCreate (checkpointHandleRead,
  318. &sectionCreationAttributes1,
  319. "Initial Data #0",
  320. strlen ("Initial Data #0") + 1);
  321. printf ("%s: Create checkpoint section on read only checkpoint\n",
  322. get_test_output (error, SA_AIS_ERR_ACCESS));
  323. error = saCkptSectionCreate (checkpointHandle,
  324. &sectionCreationAttributes1,
  325. "Initial Data #0",
  326. strlen ("Initial Data #0") + 1);
  327. printf ("%s: Create checkpoint section on writeable checkpoint\n",
  328. get_test_output (error, SA_AIS_OK));
  329. error = saCkptSectionCreate (checkpointHandle,
  330. &sectionCreationAttributes1,
  331. "Initial Data #0",
  332. strlen ("Initial Data #0") + 1);
  333. printf ("%s: Create checkpoint section when one already exists\n",
  334. get_test_output (error, 14));
  335. error = saCkptSectionDelete (checkpointHandle,
  336. &sectionId1);
  337. printf ("%s: deleting section handle\n",
  338. get_test_output (error, SA_AIS_OK));
  339. error = saCkptSectionCreate (checkpointHandle,
  340. &sectionCreationAttributes1,
  341. "Initial Data #0",
  342. strlen ("Initial Data #0") + 1);
  343. printf ("%s: replacing deleted checkpoint section\n",
  344. get_test_output (error, SA_AIS_OK));
  345. error = saCkptSectionCreate (checkpointHandle,
  346. &sectionCreationAttributes2,
  347. "Initial Data #2",
  348. strlen ("Initial Data #2") + 1);
  349. printf ("%s: creating section 2 for first time\n",
  350. get_test_output (error, SA_AIS_OK));
  351. error = saCkptSectionCreate (checkpointHandle,
  352. &sectionCreationAttributes2,
  353. "Initial Data #2",
  354. strlen ("Initial Data #2") + 1);
  355. printf ("%s: creating section 2 for second time\n",
  356. get_test_output (error, SA_AIS_ERR_EXIST));
  357. error = saCkptSectionExpirationTimeSet (checkpointHandle,
  358. &sectionId2,
  359. SA_TIME_END);
  360. printf ("%s: Setting expiration time for section 2\n",
  361. get_test_output (error, SA_AIS_OK));
  362. error = saCkptSectionOverwrite (checkpointHandle,
  363. &sectionId1,
  364. "Overwrite Data #1",
  365. strlen ("Overwrite Data #1") + 1);
  366. printf ("%s: overwriting checkpoint section 1\n",
  367. get_test_output (error, SA_AIS_OK));
  368. /*
  369. * Test checkpoint read
  370. */
  371. memset (readBuffer1, 0, sizeof (readBuffer1));
  372. memset (readBuffer2, 0, sizeof (readBuffer2));
  373. error = saCkptCheckpointRead (checkpointHandle,
  374. ReadVectorElements,
  375. 2,
  376. &erroroneousVectorIndex);
  377. printf ("%s: checkpoint read operation",
  378. get_test_output (error, SA_AIS_OK));
  379. printf ("Buffers after checkpoint read\n");
  380. printf (" buffer #1: '%s'\n", readBuffer1);
  381. printf (" buffer #2: '%s'\n", readBuffer2);
  382. for (ckptinv = 0; ckptinv < 10; ckptinv++) {
  383. /*
  384. * Test checkpoint write
  385. */
  386. error = saCkptCheckpointWrite (checkpointHandle,
  387. WriteVectorElements,
  388. 2,
  389. &erroroneousVectorIndex);
  390. if (error != SA_AIS_OK) {
  391. printf ("Writing checkpoint loop %d\n", ckptinv);
  392. printf ("saCkptCheckpointWrite result %d (should be 1)\n", error);
  393. }
  394. }
  395. printf ("%s: Testing checkpoint writes\n",
  396. get_test_output (error, SA_AIS_OK));
  397. error = saCkptCheckpointRead (checkpointHandle,
  398. ReadVectorElements,
  399. 2,
  400. &erroroneousVectorIndex);
  401. // printf ("saCkptCheckpointRead result %d (should be 1)\n", error);
  402. // printf ("Buffers after checkpoint write are:\n");
  403. // printf (" buffer #1: '%s'\n", readBuffer1);
  404. // printf (" buffer #2: '%s'\n", readBuffer2);
  405. error = saCkptCheckpointStatusGet (checkpointHandle,
  406. &checkpointStatus);
  407. printf ("%s: get checkpoint status\n",
  408. get_test_output (error, SA_AIS_OK));
  409. if (error == SA_AIS_OK) {
  410. printf ("Memory used %d in %d sections.\n",
  411. (int)checkpointStatus.memoryUsed,
  412. (int)checkpointStatus.numberOfSections);
  413. }
  414. printf ("iterating all sections 5 times\n");
  415. /*
  416. * iterate all sections 5 times
  417. */
  418. for (i = 0; i < 5; i++) {
  419. error = saCkptSectionIterationInitialize (checkpointHandle,
  420. SA_CKPT_SECTIONS_ANY,
  421. 0,
  422. &sectionIterator);
  423. printf ("%s: initialize section iterator\n",
  424. get_test_output (error, SA_AIS_OK));
  425. /*
  426. * Iterate all sections
  427. */
  428. do {
  429. printf ("Starting iteration\n");
  430. error = saCkptSectionIterationNext (sectionIterator,
  431. &sectionDescriptor);
  432. if (error == SA_AIS_ERR_NO_SECTIONS) {
  433. printf ("No more sections to iterate\n");
  434. } else {
  435. printf ("%s: Get next section in iteration\n",
  436. get_test_output (error, SA_AIS_OK));
  437. }
  438. if (error == SA_AIS_OK) {
  439. printf ("Section '%s' expires %llx size %llu state %x update %llx\n",
  440. sectionDescriptor.sectionId.id,
  441. (unsigned long long)sectionDescriptor.expirationTime,
  442. (unsigned long long)sectionDescriptor.sectionSize,
  443. sectionDescriptor.sectionState,
  444. (unsigned long long)sectionDescriptor.lastUpdate);
  445. }
  446. } while (error == SA_AIS_OK);
  447. error = saCkptSectionIterationFinalize (sectionIterator);
  448. printf ("%s: Finalize iteration\n",
  449. get_test_output (error, SA_AIS_OK));
  450. }
  451. error = saCkptSelectionObjectGet (ckptHandle, &sel_fd);
  452. error = saCkptFinalize (ckptHandle);
  453. printf ("%s: Finalize checkpoint\n",
  454. get_test_output (error, SA_AIS_OK));
  455. return (0);
  456. }