private void LoadClassifier(ContentPlacementClassifierConfiguration config) { Contract.Requires(Directory.Exists(config.QueueToMachineMapDirectory), $"QueToMachineMapDirectory directory [{config.QueueToMachineMapDirectory}] does not exists"); Contract.Requires(Directory.Exists(config.QueueDistanceMapDirectory), $"QueueDistanceMapDirectory directory [{config.QueueDistanceMapDirectory}] does not exists"); Contract.Requires(File.Exists(config.RandomForestFile), $"RandomForestFile [{config.RandomForestFile}] does not exists"); // load the forest m_forest = RandomForest.FromWekaFile(config.RandomForestFile); // now load the alternatives per queue LoadAlternativesPerQueue(config); // done }
/// <nodoc /> public Task <Result <bool> > StartupAsync() { try { var configuration = ContentPlacementClassifierConfiguration.FromJson(m_configPath); LoadClassifier(configuration); return(Task.FromResult(new Result <bool>(true))); } catch (Exception e) { return(Task.FromResult(new Result <bool>(e))); } }
private void LoadAlternativesPerQueue(ContentPlacementClassifierConfiguration config) { // for each queue foreach (var queue in Directory.EnumerateFiles(config.QueueDistanceMapDirectory)) { var queueName = Path.GetFileNameWithoutExtension(queue); var queueFileReader = new StreamReader(queue); m_alternativesPerQueue[queueName] = new List <string>(); try { if (config.LoadMode == LoadMode.MachinesFromClosestQueue) { // take the first line of this file string closestQueue; while ((closestQueue = queueFileReader.ReadLine()) != null) { closestQueue = closestQueue.Split(',')[0]; break; } // and now get some machines var machineFile = Path.Combine(config.QueueToMachineMapDirectory, closestQueue); var machineFileReader = new StreamReader(machineFile); string frequentMachine; while ((frequentMachine = machineFileReader.ReadLine()) != null) { frequentMachine = frequentMachine.Split(',')[0]; m_alternativesPerQueue[queueName].Add(frequentMachine); if (m_alternativesPerQueue[queueName].Count == config.QueueToMachineMapInstanceCount) { // done here, there should never be duplicates break; } } // done machineFileReader.Close(); } else if (config.LoadMode == LoadMode.NMachinesPerClosestQueues) { // validate this here Contract.Requires(config.QueueInstanceCount > 0, $"QueueInstanceCount has to be positive"); Contract.Requires(config.MachinesInstanceCount > 0, $"MachinesInstanceCount has to be positive"); // take the first X lines of this file var selectedQueues = new List <string>(); string line; while ((line = queueFileReader.ReadLine()) != null) { selectedQueues.Add(line.Split(',')[0]); if (selectedQueues.Count == config.QueueInstanceCount) { break; } } // now, select Y machines per queue foreach (var closeQueue in selectedQueues) { var machineFile = Path.Combine(config.QueueToMachineMapDirectory, closeQueue); var machineFileReader = new StreamReader(machineFile); var added = 0; string frequentMachine; while ((frequentMachine = machineFileReader.ReadLine()) != null) { frequentMachine = frequentMachine.Split(',')[0]; if (!m_alternativesPerQueue[queueName].Contains(frequentMachine)) { m_alternativesPerQueue[queueName].Add(frequentMachine); ++added; } if (added == config.MachinesInstanceCount) { // done here, there should never be duplicates break; } } // done machineFileReader.Close(); } } } finally { // close this queueFileReader.Close(); } } // and we should be done here }
/// <summary> /// The argument is the path to a valid ContentPlacementClassifierConfiguration /// </summary> public ContentPlacementClassifier(ContentPlacementClassifierConfiguration config) { Contract.Requires(config != null, "Config file path cannot be null"); LoadClassifier(config); }