Exemple #1
0
        /// <summary>
        /// Provision the contents of the redis caches
        /// </summary>
        /// <param name="persistentCacheConnectionString">connection string for persistent cache</param>
        /// <param name="tableStoreManager">table store manager</param>
        /// <returns>provisioning task</returns>
        private static async Task ProvisionRedisCaches(string persistentCacheConnectionString, CTStoreManager tableStoreManager)
        {
            Console.WriteLine("Provisioning Redis Caches...");

            // insert the store version number into table storage
            var versionEntity = new StoreVersionEntity {
                Version = tableStoreManager.StoreVersionString
            };

            ObjectTable versionTable = Table.GetObjectTable(
                ContainerIdentifier.ServiceConfig.ToString(),
                tableStoreManager.ServiceConfigContainerInitials,
                tableStoreManager.StoreVersionTableName,
                tableStoreManager.StoreVersionTableInitials,
                StorageMode.CacheOnly);
            var operation = Operation.Insert(versionTable, tableStoreManager.StoreVersionKey, tableStoreManager.StoreVersionKey, versionEntity);

            RedisCache redisCachePersistent = new RedisCache(persistentCacheConnectionString);
            CTStore    persistentCache      = new CTStore(null, redisCachePersistent);

            // perform the insert operation on persistent redis
            // note that we only insert the version number into persistent redis,
            // because with volatile redis each time the cache restarts the version number will be lost
            await persistentCache.ExecuteOperationAsync(operation, ConsistencyMode.Strong);
        }
        /// <summary>
        /// Provision azure table storage with tables
        /// </summary>
        /// <param name="tableStoreManager">table store manager</param>
        /// <param name="azureConnectionString">Azure connection string</param>
        /// <returns>provision task</returns>
        private static async Task ProvisionAzureStorageTables(CTStoreManager tableStoreManager, string azureConnectionString)
        {
            // Creates Social Plus containers/tables defined in ContainerIdentifier.cs/TableIdentifier.cs
            // Containers map to tables in Azure table storage (container names map to table names)
            // We create them (if not exists) through the CTStore interface
            Console.WriteLine("Creating all tables in Azure Table Store...");

            // Get azure table storage with the give connection string
            AzureTableStorage azureTableStorage = new AzureTableStorage(azureConnectionString);

            // Get CTStore using the azure table storage
            CTStore store = new CTStore(azureTableStorage, null);

            // Enumerate all the containers defined
            foreach (ContainerIdentifier containerIdentifier in Enum.GetValues(typeof(ContainerIdentifier)))
            {
                if (!ContainerTableDescriptorProvider.Containers.ContainsKey(containerIdentifier))
                {
                    Console.WriteLine("  " + containerIdentifier.ToString() + " - Descriptor not found");
                    continue;
                }

                // in Azure, table deletion can potentially take a long time.
                // this may lead to conflict exceptions if you delete a table and then attempt to
                // recreate it.  Below, we retry once every 30 seconds for up to 5 minutes if needed.

                // wait up to 5 minutes before giving up on table creation
                int attempts    = 0;
                int maxAttempts = 10;
                while (true)
                {
                    try
                    {
                        attempts++;

                        // create the container
                        await store.CreateContainerAsync(containerIdentifier.ToString());

                        // if we reach here, the create was successful
                        break;
                    }
                    catch (ConflictException e)
                    {
                        if (attempts < maxAttempts)
                        {
                            // sleep for 30 seconds before trying
                            await Task.Delay(30 * 1000);
                        }
                        else
                        {
                            // give up after reaching maxAttempts
                            throw e;
                        }
                    }
                }

                Console.WriteLine("  " + containerIdentifier.ToString() + " - Table Container Provisioned");
            }

            // insert the store version number into table storage
            var versionEntity = new StoreVersionEntity {
                Version = tableStoreManager.StoreVersionString
            };

            // the StoreVersion container has no descriptor, so we need to create it
            await store.CreateContainerAsync(ContainerIdentifier.ServiceConfig.ToString());

            Console.WriteLine("  " + ContainerIdentifier.ServiceConfig.ToString() + " - Table Container Provisioned");
            ObjectTable versionTable = Table.GetObjectTable(
                ContainerIdentifier.ServiceConfig.ToString(),
                tableStoreManager.ServiceConfigContainerInitials,
                tableStoreManager.StoreVersionTableName,
                tableStoreManager.StoreVersionTableInitials,
                StorageMode.PersistentOnly);
            var operation = Operation.Insert(versionTable, tableStoreManager.StoreVersionKey, tableStoreManager.StoreVersionKey, versionEntity);

            // perform the insert on Azure table storage
            await store.ExecuteOperationAsync(operation, ConsistencyMode.Strong);

            Console.WriteLine("  SocialPlusStoreVersion number provisioned");
        }