void
CVarDataFile::MarkSlotDeleted(UInt32 inSlotPos, RecIDT inRecID) {
	RecIDT recID = 0;							// use 0 as deleted slot marker
#if DB_DEBUG_MODE || DB_INTEGRITY_CHECKING
	ASSERT(mBytesUsed <= mAllocatedBytes);		// can't use more than we've allocated
	ASSERT((mAllocatedBytes+mFirstItemPos) == GetLength());	// LFileStream needs to be in synch
	if (GetLength() < (SInt32)inSlotPos) {
	    DB_LOG("ERROR: Delete Rec " << inRecID << " pos: "<<inSlotPos <<" FAILED, overran datafile length "<<GetLength()<<"B");
		DB_DEBUG("ERROR: Trying to delete-mark slot at offset "<<inSlotPos<<", but datafile is only "<<GetLength()<<" bytes long.", DEBUG_ERROR);
		Throw_( dbIndexCorrupt );
	}
	SetMarker(inSlotPos + kVarDBFileRecIDOffset, streamFrom_Start);	// debugging, check old ID
	ReadBlock(&recID, kSizeOfRecID);
  #if PLATFORM_LITTLE_ENDIAN
    recID = BigEndian32_ToNative(recID);
  #endif // PLATFORM_LITTLE_ENDIAN
	DB_DEBUG("MarkSlotDeleted(); deleted_id: " << recID, DEBUG_TRIVIA);
	if (recID != inRecID) {
	    DB_LOG("ERROR: Delete Rec " << inRecID << " pos: "<<inSlotPos << " FAILED, slot has Record "<<recID);
		DB_DEBUG("ERROR: Tried to delete Record ID " << inRecID << " but record " <<recID
		    <<" found in slot at "<<inSlotPos, DEBUG_ERROR);
		Throw_( dbIndexCorrupt );
	}
	if (recID == 0) {
	    DB_LOG("WARNING: Delete Rec " << inRecID << " pos: "<<inSlotPos << " previously deleted");
		DB_DEBUG("NON-CRITICAL ERROR: Deleting a slot with a zero (deleted) ID", DEBUG_ERROR);
	}
	recID = 0;							
#endif
    // writing zero, don't worry about endian swap
	SetMarker(inSlotPos + kVarDBFileRecIDOffset, streamFrom_Start);	// move to position of recID in slot
	WriteBlock(&recID, kSizeOfRecID);						// write the deletion maker
	DB_LOG("Deleted Rec " << inRecID << " pos: "<<inSlotPos);
	DB_DEBUG("MarkSlotDeleted(); pos: " << inSlotPos, DEBUG_TRIVIA);
}
ADataStore::ADataStore() {	// ** NOT THREAD SAFE **
	itsDefaultComparator = (LComparator*)nil;
	mBatchMode = false;
	mLastRecID = 0;
	mFileOpen = false;
	itsStream = (LStream*)nil;
	SetDefaultComparator(new CRecIDComparator);		// use a ID comparator as initial default
  #if DB_THREAD_SUPPORT
  	if (UEnvironment::HasFeature(env_HasThreadsManager)) {
  	 	mChangeID = new LMutexSemaphore();
		mAccessHeader = new LMutexSemaphore();
		mAccessData = new LMutexSemaphore();
		mChangeInfo = new LMutexSemaphore();
		ThrowIfNil_(mChangeID);
		ThrowIfNil_(mAccessHeader);
		ThrowIfNil_(mAccessData);
		ThrowIfNil_(mChangeInfo);
	} else {
		mChangeID = (LMutexSemaphore*)nil;
		mAccessHeader = (LMutexSemaphore*)nil;
		mAccessData = (LMutexSemaphore*)nil;
		mChangeInfo = (LMutexSemaphore*)nil;
	}
  #endif
  DB_DEBUG("Contructed v1.0 Datastore", DEBUG_TRIVIA);
}
static int _remove_clusters_from_fed(mysql_conn_t *mysql_conn, List clusters)
{
	int   rc    = SLURM_SUCCESS;
	char *query = NULL;
	char *name  = NULL;
	char *names = NULL;
	ListIterator itr = NULL;

	xassert(clusters);

	itr = list_iterator_create(clusters);
	while ((name = list_next(itr)))
	       xstrfmtcat(names, "%s'%s'", names ? "," : "", name );

	xstrfmtcat(query, "UPDATE %s "
		   	  "SET federation='', fed_id=0, fed_state=%u "
			  "WHERE name IN (%s) and deleted=0",
		   cluster_table, CLUSTER_FED_STATE_NA, names);

	if (debug_flags & DEBUG_FLAG_FEDR)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);
	xfree(query);
	if (rc)
		error("Failed to remove clusters %s from federation", names);
	xfree(names);

	return rc;
}
ADataStore::ADataStore(SInt16 inStrcResID) {	// ** NOT THREAD SAFE **
	ASSERT(inStrcResID >= 128);		// Strc ids 0..127 are reserved for internal use
	itsDefaultComparator = (LComparator*)nil;
	mBatchMode = false;
	mLastRecID = 0;
	mFileOpen = false;
	mOwnsComparator = false;
	itsStream = (LStream*)nil;
	SetDefaultComparator(new CRecIDComparator, true);	// use a ID comparator as initial default
	mStrcResID = inStrcResID;
	mStructure = (UStructure*)nil;
  #if DB_THREAD_SUPPORT
  	if (UEnvironment::HasFeature(env_HasThreadsManager)) {
  	 	mChangeID = new LMutexSemaphore();
		mAccessHeader = new LMutexSemaphore();
		mAccessData = new LMutexSemaphore();
		mChangeInfo = new LMutexSemaphore();
		ThrowIfNil_(mChangeID);
		ThrowIfNil_(mAccessHeader);
		ThrowIfNil_(mAccessData);
		ThrowIfNil_(mChangeInfo);
	} else {
		mChangeID = (LMutexSemaphore*)nil;
		mAccessHeader = (LMutexSemaphore*)nil;
		mAccessData = (LMutexSemaphore*)nil;
		mChangeInfo = (LMutexSemaphore*)nil;
	}
  #endif
  DB_DEBUG("Contructed v1.5 Datastore: struct id " << inStrcResID, DEBUG_TRIVIA);
}
UInt32
CVarDataFile::AddNewEmptySlot(SInt32 inSize) {
	if (inSize > mLargestRecSize) {
		DB_DEBUG("New largest rec is "<<inSize<<" bytes. Previously "<<mLargestRecSize, DEBUG_TRIVIA);
		mLargestRecSize = inSize;				// keep track of largest record
	}
	mItemCount++;
	mAllocatedSlots = mItemCount + 1L;			// update #slots
	UInt32 oldBytesUsed = mBytesUsed;
	mBytesUsed += inSize;
	if (mBytesUsed >= mAllocatedBytes) {
		if (!mBatchMode) {
			mAllocatedBytes = mBytesUsed + kAllocationBlockSize;
		} else {
			mAllocatedBytes = mBytesUsed + kAllocationBlockSize*8L; // big blocks for batches
	    }
		ASSERT(mAllocatedBytes > mBytesUsed);
		ASSERT(mAllocatedBytes >= (SInt32)(mItemCount*RecSizeToSlotSize(sizeof(DatabaseRec))));	// must have at least a header
		UInt32 fileSize = mFirstItemPos + mAllocatedBytes;
		// fill the newly allocated stuff with FFFF
		UInt32 eraseStart = mFirstItemPos + oldBytesUsed;
		ASSERT(eraseStart < fileSize);
		UInt32 eraseLen = fileSize - eraseStart;
		void* p = std::malloc(eraseLen);
		SetLength(fileSize);					// expand file
		if (p) {
			std::memset(p, 0xff, eraseLen);
			SetMarker(eraseStart, streamFrom_Start);
			WriteBlock(p, eraseLen);			// fill the unused part of the data file
		} else {
			DB_DEBUG("Failed to malloc "<<eraseLen<<" bytes for new space erasure in data file", DEBUG_ERROR);
		}
		if (!mBatchMode) {
			WriteHeader();						// write new # slots, etc.. in disk file header
		}
	}
	UInt32 recPos = oldBytesUsed + mFirstItemPos;
	SetMarker(recPos + kVarDBFileSlotSizeOffset, streamFrom_Start);			// write the slot size in the slot
	inSize = Native_ToBigEndian32(inSize);
	WriteBlock(&inSize, kSizeOfSlotSize);
	inSize = BigEndian32_ToNative(inSize);  // restore endianness
	RecIDT tempID = 0;					// debugging, make sure the ID is cleared from new slots
	WriteBlock(&tempID, sizeof(RecIDT));
	DB_LOG("Add New Slot size: " << inSize << "B pos: "<<recPos <<" slots: " << mAllocatedSlots);
	DB_DEBUG("AddNewEmptySlot(); size: "<<inSize<<" pos: "<<recPos<<" slots: "<<mAllocatedSlots, DEBUG_TRIVIA);
	return recPos;
}
Пример #6
0
extern int as_mysql_register_ctld(mysql_conn_t *mysql_conn,
				  char *cluster, uint16_t port)
{
	char *query = NULL;
	char *address = NULL;
	char hostname[255];
	time_t now = time(NULL);
	uint32_t flags = slurmdb_setup_cluster_flags();
	int rc = SLURM_SUCCESS;

	if (slurmdbd_conf)
		fatal("clusteracct_storage_g_register_ctld "
		      "should never be called from the slurmdbd.");

	if (check_connection(mysql_conn) != SLURM_SUCCESS)
		return ESLURM_DB_CONNECTION;

	if (!mysql_conn->cluster_name) {
		error("%s:%d no cluster name", THIS_FILE, __LINE__);
		return SLURM_ERROR;
	}

	if (!mysql_conn->cluster_name)
		mysql_conn->cluster_name = xstrdup(cluster);

	info("Registering slurmctld for cluster %s at port %u in database.",
	     cluster, port);
	gethostname(hostname, sizeof(hostname));

	/* check if we are running on the backup controller */
	if (slurmctld_conf.backup_controller
	    && !strcmp(slurmctld_conf.backup_controller, hostname)) {
		address = slurmctld_conf.backup_addr;
	} else
		address = slurmctld_conf.control_addr;

	query = xstrdup_printf(
		"update %s set deleted=0, mod_time=%ld, "
		"control_host='%s', control_port=%u, last_port=%u, "
		"rpc_version=%d, dimensions=%d, flags=%u, "
		"plugin_id_select=%d where name='%s';",
		cluster_table, now, address, port, port, SLURM_PROTOCOL_VERSION,
		SYSTEM_DIMENSIONS, flags, select_get_plugin_id(), cluster);
	xstrfmtcat(query,
		   "insert into %s "
		   "(timestamp, action, name, actor, info) "
		   "values (%ld, %d, '%s', '%s', '%s %u %u %u %u');",
		   txn_table,
		   now, DBD_MODIFY_CLUSTERS, cluster,
		   slurmctld_conf.slurm_user_name, address, port,
		   SYSTEM_DIMENSIONS, flags, select_get_plugin_id());

	if (debug_flags & DEBUG_FLAG_DB_ASSOC)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);
	xfree(query);
	return rc;
}
// updates an existing record with the data in the CRecord object
// use CRecord* ReadRecord(id) to read in the data, then modify it and call this func to update it
void	//** Thread Safe **
ADataStore::UpdateRecord(CRecord* inRecP) {
	ASSERT(inRecP != nil);	// еее pass by reference
	DB_DEBUG("UpdateRecord("<<inRecP->GetRecordID()<<")");
	AutoLockRecord lock(inRecP);	// automatically unlock the record as we exit this code block
	DatabaseRec* p = inRecP->GetRecordDataPtr();
	UpdateRecord(p);
}
RecIDT	//** Thread Safe **
ADataStore::FindRecord(CRecord* inRecP, LComparator *) {	//inComparator
	ASSERT(inRecP != nil);
	RecIDT resultID = kInvalidRecID;
	int ignore; //*** ADataStore::FindRecord() NOT YET IMPLEMENTED ***
	//*** not sure how I want to implement this yet
	DB_DEBUG("FindRecord(): "<< resultID);
	return resultID;
}
static int _add_clusters_to_fed(mysql_conn_t *mysql_conn, List clusters,
				const char *fed)
{
	int   rc      = SLURM_SUCCESS;
	char *query   = NULL;
	char *name    = NULL;
	char *names   = NULL;
	char *indexes = NULL;
	ListIterator itr = NULL;
	int   last_id = -1;

	xassert(fed);
	xassert(clusters);

	itr = list_iterator_create(clusters);
	while ((name = list_next(itr))) {
		int id;
		if ((rc = as_mysql_get_fed_cluster_id(mysql_conn, name, fed,
						      last_id, &id)))
			goto end_it;
		last_id = id;
		xstrfmtcat(indexes, "WHEN name='%s' THEN %d ", name, id);
		xstrfmtcat(names, "%s'%s'", names ? "," : "", name);
	}

	/* Keep the same fed_state if the cluster isn't changing feds.
	 * Also note that mysql evaluates from left to right and uses the
	 * updated column values in case statements. So the check for federation
	 * in the fed_state case statement must happen before fed_state is set
	 * or the federation will always equal the federation in the case
	 * statement.  */
	xstrfmtcat(query, "UPDATE %s "
		   	  "SET "
			  "fed_state = CASE WHEN federation='%s' THEN fed_state ELSE %u END, "
			  "fed_id = CASE %s END, "
		   	  "federation='%s' "
			  "WHERE name IN (%s) and deleted=0",
		   cluster_table, fed, CLUSTER_FED_STATE_ACTIVE, indexes, fed,
		   names);

	if (debug_flags & DEBUG_FLAG_FEDR)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);
	if (rc)
		error("Failed to add clusters %s to federation %s",
		      names, fed);

