public void DeleteThread(ThreadStorageView threadToDelete) { if (threadToDelete == null) { return; } IObjectContainer container = GetStorage(); try { IList <ThreadStorageView> threads = container.Query <ThreadStorageView>(delegate(ThreadStorageView threadFinder) { return(threadFinder.ThreadId == threadToDelete.ThreadId); }); if (threads.Count > 0) { container.Delete(threads[0]); } } finally { container.Close(); } }
/// <summary> /// Initializes this thread. /// </summary> /// <param name="primary">specifies if this thread is a primary thread. A thread is primary if it is created and scheduled by the same manager</param> public void Init(bool primary) { if (primary) { ThreadStorageView threadStorage = new ThreadStorageView(_AppId, _Id, ThreadState.Ready); ManagerStorageFactory.ManagerStorage().AddThread(threadStorage); } else { ApplicationStorageView applicationStorage = ManagerStorageFactory.ManagerStorage().GetApplication(_AppId); if (applicationStorage == null) { applicationStorage = new ApplicationStorageView( _AppId, ApplicationState.AwaitingManifest, DateTime.Now, false, "" /* TODO: What's the username here?*/); ManagerStorageFactory.ManagerStorage().AddApplication(applicationStorage); } ThreadStorageView threadStorage = new ThreadStorageView(_AppId, _Id, ThreadState.Ready); ManagerStorageFactory.ManagerStorage().AddThread(threadStorage); } }
public void UpdateThread(ThreadStorageView updatedThread) { ThreadStorageView thread = null; IObjectContainer container = GetStorage(); try { IList <ThreadStorageView> threads = container.Query <ThreadStorageView>(delegate(ThreadStorageView threadFinder) { return(threadFinder.ThreadId == updatedThread.ThreadId); }); if (threads.Count > 0) { thread = threads[0]; container.Delete(thread); container.Set(updatedThread); } } finally { container.Close(); } }
/// <summary> /// Return the thread with the highest priority from the pool of Ready threads. /// /// Note: pathetic way of selecting the highest priority thread. /// This should be moved into the storage for a more efficient implementation. /// </summary> /// <returns></returns> protected ThreadStorageView GetNextAvailableThread() { // achieve thread safety by locking on a static variable // this lock is not enough, we should lock until the thread status changes lock (threadChooserLock) { ThreadStorageView[] threads = ManagerStorageFactory.ManagerStorage().GetThreads( ApplicationState.Ready, ThreadState.Ready); if (threads == null || threads.Length == 0) { return(null); } ThreadStorageView highestPriorityThread = threads[0]; foreach (ThreadStorageView thread in threads) { if (thread.Priority > highestPriorityThread.Priority) { highestPriorityThread = thread; } } return(highestPriorityThread); } }
/// <summary> /// Queries the database to return the next dedicated schedule. /// </summary> /// <returns>DedicatedSchedule</returns> public DedicatedSchedule ScheduleDedicated() { ExecutorStorageView executorStorage = GetNextAvailableExecutor(); if (executorStorage == null) { return(null); } ThreadStorageView threadStorage = GetNextAvailableThread(); if (threadStorage == null) { return(null); } DedicatedSchedule dsched = null; string executorId = executorStorage.ExecutorId; string appid = threadStorage.ApplicationId; int threadId = threadStorage.ThreadId; int priority = threadStorage.Priority; ThreadIdentifier ti = new ThreadIdentifier(appid, threadId, priority); dsched = new DedicatedSchedule(ti, executorId); return(dsched); }
/// <summary> /// Compares two objects. /// </summary> /// <param name="oObject0">object 0</param> /// <param name="oObject1">object 1</param> /// <returns>comparison</returns> public int Compare(object oObject0, object oObject1) { ThreadStorageView oThread0 = oObject0 as ThreadStorageView; ThreadStorageView oThread1 = oObject1 as ThreadStorageView; return(oThread1.Priority - oThread0.Priority); }
/// <summary> /// Gets the hightest priority thread from the given threads. /// </summary> /// <param name="cThreads">threads</param> /// <returns>highest priority thread</returns> private ThreadStorageView GetHighestPriorityThread(IList cThreads) { ThreadStorageView oHighestPriorityThread = (ThreadStorageView )cThreads[0]; foreach (ThreadStorageView oThread in cThreads) { if (oThread.Priority > oHighestPriorityThread.Priority) { oHighestPriorityThread = oThread; } } return(oHighestPriorityThread); }
/// <summary> /// Resets this MThread so it can be rescheduled. /// </summary> public void Reset() { ThreadStorageView threadStorage = ManagerStorageFactory.ManagerStorage().GetThread(_AppId, _Id); //take care that only threads that are not already aborted are reset. if (threadStorage.State != ThreadState.Dead) { threadStorage.State = ThreadState.Ready; threadStorage.ExecutorId = null; threadStorage.ResetTimeStarted(); threadStorage.ResetTimeFinished(); ManagerStorageFactory.ManagerStorage().UpdateThread(threadStorage); } }
public int AddThread(ThreadStorageView thread) { if (thread == null) { return(-1); } lock (_threads) { // generate the next threadID from the length, this will make sure the thread ID is unique // generating from the length also requires thread synchronization code here thread.InternalThreadId = _threads.Count; _threads.Add(thread); } return(thread.InternalThreadId); }
/// <summary> /// Queries the database to return the next dedicated schedule. /// </summary> /// <returns>DedicatedSchedule</returns> public DedicatedSchedule ScheduleDedicated() { ExecutorStorageView executorStorage = GetNextAvailableExecutor(); if (executorStorage == null) { return(null); } ThreadStorageView threadStorage = GetNextAvailableThread(); if (threadStorage == null) { return(null); } DedicatedSchedule dsched = null; string executorId = executorStorage.ExecutorId; string appid = threadStorage.ApplicationId; int threadId = threadStorage.ThreadId; int priority = threadStorage.Priority; // if (priority == -1) // { // priority = 5; //DEFAULT PRIORITY - TODO: have to put this in some Constants.cs file or something... // } ThreadIdentifier ti = new ThreadIdentifier(appid, threadId, priority); logger.Debug(String.Format("Schedule dedicated. app_id={0},threadID={1}, executor-id={2}", appid, threadId, executorId) ); dsched = new DedicatedSchedule(ti, executorId); return(dsched); }
public void DeleteThread(ThreadStorageView threadToDelete) { if (m_threads == null || threadToDelete == null) { return; } ArrayList remainingThreads = new ArrayList(); foreach (ThreadStorageView thread in m_threads) { if (thread.ApplicationId != threadToDelete.ApplicationId || thread.ThreadId != threadToDelete.ThreadId) { remainingThreads.Add(thread); } } m_threads = remainingThreads; }
/// <summary> /// Return a non-dedicated schedule: i.e a threadIdentifier of the next thread to be executed. /// This is to support voluntary / non-dedicated execution, where an executor asks for the next /// work unit. /// </summary> /// <param name="executorId">The executorId passed in refers to the Executor which will run this thread.</param> /// <returns>ThreadIdentifier of the next available thread</returns> public ThreadIdentifier ScheduleNonDedicated(string executorId) { ThreadStorageView threadStorage = GetNextAvailableThread(); if (threadStorage == null) { return(null); } logger.Debug(String.Format("Schedule non-dedicated. app_id={0},threadID={1}", threadStorage.ApplicationId, threadStorage.ThreadId) ); string appid = threadStorage.ApplicationId; int threadId = threadStorage.ThreadId; int priority = threadStorage.Priority; return(new ThreadIdentifier(appid, threadId, priority)); }
/// <summary> /// Schedules a thread for the given executor id. /// </summary> /// <param name="strExecutorId">executor id</param> /// <returns>thread identifier</returns> public ThreadIdentifier ScheduleNonDedicated(string strExecutorId) { lock (this) { _Mapping.Update(); IList cApplicationIds = _Mapping.GetApplicationIds(strExecutorId); ThreadStorageView oThreadStorage = GetNextThread(cApplicationIds); if (oThreadStorage != null) { logger.Debug(String.Format("ScheduleNonDedicated; ApplicationId = {0}, ThreadId = {1}, ExecutorId = {2}", oThreadStorage.ApplicationId, oThreadStorage.ThreadId, strExecutorId)); string strApplicationId = oThreadStorage.ApplicationId; int nThreadId = oThreadStorage.ThreadId; int nPriority = oThreadStorage.Priority; return(new ThreadIdentifier(strApplicationId, nThreadId, nPriority)); } } return(null); }
public void UpdateThread(ThreadStorageView updatedThread) { if (m_threads == null || updatedThread == null) { return; } ArrayList newThreadList = new ArrayList(); foreach (ThreadStorageView thread in m_threads) { if (thread.InternalThreadId == updatedThread.InternalThreadId) { newThreadList.Add(updatedThread); } else { newThreadList.Add(thread); } } m_threads = newThreadList; }
public void SetData(ThreadStorageView thread) { try { this._thread = thread; btnStop.Enabled = (_thread.State != ThreadState.Dead && _thread.State != ThreadState.Finished); this.lbName.Text = _thread.ThreadId.ToString(); txApplication.Text = _thread.ApplicationId; txExecutor.Text = _thread.ExecutorId; if (_thread.TimeStarted != DateTime.MinValue) { txCreated.Text = _thread.TimeStarted.ToString(); } if (_thread.TimeFinished != DateTime.MinValue) { txCompleted.Text = _thread.TimeFinished.ToString(); } txState.Text = _thread.StateString; txPriority.Text = _thread.Priority.ToString(); ExecutorStorageView executor = console.Manager.Admon_GetExecutor(console.Credentials, _thread.ExecutorId); if (executor != null && executor.HostName != null) { txExecutor.Text = executor.HostName; } } catch (Exception ex) { MessageBox.Show("Error getting thread properties:" + ex.Message, "Thread properties", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public ThreadStorageView GetThread(string applicationId, int threadId) { ThreadStorageView thread = null; IObjectContainer container = GetStorage(); try { IList <ThreadStorageView> threads = container.Query <ThreadStorageView>(delegate(ThreadStorageView threadFinder) { return(threadFinder.ApplicationId == applicationId && threadFinder.ThreadId == threadId); }); if (threads.Count > 0) { thread = threads[0]; } } finally { container.Close(); } return(thread); }
public int AddThread(ThreadStorageView thread) { if (thread == null) { return(-1); } ArrayList threads = null; IObjectContainer container = GetStorage(); try { IList <ArrayList> threadsList = container.Query <ArrayList>(delegate(ArrayList threadsFinder) { return(true); }); if (threadsList.Count > 0) { threads = threadsList[0]; } else { threads = new ArrayList(); } thread.InternalThreadId = threads.Count; threads.Add(thread); container.Set(threads); } finally { container.Close(); } return(thread.InternalThreadId); }
/// THIS FUNCTIONALITY IS NOT FULLY IMPLEMENTED AND IT MIGHT BE DISCARDED ALTOGETHER /// /// Loading from an XML file is the perfect tool for complex storage setups which would be useful for more in-depth unit testing /// Saving to an XML file could be used to dump the current storage state for troubleshooting, for example to receive faulty storages from the field. /// <summary> /// Save the current storage state into an XML file. /// It is important that the file format is easily editable by humans so test cases can easily be maintained. /// For this reason we do not use the build-in persistence modules. /// </summary> /// <param name="filename"></param> public void SaveToHumanReadableXmlFile(String filename) { const String storageDocumentTemplate = "<storage><users/><groups/><group_permissions/><executors/><applications/><threads/></storage>"; XmlDocument storageDocument = new XmlDocument(); storageDocument.LoadXml(storageDocumentTemplate); XmlNode usersNode = storageDocument.SelectSingleNode("/storage/users"); XmlNode groupsNode = storageDocument.SelectSingleNode("/storage/groups"); //XmlNode groupPermissionsNode = storageDocument.SelectSingleNode("/storage/group_permissions"); XmlNode executorsNode = storageDocument.SelectSingleNode("/storage/executors"); XmlNode applicationsNode = storageDocument.SelectSingleNode("/storage/applications"); XmlNode threadsNode = storageDocument.SelectSingleNode("/storage/threads"); if (m_users != null) { IEnumerator usersEnumerator = m_users.GetEnumerator(); while (usersEnumerator.MoveNext()) { UserStorageView user = usersEnumerator.Current as UserStorageView; XmlElement userElement = storageDocument.CreateElement("user"); userElement.SetAttribute("username", user.Username); userElement.SetAttribute("password", user.Password); userElement.SetAttribute("groupid", user.GroupId.ToString()); usersNode.AppendChild(userElement); } } if (m_groups != null) { IEnumerator groupsEnumerator = m_groups.GetEnumerator(); while (groupsEnumerator.MoveNext()) { GroupStorageView group = groupsEnumerator.Current as GroupStorageView; XmlElement groupElement = storageDocument.CreateElement("group"); groupElement.SetAttribute("groupname", group.GroupName); groupElement.SetAttribute("groupid", group.GroupId.ToString()); groupsNode.AppendChild(groupElement); } } // private Hashtable m_groupPermissions; // if (m_groupPermissions != null) // { // IEnumerator groupPermissionsEnumerator = m_groupPermissions.GetEnumerator(); // // while(groupPermissionsEnumerator.MoveNext()) // { // GroupPermissionStorageView group = groupPermissionsEnumerator.Current as GroupStorageView; // // XmlElement groupElement = storageDocument.CreateElement("group"); // // groupElement.SetAttribute("groupname", group.GroupName); // groupElement.SetAttribute("groupid", group.GroupId.ToString()); // // groupsNode.AppendChild(groupElement); // } // } if (m_executors != null) { IEnumerator executorsEnumerator = m_executors.GetEnumerator(); while (executorsEnumerator.MoveNext()) { ExecutorStorageView executor = executorsEnumerator.Current as ExecutorStorageView; XmlElement executorElement = storageDocument.CreateElement("executor"); executorElement.SetAttribute("executorid", executor.ExecutorId); executorElement.SetAttribute("dedicated", executor.Dedicated.ToString()); executorElement.SetAttribute("connected", executor.Connected.ToString()); executorElement.SetAttribute("pingtime", executor.PingTime.ToString()); executorElement.SetAttribute("hostname", executor.HostName); executorElement.SetAttribute("port", executor.Port.ToString()); executorElement.SetAttribute("username", executor.Username); executorElement.SetAttribute("maxcpu", executor.MaxCpu.ToString()); executorElement.SetAttribute("cpuusage", executor.CpuUsage.ToString()); executorElement.SetAttribute("availablecpu", executor.AvailableCpu.ToString()); executorElement.SetAttribute("totalcpuusage", executor.TotalCpuUsage.ToString()); executorElement.SetAttribute("maxmemory", executor.MaxMemory.ToString()); executorElement.SetAttribute("maxdisk", executor.MaxDisk.ToString()); executorElement.SetAttribute("numberofcpu", executor.NumberOfCpu.ToString()); executorElement.SetAttribute("os", executor.Os); executorElement.SetAttribute("architecture", executor.Architecture); executorsNode.AppendChild(executorElement); } } if (m_applications != null) { IEnumerator applicationsEnumerator = m_applications.GetEnumerator(); while (applicationsEnumerator.MoveNext()) { ApplicationStorageView application = applicationsEnumerator.Current as ApplicationStorageView; XmlElement applicationElement = storageDocument.CreateElement("application"); applicationElement.SetAttribute("applicationid", application.ApplicationId); applicationElement.SetAttribute("state", application.State.ToString()); applicationElement.SetAttribute("timecreated", application.TimeCreated.ToString()); applicationElement.SetAttribute("primary", application.Primary.ToString()); applicationElement.SetAttribute("username", application.Username.ToString()); applicationElement.SetAttribute("totalthreads", application.TotalThreads.ToString()); applicationElement.SetAttribute("unfinishedthreads", application.UnfinishedThreads.ToString()); applicationsNode.AppendChild(applicationElement); } } if (m_threads != null) { IEnumerator threadsEnumerator = m_threads.GetEnumerator(); while (threadsEnumerator.MoveNext()) { ThreadStorageView thread = threadsEnumerator.Current as ThreadStorageView; XmlElement threadElement = storageDocument.CreateElement("thread"); threadElement.SetAttribute("internalthreadid", thread.InternalThreadId.ToString()); threadElement.SetAttribute("applicationid", thread.ApplicationId); threadElement.SetAttribute("executorid", thread.ExecutorId); threadElement.SetAttribute("threadid", thread.ThreadId.ToString()); threadElement.SetAttribute("state", thread.State.ToString()); threadElement.SetAttribute("timestarted", thread.TimeStarted.ToString()); threadElement.SetAttribute("timefinished", thread.TimeFinished.ToString()); threadElement.SetAttribute("priority", thread.Priority.ToString()); threadElement.SetAttribute("failed", thread.Failed.ToString()); threadsNode.AppendChild(threadElement); } } storageDocument.Save(filename); }