Example #1
0
        public HttpResponseMessage Stats()
        {
            if (Database != DatabasesLandlord.SystemDatabase)
            {
                return(GetMessageWithString("Admin stats can only be had from the root database", HttpStatusCode.NotFound));
            }

            var allDbs = new List <DocumentDatabase>();
            var allFs  = new List <RavenFileSystem>();

            DatabasesLandlord.ForAllDatabases(allDbs.Add);
            FileSystemsLandlord.ForAllFileSystems(allFs.Add);
            var currentConfiguration = DatabasesLandlord.SystemConfiguration;


            var stats = new AdminStatistics
            {
                ServerName            = currentConfiguration.ServerName,
                TotalNumberOfRequests = RequestManager.NumberOfRequests,
                Uptime = SystemTime.UtcNow - RequestManager.StartUpTime,
                Memory = new AdminMemoryStatistics
                {
                    DatabaseCacheSizeInMB      = ConvertBytesToMBs(DatabasesLandlord.SystemDatabase.TransactionalStorage.GetDatabaseCacheSizeInBytes()),
                    ManagedMemorySizeInMB      = ConvertBytesToMBs(GetCurrentManagedMemorySize()),
                    TotalProcessMemorySizeInMB = ConvertBytesToMBs(GetCurrentProcessPrivateMemorySize64()),
                },
                LoadedDatabases =
                    from documentDatabase in allDbs
                    let indexStorageSize = documentDatabase.GetIndexStorageSizeOnDisk()
                                           let transactionalStorageSize = documentDatabase.GetTransactionalStorageSizeOnDisk()
                                                                          let totalDatabaseSize = indexStorageSize + transactionalStorageSize.AllocatedSizeInBytes
                                                                                                  let lastUsed = DatabasesLandlord.LastRecentlyUsed.GetOrDefault(documentDatabase.Name ?? Constants.SystemDatabase)
                                                                                                                 select new LoadedDatabaseStatistics
                {
                    Name         = documentDatabase.Name,
                    LastActivity = new[]
                    {
                        lastUsed,
                        documentDatabase.WorkContext.LastWorkTime
                    }.Max(),
                TransactionalStorageAllocatedSize           = transactionalStorageSize.AllocatedSizeInBytes,
                TransactionalStorageAllocatedSizeHumaneSize = SizeHelper.Humane(transactionalStorageSize.AllocatedSizeInBytes),
                TransactionalStorageUsedSize           = transactionalStorageSize.UsedSizeInBytes,
                TransactionalStorageUsedSizeHumaneSize = SizeHelper.Humane(transactionalStorageSize.UsedSizeInBytes),
                IndexStorageSize        = indexStorageSize,
                IndexStorageHumaneSize  = SizeHelper.Humane(indexStorageSize),
                TotalDatabaseSize       = totalDatabaseSize,
                TotalDatabaseHumaneSize = SizeHelper.Humane(totalDatabaseSize),
                CountOfDocuments        = documentDatabase.Statistics.CountOfDocuments,
                CountOfAttachments      = documentDatabase.Statistics.CountOfAttachments,

                DatabaseTransactionVersionSizeInMB = ConvertBytesToMBs(documentDatabase.TransactionalStorage.GetDatabaseTransactionVersionSizeInBytes()),
                Metrics = documentDatabase.CreateMetrics()
                },
                LoadedFileSystems = from fileSystem in allFs
                                    select fileSystem.GetFileSystemStats()
            };

            return(GetMessageWithObject(stats));
        }