end_it:
	xfree(query);
	xfree(names);
	xfree(indexes);
	list_iterator_destroy(itr);

	return rc;
}
bool	// ** NOT THREAD SAFE **
ADataStore::SetBatchMode(bool inBatchMode) {
	bool oldBatchMode = mBatchMode;
	mBatchMode = inBatchMode;
	if (!mBatchMode) {
		WriteHeader();	// write the header now that we are done with batch mode
	}
	DB_DEBUG("SetBatchMode("<<inBatchMode<<")", DEBUG_TRIVIA);
	return(oldBatchMode);
}
// adds a new record with the data in the CRecordObject, returns the ID that was assigned to it
// use MakeNewEmptyRecord() to create the empty record, then you can fill in the fields and
// call this func to add it to the database
RecIDT	//** Thread Safe **
ADataStore::AddRecord(CRecord* inRecP) {
	ASSERT(inRecP != nil);	// еее pass by reference
	DB_DEBUG("AddRecord()");
	RecIDT id = 0;
	AutoLockRecord lock(inRecP);	// this will automatically unlock the record when it goes out
	DatabaseRec* p = inRecP->GetRecordDataPtr(); // of scope, whether normally or from an exception
	id = AddRecord(p);
	return id;
}
extern int as_mysql_fix_runaway_jobs(mysql_conn_t *mysql_conn, uint32_t uid,
				     List runaway_jobs)
{
	char *query = NULL, *job_ids = NULL;
	slurmdb_job_rec_t *job = NULL;
	ListIterator iter = NULL;
	int rc = SLURM_SUCCESS;
	slurmdb_job_rec_t *first_job;

	list_sort(runaway_jobs, _job_sort_by_start_time);
	first_job = list_peek(runaway_jobs);

	if (check_connection(mysql_conn) != SLURM_SUCCESS)
		return ESLURM_DB_CONNECTION;

	if (!is_user_min_admin_level(mysql_conn, uid, SLURMDB_ADMIN_OPERATOR)) {
		slurmdb_user_rec_t user;

		memset(&user, 0, sizeof(slurmdb_user_rec_t));
		user.uid = uid;

		if (!is_user_any_coord(mysql_conn, &user)) {
			error("Only admins/operators/coordinators "
			      "can fix runaway jobs");
			return ESLURM_ACCESS_DENIED;
		}
	}

	iter = list_iterator_create(runaway_jobs);
	while ((job = list_next(iter))) {
		xstrfmtcat(job_ids, "%s%d", ((job_ids) ? "," : ""), job->jobid);
	}

	query = xstrdup_printf("UPDATE \"%s_%s\" SET time_end="
			       "GREATEST(time_start, time_eligible, time_submit), "
			       "state=%d WHERE id_job IN (%s);",
			       mysql_conn->cluster_name, job_table,
			       JOB_COMPLETE, job_ids);

	if (debug_flags & DEBUG_FLAG_DB_QUERY)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);
	mysql_db_query(mysql_conn, query);
	xfree(query);
	xfree(job_ids);

	/* Set rollup to the the last day of the previous month of the first
	 * runaway job */
	rc = _first_job_roll_up(mysql_conn, first_job->start);
	if (rc != SLURM_SUCCESS) {
		error("Failed to fix runaway jobs");
		return SLURM_ERROR;
	}

	return rc;
}
bool	// ** NOT THREAD SAFE **
ADataStore::ReadHeader() {
	Bool8 fileOpen;
	itsStream->SetMarker(0, streamFrom_Start);
	*itsStream >> mStreamType;
	*itsStream >> mStreamVersion;
	*itsStream >> fileOpen;
	*itsStream >> mLastRecID;
	DB_DEBUG("ReadHeader(): "<< (fileOpen?"already open":"closed"), DEBUG_TRIVIA);
	return fileOpen;
}
int hss_mysql_query_mmeidentity(const int id_mme_identity,
                                mysql_mme_identity_t *mme_identity_p)
{
  MYSQL_RES *res;
  MYSQL_ROW row;
  char query[1000];

  if ((db_desc->db_conn == NULL) || (mme_identity_p == NULL)) {
    return EINVAL;
  }

  memset(mme_identity_p, 0, sizeof(mysql_mme_identity_t));

  sprintf(query, "SELECT mmehost,mmerealm FROM mmeidentity WHERE "
          "mmeidentity.idmmeidentity='%d' ", id_mme_identity);

  DB_DEBUG("Query: %s\n", query);

  pthread_mutex_lock(&db_desc->db_cs_mutex);

  if (mysql_query(db_desc->db_conn, query)) {
    pthread_mutex_unlock(&db_desc->db_cs_mutex);
    DB_ERROR("Query execution failed: %s\n",
             mysql_error(db_desc->db_conn));
    mysql_thread_end();
    return EINVAL;
  }

  res = mysql_store_result(db_desc->db_conn);

  pthread_mutex_unlock(&db_desc->db_cs_mutex);

  if ((row = mysql_fetch_row(res)) != NULL) {
    if (row[0] != NULL) {
      memcpy(mme_identity_p->mme_host, row[0], strlen(row[0]));
    } else {
      mme_identity_p->mme_host[0] = '\0';
    }

    if (row[1] != NULL) {
      memcpy(mme_identity_p->mme_realm, row[1], strlen(row[1]));
    } else {
      mme_identity_p->mme_realm[0] = '\0';
    }

    mysql_free_result(res);
    mysql_thread_end();
    return 0;
  }

  mysql_free_result(res);
  mysql_thread_end();
  return EINVAL;
}
Пример #15
0
extern int as_mysql_remove_resv(mysql_conn_t *mysql_conn,
				slurmdb_reservation_rec_t *resv)
{
	int rc = SLURM_SUCCESS;
	char *query = NULL;

	if (!resv) {
		error("No reservation was given to remove");
		return SLURM_ERROR;
	}

	if (!resv->id) {
		error("An id is needed to remove a reservation.");
		return SLURM_ERROR;
	}

	if (!resv->time_start) {
		error("A start time is needed to remove a reservation.");
		return SLURM_ERROR;
	}

	if (!resv->cluster || !resv->cluster[0]) {
		error("A cluster name is needed to remove a reservation.");
		return SLURM_ERROR;
	}


	/* first delete the resv that hasn't happened yet. */
	query = xstrdup_printf("delete from \"%s_%s\" where time_start > %ld "
			       "and id_resv=%u and time_start=%ld;",
			       resv->cluster, resv_table, resv->time_start_prev,
			       resv->id,
			       resv->time_start);
	/* then update the remaining ones with a deleted flag and end
	 * time of the time_start_prev which is set to when the
	 * command was issued */
	xstrfmtcat(query,
		   "update \"%s_%s\" set time_end=%ld, "
		   "deleted=1 where deleted=0 and "
		   "id_resv=%u and time_start=%ld;",
		   resv->cluster, resv_table, resv->time_start_prev,
		   resv->id, resv->time_start);

	if (debug_flags & DEBUG_FLAG_DB_RESV)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);

	xfree(query);

	return rc;
}
Пример #16
0
static int _reset_default_wckey(mysql_conn_t *mysql_conn,
                                slurmdb_wckey_rec_t *wckey)
{
    time_t now = time(NULL);
    int rc = SLURM_SUCCESS;
    char *query = NULL;
    MYSQL_RES *result = NULL;
    MYSQL_ROW row;

    if ((wckey->is_def != 1)
            || !wckey->cluster || !wckey->user || !wckey->name)
        return SLURM_ERROR;

    xstrfmtcat(query, "update \"%s_%s\" set is_def=0, mod_time=%ld "
               "where (user='******' && wckey_name!='%s' && is_def=1);"
               "select id_wckey from \"%s_%s\" "
               "where (user='******' && wckey_name!='%s' && is_def=1);",
               wckey->cluster, wckey_table, (long)now,
               wckey->user, wckey->name,
               wckey->cluster, wckey_table,
               wckey->user, wckey->name);
    if (debug_flags & DEBUG_FLAG_DB_WCKEY)
        DB_DEBUG(mysql_conn->conn, "query\n%s", query);
    if (!(result = mysql_db_query_ret(mysql_conn, query, 1))) {
        xfree(query);
        rc = SLURM_ERROR;
        goto end_it;
    }
    xfree(query);

    while ((row = mysql_fetch_row(result))) {
        slurmdb_wckey_rec_t *mod_wckey =
            xmalloc(sizeof(slurmdb_wckey_rec_t));
        slurmdb_init_wckey_rec(mod_wckey, 0);

        mod_wckey->id = slurm_atoul(row[0]);
        mod_wckey->is_def = 0;

        if (addto_update_list(mysql_conn->update_list,
                              SLURMDB_MODIFY_WCKEY,
                              mod_wckey)
                != SLURM_SUCCESS) {
            slurmdb_destroy_wckey_rec(mod_wckey);
            error("couldn't add to the update list");
            rc = SLURM_ERROR;
            break;
        }
    }
    mysql_free_result(result);
end_it:
    return rc;
}
void	//** Thread Safe **
ADataStore::WriteHeader(bool inFileOpen) {
	Bool8 fileOpen = inFileOpen;
  #if DB_THREAD_SUPPORT
	StSafeMutex mutex(mAccessHeader);
  #endif
	ASSERT(sizeof(RecIDT) == sizeof(UInt32));       // precondition for this to work correctly
	// don't write the type and version info each time
	itsStream->SetMarker(sizeof(OSType) + sizeof(UInt32), streamFrom_Start);
	*itsStream << fileOpen;	//***CW8	// platform independant files
	*itsStream << mLastRecID;
	DB_DEBUG("WriteHeader("<<(int)inFileOpen<<")", DEBUG_TRIVIA);
}
// must call Close() before deleting object
ADataStore::~ADataStore() {	// ** NOT THREAD SAFE **
	if (mOwnsComparator)
		delete itsDefaultComparator;
  #if DB_THREAD_SUPPORT
  	if (UEnvironment::HasFeature(env_HasThreadsManager)) {
  	 	delete mChangeID;
		delete mAccessHeader;
		delete mAccessData;
		delete mChangeInfo;
	}
  #endif
  DB_DEBUG("Deleted Datastore", DEBUG_TRIVIA);
}
SInt32
CVarDataFile::GetRecordSize(RecIDT inRecID) {
#if DB_DEBUG_MODE
	if (inRecID > mLastRecID) {
		DB_DEBUG("ERROR: Invalid Record ID "<<inRecID<<" requested. Last ID is "<<mLastRecID, DEBUG_ERROR);
	}
#endif
	if (0 == inRecID) {
		return(mLargestRecSize + kSlotSizeExtra + kSlotSizeSlop);	// return super-safe buffer size
	}
	SInt32 recSize;
	itsMasterIndex->FindEntry(inRecID, &recSize);	// find index entry
	return (recSize);								// return the allocation block size of the record
}
Пример #20
0
static int _process_cluster_usage(mysql_conn_t *mysql_conn,
				  char *cluster_name,
				  time_t curr_start, time_t curr_end,
				  time_t now, local_cluster_usage_t *c_usage)
{
	int rc = SLURM_SUCCESS;
	char *query = NULL;
	ListIterator itr;
	local_tres_usage_t *loc_tres;

	if (!c_usage)
		return rc;
	/* Now put the lists into the usage tables */

	xassert(c_usage->loc_tres);
	itr = list_iterator_create(c_usage->loc_tres);
	while ((loc_tres = list_next(itr))) {
		_setup_cluster_tres_usage(mysql_conn, cluster_name,
					  curr_start, curr_end, now,
					  c_usage->start, loc_tres, &query);
	}
	list_iterator_destroy(itr);

	if (!query)
		return rc;

	xstrfmtcat(query,
		   " on duplicate key update "
		   "mod_time=%ld, count=VALUES(count), "
		   "alloc_secs=VALUES(alloc_secs), "
		   "down_secs=VALUES(down_secs), "
		   "pdown_secs=VALUES(pdown_secs), "
		   "idle_secs=VALUES(idle_secs), "
		   "over_secs=VALUES(over_secs), "
		   "resv_secs=VALUES(resv_secs)",
		   now);

	/* Spacing out the inserts here instead of doing them
	   all at once in the end proves to be faster.  Just FYI
	   so we don't go testing again and again.
	*/
	if (debug_flags & DEBUG_FLAG_DB_USAGE)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);
	rc = mysql_db_query(mysql_conn, query);
	xfree(query);
	if (rc != SLURM_SUCCESS)
		error("Couldn't add cluster hour rollup");

	return rc;
}
// creates new CRecord object and fills it with the data of the record specified by inRecID
CRecord*	//** Thread Safe **
ADataStore::ReadRecord(RecIDT inRecID) {
	DB_DEBUG("ReadRecord("<<inRecID<<")");
	CRecord* theRec = (CRecord*)nil;
	long size = GetRecordSize(inRecID);
	if (size > 0) {
		temp_buffer(DatabaseRec, p, size);	// allocate a read buffer
		p->recID = inRecID;
		p->recSize = size;
		ReadRecord(p);
		theRec = MakeRecordObject(p);
	}
	return theRec;
}
// deletes the record and the object too and sets the pointer passed in to nil also catches
// the exception if the record was not found, since we were going to delete it anyway
void	//** Thread Safe **
ADataStore::DeleteRecord(CRecord* &inRecP) {
	ASSERT(inRecP != nil);
	DB_DEBUG("WriteRecord("<<inRecP->GetRecordID()<<")");
	Try_{
		DeleteRecord(inRecP->GetRecordID());
	}
	Catch_(err) {
		if (err != dbItemNotFound)
			Throw_(err);			// pass on the exception if it was something other than notFound
	}
	delete inRecP;
	inRecP = (CRecord*)nil;
}
void 
CVarDataFile::WriteHeader(bool inFileOpen) {
	if (mReadOnly) {
		return;	// v1.5.1 bug fix, don't write to read-only files
    }
    DB_DEBUG("CVarDataFile::WriteHeader", DEBUG_TRIVIA);
	CDataFile::WriteHeader(inFileOpen);
	VarDataFileHeaderT tempHeader;
	tempHeader.allocatedBytes = Native_ToBigEndian32(mAllocatedBytes);
	tempHeader.bytesUsed = Native_ToBigEndian32(mBytesUsed);
	tempHeader.largestRecSize = Native_ToBigEndian32(mLargestRecSize);
	tempHeader.fileIsDamaged = mFileIsDamaged;
	WriteBlock(&tempHeader, sizeof(VarDataFileHeaderT));
	FlushFile();
}
bool
CVarDataFile::FindRecord(DatabaseRec *ioRecP, LComparator *inComparator) {
#warning FIXME: pretty sure the loop below is broken and will stay stuck on the same record
	bool found = false;
	SInt32 slotSize;
	SInt32 recPos;
	ASSERT(mLargestRecSize > (SInt32)sizeof(DatabaseRec));	// this assertion will fail if no recs in file
	DatabaseRecPtr recP = (DatabaseRec*) std::malloc(mLargestRecSize);
	ThrowIfNil_(recP);
	if (NULL == inComparator) {
		inComparator = itsDefaultComparator;	// use one of the comparators
	}
	SetMarker(mFirstItemPos + kVarDBFileSlotSizeOffset, streamFrom_Start);	// move to start of the 1st record
	for (UInt32 i = 0; i<mItemCount; i++) {		// look at all the records in the datafile
		recPos = GetMarker();
		ReadBlock(&slotSize, kSizeOfSlotSize);				 // read the slot size into the pointer
      #if PLATFORM_LITTLE_ENDIAN
        slotSize = BigEndian32_ToNative(slotSize);
      #endif // PLATFORM_LITTLE_ENDIAN
		ReadBlock(&recP->recID, slotSize - kSizeOfSlotSize); // read the contents of the slot
      #if PLATFORM_LITTLE_ENDIAN
        recP->recID = BigEndian32_ToNative(recP->recID);
      #endif // PLATFORM_LITTLE_ENDIAN
		recP->recSize = SlotSizeToRecSize(slotSize);
		if (inComparator->IsEqualTo(ioRecP, recP, recP->recSize, recP->recSize) ) {	
			// compare the current rec to the source  еее CW 8 mod - added sizes
			found = true;
			ioRecP->recPos = recPos;			// return the found recPos in the DatabaseRecPtr
			if (SlotSizeToRecSize(slotSize) < ioRecP->recSize) {
				ioRecP->recSize = SlotSizeToRecSize(slotSize);	// return the found recSize in the DatabaseRecPtr
			}
			UInt8 *dst = (UInt8*)&ioRecP->recSize;
  			UInt8 *src = (UInt8*)&recP->recSize;			// include recSize & recID in copy
  			for (SInt32 n = RecSizeToSlotSize(ioRecP->recSize); n>0; n--) {	// copy the data to be returned
      			*dst++ = *src++;
      	    }
 			break;
		}
	}
#if DB_DEBUG_MODE
	if (found && (ioRecP->recID > mLastRecID)) {
		DB_DEBUG("ERROR: Invalid Record ID "<<ioRecP->recID<<" returned. Last ID is "<<mLastRecID, DEBUG_ERROR);
	}
#endif
    std::free(recP);    // free the temporary buffer
	return found;								// return true if found
}
SInt32
CVarDataFile::GetSlotSize(UInt32 inSlotPos) {
#if DB_DEBUG_MODE || DB_INTEGRITY_CHECKING
	ASSERT(mBytesUsed <= mAllocatedBytes);	// can't use more than we've allocated
	ASSERT((mAllocatedBytes+mFirstItemPos) == GetLength());	// LFileStream needs to be in synch
	if (GetLength() < (SInt32)inSlotPos) {
		DB_DEBUG("ERROR: Trying to read from slot at offset "<<inSlotPos<<", but datafile is only "<<GetLength()<<" bytes long.", DEBUG_ERROR);
        Throw_( dbDataCorrupt );
	}
#endif
	SInt32 slotSize;
	SetMarker(inSlotPos + kVarDBFileSlotSizeOffset, streamFrom_Start);
	ReadBlock(&slotSize, kSizeOfSlotSize);
  #if PLATFORM_LITTLE_ENDIAN
    slotSize = BigEndian32_ToNative(slotSize);
  #endif // PLATFORM_LITTLE_ENDIAN
	return slotSize;
}	
Пример #26
0
extern int as_mysql_add_resv(mysql_conn_t *mysql_conn,
			     slurmdb_reservation_rec_t *resv)
{
	int rc = SLURM_SUCCESS;
	char *cols = NULL, *vals = NULL, *extra = NULL,
		*query = NULL;

	if (!resv) {
		error("No reservation was given to add.");
		return SLURM_ERROR;
	}

	if (!resv->id) {
		error("We need an id to add a reservation.");
		return SLURM_ERROR;
	}
	if (!resv->time_start) {
		error("We need a start time to add a reservation.");
		return SLURM_ERROR;
	}
	if (!resv->cluster || !resv->cluster[0]) {
		error("We need a cluster name to add a reservation.");
		return SLURM_ERROR;
	}

	_setup_resv_limits(resv, &cols, &vals, &extra);

	xstrfmtcat(query,
		   "insert into \"%s_%s\" (id_resv%s) values (%u%s) "
		   "on duplicate key update deleted=0%s;",
		   resv->cluster, resv_table, cols, resv->id, vals, extra);

	if (debug_flags & DEBUG_FLAG_DB_RESV)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);

	xfree(query);
	xfree(cols);
	xfree(vals);
	xfree(extra);

	return rc;
}
void
CVarDataFile::UpdateRecord(DatabaseRec *inRecP) {
	SInt32 recSize = inRecP->recSize;
	SInt32 recPos = itsMasterIndex->UpdateEntry(inRecP->recID, recSize);	// find index entry
	inRecP->recPos = recPos;
#if DB_DEBUG_MODE || DB_INTEGRITY_CHECKING
	if (inRecP->recID > mLastRecID) {
		DB_DEBUG("ERROR: Invalid Record ID "<<inRecP->recID<<" updated. Last ID is "<<mLastRecID, DEBUG_ERROR);
		Throw_( dbInvalidID );
	}
	if (recSize < (SInt32)sizeof(DatabaseRec)) {
		DB_DEBUG("ERROR: Trying to update Rec "<<inRecP->recID<<" with size of "<<recSize<<" bytes, smaller than the record header alone.", DEBUG_ERROR);
        Throw_( dbDataCorrupt );
	}
	ASSERT(mBytesUsed <= mAllocatedBytes);	// can't use more than we've allocated
	ASSERT((mAllocatedBytes+mFirstItemPos) == GetLength());	// LFileStream needs to be in synch
	if (GetLength() < recPos) {
		DB_DEBUG("ERROR: Index returned offset "<<recPos<<" for Rec "<<inRecP->recID<<", but datafile is only "<<GetLength()<<" bytes long.", DEBUG_ERROR);
		mFileIsDamaged = true;
        Throw_( dbIndexCorrupt );
	}
//	DB_DEBUG("in UpdateRecord("<< inRecP->recID <<"); size: "<<recSize<<" pos: "<<recPos, DEBUG_TRIVIA);
	RecIDT oldID;
	SInt32 slotSize;
	SetMarker(recPos + kVarDBFileSlotSizeOffset, streamFrom_Start);	// debugging, check old ID
	ReadBlock(&slotSize, kSizeOfSlotSize);
	ReadBlock(&oldID, kSizeOfRecID);
  #if PLATFORM_LITTLE_ENDIAN
    slotSize = BigEndian32_ToNative(slotSize);
    oldID = BigEndian32_ToNative(oldID);
  #endif // PLATFORM_LITTLE_ENDIAN
	if ( (oldID != 0) && (oldID != inRecP->recID) ) {
	    DB_LOG("ERROR: Update Rec " << inRecP->recID << " " << recSize << "B pos: "<<recPos<<" FAILED, overwriting Rec "<<oldID);
		DB_DEBUG("ERROR: Updating "<< inRecP->recID << " into wrong place [" << recPos << "] , overwriting Rec "<<oldID, DEBUG_ERROR);
		mFileIsDamaged = true;
		Throw_( dbIndexCorrupt );
	}
	if (slotSize < RecSizeToSlotSize(recSize)) {
	    DB_LOG("ERROR: Update Rec " << inRecP->recID << " " << recSize << "B pos: "<<recPos<<" FAILED, slot too small "<<slotSize<<"B");
		DB_DEBUG("ERROR: Writing "<< inRecP->recID <<" of "<<RecSizeToSlotSize(recSize)<<" bytes into a "<<slotSize<<" byte slot at "<< recPos, DEBUG_ERROR);
		mFileIsDamaged = true;
        Throw_( dbDataCorrupt );
	}
#endif
	SetMarker(recPos + kVarDBFileRecIDOffset, streamFrom_Start);	// move to start of slot's data, skipping size
    inRecP->recID = Native_ToBigEndian32(inRecP->recID);
	WriteBlock(&inRecP->recID, RecSizeToIOSize(recSize));			// write the new record data into the slot
	inRecP->recID = BigEndian32_ToNative(inRecP->recID);
	DB_LOG("Updated Rec " << inRecP->recID << " " << recSize << "B pos: "<<recPos);
	DB_DEBUG("UpdateRecord("<< inRecP->recID <<"); size: "<<recSize<<" pos: "<<recPos, DEBUG_TRIVIA);
}
int hss_mysql_check_epc_equipment(mysql_mme_identity_t *mme_identity_p)
{
  MYSQL_RES *res;
  MYSQL_ROW row;
  char query[1000];

  if ((db_desc->db_conn == NULL) || (mme_identity_p == NULL)) {
    return EINVAL;
  }

  sprintf(query, "SELECT idmmeidentity FROM mmeidentity WHERE mmeidentity.mmehost='%s' ",
          mme_identity_p->mme_host);

  DB_DEBUG("Query: %s\n", query);

  pthread_mutex_lock(&db_desc->db_cs_mutex);

  if (mysql_query(db_desc->db_conn, query)) {
    pthread_mutex_unlock(&db_desc->db_cs_mutex);
    DB_ERROR("Query execution failed: %s\n",
             mysql_error(db_desc->db_conn));
    mysql_thread_end();
    return EINVAL;
  }

  res = mysql_store_result(db_desc->db_conn);

  pthread_mutex_unlock(&db_desc->db_cs_mutex);

  if ((row = mysql_fetch_row(res)) != NULL) {
    mysql_free_result(res);
    mysql_thread_end();
    return 0;
  }

  mysql_free_result(res);
  mysql_thread_end();
  return EINVAL;
}
/*
 * Remove all clusters from federation.
 * IN: mysql_conn - mysql connection
 * IN: fed - fed to remove clusters from
 * IN: exceptions - list of clusters to not remove.
 */
