private async Task CreatePartitionAsync(string simId, int partitionNumber, Dictionary <string, List <string> > deviceIdsByModel) { var partitionId = this.GetPartitionId(simId, partitionNumber); int partitionSize = deviceIdsByModel.Select(x => x.Value.Count).Sum(); this.log.Debug( "Creating partition...", () => new { simId, partitionId, partitionSize }); var partition = new DevicesPartition { Id = partitionId, SimulationId = simId, Size = partitionSize, DeviceIdsByModel = deviceIdsByModel }; var partitionRecord = this.partitionsStorage.BuildRecord( partition.Id, JsonConvert.SerializeObject(partition)); await this.partitionsStorage.CreateAsync(partitionRecord); this.log.Debug( "Partition created", () => new { simId, partitionId, partitionSize }); }
private async Task <bool> TryToCreateActorsForPartitionAsync(DevicesPartition partition) { var count = 0; // Prepare the models first, without creating the actors yet, to handle failures before creating actors var deviceModelsData = new List <Tuple <DeviceModel, List <string> > >(); foreach (var x in partition.DeviceIdsByModel) { string deviceModelId = x.Key; List <string> deviceIds = x.Value; try { DeviceModel deviceModel = await this.deviceModels.GetWithOverrideAsync(deviceModelId, this.simulation); deviceModelsData.Add(new Tuple <DeviceModel, List <string> >(deviceModel, deviceIds)); } catch (ResourceNotFoundException) { // Log and continue ignoring the device model this.log.Error("The device model doesn't exist", () => new { deviceModelId }); } catch (Exception e) { this.log.Error("Unexpected error while preparing the device model or starting device actors", () => new { deviceModelId, e }); return(false); } } // Create all the actors, no exceptions to handle here foreach (Tuple <DeviceModel, List <string> > deviceModelData in deviceModelsData) { DeviceModel deviceModel = deviceModelData.Item1; List <string> deviceIds = deviceModelData.Item2; foreach (var deviceId in deviceIds) { this.CreateActorsForDevice(deviceId, deviceModel, this.deviceCount); this.deviceCount++; count++; // Set ActualStartTime if required if (!this.simulation.ActualStartTime.HasValue) { this.simulation.ActualStartTime = DateTimeOffset.UtcNow; await this.simulations.UpsertAsync(this.simulation); } } } this.log.Info("Devices added to the node and started", () => new { Simulation = this.simulation.Id, DeviceAddedCount = count, TotalDeviceCount = this.deviceCount }); return(true); }
private int DeleteActorsForPartition(DevicesPartition partition) { var count = 0; foreach (var x in partition.DeviceIdsByModel) { List <string> deviceIds = x.Value; foreach (var deviceId in deviceIds) { this.DeleteDeviceActors(deviceId); this.deviceCount--; count++; } } return(count); }