Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9227 was 9227, checked in by fschoepp, 12 years ago

#1888:

  • Experiments will be saved as JSON elements within the blob store.
  • Added simple model and JSON converters.
  • Backend stores and runs experiments.
  • Updated interfaces to save/read experiments.
  • Added a binding to automatically map incoming JSON ajax requests to experiment models.
  • Added the javascript DatatypeMapper to map parameter inputs to the right html elements and vice versa.
  • Added smartwizard to generate Wizards for creating new experiments (New.cshtml).
File size: 11.4 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 experimentUrl) {
104      PartitionKey = "ScenarioPartition";
105      RowKey = user + "_" + experimentName;     
106      User = user;
107      ExperimentJsonUrl = experimentUrl;
108    }
109
110    public string User { get; set; }
111
112    public string ExperimentJsonUrl { get; set; }
113
114  }
115
116  public class ExperimentDao : IExperimentDao {
117    public CloudBlobClient BlobClient { get; set; }
118    public CloudTableClient TableClient { get; set; }
119
120    public bool Add(string username, Model.Experiment experiment) {
121      if (FindByName(username, experiment.Name) != null)
122        return false;
123
124      // TODO: Save the whole experiment, not just the algorithm names!!!
125      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
126      container.CreateIfNotExist();
127      // For now we store it as JSON element in the blob store
128      // TODO: Make sure, that all required properties are part of the experiment!!
129      var experimentJson = AlgorithmConverter.ConvertJson(experiment);
130      Guid experimentJsonGuid = Guid.NewGuid();
131      var experimentJsonId = experiment.Name + "_" + experimentJsonGuid.ToString();
132      var blob = container.GetBlobReference(experimentJsonId);     
133      blob.UploadText(experimentJson);
134
135      TableServiceContext serviceContext = TableClient.GetDataServiceContext();     
136      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
137      var entity = new ExperimentEntity(username, experiment.Name, blob.Uri.ToString());
138      serviceContext.AddObject(AzureConstants.EXPERIMENT_TABLE, entity);
139      serviceContext.SaveChangesWithRetries();
140      return true;
141    }
142
143    public bool DeleteByName(string username, string experiment) {
144      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
145      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
146      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
147                    where e.RowKey == (username + "_" + experiment)
148                    select e).FirstOrDefault();
149     
150      if (entity == null)
151        return false;
152
153      if (entity.ExperimentJsonUrl != null) {
154        CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
155        container.CreateIfNotExist();
156        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
157        blob.DeleteIfExists();
158      }
159
160      serviceContext.DeleteObject(entity);
161      serviceContext.SaveChangesWithRetries();
162      return true;
163    }
164
165    public Model.Experiment FindByName(string username, string experiment) {
166      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
167      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
168      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
169                    where e.RowKey == (username + "_" + experiment)
170                    select e).FirstOrDefault();
171     
172      if (entity == null) {
173        return null;
174      }
175
176      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
177      container.CreateIfNotExist();
178      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
179      return AlgorithmConverter.ConvertExperimentSimple(blob.DownloadText());
180    }
181
182    //private Experiment Convert(ExperimentEntity entity, string entityJson) {
183    //  // TODO: Read the whole experiment, not just the names!
184    //  var exp = new Experiment() { Name = entity.RowKey.Split('_')[1] };
185    //  foreach (var scenarioName in entity.Algorithms.Split(','))
186    //    exp.Algorithm.Add(new Algorithm() { Name = scenarioName });
187    //  return exp;
188    //}
189
190    public IEnumerable<Model.Experiment> GetExperiments(string user) {
191      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
192      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
193      var entites = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
194                    where e.User == user
195                    select e).ToList();
196      var experiments = new List<Experiment>();
197      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
198      container.CreateIfNotExist();
199      foreach (var entity in entites) {
200        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
201        experiments.Add(AlgorithmConverter.ConvertExperimentSimple(blob.DownloadText()));
202      }
203      return experiments;
204    }
205
206
207    public Experiment GetExperimentByName(string username, string scenario) {
208      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
209      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
210      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
211                      where e.RowKey == username + "_" + scenario
212                      select e).FirstOrDefault();
213      if (entity == null || entity.ExperimentJsonUrl == null) {
214        return null;
215      }
216
217      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
218      container.CreateIfNotExist();
219      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
220      var exp = AlgorithmConverter.ConvertExperimentSimple(blob.DownloadText());
221      return exp;
222    }
223  }
224
225  public class AzureDataAccessLayer : IDataAccessLayer {
226    private IScenarioDao scenarioDao;
227    private IBlobDao blobDao;
228    private IExperimentDao expDao;
229
230    private CloudStorageAccount storageAccount;
231
232    private CloudStorageAccount StorageAccount {
233      get {
234        if (storageAccount == null) {
235          try {
236            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
237          }
238          catch (Exception ex) {
239            Trace.WriteLine(ex.Message);
240            storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=optimizationstorage1;AccountKey=n7Leom8ZFWkof/VQ2a4aRSvwOlX+Gwr3uojQF9CFJw1osmGCV0WwaNC8s7nkZ+qteLduAgW2l75WFpbXrkvG4Q==");
241          }
242        }
243        return storageAccount;
244      }     
245    }
246
247    public IScenarioDao ScenarioDao {
248      get {
249        if (scenarioDao == null) {
250          scenarioDao = new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
251        }
252        return scenarioDao;
253      }
254    }
255
256    public IBlobDao BlobDao {
257      get {
258        if (blobDao == null) {
259          blobDao = new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
260        }
261        return blobDao;
262      }
263    }
264
265    public IExperimentDao ExperimentDao {
266      get {
267        if (expDao == null) {
268          expDao = new ExperimentDao() { TableClient = StorageAccount.CreateCloudTableClient(), BlobClient = StorageAccount.CreateCloudBlobClient() };
269        }
270        return expDao;
271      }
272    }
273  }
274}
Note: See TracBrowser for help on using the repository browser.