static int _remove_all_clusters_from_fed(mysql_conn_t *mysql_conn,
					 const char *fed, List exceptions)
{
	int   rc    = SLURM_SUCCESS;
	char *query = NULL;
	char *exception_names = NULL;

	if (exceptions && list_count(exceptions)) {
		char *tmp_name;
		ListIterator itr;

		itr = list_iterator_create(exceptions);
		while ((tmp_name = list_next(itr)))
			xstrfmtcat(exception_names, "%s'%s'",
				   (exception_names) ? "," : "",
				   tmp_name);
		list_iterator_destroy(itr);
	}

	xstrfmtcat(query, "UPDATE %s "
		   	  "SET federation='', fed_id=0, fed_state=%u "
			  "WHERE federation='%s' and deleted=0",
		   cluster_table, CLUSTER_FED_STATE_NA, fed);
	if (exception_names)
		xstrfmtcat(query, " AND name NOT IN (%s)", exception_names);

	if (debug_flags & DEBUG_FLAG_FEDR)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);

	rc = mysql_db_query(mysql_conn, query);
	xfree(query);
	if (rc)
		error("Failed to remove all clusters from federation %s", fed);

	if (exception_names)
		xfree(exception_names);

	return rc;
}
Пример #30
0
static int _change_user_name(mysql_conn_t *mysql_conn, slurmdb_user_rec_t *user)
{
	int rc = SLURM_SUCCESS;
	char *query = NULL;
	ListIterator itr = NULL;
	char *cluster_name = NULL;

	xassert(user->old_name);
	xassert(user->name);

	slurm_mutex_lock(&as_mysql_cluster_list_lock);
	itr = list_iterator_create(as_mysql_cluster_list);
	while ((cluster_name = list_next(itr))) {
		// Change assoc_tables
		xstrfmtcat(query, "update \"%s_%s\" set user='******' "
			   "where user='******';", cluster_name, assoc_table,
			   user->name, user->old_name);
		// Change wckey_tables
		xstrfmtcat(query, "update \"%s_%s\" set user='******' "
			   "where user='******';", cluster_name, wckey_table,
			   user->name, user->old_name);
	}
	list_iterator_destroy(itr);
	slurm_mutex_unlock(&as_mysql_cluster_list_lock);
	// Change coord_tables
	xstrfmtcat(query, "update %s set user='******' where user='******';",
		   acct_coord_table, user->name, user->old_name);

	if (debug_flags & DEBUG_FLAG_DB_ASSOC)
		DB_DEBUG(mysql_conn->conn, "query\n%s", query);
	rc = mysql_db_query(mysql_conn, query);
	xfree(query);

	if (rc != SLURM_SUCCESS)
		reset_mysql_conn(mysql_conn);

	return rc;
}