Exemplo n.º 1
0
/**
 * Adds the key-value pair to the mapreduce data structure.  This may
 * require a reduce() operation.
 *
 * @param key
 *    The key of the key-value pair.  The key has been malloc()'d by
 *    read_from_fd() and must be free()'d by you at some point.
 * @param value
 *    The value of the key-value pair.  The value has been malloc()'d
 *    by read_from_fd() and must be free()'d by you at some point.
 * @param mr
 *    The pass-through mapreduce data structure (from read_from_fd()).
 */
static void process_key_value(const char *key, const char *value, mapreduce_t *mr)
{
	unsigned long rev;
	const char * oldVal= datastore_get(mr->data, key, &rev);
	if(!oldVal){
		datastore_put(mr->data, key, value);
	}
	else{
		const char * newVal= mr->myreduce(oldVal, value);
		datastore_update(mr->data, key, newVal, rev);
		free((char *)newVal);
	}
	if(oldVal){
		free((char *)oldVal);
	}
	free((char *)value);
	free((char *)key);
}
Exemplo n.º 2
0
/**
 * Adds the key-value pair to the mapreduce data structure.  This may
 * require a reduce() operation.
 *
 * @param key
 *    The key of the key-value pair.  The key has been malloc()'d by
 *    read_from_fd() and must be free()'d by you at some point.
 * @param value
 *    The value of the key-value pair.  The value has been malloc()'d
 *    by read_from_fd() and must be free()'d by you at some point.
 * @param mr
 *    The pass-through mapreduce data structure (from read_from_fd()).
 */
static void process_key_value(const char *key, const char *value, mapreduce_t *mr)
{
	//printf("DO YOU EVEN COME HERE?\n");
	datastore_t * curr = mr -> dictionary; 
	char * old; 
	char * add; 
	unsigned long * rev = malloc(sizeof(unsigned long)); 

	//get it - if null, put 
	//else temp = reduce, then update it 
	//printf("This is the pair (%s,%s)\n", key, value);

	old = (char*)datastore_get(curr, key, (unsigned long *)rev);

	if (old == NULL){
		//printf("***************************************************\n");
		datastore_put(curr, key, value);
		free( (char *)key);
		free((char *) value);
		free (rev);
		return; 
	}
	else {
		old = (char*)datastore_get(curr, key, (unsigned long *)rev);
		//printf("this is old %s\n", old);
		//fprintf(stderr,"THIS IS THE VALUE OF MR %s\n", (char *) mr);
		//printf("this is the value of old %s and the value of new %s\n", old, value);


		if (old != NULL){
		add = (char*) mr->myreduce_function(old, value);
		//printf("this is the one we will add %s\n", add);
		free(old); 

		//printf("This is what is being sent in..... curr -> %s, key -> %s, add -> %s, rev -> %lu\n", curr, key, add, *rev);
		//printf("this is the key %s\n", key);

		datastore_update(curr, key, add, *rev);
		free(add);}
	}
	free((char *) value);
	free((char *) key);
	free((char *) rev);
}
Exemplo n.º 3
0
/**
 * Adds the key-value pair to the mapreduce data structure.  This may
 * require a reduce() operation.
 *
 * @param key
 *    The key of the key-value pair.  The key has been malloc()'d by
 *    read_from_fd() and must be free()'d by you at some point.
 * @param value
 *    The value of the key-value pair.  The value has been malloc()'d
 *    by read_from_fd() and must be free()'d by you at some point.
 * @param mr
 *    The pass-through mapreduce data structure (from read_from_fd()).
 */
