private async Task <Int64RangePartitionInformation[]> GetServicePartitionsListAsync() { if (this.servicePartitionsTaskCompletionSource == null) { bool isNewTaskCompletionSourceCreated = false; lock (tcsLockObject) { if (this.servicePartitionsTaskCompletionSource == null) { this.servicePartitionsTaskCompletionSource = new TaskCompletionSource <Int64RangePartitionInformation[]>(); isNewTaskCompletionSourceCreated = true; } } if (isNewTaskCompletionSourceCreated) { using (var fabricClient = new FabricClient()) { ServicePartitionList partitionList = await fabricClient.QueryManager.GetPartitionListAsync(this.serviceUri); Int64RangePartitionInformation[] orderedPartitions = partitionList.Select(x => x.PartitionInformation) .OfType <Int64RangePartitionInformation>() .OrderBy(x => x.LowKey).ToArray(); if (partitionList.Count != orderedPartitions.Length) { throw new NotSupportedException("FabricPartitionEndpointResolver doesn't support non Int64RangePartitionInformation partitions"); } this.servicePartitionsTaskCompletionSource.SetResult(orderedPartitions); } } } return(this.servicePartitionsTaskCompletionSource.Task.Result); }
public async Task <int> FindServicePartitionIndexAsync(Uri serviceUri, Guid partitionId) { ServicePartitionList partitions = await _fabricClient.QueryManager.GetPartitionListAsync(serviceUri); List <Guid> partitionIdList = partitions.Select(c => c.PartitionInformation.Id).ToList(); return(partitionIdList.FindIndex(c => c.Equals(partitionId))); }
public static async Task <SelectedPartition> GetSelectedPartitionStateAsync( FabricClient fabricClient, PartitionSelector partitionSelector, TimeSpan requestTimeout, TimeSpan operationTimeout, CancellationToken cancellationToken) { ThrowIf.Null(partitionSelector, "PartitionSelector"); Guid partitionId = Guid.Empty; Uri serviceName; if (!partitionSelector.TryGetPartitionIdIfNotGetServiceName(out partitionId, out serviceName)) { // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors. We do not want to retry "service not found". ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>( () => fabricClient.QueryManager.GetPartitionListAsync( serviceName, null, default(string), requestTimeout, cancellationToken), operationTimeout, cancellationToken).ConfigureAwait(false); Partition partitionResult = partitionSelector.GetSelectedPartition(partitionsResult.ToArray(), new Random()); partitionId = partitionResult.PartitionInformation.Id; } else { // Validate the partition specified is actually from the service specified. // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors. We do not want to retry "service not found". ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>( () => fabricClient.QueryManager.GetPartitionListAsync( serviceName, null, default(string), requestTimeout, cancellationToken), operationTimeout, cancellationToken).ConfigureAwait(false); var guids = partitionsResult.Select(p => p.PartitionId()).ToList(); if (!guids.Contains(partitionId)) { // There is no partition in the service specified that has a partition with the id specified. throw new FabricException("Partition not found in this service", FabricErrorCode.PartitionNotFound); } } return(new SelectedPartition(serviceName, partitionId)); }
protected override async Task ExecuteActionAsync(FabricTestContext testContext, GetSelectedPartitionStateAction action, CancellationToken cancellationToken) { ThrowIf.Null(action.PartitionSelector, "PartitionSelector"); Guid partitionId; Uri serviceName; if (!action.PartitionSelector.TryGetPartitionIdIfNotGetServiceName(out partitionId, out serviceName)) { // TODO: make these actions which store state locally as well. ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>( () => testContext.FabricClient.QueryManager.GetPartitionListAsync( serviceName, null, default(string), action.RequestTimeout, cancellationToken), action.ActionTimeout, cancellationToken).ConfigureAwait(false); Partition partitionResult = action.PartitionSelector.GetSelectedPartition(partitionsResult.ToArray(), testContext.Random); partitionId = partitionResult.PartitionInformation.Id; } else { // Validate the partition specified is actually from the service specified. // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors. We do not want to retry "service not found". ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>( () => testContext.FabricClient.QueryManager.GetPartitionListAsync( serviceName, null, default(string), action.RequestTimeout, cancellationToken), action.ActionTimeout, cancellationToken).ConfigureAwait(false); var guids = partitionsResult.Select(p => p.PartitionId()).ToList(); if (!guids.Contains(partitionId)) { // The message in the first arg is only for debugging, it is not returned to the user. throw new FabricException("Partition not found", FabricErrorCode.PartitionNotFound); } } action.Result = new SelectedPartition(serviceName, partitionId); ResultTraceString = StringHelper.Format("PartitionSelector Selected Partition with ID {0}", action.Result); }
public async Task <IEnumerable <Uri> > LocateServicesAsync(IEnumerable <ServiceType> serviceTypes, CancellationToken cancellationToken) { var resolver = ServicePartitionResolver.GetDefault(); var fabricClient = new FabricClient(); var result = new ConcurrentBag <Uri>(); async Task getUri(ServiceType serviceType, Partition partition) { ServicePartitionKey key; switch (partition.PartitionInformation.Kind) { case ServicePartitionKind.Singleton: key = ServicePartitionKey.Singleton; break; case ServicePartitionKind.Int64Range: var longKey = (Int64RangePartitionInformation)partition.PartitionInformation; key = new ServicePartitionKey(longKey.LowKey); break; case ServicePartitionKind.Named: var namedKey = (NamedPartitionInformation)partition.PartitionInformation; key = new ServicePartitionKey(namedKey.Name); break; default: throw new ArgumentOutOfRangeException("partition.PartitionInformation.Kind"); } result.Add(await LocateServiceAsync(serviceType, key, cancellationToken)); } async Task <IEnumerable <Task> > getUriTasks(ServiceType serviceType) { string serviceTypeName = GetServiceName(serviceType); ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(new Uri($"fabric:/{_appTypeName}/{serviceTypeName}")); return(partitions.Select(p => getUri(serviceType, p))); } IEnumerable <Task <IEnumerable <Task> > > getUrisTasks = serviceTypes.Select(getUriTasks); IEnumerable <Task> getUris = (await Task.WhenAll(getUrisTasks)).SelectMany(t => t); await Task.WhenAll(getUris); return(result); }
private async Task <long> GetRandomPartitionKeyAsync(Uri serviceUri) { ServicePartitionList partitions = await _fabricClient.QueryManager.GetPartitionListAsync(serviceUri); if ((partitions == null) || (!partitions.Any())) { throw new PartitionNotAvailableException($"{serviceUri.ToString()} is not Available now."); } List <long> list = partitions.Select(c => ((Int64RangePartitionInformation)c.PartitionInformation).LowKey).ToList(); int max = list.Count; int index; lock (_syncLock) { index = _random.Next(0, max); } return(list[index]); }