[2058] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 27 | using HeuristicLab.Hive.Contracts;
|
---|
| 28 | using System.IO;
|
---|
| 29 | using System.IO.Compression;
|
---|
| 30 | using System.Xml;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Grid.HiveBridge {
|
---|
| 36 | public class HiveGridServerWrapper : IGridServer {
|
---|
| 37 | private string address;
|
---|
| 38 |
|
---|
| 39 | public HiveGridServerWrapper(string address) {
|
---|
| 40 | this.address = address;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public JobState JobState(Guid guid) {
|
---|
| 44 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(address);
|
---|
| 45 | ResponseObject<JobResult> response = executionEngineFacade.GetLastResult(guid, false);
|
---|
| 46 | if (response.Success == true &&
|
---|
| 47 | (response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE ||
|
---|
| 48 | response.StatusMessage == ApplicationConstants.RESPONSE_JOB_REQUEST_SET ||
|
---|
| 49 | response.StatusMessage == ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET ||
|
---|
| 50 | response.StatusMessage == ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT)) {
|
---|
| 51 | return HeuristicLab.Grid.JobState.Busy;
|
---|
| 52 | } else return HeuristicLab.Grid.JobState.Unknown;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public Guid BeginExecuteEngine(byte[] engine) {
|
---|
| 56 | var jobObj = CreateJobObj(engine);
|
---|
| 57 |
|
---|
| 58 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(address);
|
---|
| 59 | ResponseObject<HeuristicLab.Hive.Contracts.BusinessObjects.Job> res = executionEngineFacade.AddJob(jobObj);
|
---|
| 60 | return res.Obj.Id;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public byte[] TryEndExecuteEngine(Guid guid) {
|
---|
| 64 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(address);
|
---|
| 65 | ResponseObject<JobResult> response = executionEngineFacade.GetLastResult(guid, false);
|
---|
| 66 | if (response.Success && response.Obj != null) {
|
---|
[2064] | 67 | HeuristicLab.Hive.Engine.Job restoredJob = (HeuristicLab.Hive.Engine.Job)PersistenceManager.RestoreFromGZip(response.Obj.Result);
|
---|
| 68 | // Serialize the engine
|
---|
| 69 | MemoryStream memStream = new MemoryStream();
|
---|
| 70 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
| 71 | XmlDocument document = PersistenceManager.CreateXmlDocument();
|
---|
| 72 | Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
|
---|
| 73 | XmlNode rootNode = document.CreateElement("Root");
|
---|
| 74 | document.AppendChild(rootNode);
|
---|
| 75 | rootNode.AppendChild(PersistenceManager.Persist(restoredJob.Engine, document, dictionary));
|
---|
| 76 | document.Save(stream);
|
---|
| 77 | stream.Close();
|
---|
| 78 | return memStream.ToArray();
|
---|
[2058] | 79 | } else return null;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private HeuristicLab.Hive.Contracts.BusinessObjects.Job CreateJobObj(byte[] serializedEngine) {
|
---|
| 83 | HeuristicLab.Hive.Contracts.BusinessObjects.Job jobObj = new HeuristicLab.Hive.Contracts.BusinessObjects.Job();
|
---|
| 84 |
|
---|
[2064] | 85 | List<HivePluginInfo> requiredPlugins = new List<HivePluginInfo>();
|
---|
| 86 | IEngine engine = RestoreEngine(serializedEngine, requiredPlugins);
|
---|
| 87 |
|
---|
| 88 | HeuristicLab.Hive.Engine.Job job = new HeuristicLab.Hive.Engine.Job();
|
---|
| 89 | job.Engine.OperatorGraph.AddOperator(engine.OperatorGraph.InitialOperator);
|
---|
| 90 | job.Engine.OperatorGraph.InitialOperator = engine.OperatorGraph.InitialOperator;
|
---|
| 91 |
|
---|
| 92 | // Serialize the job
|
---|
| 93 | MemoryStream memStream = new MemoryStream();
|
---|
| 94 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
| 95 | XmlDocument document = PersistenceManager.CreateXmlDocument();
|
---|
| 96 | Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
|
---|
| 97 | XmlNode rootNode = document.CreateElement("Root");
|
---|
| 98 | document.AppendChild(rootNode);
|
---|
| 99 | rootNode.AppendChild(PersistenceManager.Persist(job, document, dictionary));
|
---|
| 100 | document.Save(stream);
|
---|
| 101 | stream.Close();
|
---|
| 102 |
|
---|
| 103 | jobObj.SerializedJob = memStream.ToArray();
|
---|
| 104 | jobObj.CoresNeeded = 1;
|
---|
| 105 | jobObj.PluginsNeeded = requiredPlugins;
|
---|
| 106 | jobObj.State = HeuristicLab.Hive.Contracts.BusinessObjects.State.offline;
|
---|
| 107 | return jobObj;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private IEngine RestoreEngine(byte[] serializedEngine, List<HivePluginInfo> requiredPlugins) {
|
---|
[2058] | 111 | // unzip and restore to determine the list of required plugins (NB: inefficient!)
|
---|
[2064] | 112 | MemoryStream memStream = new MemoryStream(serializedEngine);
|
---|
[2058] | 113 | GZipStream stream = new GZipStream(memStream, CompressionMode.Decompress, true);
|
---|
| 114 | XmlDocument document = new XmlDocument();
|
---|
| 115 | document.Load(stream);
|
---|
| 116 |
|
---|
| 117 | Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
|
---|
[2064] | 118 | XmlNode rootNode = document.ChildNodes[1].ChildNodes[0];
|
---|
| 119 | IEngine engine = (IEngine)PersistenceManager.Restore(rootNode, dictionary);
|
---|
[2058] | 120 | stream.Close();
|
---|
| 121 |
|
---|
| 122 | DiscoveryService service = new DiscoveryService();
|
---|
| 123 | List<PluginInfo> plugins = new List<PluginInfo>();
|
---|
| 124 |
|
---|
| 125 | foreach (IStorable storeable in dictionary.Values) {
|
---|
| 126 | PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
|
---|
| 127 | if (!plugins.Contains(pluginInfo)) {
|
---|
| 128 | plugins.Add(pluginInfo);
|
---|
| 129 | foreach (var dependency in pluginInfo.Dependencies) {
|
---|
| 130 | if (!plugins.Contains(dependency)) plugins.Add(dependency);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | foreach (PluginInfo uniquePlugin in plugins) {
|
---|
| 136 | HivePluginInfo pluginInfo =
|
---|
| 137 | new HivePluginInfo();
|
---|
| 138 | pluginInfo.Name = uniquePlugin.Name;
|
---|
| 139 | pluginInfo.Version = uniquePlugin.Version.ToString();
|
---|
| 140 | pluginInfo.BuildDate = uniquePlugin.BuildDate;
|
---|
[2064] | 141 | requiredPlugins.Add(pluginInfo);
|
---|
[2058] | 142 | }
|
---|
[2064] | 143 | return engine;
|
---|
[2058] | 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|