static void process_key_value(const char *key, const char *value, mapreduce_t *mr)
{       
	// printf("key=%s value=%s  \n",key,value);
       // pthread_mutex_lock(&m);
       unsigned long ret_value; int k=0;
       ret_value=datastore_put(&mr->ds,key,value);
      // printf("ret_value is %d\n",ret_value);
       if(ret_value==0)
         {   
        const char *val=NULL;
        const char *val2=NULL;
		 unsigned long ret_get_value;
                
		 val=datastore_get(&mr->ds,key,&ret_get_value);
                 //printf("before reduce valu1=%s value2=%s revision\n",val,value);
                 val2=mr->myreduce(value,val);
                 //printf("after reduce %s \n",val);   
                 k=datastore_update(&mr->ds,key,val2,ret_get_value);
      	
	//if(val2!=NULL) {
	free(val2);
        //printf("free val2 %s\n",val2);
	val2=NULL;
	//}
        //if(val!=NULL)	{ 
	free(val);
	//printf("free val %s \n",val);
	val= NULL;     
	//}
	}
	 //if(key!=NULL)
         free(key);
         key=NULL;
 	 //if(value!=NULL){
         //printf("free value %s \n",value);
         free(value);
           value=NULL;
        //}
 	//pthread_mutex_unlock(&m);	
       //printf("key=%s value=%s revision=%d \n",key,value,revision);
}
Exemplo n.º 4
0
jsonres_t process_request(const char * uri, jsonreq_t reqss)
{
    jsonres_t response;
    datastore_entry_t request;
    request.key = reqss.key;
    request.value = reqss.value;
    request.rev = reqss.rev;
    if (strcmp(uri,"/add")==0)
    {
	if(datastore_put(map,request.key, request.value)==0){
	response.success = "KEY ALREADY EXISTS";
	//response.value = request.value;
	response.rev = 0;
	}
	else{
	response.success = "true";
	response.value = request.value;
	response.rev = 0;
	}
	return response;
    }
    else if (strcmp(uri,"/update")==0){
    unsigned long ret;
    ret = datastore_update(map, request.key, request.value,request.rev);
    if(ret == 0)
    {
    response.success = "KEY DOES NOT EXIST";
	//response.value = "0";
	response.rev = 0;
    }
    else if (ret == -1)
    {
    response.success = "REVISION NUMBER DOES NOT MATCH";
	//response.value = request.value;
	response.rev = 0;
    }
    else
    {
    response.success = "true";
	response.value = request.value;
	response.rev = (int)ret;
    }
    return response;
    }
    else if (strcmp(uri,"/get")==0){
    char* ret = NULL;
    int r = 0;
    ret = datastore_get(map, request.key, &r);
    if(ret==NULL)
    {
    response.success = "KEY DOES NOT EXIST";
	//response.value = "0";
	response.rev = 0;
    }
    else
    {
    response.success = "true";
	response.value = ret;
	response.rev = r;
    }
    return response;
    }
    else if (strcmp(uri,"/delete")==0){
    int dd = datastore_delete(map,request.key);
    if(dd==0)
    {
    response.success = "KEY DOES NOT EXIST";
	//response.value = "0";
	response.rev = 0;
    }
    else
    {
    response.success = "true";
	//response.value = "0";
	response.rev = 0;
    }
    return response;
    }
    return response;


}
Exemplo n.º 5
0
int main (int argc, char ** argv)
{
    // loop
    // 1. get the metadata catalog
    // 2. select oldest version of one var
    // 3. mark as in process in MD
    // 4. retrieve list of chunks for this var
    // 5. process
    // 6. write out new chunks
    // 7. make new MD entries
    // 8. delete old var version
    // end after x versions

    int size;
    int rank;

    int rank_to_fail = -1;
    if (argc > 1)
        rank_to_fail = atoi (argv [1]);

    memset (&timing, 0, sizeof (struct timing_metrics));

    MPI_Init (&argc, &argv);
    MPI_Comm_size (MPI_COMM_WORLD, &size);
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);

    // number of sub-coordinators (min 2, otherwise 256 procs per)
    uint32_t num_groups = (size < 512) ? 2 : size / 256;
    struct transaction_comm * comm_id; // which comm we initialized for transactions
    
    struct md_config metadata_config;
    struct metadata_server * md_server;
    struct md_config metadata_config2;
    struct metadata_server * md_server2;

    struct ds_config datastore_config;
    struct datastore_server * ds_server;
    struct ds_config datastore_config2;
    struct datastore_server * ds_server2;

    // global setup for this communicator. num_groups will probably fold into
    // itself shortly
    comm_id = txn_init(MPI_COMM_WORLD, num_groups);

    logger_init ((log_level) atoi (getenv ("NSSI_LOG_LEVEL")), NULL);

    // for the sim to write to the first staging area
    PERFORM (metadata_get_config_from_env ("METADATA_CONFIG_FILE", &metadata_config));
    PERFORM (metadata_init (metadata_config.server_urls [0], &md_server));
    // for the analysis code to write to the next step analysis process
    PERFORM (metadata_get_config_from_env ("METADATA_CONFIG_FILE2", &metadata_config2));
    PERFORM (metadata_init (metadata_config2.server_urls [0], &md_server2));

    // for the sim to write to the first staging area
    PERFORM (datastore_get_config_from_env ("DATASTORE_CONFIG_FILE", &datastore_config));
    PERFORM (datastore_init (datastore_config.server_urls [0], &ds_server));
    // for the analysis code to write to the next step analysis process
    PERFORM (datastore_get_config_from_env ("DATASTORE_CONFIG_FILE2", &datastore_config2));
    PERFORM (datastore_init (datastore_config2.server_urls [0], &ds_server2));

    int rc = NSSI_OK;
    uint32_t catalog_items = 0;
    struct md_catalog_entry * entries = NULL;
    uint32_t chunk_items = 0;
    struct md_chunk_entry * chunks = NULL;
    uint32_t var_version = 0;
    uint64_t old_var_id = 0;
    uint64_t new_var_id = 0;
    const char * var_name = "A";
    const char * var_path = "/";

    uint64_t txid = 10;
    txn_vote vote = TXN_VOTE_UNKNOWN;
    transaction * mdtrans = NULL;
    struct _subtxn
    {
        const char * name;
        sub_transaction * sub;
    } sub_txns [] = {{"metadata_catalog", NULL}   // 0
                    ,{"get_chunk_list",NULL}      // 1
                    ,{"processing_var",NULL}      // 2
                    ,{"datastore_get",NULL}       // 3
                    ,{"create_var",NULL}          // 4
                    ,{"datastore_put",NULL}       // 5
                    ,{"delete_var",NULL}          // 6
                    ,{"location_remove",NULL}     // 7
                    ,{"activate_var",NULL}        // 8
                    ,{"pre-begin-test",NULL}      // 9
                    };
    
    // create a transaction.
    mdtrans = txn_create_transaction (comm_id, txid);