Example #2
0
        private AdminStatistics CreateAdminStats()
        {
            var allDbs = new List <DocumentDatabase>();
            var allFs  = new List <RavenFileSystem>();

            DatabasesLandlord.ForAllDatabases(allDbs.Add);
            FileSystemsLandlord.ForAllFileSystems(allFs.Add);
            var currentConfiguration = DatabasesLandlord.SystemConfiguration;


            var stats = new AdminStatistics
            {
                ServerName            = currentConfiguration.ServerName,
                TotalNumberOfRequests = RequestManager.NumberOfRequests,
                Uptime = SystemTime.UtcNow - RequestManager.StartUpTime,
                Memory = new AdminMemoryStatistics
                {
                    DatabaseCacheSizeInMB      = ConvertBytesToMBs(DatabasesLandlord.SystemDatabase.TransactionalStorage.GetDatabaseCacheSizeInBytes()),
                    ManagedMemorySizeInMB      = ConvertBytesToMBs(GetCurrentManagedMemorySize()),
                    TotalProcessMemorySizeInMB = ConvertBytesToMBs(GetCurrentProcessPrivateMemorySize64()),
                },
                LoadedDatabases   = LoadedDatabasesStats(allDbs),
                LoadedFileSystems = from fileSystem in allFs
                                    select fileSystem.GetFileSystemStats()
            };

            return(stats);
        }
Example #3
0
        public HttpResponseMessage InfoPackage()
        {
            var tempFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                var jsonSerializer = new JsonSerializer {
                    Formatting = Formatting.Indented
                };
                jsonSerializer.Converters.Add(new EtagJsonConverter());

                using (var file = new FileStream(tempFileName, FileMode.Create))
                    using (var package = new ZipArchive(file, ZipArchiveMode.Create))
                    {
                        var adminStats = package.CreateEntry("admin_stats.txt", CompressionLevel.Optimal);

                        using (var metricsStream = adminStats.Open())
                            using (var streamWriter = new StreamWriter(metricsStream))
                            {
                                jsonSerializer.Serialize(streamWriter, CreateAdminStats());
                                streamWriter.Flush();
                            }

                        DatabasesLandlord.ForAllDatabases(database =>
                        {
                            var prefix = string.IsNullOrWhiteSpace(database.Name) ? "System" : database.Name;
                            DebugInfoProvider.CreateInfoPackageForDatabase(package, database, RequestManager, prefix + "/");
                        });

                        var stacktraceRequsted = GetQueryStringValue("stacktrace");
                        if (stacktraceRequsted != null)
                        {
                            DumpStacktrace(package);
                        }
                    }

                var response = new HttpResponseMessage();

                response.Content = new StreamContent(new FileStream(tempFileName, FileMode.Open, FileAccess.Read))
                {
                    Headers =
                    {
                        ContentDisposition = new ContentDispositionHeaderValue("attachment")
                        {
                            FileName       = string.Format("Admin-Debug-Info-{0}.zip",SystemTime.UtcNow),
                        },
                        ContentType        = new MediaTypeHeaderValue("application/octet-stream")
                    }
                };

                return(response);
            }
            finally
            {
                IOExtensions.DeleteFile(tempFileName);
            }
        }
Example #4
0
        protected override Gauge32 GetData()
        {
            var indexingErrors = 0;

            landlord.ForAllDatabases(database =>
            {
                indexingErrors += database.WorkContext.Errors.Length;
            });

            return(new Gauge32(indexingErrors));
        }
Example #5
0
 public HttpResponseMessage DisableQueryTiming()
 {
     if (Database.IsSystemDatabase())
     {
         DatabasesLandlord.ForAllDatabases(database => database.WorkContext.ShowTimingByDefaultUntil = null);
     }
     else
     {
         Database.WorkContext.ShowTimingByDefaultUntil = null;
     }
     return(GetMessageWithObject(new { Enabled = false }));
 }
Example #6
0
        public HttpResponseMessage EnableQueryTiming()
        {
            var time = SystemTime.UtcNow + TimeSpan.FromMinutes(5);

            if (Database.IsSystemDatabase())
            {
                DatabasesLandlord.ForAllDatabases(database => database.WorkContext.ShowTimingByDefaultUntil = time);
            }
            else
            {
                Database.WorkContext.ShowTimingByDefaultUntil = time;
            }
            return(GetMessageWithObject(new { Enabled = true, Until = time }));
        }
Example #7
0
        public HttpResponseMessage Gc()
        {
            if (EnsureSystemDatabase() == false)
            {
                return(GetMessageWithString("Garbage Collection is only possible from the system database", HttpStatusCode.BadRequest));
            }


            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            DatabasesLandlord.ForAllDatabases(documentDatabase => documentDatabase.TransactionalStorage.ClearCaches());
            GC.WaitForPendingFinalizers();

            return(GetMessageWithString("GC Done"));
        }
