Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Azure/DAL.cs @ 9305

Last change on this file since 9305 was 9305, checked in by fschoepp, 11 years ago

#1888:

  • Added an Update / GetExperiment... methods to the controller for updating and querying experiments.
  • The AlgorithmConverter class now properly converts from/to JSON format.
  • Integrated backbone js as MVC provider for JavaScript + jquery.
  • Added experiment.model.js + experiment.view.js + experiment.controller.js containing the MVC impl. for the Experiment pages.
  • Added new methods to the ExperimentController usable by the backbone js model implementation.
  • Added the experiment dialog from HL 3.3.7 (variate experiment parameters). It's capable of variating the algorithm parameters.
File size: 13.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using Microsoft.WindowsAzure;
7using Microsoft.WindowsAzure.StorageClient;
8using Microsoft.WindowsAzure.ServiceRuntime;
9using System.Diagnostics;
10using HeuristicLab.Services.Optimization.ControllerService.Model;
11using HeuristicLab.Services.Optimization.ControllerService.Parsers;
12
13namespace HeuristicLab.Services.Optimization.ControllerService.Azure {
14  public static class AzureConstants {
15    public static readonly string SCENARIO_TABLE = "Scenario";
16    public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
17    public static readonly string EXPERIMENT_TABLE = "Experiment";
18    public static readonly string EXPERIMENT_BLOB_CONTAINER = "experiment";
19    public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
20
21   
22  }
23
24  public class ScenarioDao : IScenarioDao {   
25    public CloudTableClient TableClient { get; set; }
26
27    public bool Add(ScenarioEntity entity) {
28      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
29      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
30      ScenarioEntity dbEntity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
31                               where e.RowKey == entity.RowKey
32                               select e).FirstOrDefault();
33      if (dbEntity != null)
34        return false;
35
36      serviceContext.AddObject(AzureConstants.SCENARIO_TABLE, entity);
37      serviceContext.SaveChanges();
38      return true;
39    }
40
41    public bool DeleteByName(string scenarioName) {
42      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
43      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
44      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
45                               where e.RowKey == scenarioName
46                               select e).FirstOrDefault();
47      if (entity == null)
48        return false;
49
50      serviceContext.DeleteObject(entity);
51      serviceContext.SaveChangesWithRetries();
52      return true;
53    }
54
55    public ScenarioEntity FindByName(string scenarioName) {
56      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
57      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
58      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
59                               where e.RowKey == scenarioName
60                               select e).FirstOrDefault();
61      return entity;
62    }
63
64
65    public IEnumerable<ScenarioEntity> GetAllEntities() {
66      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
67      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
68      return (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
69             select e).AsEnumerable();
70    }
71  }
72
73  public class BlobDao : IBlobDao {
74    public CloudBlobClient BlobClient { get; set; }
75
76    public bool Add(StringEntry entry) {
77      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
78      container.CreateIfNotExist();
79      var blob = container.GetBlobReference(entry.Key);
80      blob.UploadText(entry.Text);
81      return true;
82    }
83
84    public bool DeleteByKey(string entryKey) {
85      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
86      container.CreateIfNotExist();
87      var blob = container.GetBlobReference(entryKey);
88      return blob.DeleteIfExists();
89    }
90
91    public StringEntry FindByKey(string entryKey) {
92      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
93      container.CreateIfNotExist();
94      var blob = container.GetBlobReference(entryKey);
95      return new StringEntry() { Key = entryKey, Text = blob.DownloadText() };     
96    }
97  }
98
99  internal sealed class ExperimentEntity : TableServiceEntity {
100    public ExperimentEntity() {
101    }
102
103    public ExperimentEntity(string user, string experimentName, string experimentId, string experimentUrl) {
104      PartitionKey = "ScenarioPartition";
105      RowKey = user + "_" + experimentName;     
106      User = user;
107      ExperimentId = experimentId;
108      ExperimentJsonUrl = experimentUrl;
109    }
110
111    public string ExperimentId { get; set; }
112
113    public string User { get; set; }
114
115    public string ExperimentJsonUrl { get; set; }
116
117  }
118
119  public class ExperimentDao : IExperimentDao {
120    public CloudBlobClient BlobClient { get; set; }
121    public CloudTableClient TableClient { get; set; }
122
123    public bool Add(string username, Model.Experiment experiment) {
124      if (FindByName(username, experiment.Name) != null)
125        return false;
126
127      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
128      container.CreateIfNotExist();
129      // For now we store it as JSON element in the blob store
130      var experimentJson = AlgorithmConverter.ConvertExperimentToJson(experiment);
131      Guid experimentJsonGuid = Guid.NewGuid();
132      var experimentJsonId = experiment.Name + "_" + experimentJsonGuid.ToString();
133      experimentJson["nodeId"] = experimentJsonId.ToString();
134      var blob = container.GetBlobReference(experimentJsonId);     
135      blob.UploadText(experimentJson.ToString());
136      experiment.Id = experimentJsonId;
137
138      TableServiceContext serviceContext = TableClient.GetDataServiceContext();     
139      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
140      var entity = new ExperimentEntity(username, experiment.Name, experiment.Id, blob.Uri.ToString());
141      serviceContext.AddObject(AzureConstants.EXPERIMENT_TABLE, entity);
142      serviceContext.SaveChangesWithRetries();
143      return true;
144    }
145
146    public bool Update(string username, Experiment experiment) {
147      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
148      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
149      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
150                    where e.ExperimentId == experiment.Id
151                    select e).FirstOrDefault();
152      if (entity == null) {
153        return false;
154      }
155
156      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
157      container.CreateIfNotExist();
158      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
159      var experimentJson = AlgorithmConverter.ConvertExperimentToJson(experiment).ToString();
160      blob.UploadText(experimentJson);
161      return true;
162    }
163
164    public bool DeleteByName(string username, string experiment) {
165      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
166      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
167      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
168                    where e.RowKey == (username + "_" + experiment)
169                    select e).FirstOrDefault();
170     
171      if (entity == null)
172        return false;
173
174      if (entity.ExperimentJsonUrl != null) {
175        CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
176        container.CreateIfNotExist();
177        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
178        blob.DeleteIfExists();
179      }
180
181      serviceContext.DeleteObject(entity);
182      serviceContext.SaveChangesWithRetries();
183      return true;
184    }
185
186    public Model.Experiment FindByName(string username, string experiment) {
187      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
188      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
189      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
190                    where e.RowKey == (username + "_" + experiment)
191                    select e).FirstOrDefault();
192     
193      if (entity == null) {
194        return null;
195      }
196
197      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
198      container.CreateIfNotExist();
199      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
200      return AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
201    }
202
203    //private Experiment Convert(ExperimentEntity entity, string entityJson) {
204    //  // TODO: Read the whole experiment, not just the names!
205    //  var exp = new Experiment() { Name = entity.RowKey.Split('_')[1] };
206    //  foreach (var scenarioName in entity.Algorithms.Split(','))
207    //    exp.Algorithm.Add(new Algorithm() { Name = scenarioName });
208    //  return exp;
209    //}
210
211    public IEnumerable<Model.Experiment> GetExperiments(string user) {
212      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
213      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
214      var entites = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
215                    where e.User == user
216                    select e).ToList();
217      var experiments = new List<Experiment>();
218      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
219      container.CreateIfNotExist();
220      foreach (var entity in entites) {
221        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
222        experiments.Add(AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText()));
223      }
224      return experiments;
225    }
226
227
228    public Experiment GetExperimentByName(string username, string scenario) {
229      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
230      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
231      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
232                      where e.RowKey == username + "_" + scenario
233                      select e).FirstOrDefault();
234      if (entity == null || entity.ExperimentJsonUrl == null) {
235        return null;
236      }
237
238      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
239      container.CreateIfNotExist();
240      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
241      var exp = AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
242      return exp;
243    }
244
245
246    public Experiment GetExperimentById(User user, string nodeId) {
247      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
248      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
249      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
250                    where e.ExperimentId == nodeId
251                    select e).FirstOrDefault();
252      if (entity == null || entity.ExperimentJsonUrl == null) {
253        return null;
254      }
255
256      if (entity.User != user.Username)
257        return null;
258
259      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
260      container.CreateIfNotExist();
261      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
262      return AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
263    }
264
265  }
266
267  public class AzureDataAccessLayer : IDataAccessLayer {
268    private IScenarioDao scenarioDao;
269    private IBlobDao blobDao;
270    private IExperimentDao expDao;
271
272    private CloudStorageAccount storageAccount;
273
274    private CloudStorageAccount StorageAccount {
275      get {
276        if (storageAccount == null) {
277          try {
278            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
279          }
280          catch (Exception ex) {
281            Trace.WriteLine(ex.Message);
282            storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=optimizationstorage1;AccountKey=n7Leom8ZFWkof/VQ2a4aRSvwOlX+Gwr3uojQF9CFJw1osmGCV0WwaNC8s7nkZ+qteLduAgW2l75WFpbXrkvG4Q==");
283          }
284        }
285        return storageAccount;
286      }     
287    }
288
289    public IScenarioDao ScenarioDao {
290      get {
291        if (scenarioDao == null) {
292          scenarioDao = new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
293        }
294        return scenarioDao;
295      }
296    }
297
298    public IBlobDao BlobDao {
299      get {
300        if (blobDao == null) {
301          blobDao = new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
302        }
303        return blobDao;
304      }
305    }
306
307    public IExperimentDao ExperimentDao {
308      get {
309        if (expDao == null) {
310          expDao = new ExperimentDao() { TableClient = StorageAccount.CreateCloudTableClient(), BlobClient = StorageAccount.CreateCloudBlobClient() };
311        }
312        return expDao;
313      }
314    }
315  }
316}
Note: See TracBrowser for help on using the repository browser.