//============================================================================
    if (rank == 0)
    {
        // begin the transaction. Any add_sub_txn before this will be done as a
        // single action rather than individually

        // create a sub transaction for the metadata_catalog call
        sub_txns [0].sub = txn_create_sub_transaction(mdtrans, sub_txns [0].name, rank);
        assert (sub_txns [0].sub);

        // create a sub transaction for the metadata_get_chunk_list call
        sub_txns [1].sub = txn_create_sub_transaction(mdtrans, sub_txns [1].name, rank);
        assert (sub_txns [1].sub);

        // Creating sub transaction for metadata_processing_var call.
        sub_txns [2].sub = txn_create_sub_transaction(mdtrans, sub_txns [2].name, rank);
        assert (sub_txns [2].sub);

        // sub_txn [3] is _all

        // create the new var that we are writing
        sub_txns [4].sub = txn_create_sub_transaction(mdtrans, sub_txns [4].name, rank);
        assert (sub_txns [4].sub);

        // sub_txn [5] is _all

        // delete the var once we are done processing and already wrote
        sub_txns [6].sub = txn_create_sub_transaction(mdtrans, sub_txns [6].name, rank);
        assert (sub_txns [6].sub);

        // sub_txn [7] is _all

        // delete the var once we are done processing and already wrote
        sub_txns [8].sub = txn_create_sub_transaction(mdtrans, sub_txns [8].name, rank);
        assert (sub_txns [8].sub);
    }
    // make one global sub-txn before the begin transaction to see if that works
    sub_txns [9].sub = txn_create_sub_transaction_all(mdtrans, sub_txns [9].name, rank);
    assert (sub_txns [9].sub);