Example #8
0
        public HttpResponseMessage Gc()
        {
            if (EnsureSystemDatabase() == false)
            {
                return(GetMessageWithString("Garbage Collection is only possible from the system database", HttpStatusCode.BadRequest));
            }

            Action <DocumentDatabase> clearCaches = documentDatabase => documentDatabase.TransactionalStorage.ClearCaches();
            Action afterCollect = () => DatabasesLandlord.ForAllDatabases(clearCaches);

            RavenGC.CollectGarbage(false, afterCollect, true);

            return(GetMessageWithString("GC Done"));
        }
Example #9
0
        public HttpResponseMessage LohCompaction()
        {
            if (EnsureSystemDatabase() == false)
            {
                return(GetMessageWithString("Large Object Heap Garbage Collection is only possiable from the system database", HttpStatusCode.BadRequest));
            }


            Action <DocumentDatabase> clearCaches = documentDatabase => documentDatabase.TransactionalStorage.ClearCaches();
            Action afterCollect = () => DatabasesLandlord.ForAllDatabases(clearCaches);

            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
            RavenGC.CollectGarbage(true, afterCollect);

            return(GetMessageWithString("LOH GC Done"));
        }
Example #10
0
        private void OnProposingCandidacy(object sender, ProposingCandidacyResult e)
        {
            var clusterConfigurationsDoc = DatabasesLandlord.SystemDatabase.Documents.Get(Constants.Cluster.ClusterConfigurationDocumentKey, null);

            if (clusterConfigurationsDoc == null)
            {
                return;
            }
            var clusterConfigurations = clusterConfigurationsDoc.DataAsJson.JsonDeserialization <ClusterConfiguration>();

            if (clusterConfigurations == null)
            {
                return;
            }
            if (clusterConfigurations.DisableReplicationStateChecks == true)
            {
                return;
            }
            var replicationStateDoc = DatabasesLandlord.SystemDatabase.Documents.Get(Constants.Cluster.ClusterReplicationStateDocumentKey, null);

            if (replicationStateDoc == null)
            {
                //This is a case of a node loading for the first time and just never got any replication state.
                //If we prevent this than a cluster will be non-respnosive when loaded (mostly a test senario but could be a real issue)
                if (clusterManagerStartTime + maxReplicationLatency + maxReplicationLatency < DateTime.UtcNow)
                {
                    e.VetoCandidacy = true;
                    e.Reason        = "Could not find replication state document";
                }
                return;
            }
            var replicationState = replicationStateDoc.DataAsJson.ToObject <ReplicationState>();

            if (replicationState == null)
            {
                e.VetoCandidacy = true;
                e.Reason        = "Could not deserialize replication state document";
                return;
            }

            var anyDatabaseUptodate = false;
            //preventing the case where all databases are inactive (long overnight inactivity).
            var anyDatabaseUp = false;

            DatabasesLandlord.ForAllDatabases(database =>
            {
                anyDatabaseUp = true;
                LastModificationTimeAndTransactionalId modification;
                //if the source document doesn't contain this databse it means it was not active in the source
                //nothing we can do but ignore this.
                if (replicationState.DatabasesToLastModification.TryGetValue(database.Name, out modification) == false)
                {
                    return;
                }
                var docKey = $"{Constants.RavenReplicationSourcesBasePath}/{modification.DatabaseId}";
                var doc    = database.Documents.Get(docKey, null);
                if (doc == null)
                {
                    return;
                }
                var sourceInformation = doc.DataAsJson.JsonDeserialization <SourceReplicationInformation>();
                if (sourceInformation == null)
                {
                    return;
                }
                var lastUpdate = sourceInformation.LastModifiedAtSource ?? DateTime.MaxValue;
                if (lastUpdate == DateTime.MaxValue || lastUpdate + maxReplicationLatency >= modification.LastModified)
                {
                    anyDatabaseUptodate = true;
                }
            }, true);
            if (anyDatabaseUptodate == false && anyDatabaseUp)
            {
                e.VetoCandidacy = true;
                e.Reason        = "None of the active databases are up to date with the leader last replication state";
            }
        }