Пример #1
0
 /// <summary>
 /// Creates an instance of the partition instance
 /// </summary>
 /// <param name="name">Name of the partition instance</param>
 /// <param name="type">Type of the partition</param>
 /// <param name="stringPartitionKey">Name of the partition if Named partition type</param>
 /// <param name="intPartitionKey">Range of the partition if range partition type</param>
 public PartitionRecord(Uri name, PartitionKeyType type, string stringPartitionKey, IntegerPartitionKeyRange intPartitionKey)
 {
     this.InstanceName  = name;
     this.PartitionType = type;
     this.StringPartitionPartitionKey = stringPartitionKey;
     this.Int64PartitionPartitionKey  = intPartitionKey;
     this.InboundSessions             = new Dictionary <string, IReliableMessagingSession>();
 }
Пример #2
0
        /// <summary>
        /// Create and Initialize partitions for the given service instance .
        /// </summary>
        /// <param name="instance">Initialize partitions for this service instance</param>
        /// <param name="isRandomPartitionType">true - if the partition type is selected randomly - String or Int64</param>
        /// <param name="type">Set the partition type to this parameter, if isRandomPartitionType is false.
        /// Singleton type is not allowed as we will set this type if instance partitions count = 1</param>
        /// <param name="int64PartitionKeyLowValMultiplier"></param>
        static void InitializePartitions(InstanceRecord instance, bool isRandomPartitionType, PartitionKeyType type, int int64PartitionKeyLowValMultiplier)
        {
            // Singletone is not a valid type
            VS.Assert.IsTrue(type != PartitionKeyType.Singleton, "unexpected partition key type {0} in StartSessionManagersForServiceInstance", type);

            // Ensure we have a valid partitions count > 0
            Uri instanceName   = instance.InstanceName;
            int partitionCount = instance.CountOfPartitions;

            VS.Assert.IsTrue(partitionCount > 0, "unexpected partitionCount {0} in StartSessionManagersForServiceInstance", partitionCount);

            // singleton case
            if (partitionCount == 1)
            {
                PartitionRecord newPartition = new PartitionRecord(
                    instanceName,
                    PartitionKeyType.Singleton,
                    null,
                    new IntegerPartitionKeyRange());
                instance.Partitions[0] = newPartition;
                return;
            }

            bool stringPartitions = false;

            // randomly allocate string or int64 partition type.
            if (isRandomPartitionType)
            {
                stringPartitions = (RandGen.Next() % 2) == 0; // decide if we have string or int64 partitions
            }
            else
            {
                if (type == PartitionKeyType.StringKey)
                {
                    stringPartitions = true;
                }
            }

            for (int i = 0; i < partitionCount; i++)
            {
                PartitionRecord newPartition;

                if (stringPartitions)
                {
                    // string partition key
                    string partitionKey = i.ToString(CultureInfo.InvariantCulture);
                    newPartition = new PartitionRecord(
                        instanceName,
                        PartitionKeyType.StringKey,
                        partitionKey,
                        new IntegerPartitionKeyRange());

                    instance.Partitions[i] = newPartition;
                    LogHelper.Log("Created; Instance: {0} StringPartition: {1}", instanceName, partitionKey);
                }
                // numerical partition key -- single number range
                IntegerPartitionKeyRange partitionInt64Key = new IntegerPartitionKeyRange
                {
                    IntegerKeyLow  = i * int64PartitionKeyLowValMultiplier,
                    IntegerKeyHigh = i * int64PartitionKeyLowValMultiplier + (RandGen.Next() % (int64PartitionKeyLowValMultiplier - 1))
                };

                newPartition = new PartitionRecord(
                    instanceName,
                    PartitionKeyType.Int64Key,
                    null,
                    partitionInt64Key);

                instance.Partitions[i] = newPartition;
                LogHelper.Log("Created; Instance: {0} Int64Partition: {1}-{2}", instanceName, partitionInt64Key.IntegerKeyLow, partitionInt64Key.IntegerKeyHigh);
            }
        }