//============================================================================

    int pass = txn_begin_transaction(mdtrans);
    assert (pass == TXN_SUCCESS);

    if(pass != TXN_SUCCESS)
    {
        txn_finalize (comm_id, true);
        mdtrans = NULL;
        fprintf(stderr, "transaction could not be initialized.\n");
        // do something to fail.
    }

    // get the first var at the root
    // get the list of chunks
    // scatter a chunk to each process for analysis routines
    // mark the var as 'in process' hiding it from others
    if (rank == 0)
    {
        // sub_txns [0]
        PERFORM (rc = metadata_catalog (md_server, txid, &catalog_items, &entries));
        PERFORM (print_catalog (catalog_items, entries));
        // if it succeeded, then we can commit.
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [0].sub);
            vote = TXN_VOTE_COMMIT;
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [0].sub);
            vote = TXN_VOTE_ABORT;
        }

#if DO_OPS
        if (strcmp (entries [0].name, var_name) == 0 && vote == TXN_VOTE_COMMIT)
        {
            var_version = entries [0].version;
            old_var_id = entries [0].var_id;

            // sub_txns [1]
            rc = metadata_get_chunk_list (md_server, txid, var_name, var_path, var_version, &chunk_items, &chunks);
            print_chunks (chunk_items, chunks);
            // if it's successful, we can commit this.
#endif
            if(rc == NSSI_OK)
            {
                txn_commit_sub_transaction (mdtrans, sub_txns [1].sub);
            }
            else
            {
                txn_abort_sub_transaction (mdtrans, sub_txns [1].sub);
                vote = TXN_VOTE_ABORT;
            }
#if DO_OPS
            MPI_Scatter (chunks, sizeof (struct md_chunk_entry), MPI_CHAR, MPI_IN_PLACE, sizeof (struct md_chunk_entry), MPI_CHAR, 0, MPI_COMM_WORLD);
        }
#endif

        if (vote == TXN_VOTE_COMMIT)
        {
            // sub_txn [2]
            PERFORM (rc = metadata_processing_var (md_server, txid, var_name, var_path, var_version));
            if(rc == NSSI_OK)
            {
                txn_commit_sub_transaction (mdtrans, sub_txns [2].sub);
            }
            else
            {
                txn_abort_sub_transaction (mdtrans, sub_txns [2].sub);
                vote = TXN_VOTE_ABORT;
            }
        }
    }
    else
    {
        chunks = (struct md_chunk_entry *) malloc (sizeof (struct md_chunk_entry));
#if DO_OPS
        MPI_Scatter (NULL, 0, MPI_CHAR, chunks, sizeof (struct md_chunk_entry), MPI_CHAR, 0, MPI_COMM_WORLD);
#endif
    }

    size_t chunk_size;
    void * chunk_data;

    if (vote != TXN_VOTE_ABORT)
    {
        sub_txns [3].sub = txn_create_sub_transaction_all (mdtrans, sub_txns [3].name, rank);
        assert (sub_txns [3].sub);
        PERFORM (rc = datastore_location_get (ds_server, txid, chunks [0].chunk_id, chunks [0].connection, &chunk_size, &chunk_data));
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [3].sub);
            vote = TXN_VOTE_COMMIT;
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [3].sub);
            vote = TXN_VOTE_ABORT;
        }
    }

    // process the chunks
    double * d = (double *) chunk_data;
    size_t chunk_item_count = chunk_size / sizeof (double);

#if DO_OPS
    for (int i = 0; i < chunk_item_count; i++)
    {
        d [i] *= rank; // just something stupid for now
    }
#endif

    // write to the next ds and md service
    if (rank == 0)
    {
        PERFORM (rc = metadata_create_var (md_server2, txid, &new_var_id, &entries [0]));
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [4].sub);
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [4].sub);
            vote = TXN_VOTE_ABORT;
        }
        PERFORM (MPI_Bcast (&new_var_id, 1, MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD));
    }
    else
    {
        PERFORM (MPI_Bcast (&new_var_id, 1, MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD));
    }

