private void HeartbeatVersion41200(TransactionOperationContext ctx, Dictionary <string, DatabaseStatusReport> nodeReport) { using (var writer = new BlittableJsonTextWriter(ctx, _tcp.Stream)) { ctx.Write(writer, DynamicJsonValue.Convert(nodeReport)); } }
public unsafe bool TryWriteOfflineDatabaseStatusToRequest(TransactionOperationContext ctx, BlittableJsonTextWriter writer, string databaseName, bool disabled, IndexRunningStatus indexingStatus, NodesTopology topology) { using (_contextPool.AllocateOperationContext(out TransactionOperationContext context)) using (var tx = context.OpenReadTransaction()) { var table = tx.InnerTransaction.OpenTable(_databaseInfoSchema, DatabaseInfoSchema.DatabaseInfoTree); TableValueReader infoTvr; using (Slice.From(tx.InnerTransaction.Allocator, databaseName.ToLowerInvariant(), out Slice databaseNameAsSlice)) { if (table.ReadByKey(databaseNameAsSlice, out infoTvr) == false) { return(false); } } //It seems like the database was shutdown rudely and never wrote it stats onto the disk if (infoTvr.Pointer == null) { return(false); } using (var databaseInfoJson = Read(context, ref infoTvr)) { databaseInfoJson.Modifications = new DynamicJsonValue(databaseInfoJson) { [nameof(DatabaseInfo.Disabled)] = disabled, [nameof(DatabaseInfo.IndexingStatus)] = indexingStatus.ToString(), [nameof(DatabaseInfo.NodesTopology)] = topology.ToJson() }; ctx.Write(writer, databaseInfoJson); return(true); } } }
public bool TryWriteOfflineDatabaseStatustoRequest(TransactionOperationContext ctx, BlittableJsonTextWriter writer, string databaseName, bool disabled) { TransactionOperationContext context; using (_contextPool.AllocateOperationContext(out context)) using (var tx = context.OpenReadTransaction()) { var table = tx.InnerTransaction.OpenTable(_databaseInfoSchema, DatabaseInfoSchema.DatabaseInfoTree); Slice databaseNameAsSlice; TableValueReader infoTvr; using (Slice.From(tx.InnerTransaction.Allocator, databaseName.ToLowerInvariant(), out databaseNameAsSlice)) { infoTvr = table.ReadByKey(databaseNameAsSlice); } //It seems like the database was shutdown rudly and never wrote it stats onto the disk if (infoTvr == null) { return(false); } var databaseInfoJson = Read(context, infoTvr); databaseInfoJson.Modifications = new DynamicJsonValue(databaseInfoJson) { [nameof(ResourceInfo.Disabled)] = disabled }; ctx.Write(writer, databaseInfoJson); return(true); } }
private void WriteDatabaseInfo(string databaseName, BlittableJsonReaderObject data, TransactionOperationContext context, BlittableJsonTextWriter writer) { bool disabled; data.TryGet("Disabled", out disabled); Task <DocumentDatabase> dbTask; var online = ServerStore.DatabasesLandlord.ResourcesStoresCache.TryGetValue(databaseName, out dbTask) && dbTask != null && dbTask.IsCompleted; var db = online ? dbTask.Result : null; if (online == false) { // if state of database is found in the cache we can continue if (ServerStore.DatabaseInfoCache.TryWriteOfflineDatabaseStatustoRequest( context, writer, databaseName, disabled)) { return; } // we won't find it if it is a new database or after a dirty shutdown, so just report empty values then } var indexingStatus = dbTask != null && dbTask.IsCompleted ? dbTask.Result.IndexStore.Status.ToString() : null; var size = new Size(GetTotalSize(db)); var backupInfo = GetBackupInfo(db); var doc = new DynamicJsonValue { [nameof(ResourceInfo.Bundles)] = new DynamicJsonArray(GetBundles(db)), [nameof(ResourceInfo.IsAdmin)] = true, //TODO: implement me! [nameof(ResourceInfo.Name)] = databaseName, [nameof(ResourceInfo.Disabled)] = disabled, [nameof(ResourceInfo.TotalSize)] = new DynamicJsonValue { [nameof(Size.HumaneSize)] = size.HumaneSize, [nameof(Size.SizeInBytes)] = size.SizeInBytes }, [nameof(ResourceInfo.Errors)] = online ? db.IndexStore.GetIndexes().Sum(index => index.GetErrors().Count) : 0, [nameof(ResourceInfo.Alerts)] = online ? db.Alerts.GetAlertCount() : 0, [nameof(ResourceInfo.UpTime)] = online ? GetUptime(db).ToString() : null, [nameof(ResourceInfo.BackupInfo)] = backupInfo, [nameof(DatabaseInfo.DocumentsCount)] = online ? db.DocumentsStorage.GetNumberOfDocuments() : 0, [nameof(DatabaseInfo.IndexesCount)] = online ? db.IndexStore.GetIndexes().Count() : 0, [nameof(DatabaseInfo.RejectClients)] = false, //TODO: implement me! [nameof(DatabaseInfo.IndexingStatus)] = indexingStatus }; context.Write(writer, doc); }
private async Task SendConfigurationResponseAsync(TransactionOperationContext context, long index) { await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream())) { var response = new DynamicJsonValue { ["RaftCommandIndex"] = index, }; context.Write(writer, response); } }
private void SendConfigurationResponse(TransactionOperationContext context, long index) { using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream())) { var response = new DynamicJsonValue { ["RaftCommandIndex"] = index, }; context.Write(writer, response); writer.Flush(); } }
private void HeartbeatVersion42000(TransactionOperationContext ctx, MaintenanceReport report) { report.ServerReport = new ServerReport { OutOfCpuCredits = _server.Server.CpuCreditsBalance.BackgroundTasksAlertRaised.IsRaised() }; using (var writer = new BlittableJsonTextWriter(ctx, _tcp.Stream)) { ctx.Write(writer, report.ToJson()); } }
private void HeartbeatVersion42000(TransactionOperationContext ctx, MaintenanceReport report) { report.ServerReport = new ServerReport { OutOfCpuCredits = _server.Server.CpuCreditsBalance.BackgroundTasksAlertRaised.IsRaised(), EarlyOutOfMemory = LowMemoryNotification.Instance.IsEarlyOutOfMemory, HighDirtyMemory = LowMemoryNotification.Instance.DirtyMemoryState.IsHighDirty }; using (var writer = new BlittableJsonTextWriter(ctx, _tcp.Stream)) { ctx.Write(writer, report.ToJson()); } }
private void WriteDatabaseInfo(string databaseName, BlittableJsonReaderObject dbRecordBlittable, TransactionOperationContext context, AbstractBlittableJsonTextWriter writer) { try { var online = ServerStore.DatabasesLandlord.DatabasesCache.TryGetValue(databaseName, out Task <DocumentDatabase> dbTask) && dbTask != null && dbTask.IsCompleted; // Check for exceptions if (dbTask != null && dbTask.IsFaulted) { var exception = dbTask.Exception.ExtractSingleInnerException(); WriteFaultedDatabaseInfo(databaseName, exception, context, writer); return; } var dbRecord = JsonDeserializationCluster.DatabaseRecord(dbRecordBlittable); var db = online ? dbTask.Result : null; var indexingStatus = db?.IndexStore?.Status ?? IndexRunningStatus.Running; // Looking for disabled indexing flag inside the database settings for offline database status if (dbRecord.Settings.TryGetValue(RavenConfiguration.GetKey(x => x.Indexing.Disabled), out var val) && bool.TryParse(val, out var indexingDisabled) && indexingDisabled) { indexingStatus = IndexRunningStatus.Disabled; } var disabled = dbRecord.Disabled; var topology = dbRecord.Topology; var clusterTopology = ServerStore.GetClusterTopology(context); clusterTopology.ReplaceCurrentNodeUrlWithClientRequestedNodeUrlIfNecessary(ServerStore, HttpContext); var studioEnvironment = StudioConfiguration.StudioEnvironment.None; if (dbRecord.Studio != null && !dbRecord.Studio.Disabled) { studioEnvironment = dbRecord.Studio.Environment; } var nodesTopology = new NodesTopology(); var statuses = ServerStore.GetNodesStatuses(); if (topology != null) { foreach (var member in topology.Members) { var url = clusterTopology.GetUrlFromTag(member); var node = new InternalReplication { Database = databaseName, NodeTag = member, Url = url }; nodesTopology.Members.Add(GetNodeId(node)); SetNodeStatus(topology, member, nodesTopology, statuses); } foreach (var promotable in topology.Promotables) { topology.PredefinedMentors.TryGetValue(promotable, out var mentorCandidate); var node = GetNode(databaseName, clusterTopology, promotable, mentorCandidate, out var promotableTask); var mentor = topology.WhoseTaskIsIt(ServerStore.Engine.CurrentState, promotableTask, null); nodesTopology.Promotables.Add(GetNodeId(node, mentor)); SetNodeStatus(topology, promotable, nodesTopology, statuses); } foreach (var rehab in topology.Rehabs) { var node = GetNode(databaseName, clusterTopology, rehab, null, out var promotableTask); var mentor = topology.WhoseTaskIsIt(ServerStore.Engine.CurrentState, promotableTask, null); nodesTopology.Rehabs.Add(GetNodeId(node, mentor)); SetNodeStatus(topology, rehab, nodesTopology, statuses); } } if (online == false) { // if state of database is found in the cache we can continue if (ServerStore.DatabaseInfoCache.TryGet(databaseName, databaseInfoJson => { databaseInfoJson.Modifications = new DynamicJsonValue(databaseInfoJson) { [nameof(DatabaseInfo.Disabled)] = disabled, [nameof(DatabaseInfo.IndexingStatus)] = indexingStatus.ToString(), [nameof(DatabaseInfo.NodesTopology)] = nodesTopology.ToJson(), [nameof(DatabaseInfo.DeletionInProgress)] = DynamicJsonValue.Convert(dbRecord.DeletionInProgress), [nameof(DatabaseInfo.Environment)] = studioEnvironment }; context.Write(writer, databaseInfoJson); })) { return; } // we won't find it if it is a new database or after a dirty shutdown, // so just report empty values then } var size = db?.GetSizeOnDisk() ?? (new Size(0), new Size(0)); var databaseInfo = new DatabaseInfo { Name = databaseName, Disabled = disabled, TotalSize = size.Data, TempBuffersSize = size.TempBuffers, IsAdmin = true, IsEncrypted = dbRecord.Encrypted, UpTime = online ? (TimeSpan?)GetUptime(db) : null, BackupInfo = GetBackupInfo(db), Alerts = db?.NotificationCenter.GetAlertCount() ?? 0, RejectClients = false, LoadError = null, IndexingErrors = db?.IndexStore?.GetIndexes()?.Sum(index => index.GetErrorCount()) ?? 0, DocumentsCount = db?.DocumentsStorage.GetNumberOfDocuments() ?? 0, HasRevisionsConfiguration = db?.DocumentsStorage.RevisionsStorage.Configuration != null, HasExpirationConfiguration = db?.ExpiredDocumentsCleaner != null, IndexesCount = db?.IndexStore?.GetIndexes()?.Count() ?? 0, IndexingStatus = indexingStatus, Environment = studioEnvironment, NodesTopology = nodesTopology, ReplicationFactor = topology?.ReplicationFactor ?? -1, DynamicNodesDistribution = topology?.DynamicNodesDistribution ?? false, DeletionInProgress = dbRecord.DeletionInProgress }; var doc = databaseInfo.ToJson(); context.Write(writer, doc); } catch (Exception e) { if (Logger.IsInfoEnabled) { Logger.Info($"Failed to get database info for: {databaseName}", e); } WriteFaultedDatabaseInfo(databaseName, e, context, writer); } }
private void WriteDatabaseInfo(string databaseName, BlittableJsonReaderObject dbRecordBlittable, TransactionOperationContext context, BlittableJsonTextWriter writer) { var online = ServerStore.DatabasesLandlord.DatabasesCache.TryGetValue(databaseName, out Task <DocumentDatabase> dbTask) && dbTask != null && dbTask.IsCompleted; // Check for exceptions if (dbTask != null && dbTask.IsFaulted) { WriteFaultedDatabaseInfo(context, writer, dbTask, databaseName); return; } var dbRecord = JsonDeserializationCluster.DatabaseRecord(dbRecordBlittable); var db = online ? dbTask.Result : null; var indexingStatus = db?.IndexStore.Status ?? IndexRunningStatus.Running; // Looking for disabled indexing flag inside the database settings for offline database status if (dbRecord.Settings.TryGetValue(RavenConfiguration.GetKey(x => x.Indexing.Disabled), out var val) && val == "true") { indexingStatus = IndexRunningStatus.Disabled; } var disabled = dbRecord.Disabled; var topology = dbRecord.Topology; var clusterTopology = ServerStore.GetClusterTopology(context); var nodesTopology = new NodesTopology(); if (topology != null) { foreach (var member in topology.Members) { var url = clusterTopology.GetUrlFromTag(member); var node = new InternalReplication { Database = databaseName, NodeTag = member, Url = url }; nodesTopology.Members.Add(GetNodeId(node)); SetNodeStatus(topology, member, nodesTopology); } foreach (var promotable in topology.Promotables) { var node = GetNode(databaseName, clusterTopology, promotable, out var promotableTask); var mentor = topology.WhoseTaskIsIt(promotableTask, ServerStore.IsPassive()); nodesTopology.Promotables.Add(GetNodeId(node, mentor)); SetNodeStatus(topology, promotable, nodesTopology); } foreach (var rehab in topology.Rehabs) { var node = GetNode(databaseName, clusterTopology, rehab, out var promotableTask); var mentor = topology.WhoseTaskIsIt(promotableTask, ServerStore.IsPassive()); nodesTopology.Rehabs.Add(GetNodeId(node, mentor)); SetNodeStatus(topology, rehab, nodesTopology); } } if (online == false) { // If state of database is found in the cache we can continue if (ServerStore.DatabaseInfoCache.TryWriteOfflineDatabaseStatusToRequest( context, writer, databaseName, disabled, indexingStatus, nodesTopology)) { return; } // We won't find it if it is a new database or after a dirty shutdown, so just report empty values then } var size = new Size(GetTotalSize(db)); var databaseInfo = new DatabaseInfo { Name = databaseName, Disabled = disabled, TotalSize = size, IsAdmin = true, //TODO: implement me! IsEncrypted = dbRecord.Encrypted, UpTime = online ? (TimeSpan?)GetUptime(db) : null, BackupInfo = GetBackupInfo(db), Alerts = db?.NotificationCenter.GetAlertCount() ?? 0, RejectClients = false, //TODO: implement me! LoadError = null, IndexingErrors = db?.IndexStore.GetIndexes().Sum(index => index.GetErrorCount()) ?? 0, DocumentsCount = db?.DocumentsStorage.GetNumberOfDocuments() ?? 0, HasRevisionsConfiguration = db?.DocumentsStorage.RevisionsStorage.Configuration != null, HasExpirationConfiguration = db?.ExpiredDocumentsCleaner != null, IndexesCount = db?.IndexStore.GetIndexes().Count() ?? 0, IndexingStatus = indexingStatus, NodesTopology = nodesTopology, ReplicationFactor = topology?.ReplicationFactor ?? -1, DynamicNodesDistribution = topology?.DynamicNodesDistribution ?? false }; var doc = databaseInfo.ToJson(); context.Write(writer, doc); }