Ejemplo n.º 1
0
        public void CanDeserializeDatabaseInfoForDisabledDb()
        {
            using (var store = GetDocumentStore())
            {
                new UserIndex().Execute(store);
                using (var session = store.OpenSession())
                {
                    session.Store(new User()
                    {
                        Name = "egor"
                    });
                    session.SaveChanges();
                }

                Indexes.WaitForIndexing(store);
                store.Maintenance.Server.Send(new ToggleDatabasesStateOperation(store.Database, true));

                var client   = store.GetRequestExecutor().HttpClient;
                var response = client.GetAsync(store.Urls.First() + $"/databases?name={store.Database}", CancellationToken.None).Result;
                var result   = response.Content.ReadAsStringAsync().Result;

                using (var ctx = JsonOperationContext.ShortTermSingleUse())
                {
                    using var bjro = ctx.Sync.ReadForMemory(result, "test");
                    Assert.True(bjro.TryGetMember(nameof(DatabaseInfo.IndexingStatus), out var indexingStatus));
                    Assert.Null(indexingStatus);
                    var dbInfo = JsonDeserializationServer.DatabaseInfo(bjro);
                    Assert.Equal(IndexRunningStatus.Running, dbInfo.IndexingStatus);
                }
            }
        }
Ejemplo n.º 2
0
        private static void UpdateDatabaseInfo(RawDatabaseRecord databaseRecord, ServerStore serverStore, string databaseName, DrivesUsage existingDrivesUsage,
                                               DatabaseInfoItem databaseInfoItem)
        {
            DatabaseInfo databaseInfo = null;

            if (serverStore.DatabaseInfoCache.TryGet(databaseName, databaseInfoJson =>
            {
                databaseInfo = JsonDeserializationServer.DatabaseInfo(databaseInfoJson);
            }) == false)
            {
                return;
            }

            Debug.Assert(databaseInfo != null);
            var databaseTopology = databaseRecord.Topology;
            var indexesCount     = databaseRecord.CountOfIndexes;

            databaseInfoItem.DocumentsCount      = databaseInfo.DocumentsCount ?? 0;
            databaseInfoItem.IndexesCount        = databaseInfo.IndexesCount ?? indexesCount;
            databaseInfoItem.ReplicationFactor   = databaseTopology?.ReplicationFactor ?? databaseInfo.ReplicationFactor;
            databaseInfoItem.ErroredIndexesCount = databaseInfo.IndexingErrors ?? 0;

            if (databaseInfo.MountPointsUsage == null)
            {
                return;
            }

            foreach (var mountPointUsage in databaseInfo.MountPointsUsage)
            {
                var driveName       = mountPointUsage.DiskSpaceResult.DriveName;
                var diskSpaceResult = DiskSpaceChecker.GetDiskSpaceInfo(
                    mountPointUsage.DiskSpaceResult.DriveName,
                    new DriveInfoBase
                {
                    DriveName = driveName
                });

                if (diskSpaceResult != null)
                {
                    // update the latest drive info
                    mountPointUsage.DiskSpaceResult = new Client.ServerWide.Operations.DiskSpaceResult
                    {
                        DriveName             = diskSpaceResult.DriveName,
                        VolumeLabel           = diskSpaceResult.VolumeLabel,
                        TotalFreeSpaceInBytes = diskSpaceResult.TotalFreeSpace.GetValue(SizeUnit.Bytes),
                        TotalSizeInBytes      = diskSpaceResult.TotalSize.GetValue(SizeUnit.Bytes)
                    };
                }

                UpdateMountPoint(serverStore.Configuration.Storage, mountPointUsage, databaseName, existingDrivesUsage);
            }
        }
        private static void SetOfflineDatabaseInfo(
            ServerStore serverStore,
            string databaseName,
            DatabasesInfo existingDatabasesInfo,
            DrivesUsage existingDrivesUsage,
            bool disabled)
        {
            var databaseRecord = serverStore.LoadDatabaseRecord(databaseName, out var _);

            if (databaseRecord == null)
            {
                // database doesn't exist
                return;
            }

            var databaseInfoItem = new DatabaseInfoItem
            {
                Database   = databaseName,
                Online     = false,
                Disabled   = disabled,
                Irrelevant = databaseRecord.Topology?.AllNodes.Contains(serverStore.NodeTag) ?? true
            };

            DatabaseInfo databaseInfo = null;

            if (serverStore.DatabaseInfoCache.TryGet(databaseName,
                                                     databaseInfoJson => databaseInfo = JsonDeserializationServer.DatabaseInfo(databaseInfoJson)))
            {
                Debug.Assert(databaseInfo != null);

                databaseInfoItem.DocumentsCount      = databaseInfo.DocumentsCount ?? 0;
                databaseInfoItem.IndexesCount        = databaseInfo.IndexesCount ?? databaseRecord.Indexes.Count;
                databaseInfoItem.ReplicationFactor   = databaseRecord.Topology?.ReplicationFactor ?? databaseInfo.ReplicationFactor;
                databaseInfoItem.ErroredIndexesCount = databaseInfo.IndexingErrors ?? 0;

                if (databaseInfo.MountPointsUsage != null)
                {
                    var drives = DriveInfo.GetDrives();
                    foreach (var mountPointUsage in databaseInfo.MountPointsUsage)
                    {
                        var diskSpaceResult = DiskSpaceChecker.GetFreeDiskSpace(mountPointUsage.DiskSpaceResult.DriveName, drives);
                        if (diskSpaceResult != null)
                        {
                            // update the latest drive info
                            mountPointUsage.DiskSpaceResult = new Client.ServerWide.Operations.DiskSpaceResult
                            {
                                DriveName             = diskSpaceResult.DriveName,
                                VolumeLabel           = diskSpaceResult.VolumeLabel,
                                TotalFreeSpaceInBytes = diskSpaceResult.TotalFreeSpace.GetValue(SizeUnit.Bytes),
                                TotalSizeInBytes      = diskSpaceResult.TotalSize.GetValue(SizeUnit.Bytes)
                            };
                        }

                        UpdateMountPoint(mountPointUsage, databaseName, existingDrivesUsage);
                    }
                }
            }

            existingDatabasesInfo.Items.Add(databaseInfoItem);
        }
