private async Task Repartition(IList <int> brokerPartitionCount, IReadOnlyList <bool> partitionsCorrectlyPlaced, int wantedPartitionCountPrBroker, BrokersObject brokersObject, string producerTablePrefixKey) { var brokerNumber = brokerPartitionCount.Count - 1; for (var partitionNumber = 0; partitionNumber < partitionsCorrectlyPlaced.Count; partitionNumber++) { // If Partition Is Not Correctly placed if (partitionsCorrectlyPlaced[partitionNumber]) { continue; } if (brokersObject.BrokerExistArray[brokerNumber] && brokerPartitionCount[brokerNumber] < wantedPartitionCountPrBroker) { await UpdateBrokerForPartition(brokersObject, brokerNumber, partitionNumber, producerTablePrefixKey); //Console.WriteLine($"Updated broker {brokerNumber} Partition {partitionNumber}"); brokerPartitionCount[brokerNumber]++; } else { partitionNumber--; brokerNumber--; //Console.WriteLine($"Went to next broker {brokerNumber} Partition {partitionNumber}"); //if(brokerNumber < 0) // Console.WriteLine("This is bad managed to get to negative broker number and this is gonna crash"); } } }
private async Task <(bool[] partitionsCorrectlyPlaced, int[] brokerPartitionCount)> GetPartitionsToRepartition( string producerTablePrefixKey, BrokersObject brokersObject, int wantedPartitionCountPrBroker, int wantedPartitionCount) { var rangeResponseTopic = await _client.GetRangeAsync(producerTablePrefixKey); var partitionsCorrectlyPlaced = new bool[wantedPartitionCount]; var brokerPartitionCount = PopulatePartitionsToRepartition(rangeResponseTopic, producerTablePrefixKey, brokersObject, wantedPartitionCountPrBroker, ref partitionsCorrectlyPlaced); return(partitionsCorrectlyPlaced, brokerPartitionCount); }
private static int[] PopulatePartitionsToRepartition(RangeResponse rangeResponseTopic, string producerTablePrefixKey, BrokersObject brokersObject, int wantedPartitionCountPrBroker, ref bool[] partitionsCorrectlyPlaced) { var brokerPartitionCount = new int[brokersObject.BrokerExistArray.Length]; foreach (var keyValue in rangeResponseTopic.Kvs) { var partitionString = keyValue.Key.ToStringUtf8().Substring(producerTablePrefixKey.Length); var brokerNumberString = keyValue.Value.ToStringUtf8().Split('-').Last(); int.TryParse(brokerNumberString, out var brokerNumber); // if broker is alive and it does not have more than the wanted partitions if (brokersObject.BrokerExistArray[brokerNumber] && brokerPartitionCount[brokerNumber] < wantedPartitionCountPrBroker) { brokerPartitionCount[brokerNumber]++; int.TryParse(partitionString, out var partition); partitionsCorrectlyPlaced[partition] = true; } } return(brokerPartitionCount); }
private async Task UpdateBrokerForPartition(BrokersObject brokersObject, int brokerNumber, int partitionNumber, string producerTablePrefixKey) { var key = producerTablePrefixKey + partitionNumber; var value = brokersObject.Name + brokerNumber; await _client.PutAsync(key, value); }