int main (void) { SaCkptHandleT ckpt_handles[CHECKPOINT_THREADS_MAX]; SaCkptCheckpointHandleT checkpoint_handles[CHECKPOINT_THREADS_MAX]; SaAisErrorT error; int size; int i, j; signal (SIGALRM, sigalrm_handler); printf ("Creating (%d) checkpoints.\n", CHECKPOINT_THREADS_MAX); /* * Create CHECPOINT_THREADS_MAX checkpoints */ for (i = 0; i < CHECKPOINT_THREADS_MAX; i++) { sprintf ((char *)checkpointName.value, "checkpoint (%d)", i); checkpointName.length = strlen ((char *)checkpointName.value); do { error = saCkptInitialize (&ckpt_handles[i], &callbacks, &version); } while (error == SA_AIS_ERR_TRY_AGAIN); assert (error == SA_AIS_OK); do { error = saCkptCheckpointOpen (ckpt_handles[i], &checkpointName, &checkpointCreationAttributes, SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE, SA_TIME_END, &checkpoint_handles[i]); } while (error == SA_AIS_ERR_TRY_AGAIN); assert (error == SA_AIS_OK); do { error = saCkptSectionCreate (checkpoint_handles[i], §ionCreationAttributes1, "Initial Data #0", strlen ("Initial Data #0") + 1); } while (error == SA_AIS_ERR_TRY_AGAIN); assert (error == SA_AIS_OK); do { error = saCkptSectionCreate (checkpoint_handles[i], §ionCreationAttributes2, "Initial Data #0", strlen ("Initial Data #0") + 1); } while (error == SA_AIS_ERR_TRY_AGAIN); assert (error == SA_AIS_OK); } for (i = CHECKPOINT_THREADS_START; i < CHECKPOINT_THREADS_MAX; i++) { /* i threads */ printf ("Starting benchmark with (%d) threads.\n", i); size = 10000; /* initial size */ for (j = 0; j < 5; j++) { /* number of runs with i threads */ alarm (10); threaded_bench (ckpt_handles, checkpoint_handles, i, size); size += 1000; } } return (0); }
SaAisErrorT checkpoint_write_seq(ClUint32T seq) { SaAisErrorT rc = SA_AIS_OK; ClUint32T seq_no; /* Putting data in network byte order */ seq_no = htonl(seq); /* Write checkpoint */ rc = saCkptSectionOverwrite(ckpt_handle, &ckpt_sid, &seq_no, sizeof(ClUint32T)); if (rc != SA_AIS_OK) { /* The error SA_AIS_ERROR_NOT_EXIST can occur either because we are not the active replica OR if the overwrite failed because the section did not exist then try to create the section. But in this case we know our application will not attempt to write unless it has set itself as the active replica so the problem MUST be a missing section. */ if ((rc == 0x1000a) || (rc == SA_AIS_ERR_NOT_EXIST)) { SaCkptSectionCreationAttributesT section_cr_attr; /* Expiration time for section can also be varied * once parameters are read from a configuration file */ section_cr_attr.expirationTime = SA_TIME_END; section_cr_attr.sectionId = &ckpt_sid; rc = saCkptSectionCreate(ckpt_handle,§ion_cr_attr,(SaUint8T*) &seq_no, sizeof(ClUint32T)); } if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"Failed [0x%x] to write to section", rc); } } if (rc == SA_AIS_OK) { /* * Synchronize the checkpoint to all the replicas. */ #if 0 /* Synchronous synchronize example */ rc = saCkptCheckpointSynchronize(ckpt_handle, SA_TIME_END ); if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"Failed [0x%x] to synchronize the checkpoint", rc); } #else rc = saCkptCheckpointSynchronizeAsync(ckpt_handle,syncCount); syncCount++; #endif } return rc; }
int main (void) { SaCkptHandleT ckptHandle; SaCkptCheckpointHandleT checkpointHandle; SaAisErrorT error; int i; pthread_t dispatch_thread; error = saCkptInitialize (&ckptHandle, &callbacks, &version); error = saCkptCheckpointOpen (ckptHandle, &checkpointName, &checkpointCreationAttributes, SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE, 0, &checkpointHandle); printf ("first open result %d (should be 1)\n", error); error = saCkptSectionCreate (checkpointHandle, §ionCreationAttributes1, "Initial Data #0", strlen ("Initial Data #0") + 1); printf ("create2 error is %d\n", error); error = saCkptSectionCreate (checkpointHandle, §ionCreationAttributes2, "Initial Data #0", strlen ("Initial Data #0") + 1); printf ("create2 error is %d\n", error); for (i = 0; i < 40; i++) { struct thread_data *td; td = malloc (sizeof (struct thread_data)); td->thread_no = i; pthread_create (&dispatch_thread, NULL, th_dispatch, td); } pthread_join (dispatch_thread, NULL); error = saCkptInitialize (&ckptHandle, &callbacks, &version); return (0); }
int main (void) { SaCkptHandleT ckptHandle; SaCkptCheckpointHandleT checkpointHandle; SaAisErrorT error; int size; int i; signal (SIGALRM, sigalrm_handler); error = saCkptInitialize (&ckptHandle, &callbacks, &version); fail_on_error(error, "saCkptInitialize"); error = saCkptCheckpointOpen (ckptHandle, &checkpointName, &checkpointCreationAttributes, SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE, 0, &checkpointHandle); fail_on_error(error, "saCkptCheckpointOpen"); error = saCkptSectionCreate (checkpointHandle, §ionCreationAttributes1, "Initial Data #0", strlen ("Initial Data #0") + 1); fail_on_error(error, "saCkptCheckpointSectionCreate"); error = saCkptSectionCreate (checkpointHandle, §ionCreationAttributes2, "Initial Data #0", strlen ("Initial Data #0") + 1); fail_on_error(error, "saCkptCheckpointSectionCreate"); size = 1; for (i = 0; i < 50; i++) { /* number of repetitions - up to 50k */ ckpt_benchmark (checkpointHandle, size); size += 1000; } error = saCkptFinalize (ckptHandle); return (0); }
static SaAisErrorT alarmClockCkptSectionCreate(SaCkptCheckpointHandleT ckpt_hdl, ClUint32T section_index) { SaCkptSectionCreationAttributesT section_cr_attr; SaAisErrorT ret_code = SA_AIS_OK; if(section_index > NUM_SECTION_ID_MAPS) return SA_AIS_ERR_INVALID_PARAM; memset(§ion_cr_attr, 0, sizeof(section_cr_attr)); /* Expiration time for section can also be varied * once parameters are read from a configuration file */ section_cr_attr.expirationTime = SA_TIME_END; section_cr_attr.sectionId = §ionIdMap[section_index-1].ctrlSectionId; ret_code = saCkptSectionCreate(ckpt_hdl, §ion_cr_attr, NULL, 0); section_cr_attr.sectionId = §ionIdMap[section_index-1].sectionId; ret_code |= saCkptSectionCreate(ckpt_hdl, §ion_cr_attr, NULL, 0); sectionCreated = CL_TRUE; return ret_code; }
int main () { SaAisErrorT error; SaNameT* WriteCheckpointName = (SaNameT*) malloc(sizeof(SaNameT)); char name[10]; SaCkptHandleT ckptHandle; sprintf(name,"ckpt%d",1); int namelen = strlen(name) + 1; memcpy(WriteCheckpointName->value, name, namelen); WriteCheckpointName->length = namelen; error = saCkptInitialize (&ckptHandle, &callbacks, &version); error = saCkptCheckpointOpen ( ckptHandle, WriteCheckpointName, &checkpointCreationAttributes, SA_CKPT_CHECKPOINT_WRITE, 1000000000, /* 1 Second */ &WriteCheckpointHandle); if (error != SA_AIS_OK) { fprintf(stderr,"saCkptCheckpointOpen result %d (should be 1)\n", error); return error; } error = saCkptSectionCreate ( WriteCheckpointHandle, §ionCreationAttributes, "Initial Data #0", strlen ("Initial Data #0") + 1); if (error != SA_AIS_OK) { fprintf(stderr,"saCkptSectionCreate result = %d\n", error); return error; } struct timespec tv; tv.tv_sec = 0; tv.tv_nsec = 15000000; //15 milliseconds while(1) { process_message(); nanosleep(&tv,NULL); } return 1; }
SaAisErrorT alarmClockCkptActivate(SaCkptCheckpointHandleT ckptHdl, ClUint32T section_num) { SaAisErrorT rc = SA_AIS_OK; SaCkptSectionIdT id; id.idLen = sizeof(section_num); id.id = (ClUint8T*)§ion_num; rc = saCkptActiveReplicaSet(ckptHdl); if(rc != SA_AIS_OK) { alarmClockLogWrite(CL_LOG_SEV_ERROR, "alarmClockReplicaSet: returned [%#x]", rc); return rc; } rc = clCkptSectionCheck(ckptHdl, (ClCkptSectionIdT*)&id); if(rc != CL_OK) { if(CL_GET_ERROR_CODE(rc) == CL_ERR_NOT_EXIST) { SaCkptSectionCreationAttributesT attr; attr.sectionId = &id; attr.expirationTime = (ClTimeT)CL_TIME_END; rc = saCkptSectionCreate(ckptHdl, &attr, NULL, 0); if(rc == SA_AIS_OK) { alarmClockLogWrite(CL_LOG_SEV_INFO, "alarmClockActivate: Section [%d] created successfully", section_num); } } else rc = SA_AIS_ERR_UNAVAILABLE; /* internal error */ } else rc = SA_AIS_OK; if(rc != SA_AIS_OK) { alarmClockLogWrite(CL_LOG_SEV_INFO, "alarmClockActivate: Section operation on section [%d] " "failed with [%#x]", section_num, rc); } return rc; }
/******************************************************************************* Feature API: alarmClockCkptWrite Description : This API will not attempt to validate arguments, the thinking being that this is a volitional act by the invoker of this API to ensure that invalid parameters can be handled gracefully by the subsystem Arguments In: 1. ClCkptHdlT : ckpt handle 2. section number 3. data to write 4. size of data to write *******************************************************************************/ SaAisErrorT alarmClockCkptWrite ( SaCkptCheckpointHandleT ckpt_hdl, SaUint32T section_num, void* data, SaUint32T data_size ) { SaAisErrorT ret_code = SA_AIS_OK; SaCkptSectionIdT section_id; section_id.id = (SaUint8T*)§ion_num; section_id.idLen = sizeof(section_num); ret_code = saCkptSectionOverwrite(ckpt_hdl, §ion_id, data, data_size); if (ret_code != SA_AIS_OK) { /* If the overwrite failed because the section did not exist then try to create the section */ if ((ret_code == 0x1000a) || (ret_code == SA_AIS_ERR_NOT_EXIST)) { SaCkptSectionCreationAttributesT section_cr_attr; /* Expiration time for section can also be varied * once parameters are read from a configuration file */ section_cr_attr.expirationTime = SA_TIME_END; section_cr_attr.sectionId = §ion_id; ret_code = saCkptSectionCreate(ckpt_hdl,§ion_cr_attr,data, data_size); } } if (ret_code != SA_AIS_OK) { alarmClockLogWrite(CL_LOG_SEV_ERROR, "alarmClockCkptWrite(hdl=%llx): Failed to write: 0x%x\n", ckpt_hdl, ret_code); } else { alarmClockLogWrite(CL_LOG_SEV_DEBUG, "alarmClockCkptWrite: wrote %d bytes to section: %d\n", data_size, section_num); } return ret_code; }
int ckpt_write(void *hp, const char *secid, void *buf, size_t maxlen) { ckpt_handle *h = (ckpt_handle *)hp; SaCkptIOVectorElementT iov = {SA_CKPT_DEFAULT_SECTION_ID, NULL, 0, 0, 0}; SaAisErrorT err; SaCkptSectionCreationAttributesT attrs; VALIDATE(h); /* Set section ID here */ iov.sectionId.id = (uint8_t *)secid; iov.sectionId.idLen = strlen(secid); iov.dataBuffer = buf; iov.dataSize = (SaSizeT)maxlen; iov.dataOffset = 0; iov.readSize = 0; err = saCkptCheckpointWrite(h->ck_checkpoint, &iov, 1, NULL); if (err == SA_AIS_ERR_NOT_EXIST) { attrs.sectionId = &iov.sectionId; attrs.expirationTime = SA_TIME_END; err = saCkptSectionCreate(h->ck_checkpoint, &attrs, buf, maxlen); } if (err == SA_AIS_OK) saCkptCheckpointSynchronize(h->ck_checkpoint, h->ck_timeout); errno = ais_to_posix(err); if (errno) return -1; return maxlen; /* XXX */ }
/**************************************************************************** * Name : cpsv_test_sync_app_process * * Description : This is the function which is given as the input to the * Application task. * * Arguments : info - This is the information which is passed during * spawing Application task. * * Return Values : None. * * Notes : None. *****************************************************************************/ void cpsv_test_sync_app_process(void *info) { SaCkptHandleT ckptHandle; SaCkptCheckpointHandleT checkpointHandle; SaCkptCallbacksT callbk; SaVersionT version; SaNameT ckptName; SaAisErrorT rc; SaCkptCheckpointCreationAttributesT ckptCreateAttr; SaCkptCheckpointOpenFlagsT ckptOpenFlags; SaCkptSectionCreationAttributesT sectionCreationAttributes; SaCkptIOVectorElementT writeVector, readVector; SaUint32T erroneousVectorIndex; void *initialData = "Default data in the section"; unsigned char read_buff[100] = {0}; SaTimeT timeout = 1000000000; unsigned int temp_var = (unsigned int)(long)info; memset(&ckptName, 0, 255); ckptName.length = strlen(DEMO_CKPT_NAME); memcpy(ckptName.value,DEMO_CKPT_NAME,strlen(DEMO_CKPT_NAME)); callbk.saCkptCheckpointOpenCallback = AppCkptOpenCallback; callbk.saCkptCheckpointSynchronizeCallback = AppCkptSyncCallback; version.releaseCode= 'B'; version.majorVersion = 2; version.minorVersion = 2; printf("*******************************************************************\n"); printf("Demonstrating Checkpoint Service Usage with a collocated Checkpoint \n"); printf("*******************************************************************\n"); sleep(2); printf("Initialising With Checkpoint Service....\n"); rc = saCkptInitialize(&ckptHandle,&callbk,&version); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); ckptCreateAttr.creationFlags = SA_CKPT_CHECKPOINT_COLLOCATED|SA_CKPT_WR_ACTIVE_REPLICA; ckptCreateAttr.checkpointSize = 1024; ckptCreateAttr.retentionDuration= 100000; ckptCreateAttr.maxSections= 2; ckptCreateAttr.maxSectionSize = 700; ckptCreateAttr.maxSectionIdSize = 4; ckptOpenFlags = SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE; printf("Opening Collocated Checkpoint = %s with create flags....\n",ckptName.value); rc = saCkptCheckpointOpen(ckptHandle,&ckptName,&ckptCreateAttr,ckptOpenFlags,timeout,&checkpointHandle); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); if(temp_var == 1) { printf("Setting the Active Replica for my checkpoint ....\t"); rc = saCkptActiveReplicaSet(checkpointHandle); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); sectionCreationAttributes.sectionId = (SaCkptSectionIdT*) malloc(sizeof \ (SaCkptSectionIdT)); sectionCreationAttributes.sectionId->id = (unsigned char *)"11"; sectionCreationAttributes.sectionId->idLen = 2; sectionCreationAttributes.expirationTime = 3600000000000ll; /* One Hour */ printf("Created Section ....\t"); rc = saCkptSectionCreate(checkpointHandle,§ionCreationAttributes,initialData,28); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); writeVector.sectionId.id = (unsigned char *)"11"; writeVector.sectionId.idLen = 2; writeVector.dataBuffer = "The Checkpoint Service provides a facility for processes to store checkpoint data"; writeVector.dataSize = strlen(writeVector.dataBuffer); writeVector.dataOffset = 0; writeVector.readSize = 0; printf("Writing to Checkpoint %s ....\n",DEMO_CKPT_NAME); printf("Section-Id = %s ....\n",writeVector.sectionId.id); printf("CheckpointData being written = \"%s\"\n",(char *)writeVector.dataBuffer); printf("DataOffset = %llu ....\n",writeVector.dataOffset); rc = saCkptCheckpointWrite(checkpointHandle,&writeVector,1,&erroneousVectorIndex); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); sleep(1); printf("Press <Enter> key to continue...\n"); getchar(); } else { sleep(4); readVector.sectionId.id = (unsigned char *)"11"; readVector.sectionId.idLen = 2; readVector.dataBuffer = read_buff; readVector.dataSize = 90; readVector.dataOffset = 0; printf("Waiting to Read from Checkpoint %s....\n",DEMO_CKPT_NAME); printf("Press <Enter> key to continue...\n"); getchar(); rc = saCkptCheckpointRead(checkpointHandle,&readVector,1,&erroneousVectorIndex); printf("Checkpoint Data Read = \"%s\"\n",(char *)readVector.dataBuffer); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); } printf("Synchronizing My Checkpoint being called ....\n"); rc = saCkptCheckpointSynchronize(checkpointHandle,timeout); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); if(temp_var==1) { printf("Unlink My Checkpoint ....\t"); rc = saCkptCheckpointUnlink(ckptHandle,&ckptName); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); } printf("Ckpt Closed ....\t"); rc = saCkptCheckpointClose(checkpointHandle); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); printf("Ckpt Finalize being called ....\t"); rc = saCkptFinalize(ckptHandle); if(rc == SA_AIS_OK) printf("PASSED \n"); else printf("Failed \n"); sleep(2); return; }
static ClRcT checkpoint_initialize() { SaAisErrorT rc = SA_AIS_OK; SaVersionT ckpt_version = {'B', 1, 1}; SaNameT ckpt_name = { strlen(CKPT_NAME), CKPT_NAME }; SaUint32T seq_no; SaCkptCheckpointCreationAttributesT create_atts = { .creationFlags = SA_CKPT_WR_ACTIVE_REPLICA_WEAK | SA_CKPT_CHECKPOINT_COLLOCATED, .checkpointSize = sizeof(SaUint32T), .retentionDuration = (SaTimeT)0, .maxSections = 2, // default section, plus section we create .maxSectionSize = sizeof(SaSizeT), .maxSectionIdSize = (SaSizeT)64 }; SaCkptSectionCreationAttributesT section_atts = { .sectionId = &ckpt_sid, .expirationTime = SA_TIME_END }; clprintf(CL_LOG_SEV_INFO,"%s: checkpoint_initialize\n", appname); /* Initialize checkpointing service instance */ rc = saCkptInitialize(&ckpt_svc_handle, /* Checkpoint service handle */ NULL, /* Optional callbacks table */ &ckpt_version); /* Required verison number */ if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"%s: ERROR: Failed to initialize checkpoint service\n", appname); return rc; } clprintf(CL_LOG_SEV_INFO,"%s: Checkpoint service initialized (handle=0x%x)\n", appname,(unsigned int) ckpt_svc_handle); // // Create the checkpoint for read and write. If we fail to open it // then open it without CREATE. The retry loop is to deal with the // possibility that multiple processes might all come along here // and fail to open the checkpoint because it hasn't been created // yet and then they all try to create the checkpoint. All but // the first would fail. This retry allows all the rest of the // processes to come through and open without CREATE // TODO: change it to a straight retry loop where we start off with // mode = (CL_CKPT_CHECKPOINT_READ | CL_CKPT_CHECKPOINT_WRITE) // and on each iteration of the loop we xor in // CL_CKPT_CHECKPOINT_CREATE. That should let us do the same thing // but without the duplication of the clCkptCheckpointOpen call rc = saCkptCheckpointOpen(ckpt_svc_handle, // Service handle &ckpt_name, // Checkpoint name &create_atts, // Optional creation attr. (SA_CKPT_CHECKPOINT_READ | SA_CKPT_CHECKPOINT_WRITE | SA_CKPT_CHECKPOINT_CREATE), (SaTimeT)-1, // No timeout &ckpt_handle); // Checkpoint handle if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"%s: ERROR: Failed [0x%x] to open checkpoint\n", appname, rc); (void)saCkptFinalize(ckpt_svc_handle); return rc; } clprintf(CL_LOG_SEV_INFO,"%s: Checkpoint opened (handle=0x%x)\n", appname, (unsigned int) ckpt_handle); /* * Try to create a section so that updates can operate by overwriting * the section over and over again. * If subsequent processes come through here, they will fail to create * the section. That is OK, even though it will cause an error message * If the section create fails because the section is already there, then * read the sequence number */ // Put data in network byte order seq_no = htonl(seq); // Creating the section checkpoint_replica_activate(); rc = saCkptSectionCreate(ckpt_handle, // Checkpoint handle §ion_atts, // Section attributes (SaUint8T*)&seq_no, // Initial data (SaSizeT)sizeof(seq_no)); // Size of data if (rc != SA_AIS_OK && rc != SA_AIS_ERR_EXIST) { clprintf(CL_LOG_SEV_ERROR,"%s: ERROR: Failed [0x %x] to create checkpoint section\n", appname, rc); (void)saCkptCheckpointClose(ckpt_handle); (void)saCkptFinalize(ckpt_svc_handle); return rc; } else if (rc != SA_AIS_OK && rc == SA_AIS_ERR_EXIST) { rc = checkpoint_read_seq(&seq); if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"%s: ERROR: Failed [0x%x] to read checkpoint section\n", appname, rc); (void)saCkptCheckpointClose(ckpt_handle); (void)saCkptFinalize(ckpt_svc_handle); return rc; } } else { clprintf(CL_LOG_SEV_INFO,"%s: Section created\n", appname); } /* Open checkpoint for read to write depending on input argument */ return rc; } static SaAisErrorT checkpoint_finalize(void) { SaAisErrorT rc; rc = saCkptCheckpointClose(ckpt_handle); if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"%s: failed: [0x%x] to close checkpoint handle %d\n", appname, rc, (unsigned int) ckpt_handle); } rc = saCkptFinalize(ckpt_svc_handle); if (rc != SA_AIS_OK) { clprintf(CL_LOG_SEV_ERROR,"%s: failed: [0x%x] to finalize checkpoint\n", appname, rc); } return SA_AIS_OK; }