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