#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Clients.Hive {
[Item("HiveClient", "Hive client.")]
public sealed class HiveClient : IContent {
private static HiveClient instance;
public static HiveClient Instance {
get {
if (instance == null) instance = new HiveClient();
return instance;
}
}
#region Properties
private ItemCollection hiveExperiments;
public ItemCollection HiveExperiments {
get { return hiveExperiments; }
set {
if (value != hiveExperiments) {
hiveExperiments = value;
OnHiveExperimentsChanged();
}
}
}
private List onlinePlugins;
public List OnlinePlugins {
get { return onlinePlugins; }
set { onlinePlugins = value; }
}
private List alreadyUploadedPlugins;
public List AlreadyUploadedPlugins {
get { return alreadyUploadedPlugins; }
set { alreadyUploadedPlugins = value; }
}
#endregion
public HiveClient() { }
#region Refresh
public void Refresh() {
OnRefreshing();
try {
hiveExperiments = new HiveItemCollection();
var he = ServiceLocator.Instance.CallHiveService>(s => s.GetHiveExperiments());
hiveExperiments.AddRange(he.Select(x => new RefreshableHiveExperiment(x)).OrderBy(x => x.HiveExperiment.Name));
}
catch {
hiveExperiments = null;
throw;
}
finally {
OnRefreshed();
}
}
public void RefreshAsync(Action exceptionCallback) {
var call = new Func(delegate() {
try {
Refresh();
}
catch (Exception ex) {
return ex;
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region Store
public static void Store(IHiveItem item) {
if (item.Id == Guid.Empty) {
if (item is RefreshableHiveExperiment) {
HiveClient.Instance.UploadExperiment((RefreshableHiveExperiment)item);
}
} else {
if (item is HiveExperiment)
ServiceLocator.Instance.CallHiveService(s => s.UpdateHiveExperiment((HiveExperiment)item));
}
}
public static void StoreAsync(Action exceptionCallback, IHiveItem item) {
var call = new Func(delegate() {
try {
Store(item);
}
catch (Exception ex) {
return ex;
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region Delete
public static void Delete(IHiveItem item) {
if (item is HiveExperiment)
ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(item.Id));
if (item is RefreshableHiveExperiment)
ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(item.Id));
item.Id = Guid.Empty;
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
var handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler HiveExperimentsChanged;
private void OnHiveExperimentsChanged() {
var handler = HiveExperimentsChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
public static void StartExperiment(Action exceptionCallback, RefreshableHiveExperiment refreshableHiveExperiment) {
HiveClient.StoreAsync(
new Action((Exception ex) => {
refreshableHiveExperiment.HiveExperiment.ExecutionState = ExecutionState.Prepared;
exceptionCallback(ex);
}), refreshableHiveExperiment);
refreshableHiveExperiment.HiveExperiment.ExecutionState = ExecutionState.Started;
}
public static void PauseExperiment(HiveExperiment hiveExperiment) {
ServiceLocator.Instance.CallHiveService(service => {
foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
service.PauseJob(job.Job.Id);
}
});
hiveExperiment.ExecutionState = ExecutionState.Paused;
}
public static void StopExperiment(HiveExperiment hiveExperiment) {
ServiceLocator.Instance.CallHiveService(service => {
foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
service.StopJob(job.Job.Id);
}
});
// execution state does not need to be set. it will be set to Stopped, when all jobs have been downloaded
}
#region Upload Experiment
private void UploadExperiment(RefreshableHiveExperiment refreshableHiveExperiment) {
try {
refreshableHiveExperiment.HiveExperiment.Progress = new Progress("Connecting to server...");
refreshableHiveExperiment.HiveExperiment.IsProgressing = true;
ServiceLocator.Instance.CallHiveService(service => {
IEnumerable resourceNames = ToResourceNameList(refreshableHiveExperiment.HiveExperiment.ResourceNames);
var resourceIds = new List();
foreach (var resourceName in resourceNames) {
Guid resourceId = service.GetResourceId(resourceName);
if (resourceId == Guid.Empty) {
throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
}
resourceIds.Add(resourceId);
}
foreach (OptimizerHiveJob hiveJob in refreshableHiveExperiment.HiveExperiment.HiveJobs.OfType()) {
hiveJob.SetIndexInParentOptimizerList(null);
}
// upload HiveExperiment
refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading HiveExperiment...";
refreshableHiveExperiment.HiveExperiment.Id = service.AddHiveExperiment(refreshableHiveExperiment.HiveExperiment);
int totalJobCount = refreshableHiveExperiment.HiveExperiment.GetAllHiveJobs().Count();
int jobCount = 0;
// upload plugins
refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading plugins...";
this.OnlinePlugins = service.GetPlugins();
this.AlreadyUploadedPlugins = new List();
Plugin configFilePlugin = UploadConfigurationFile(service);
this.alreadyUploadedPlugins.Add(configFilePlugin);
// upload jobs
refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading jobs...";
foreach (HiveJob hiveJob in refreshableHiveExperiment.HiveExperiment.HiveJobs) {
UploadJobWithChildren(refreshableHiveExperiment.HiveExperiment.Progress, service, hiveJob, null, resourceIds, ref jobCount, totalJobCount, configFilePlugin.Id, refreshableHiveExperiment.HiveExperiment.UseLocalPlugins, refreshableHiveExperiment.HiveExperiment.Id, refreshableHiveExperiment.Log);
}
if (refreshableHiveExperiment.RefreshAutomatically) refreshableHiveExperiment.StartResultPolling();
});
}
finally {
refreshableHiveExperiment.HiveExperiment.IsProgressing = false;
}
}
///
/// Uploads the local configuration file as plugin
///
private static Plugin UploadConfigurationFile(IHiveService service) {
string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HeuristicLab 3.3.exe");
string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
Plugin configPlugin = new Plugin() { Name = "Configuration", IsLocal = true, Version = new Version() };
PluginData configFile = new PluginData() { FileName = configFileName, Data = File.ReadAllBytes(configFilePath) };
configPlugin.Id = service.AddPlugin(configPlugin, new List { configFile });
return configPlugin;
}
///
/// Uploads the given job and all its child-jobs while setting the proper parentJobId values for the childs
///
///
///
/// shall be null if its the root job
///
private void UploadJobWithChildren(IProgress progress, IHiveService service, HiveJob hiveJob, HiveJob parentHiveJob, IEnumerable groups, ref int jobCount, int totalJobCount, Guid configPluginId, bool useLocalPlugins, Guid hiveExperimentId, ILog log) {
jobCount++;
progress.Status = string.Format("Serializing job {0} of {1}", jobCount, totalJobCount);
JobData jobData;
List plugins;
if (hiveJob.ItemJob.ComputeInParallel &&
(hiveJob.ItemJob.Item is Optimization.Experiment || hiveJob.ItemJob.Item is Optimization.BatchRun)) {
hiveJob.Job.IsParentJob = true;
hiveJob.Job.FinishWhenChildJobsFinished = true;
jobData = hiveJob.GetAsJobData(true, out plugins);
} else {
hiveJob.Job.IsParentJob = false;
hiveJob.Job.FinishWhenChildJobsFinished = false;
jobData = hiveJob.GetAsJobData(false, out plugins);
}
TryAndRepeat(() => {
hiveJob.Job.PluginsNeededIds = PluginUtil.GetPluginDependencies(service, this.onlinePlugins, this.alreadyUploadedPlugins, plugins, useLocalPlugins);
}, -1, "Failed to upload plugins");
hiveJob.Job.PluginsNeededIds.Add(configPluginId);
hiveJob.Job.HiveExperimentId = hiveExperimentId;
progress.Status = string.Format("Uploading job {0} of {1} ({2} kb, {3} objects)", jobCount, totalJobCount, jobData.Data.Count() / 1024, hiveJob.ItemJob.GetObjectGraphObjects().Count());
progress.ProgressValue = (double)jobCount / totalJobCount;
log.LogMessage(progress.Status);
TryAndRepeat(() => {
if (parentHiveJob != null) {
hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
} else {
hiveJob.Job.Id = service.AddJob(hiveJob.Job, jobData, groups.ToList());
}
}, -1, "Failed to add job");
foreach (HiveJob child in hiveJob.ChildHiveJobs) {
UploadJobWithChildren(progress, service, child, hiveJob, groups, ref jobCount, totalJobCount, configPluginId, useLocalPlugins, hiveExperimentId, log);
}
}
#endregion
#region Download Experiment
public static void LoadExperiment(HiveExperiment hiveExperiment) {
hiveExperiment.Progress = new Progress();
try {
hiveExperiment.IsProgressing = true;
int totalJobCount = 0;
IEnumerable allJobs;
hiveExperiment.Progress.Status = "Connecting to Server...";
// fetch all Job objects to create the full tree of tree of HiveJob objects
hiveExperiment.Progress.Status = "Downloading list of jobs...";
allJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightExperimentJobs(hiveExperiment.Id));
totalJobCount = allJobs.Count();
HiveJobDownloader downloader = new HiveJobDownloader(allJobs.Select(x => x.Id));
downloader.StartAsync();
while (!downloader.IsFinished) {
hiveExperiment.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
hiveExperiment.Progress.Status = string.Format("Downloading/deserializing jobs... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
Thread.Sleep(500);
if (downloader.IsFaulted) {
throw downloader.Exception;
}
}
IDictionary allHiveJobs = downloader.Results;
hiveExperiment.HiveJobs = new ItemCollection(allHiveJobs.Values.Where(x => !x.Job.ParentJobId.HasValue));
if (hiveExperiment.IsFinished()) {
hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
} else {
hiveExperiment.ExecutionState = Core.ExecutionState.Started;
}
// build child-job tree
foreach (HiveJob hiveJob in hiveExperiment.HiveJobs) {
BuildHiveJobTree(hiveJob, allJobs, allHiveJobs);
}
hiveExperiment.OnLoaded();
}
finally {
hiveExperiment.IsProgressing = false;
}
}
private static void BuildHiveJobTree(HiveJob parentHiveJob, IEnumerable allJobs, IDictionary allHiveJobs) {
IEnumerable childJobs = from job in allJobs
where job.ParentJobId.HasValue && job.ParentJobId.Value == parentHiveJob.Job.Id
orderby job.DateCreated ascending
select job;
foreach (LightweightJob job in childJobs) {
HiveJob childHiveJob = allHiveJobs[job.Id];
parentHiveJob.AddChildHiveJob(childHiveJob);
BuildHiveJobTree(childHiveJob, allJobs, allHiveJobs);
}
}
#endregion
///
/// Converts a string which can contain Ids separated by ';' to a enumerable
///
private static IEnumerable ToResourceNameList(string resourceNames) {
if (!string.IsNullOrEmpty(resourceNames)) {
return resourceNames.Split(';');
} else {
return new List();
}
}
public static ItemJob LoadItemJob(Guid jobId) {
JobData jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
try {
return PersistenceUtil.Deserialize(jobData.Data);
}
catch {
return null;
}
}
///
/// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
/// If repetitions is -1, it is repeated infinitely.
///
public static void TryAndRepeat(Action action, int repetitions, string errorMessage) {
while (true) {
try { action(); return; }
catch (Exception e) {
if (repetitions == 0)
throw new HiveException(errorMessage, e);
repetitions--;
}
}
}
}
}