if (rank != rank_to_fail)
{
    sub_txns [5].sub = txn_create_sub_transaction_all (mdtrans, sub_txns [5].name, rank);
}
if (rank != rank_to_fail)
{
    assert (sub_txns [5].sub);
    if (vote == TXN_VOTE_COMMIT)
    {
        PERFORM (rc = datastore_put (ds_server2, txid, &chunks [0].chunk_id, chunk_size, chunk_data));
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [5].sub);
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [5].sub);
            vote = TXN_VOTE_ABORT;
        }
    }

    PERFORM (metadata_insert_chunk (md_server2, new_var_id, &chunks [0]));

    // remove the chunks and md from the original set
    if (rank == 0)
    {
        PERFORM (rc = metadata_delete_var (md_server, old_var_id, var_name, var_path, var_version));
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [6].sub);
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [6].sub);
        }
    }

    sub_txns [7].sub = txn_create_sub_transaction_all (mdtrans, sub_txns [7].name, rank);
    assert (sub_txns [7].sub);
    PERFORM (rc = datastore_location_remove (ds_server, chunks [0].connection, txid, chunks [0].chunk_id));
    if(rc == NSSI_OK)
    {
        txn_commit_sub_transaction (mdtrans, sub_txns [7].sub);
    }
    else
    {
        txn_abort_sub_transaction (mdtrans, sub_txns [7].sub);
    }

    if (rank == 0)
    {
        // activate the var in the new md
        PERFORM (rc = metadata_activate_var (md_server2, txid, var_name, var_path, var_version));
        PERFORM (rc = datastore_activate (ds_server2, txid,  chunks [0].chunk_id));
        if(rc == NSSI_OK)
        {
            txn_commit_sub_transaction (mdtrans, sub_txns [8].sub);
        }
        else
        {
            txn_abort_sub_transaction (mdtrans, sub_txns [8].sub);
        }
    }
    txn_commit_sub_transaction (mdtrans, sub_txns [9].sub);

    if (chunks)
        free (chunks);

    // see where things are now
    if (entries)
        free (entries);

#if 0
switch (vote)
{
    case TXN_VOTE_COMMIT: printf ("rank: %d vote: TXN_VOTE_COMMIT\n", rank); fflush (stdout);
break;
    case TXN_VOTE_ABORT: printf ("rank: %d vote: TXN_VOTE_ABORT\n", rank); fflush (stdout);
break;
    case TXN_VOTE_UNKNOWN: printf ("rank: %d vote: TXN_VOTE_UNKNOWN\n", rank); fflush (stdout);
break;
}
#endif
    pass = txn_vote_transaction(mdtrans, rank, vote);
    if(pass == TXN_SUCCESS && vote == TXN_VOTE_COMMIT){
        pass = txn_commit_transaction(mdtrans);
    }else{
        pass = txn_abort_transaction(mdtrans);
    }
    // if the protocol fails to commit after pass, then we have to abort.

    txn_finalize_transaction (mdtrans);

    // no need to do this within a transaction because it is read only and
    // we do not need to read things in progress
    if (rank == 0)
    {
        printf ("Metadata catalog 1:\n"); fflush (stdout);
        PERFORM (rc = metadata_catalog (md_server, txid, &catalog_items, &entries));
        print_catalog (catalog_items, entries);
        if (entries)
            free (entries);

        printf ("Metadata catalog 2:\n"); fflush (stdout);
        PERFORM (rc = metadata_catalog (md_server2, txid, &catalog_items, &entries));
        print_catalog (catalog_items, entries);
        if (entries)
            free (entries);
    }

    // no need to block before the finalizes because only the root will do
    // the actual shutdown.

    // wait for everyone before shutting down MPI
    MPI_Barrier (MPI_COMM_WORLD);

    // clean up and shut down
    PERFORM (datastore_finalize (ds_server, &datastore_config, rank == 0 ? true : false));
    PERFORM (metadata_finalize (md_server, &metadata_config, rank == 0 ? true : false));
    PERFORM (datastore_finalize (ds_server2, &datastore_config2, rank == 0 ? true : false));
    PERFORM (metadata_finalize (md_server2, &metadata_config2, rank == 0 ? true : false));

    // cleans up all initiazed comms. assume no server or managed elsewhere
    txn_finalize (comm_id, false);
}

    MPI_Finalize ();
}
Exemplo n.º 6
0
void do_put(request_t* req)
{
	req->reply.rc = datastore_put(req->client->datastore, req->argv[0], req->argv[1]);
}