Ejemplo n.º 4
0
        public static IEnumerable <AbstractDashboardNotification> FetchDatabasesInfo(ServerStore serverStore, CancellationTokenSource cts)
        {
            var databasesInfo = new DatabasesInfo();
            var indexingSpeed = new IndexingSpeed();
            var trafficWatch  = new TrafficWatch();
            var drivesUsage   = new DrivesUsage();

            using (serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext transactionContext))
                using (transactionContext.OpenReadTransaction())
                {
                    foreach (var databaseTuple in serverStore.Cluster.ItemsStartingWith(transactionContext, Constants.Documents.Prefix, 0, int.MaxValue))
                    {
                        var databaseName = databaseTuple.ItemName.Substring(3);
                        if (cts.IsCancellationRequested)
                        {
                            yield break;
                        }

                        if (serverStore.DatabasesLandlord.DatabasesCache.TryGetValue(databaseName, out var databaseTask) == false)
                        {
                            // database does not exist in this server or disabled
                            continue;
                        }

                        var databaseOnline = IsDatabaseOnline(databaseTask, out var database);
                        if (databaseOnline == false)
                        {
                            var databaseRecord = serverStore.LoadDatabaseRecord(databaseName, out var _);
                            if (databaseRecord == null)
                            {
                                // database doesn't exist
                                continue;
                            }

                            var databaseInfoItem = new DatabaseInfoItem
                            {
                                Database = databaseName,
                                Online   = false
                            };

                            DatabaseInfo databaseInfo = null;
                            if (serverStore.DatabaseInfoCache.TryGet(databaseName,
                                                                     databaseInfoJson => databaseInfo = JsonDeserializationServer.DatabaseInfo(databaseInfoJson)))
                            {
                                Debug.Assert(databaseInfo != null);

                                databaseInfoItem.DocumentsCount      = databaseInfo.DocumentsCount ?? 0;
                                databaseInfoItem.IndexesCount        = databaseInfo.IndexesCount ?? databaseRecord.Indexes.Count;
                                databaseInfoItem.ReplicationFactor   = databaseRecord.Topology?.ReplicationFactor ?? databaseInfo.ReplicationFactor;
                                databaseInfoItem.ErroredIndexesCount = databaseInfo.IndexingErrors ?? 0;
                            }

                            databasesInfo.Items.Add(databaseInfoItem);
                            continue;
                        }

                        var indexingSpeedItem = new IndexingSpeedItem
                        {
                            Database         = database.Name,
                            IndexedPerSecond = database.Metrics.MapIndexes.IndexedPerSec.FiveSecondRate,
                            MappedPerSecond  = database.Metrics.MapReduceIndexes.MappedPerSec.FiveSecondRate,
                            ReducedPerSecond = database.Metrics.MapReduceIndexes.ReducedPerSec.FiveSecondRate
                        };
                        indexingSpeed.Items.Add(indexingSpeedItem);

                        var replicationFactor = GetReplicationFactor(databaseTuple.Value);
                        var documentsStorage  = database.DocumentsStorage;
                        var indexStorage      = database.IndexStore;
                        using (documentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext documentsContext))
                            using (documentsContext.OpenReadTransaction())
                            {
                                var databaseInfoItem = new DatabaseInfoItem
                                {
                                    Database            = databaseName,
                                    DocumentsCount      = documentsStorage.GetNumberOfDocuments(documentsContext),
                                    IndexesCount        = database.IndexStore.Count,
                                    AlertsCount         = database.NotificationCenter.GetAlertCount(),
                                    ReplicationFactor   = replicationFactor,
                                    ErroredIndexesCount = indexStorage.GetIndexes().Count(index => index.GetErrorCount() > 0),
                                    Online = true
                                };
                                databasesInfo.Items.Add(databaseInfoItem);
                            }

                        var trafficWatchItem = new TrafficWatchItem
                        {
                            Database            = databaseName,
                            RequestsPerSecond   = (int)database.Metrics.Requests.RequestsPerSec.FiveSecondRate,
                            WritesPerSecond     = (int)database.Metrics.Docs.PutsPerSec.FiveSecondRate,
                            WriteBytesPerSecond = database.Metrics.Docs.BytesPutsPerSec.FiveSecondRate
                        };
                        trafficWatch.Items.Add(trafficWatchItem);

                        foreach (var mountPointUsage in database.GetMountPointsUsage())
                        {
                            if (cts.IsCancellationRequested)
                            {
                                yield break;
                            }

                            var mountPoint = mountPointUsage.DiskSpaceResult.DriveName;
                            var usage      = drivesUsage.Items.FirstOrDefault(x => x.MountPoint == mountPoint);
                            if (usage == null)
                            {
                                usage = new MountPointUsage
                                {
                                    MountPoint    = mountPoint,
                                    VolumeLabel   = mountPointUsage.DiskSpaceResult.VolumeLabel,
                                    FreeSpace     = mountPointUsage.DiskSpaceResult.TotalFreeSpace.GetValue(SizeUnit.Bytes),
                                    TotalCapacity = mountPointUsage.DiskSpaceResult.TotalSize.GetValue(SizeUnit.Bytes)
                                };
                                drivesUsage.Items.Add(usage);
                            }

                            var existingDatabaseUsage = usage.Items.FirstOrDefault(x => x.Database == databaseName);
                            if (existingDatabaseUsage == null)
                            {
                                existingDatabaseUsage = new DatabaseDiskUsage
                                {
                                    Database = databaseName
                                };
                                usage.Items.Add(existingDatabaseUsage);
                            }

                            existingDatabaseUsage.Size += mountPointUsage.UsedSpace;
                        }
                    }
                }

            yield return(databasesInfo);

            yield return(indexingSpeed);

            yield return(trafficWatch);

            yield return(drivesUsage);
        }