Exemple #1
0
        private void OnBeforeRequest(object sender, RequestWebApiEventArgs args)
        {
            var documentDatabase = args.Database;

            if (documentDatabase != null)
            {
                documentDatabase.WorkContext.MetricsCounters.ConcurrentRequests.Mark();
                documentDatabase.WorkContext.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref documentDatabase.WorkContext.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var fileSystem = args.FileSystem;

            if (fileSystem != null)
            {
                fileSystem.MetricsCounters.ConcurrentRequests.Mark();
                fileSystem.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref fileSystem.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var counters = args.Counters;

            if (counters != null)
            {
                counters.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref counters.MetricsCounters.ConcurrentRequestsCount);
            }
        }
Exemple #2
0
        private void OnAfterRequest(object sender, RequestWebApiEventArgs args)
        {
            var documentDatabase = args.Database;

            if (documentDatabase != null)
            {
                Interlocked.Decrement(ref documentDatabase.WorkContext.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var fileSystem = args.FileSystem;

            if (fileSystem != null)
            {
                Interlocked.Decrement(ref fileSystem.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var counters = args.Counters;

            if (counters != null)
            {
                Interlocked.Decrement(ref counters.MetricsCounters.ConcurrentRequestsCount);
            }
        }
Exemple #3
0
        private void OnBeforeRequest(object sender, RequestWebApiEventArgs args)
        {
            if (args.ResourceType == ResourceType.Database)
            {
                var documentDatabase = (DocumentDatabase)args.Resource;
                documentDatabase.WorkContext.MetricsCounters.ConcurrentRequests.Mark();
                documentDatabase.WorkContext.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref documentDatabase.WorkContext.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            if (args.ResourceType == ResourceType.FileSystem)
            {
                var fileSystem = (RavenFileSystem)args.Resource;
                fileSystem.MetricsCounters.ConcurrentRequests.Mark();
                fileSystem.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref fileSystem.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            if (args.ResourceType == ResourceType.Counter)
            {
                var counters = (CounterStorage)args.Resource;
                counters.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref counters.MetricsCounters.ConcurrentRequestsCount);
            }
        }
Exemple #4
0
 private void RequestManagerOnBeforeRequest(object sender, RequestWebApiEventArgs requestWebApiEventArgs)
 {
     if (shouldRecordRequests)
     {
         requestLog.Add(requestWebApiEventArgs.Controller.InnerRequest);
     }
 }
Exemple #5
0
        public virtual void OnAfterRequest(RequestWebApiEventArgs e)
        {
            var handler = AfterRequest;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        public override bool TrySetupRequestToProperResource(out RequestWebApiEventArgs args)
        {
            var tenantId = this.DatabaseName;
            var landlord = this.DatabasesLandlord;

            if (string.IsNullOrWhiteSpace(tenantId) || tenantId == "<system>")
            {
                landlord.LastRecentlyUsed.AddOrUpdate("System", SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);

                args = new RequestWebApiEventArgs
                {
                    Controller = this,
                    IgnoreRequest = false,
                    TenantId = "System",
                    Database = landlord.SystemDatabase
                };

                if (args.IgnoreRequest)
                    return false;
                return true;
            }

            Task<DocumentDatabase> resourceStoreTask;
            bool hasDb;
            try
            {
                hasDb = landlord.TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
            }
            catch (Exception e)
            {
                var cle = e as ConcurrentLoadTimeoutException;
                string msg;
                if (cle != null)
                {
                    msg = string.Format("The database {0} is currently being loaded, but there are too many requests waiting for database load. Please try again later, database loading continues.", tenantId);
                }
                else
                {
                    msg = "Could not open database named: " + tenantId + " " + e.Message;
                }

                Logger.WarnException(msg, e);
                throw new HttpException(503, msg, e);
            }
            if (hasDb)
            {
                try
                {
                    int TimeToWaitForDatabaseToLoad = MaxSecondsForTaskToWaitForDatabaseToLoad;
                    if (resourceStoreTask.IsCompleted == false && resourceStoreTask.IsFaulted == false)
                    {
                        if (MaxNumberOfThreadsForDatabaseToLoad.Wait(0) == false)
                        {
                            var msg = string.Format("The database {0} is currently being loaded, but there are too many requests waiting for database load. Please try again later, database loading continues.", tenantId);
                            Logger.Warn(msg);
                            throw new TimeoutException(msg);
                        }

                        try
                        {
                            if (resourceStoreTask.Wait(TimeSpan.FromSeconds(TimeToWaitForDatabaseToLoad)) == false)
                            {
                                var msg = string.Format("The database {0} is currently being loaded, but after {1} seconds, this request has been aborted. Please try again later, database loading continues.", tenantId, TimeToWaitForDatabaseToLoad);
                                Logger.Warn(msg);
                                throw new TimeoutException(msg);
                            }
                        }
                        finally
                        {
                            MaxNumberOfThreadsForDatabaseToLoad.Release();
                        }
                    }

                    args = new RequestWebApiEventArgs()
                    {
                        Controller = this,
                        IgnoreRequest = false,
                        TenantId = tenantId,
                        Database = resourceStoreTask.Result
                    };

                    if (args.IgnoreRequest)
                        return false;
                }
                catch (Exception e)
                {
                    var msg = "Could not open database named: " + tenantId + Environment.NewLine + e;

                    Logger.WarnException(msg, e);
                    throw new HttpException(503, msg, e);
                }

                landlord.LastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
            }
            else
            {
                var msg = "Could not find a database named: " + tenantId;
                Logger.Warn(msg);
                throw new HttpException(503, msg);
            }
            return true;
        }
 public abstract bool TrySetupRequestToProperResource(out RequestWebApiEventArgs args);
        public override bool TrySetupRequestToProperResource(out RequestWebApiEventArgs args)
        {
            var tenantId = CountersName;

            if (string.IsNullOrWhiteSpace(tenantId))
            {
                throw new HttpException(503, "Could not find a counter with no name");
            }

            Task <CounterStorage> resourceStoreTask;
            bool hasDb;

            try
            {
                hasDb = landlord.TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
            }
            catch (Exception e)
            {
                var    cle = e as ConcurrentLoadTimeoutException;
                string msg;
                if (cle != null)
                {
                    msg = string.Format("The counter {0} is currently being loaded, but there are too many requests waiting for database load. Please try again later, database loading continues.", tenantId);
                }
                else
                {
                    msg = "Could not open database named: " + tenantId + " " + e.Message;
                }

                Logger.WarnException(msg, e);
                throw new HttpException(503, msg, e);
            }

            if (hasDb)
            {
                try
                {
                    if (resourceStoreTask.Wait(TimeSpan.FromSeconds(30)) == false)
                    {
                        var msg = "The counter " + tenantId +
                                  " is currently being loaded, but after 30 seconds, this request has been aborted. Please try again later, file system loading continues.";
                        Logger.Warn(msg);
                        throw new HttpException(503, msg);
                    }

                    args = new RequestWebApiEventArgs
                    {
                        Controller    = this,
                        IgnoreRequest = false,
                        TenantId      = tenantId,
                        Counters      = resourceStoreTask.Result
                    };

                    if (args.IgnoreRequest)
                    {
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    var msg = "Could open counters named: " + tenantId;
                    Logger.WarnException(msg, e);
                    throw new HttpException(503, msg, e);
                }

                landlord.LastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
            }
            else
            {
                var msg = "Could not find a counter named: " + tenantId;
                Logger.Warn(msg);
                throw new HttpException(503, msg);
            }
            return(true);
        }
Exemple #9
0
        public override bool TrySetupRequestToProperResource(out RequestWebApiEventArgs args)
        {
            var tenantId = this.DatabaseName;
            var landlord = this.DatabasesLandlord;

            if (string.IsNullOrWhiteSpace(tenantId) || tenantId == "<system>")
            {
                landlord.LastRecentlyUsed.AddOrUpdate("System", SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);

                args = new RequestWebApiEventArgs
                {
                    Controller    = this,
                    IgnoreRequest = false,
                    TenantId      = "System",
                    Database      = landlord.SystemDatabase
                };

                if (args.IgnoreRequest)
                {
                    return(false);
                }
                return(true);
            }

            Task <DocumentDatabase> resourceStoreTask;
            bool hasDb;

            try
            {
                hasDb = landlord.TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
            }
            catch (Exception e)
            {
                var msg = "Could not open database named: " + tenantId + " " + e.Message;
                Logger.WarnException(msg, e);
                throw new HttpException(503, msg, e);
            }
            if (hasDb)
            {
                try
                {
                    int TimeToWaitForDatabaseToLoad = MaxSecondsForTaskToWaitForDatabaseToLoad;
                    if (resourceStoreTask.IsCompleted == false && resourceStoreTask.IsFaulted == false)
                    {
                        if (MaxNumberOfThreadsForDatabaseToLoad.Wait(0) == false)
                        {
                            var msg = string.Format("The database {0} is currently being loaded, but there are too many requests waiting for database load. Please try again later, database loading continues.", tenantId);
                            Logger.Warn(msg);
                            throw new TimeoutException(msg);
                        }

                        try
                        {
                            if (resourceStoreTask.Wait(TimeSpan.FromSeconds(TimeToWaitForDatabaseToLoad)) == false)
                            {
                                var msg = string.Format("The database {0} is currently being loaded, but after {1} seconds, this request has been aborted. Please try again later, database loading continues.", tenantId, TimeToWaitForDatabaseToLoad);
                                Logger.Warn(msg);
                                throw new TimeoutException(msg);
                            }
                        }
                        finally
                        {
                            MaxNumberOfThreadsForDatabaseToLoad.Release();
                        }
                    }

                    args = new RequestWebApiEventArgs()
                    {
                        Controller    = this,
                        IgnoreRequest = false,
                        TenantId      = tenantId,
                        Database      = resourceStoreTask.Result
                    };

                    if (args.IgnoreRequest)
                    {
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    var msg = "Could not open database named: " + tenantId + Environment.NewLine + e;

                    Logger.WarnException(msg, e);
                    throw new HttpException(503, msg, e);
                }

                landlord.LastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
            }
            else
            {
                var msg = "Could not find a database named: " + tenantId;
                Logger.Warn(msg);
                throw new HttpException(503, msg);
            }
            return(true);
        }
Exemple #10
0
        public async Task <HttpResponseMessage> HandleActualRequest(IResourceApiController controller,
                                                                    HttpControllerContext controllerContext,
                                                                    Func <Task <HttpResponseMessage> > action,
                                                                    Func <HttpException, HttpResponseMessage> onHttpException)
        {
            HttpResponseMessage response = null;

            cancellationToken.ThrowIfCancellationRequested();

            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                LastRequestTime = SystemTime.UtcNow;
                Interlocked.Increment(ref concurrentRequests);

                IncrementRequestNumberAndLog(controller, controllerContext);

                RequestWebApiEventArgs args = await controller.TrySetupRequestToProperResource().ConfigureAwait(false);

                if (args != null)
                {
                    OnBeforeRequest(args);

                    try
                    {
                        if (controllerContext.Request.Headers.Contains(Constants.RavenClientVersion))
                        {
                            if (controller.RejectClientRequests)
                            {
                                response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
                                {
                                    Content = new MultiGetSafeStringContent("This service is not accepting clients calls")
                                };
                            }
                            else if (IsInHotSpareMode)
                            {
                                response = new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
                                {
                                    Content = new MultiGetSafeStringContent("This service is not accepting clients calls because this is a 'Hot Spare' server")
                                };
                            }
                            else
                            {
                                response = await action().ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            response = await action().ConfigureAwait(false);
                        }
                    }
                    finally
                    {
                        OnAfterRequest(args);
                    }
                }
            }
            catch (HttpException httpException)
            {
                response = onHttpException(httpException);
            }
            finally
            {
                Interlocked.Decrement(ref concurrentRequests);
                try
                {
                    FinalizeRequestProcessing(controller, response, sw);
                }
                catch (Exception e)
                {
                    var aggregateException = e as AggregateException;
                    if (aggregateException != null)
                    {
                        e = aggregateException.ExtractSingleInnerException();
                    }

                    Logger.ErrorException("Could not finalize request properly", e);
                }
            }
            return(response);
        }
Exemple #11
0
        private void OnBeforeRequest(object sender, RequestWebApiEventArgs args)
        {
            var documentDatabase = args.Database;
            if (documentDatabase != null)
            {
                documentDatabase.WorkContext.MetricsCounters.ConcurrentRequests.Mark();
                documentDatabase.WorkContext.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref documentDatabase.WorkContext.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var fileSystem = args.FileSystem;
            if (fileSystem != null)
            {
                fileSystem.MetricsCounters.ConcurrentRequests.Mark();
                fileSystem.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref fileSystem.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var counters = args.Counters;
            if (counters != null)
            {
                counters.MetricsCounters.RequestsPerSecondCounter.Mark();
                Interlocked.Increment(ref counters.MetricsCounters.ConcurrentRequestsCount);
            }
        }
Exemple #12
0
 public virtual void OnAfterRequest(RequestWebApiEventArgs e)
 {
     var handler = AfterRequest;
     if (handler != null) handler(this, e);
 }
Exemple #13
0
        private void OnAfterRequest(object sender, RequestWebApiEventArgs args)
        {
            var documentDatabase = args.Database;
            if (documentDatabase != null)
            {
                Interlocked.Decrement(ref documentDatabase.WorkContext.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var fileSystem = args.FileSystem;
            if (fileSystem != null)
            {
                Interlocked.Decrement(ref fileSystem.MetricsCounters.ConcurrentRequestsCount);
                return;
            }

            var counters = args.Counters;
            if (counters != null)
            {
                Interlocked.Decrement(ref counters.MetricsCounters.ConcurrentRequestsCount);
            }
        }
Exemple #14
0
        public override bool TrySetupRequestToProperResource(out RequestWebApiEventArgs args)
        {
            if (!RavenFileSystem.IsRemoteDifferentialCompressionInstalled)
            {
                throw new HttpException(503, "File Systems functionality is not supported. Remote Differential Compression is not installed.");
            }

            var tenantId = FileSystemName;

            if (string.IsNullOrWhiteSpace(tenantId))
            {
                throw new HttpException(503, "Could not find a file system with no name");
            }

            var landlord = this.FileSystemsLandlord;

            Task <RavenFileSystem> resourceStoreTask;
            bool hasDb;

            try
            {
                hasDb = landlord.TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
            }
            catch (Exception e)
            {
                var se  = e.SimplifyException();
                var msg = "Could not open file system named: " + tenantId + ", " + se.Message;
                Logger.WarnException(msg, e);
                throw new HttpException(503, msg, e);
            }
            if (hasDb)
            {
                try
                {
                    if (resourceStoreTask.Wait(TimeSpan.FromSeconds(30)) == false)
                    {
                        var msg = "The filesystem " + tenantId +
                                  " is currently being loaded, but after 30 seconds, this request has been aborted. Please try again later, file system loading continues.";
                        Logger.Warn(msg);
                        throw new HttpException(503, msg);
                    }

                    args = new RequestWebApiEventArgs()
                    {
                        Controller    = this,
                        IgnoreRequest = false,
                        TenantId      = tenantId,
                        FileSystem    = resourceStoreTask.Result
                    };

                    if (args.IgnoreRequest)
                    {
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    var msg = "Could not open file system named: " + tenantId + ", " + e.InnerException.Message;
                    Logger.WarnException(msg, e);
                    throw new HttpException(503, msg, e);
                }

                landlord.LastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
            }
            else
            {
                var msg = "Could not find a file system named: " + tenantId;
                Logger.Warn(msg);
                throw new HttpException(503, msg);
            }

            return(true);
        }