예제 #1
0
        private async Task InitializeMetaDataOperator(StateMetaDataStorage metaDataStorage)
        {
            if (_metaDataOperator == null)
            {
                switch (metaDataStorage)
                {
                case StateMetaDataStorage.PermamentCosmosCollection:
                    _metaDataOperator = new PermanentCollectionMetaDataOperator(_client, _databaseName);
                    break;

                //case StateMetaDataStorage.TemporaryCosmosCollection:
                //    _metaDataOperator = new TemporaryCollectionMetaDataOperator();
                //    break;
                case StateMetaDataStorage.InMemoryCollection:
                    _metaDataOperator = new InMemoryMetaDataOperator();
                    break;
                }
            }

            await _metaDataOperator.AddActivity(_databaseName, _collectionName, DateTimeOffset.Now, ActivityStrength.Cold);

            await _metaDataOperator.AddActiveCollection(_databaseName, _collectionName, _minRu);
        }
예제 #2
0
        public static async Task <ScaleOperation> ScaleDownCollectionAsync(DocumentClient client, IMetaDataOperator metaDataOperator, string databaseName, string collectionName, int minRu)
        {
            try
            {
                Database database = client.CreateDatabaseQuery($"SELECT * FROM d WHERE d.id = \"{databaseName}\"").AsEnumerable().First();

                List <DocumentCollection> collections = client.CreateDocumentCollectionQuery((String)database.SelfLink).ToList();

                foreach (var collection in collections)
                {
                    if (collection.Id == collectionName)
                    {
                        var currentRu = GetCurrentRU(client, collection, out OfferV2 offer);

                        if (currentRu <= minRu)
                        {
                            return(new ScaleOperation()
                            {
                                ScaledSuccess = false,
                                ScaleFailReason = "RU already at minimum."
                            });
                        }

                        offer = new OfferV2(offer, minRu);

                        await client.ReplaceOfferAsync(offer);

                        Trace.WriteLine($"Scaled {databaseName}|{collectionName} to {(int)minRu}RU. ({DateTimeOffset.Now})");

                        return(new ScaleOperation()
                        {
                            ScaledTo = minRu,
                            ScaledSuccess = true,
                            OperationTime = DateTimeOffset.Now
                        });
                    }
                }

                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = "Collection not found."
                });
            } catch (Exception e)
            {
                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = e.Message
                });
            }
        }
예제 #3
0
        public static ScaleOperation ScaleUpCollectionAsync(DocumentClient client, IMetaDataOperator metaDataOperator, string databaseName, string collectionName, int minRu, int maxRu)
        {
            try
            {
                var latestActivty = metaDataOperator.GetLatestScaleUp(databaseName, collectionName);

                if (DateTimeOffset.Now.AddSeconds(-5) < latestActivty)
                {
                    return(new ScaleOperation()
                    {
                        ScaledSuccess = false,
                        ScaleFailReason = "There has been another scale within 5 second span."
                    });
                }

                Database database = client.CreateDatabaseQuery($"SELECT * FROM d WHERE d.id = \"{databaseName}\"").AsEnumerable().First();

                List <DocumentCollection> collections = client.CreateDocumentCollectionQuery((String)database.SelfLink).ToList();

                bool scaled = false;
                foreach (var collection in collections)
                {
                    if (collection.Id == collectionName)
                    {
                        lock (_lockingObject)
                        {
                            if (scaled)
                            {
                                return(new ScaleOperation()
                                {
                                    ScaledSuccess = false,
                                    ScaleFailReason = "Another thread already scaled."
                                });
                            }

                            var currentRu = GetCurrentRU(client, collection, out OfferV2 offer);

                            if (currentRu <= minRu)
                            {
                                return(new ScaleOperation()
                                {
                                    ScaledSuccess = false,
                                    ScaleFailReason = "RU already at minimum."
                                });
                            }

                            var newRu = currentRu + 500;

                            if (newRu <= maxRu)
                            {
                                offer = new OfferV2(offer, (int)newRu);

                                client.ReplaceOfferAsync(offer).Wait();
                                Trace.WriteLine($"Scaled {databaseName}|{collectionName} to {(int)newRu} ({DateTimeOffset.Now})");

                                metaDataOperator.AddScaleActivity(databaseName, collectionName, (int)newRu, DateTimeOffset.Now);

                                ScaleOperation op = new ScaleOperation();
                                op.ScaledFrom    = (int)currentRu;
                                op.ScaledTo      = (int)newRu;
                                op.OperationTime = DateTimeOffset.Now;


                                scaled = true;
                                return(op);
                            }
                            else
                            {
                                return(new ScaleOperation()
                                {
                                    ScaledSuccess = false,
                                    ScaleFailReason = "Maximum RU reached."
                                });
                            }
                        }
                    }
                }

                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = "Could not find the collection to scale."
                });
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message + $" ({DateTimeOffset.Now})");

                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = e.Message
                });
            }
        }
예제 #4
0
        public static async Task <ScaleOperation> ScaleUpMaxCollectionAsync(DocumentClient client, IMetaDataOperator metaDataOperator, string databaseName, string collectionName, int minRu, int maxRu)
        {
            try
            {
                Database database = client.CreateDatabaseQuery($"SELECT * FROM d WHERE d.id = \"{databaseName}\"").AsEnumerable().First();

                List <DocumentCollection> collections = client.CreateDocumentCollectionQuery((String)database.SelfLink).ToList();

                foreach (var collection in collections)
                {
                    if (collection.Id == collectionName)
                    {
                        var currentRu = GetCurrentRU(client, collection, out OfferV2 offer);

                        if (currentRu >= maxRu)
                        {
                            return(new ScaleOperation()
                            {
                                ScaledSuccess = false,
                                ScaleFailReason = "RU already at maximum."
                            });
                        }

                        if (currentRu < maxRu)
                        {
                            offer = new OfferV2(offer, (int)maxRu);

                            await client.ReplaceOfferAsync(offer);

                            Trace.WriteLine($"Scaled {databaseName}|{collectionName} to {(int)maxRu}RU. ({DateTimeOffset.Now})");

                            await metaDataOperator.AddScaleActivity(databaseName, collectionName, (int)maxRu, DateTimeOffset.Now);

                            ScaleOperation op = new ScaleOperation();
                            op.ScaledFrom    = (int)currentRu;
                            op.ScaledTo      = (int)maxRu;
                            op.OperationTime = DateTimeOffset.Now;

                            return(op);
                        }
                        else
                        {
                            return(new ScaleOperation()
                            {
                                ScaledSuccess = false,
                                ScaleFailReason = "Maximum RU reached."
                            });
                        }
                    }
                }

                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = "Could not find the collection to scale."
                });
            }
            catch (Exception e)
            {
                return(new ScaleOperation()
                {
                    ScaledSuccess = false,
                    ScaleFailReason = e.Message
                });
            }
        }