Example #1
0
        private List <DatabaseData> GetDatabasesData(IEnumerable <RavenJToken> databases)
        {
            return(databases
                   .Select(database =>
            {
                var bundles = new string[] {};
                var settings = database.Value <RavenJObject>("Settings");
                if (settings != null)
                {
                    var activeBundles = settings.Value <string>(RavenConfiguration.GetKey(x => x.Core._ActiveBundlesString));
                    if (activeBundles != null)
                    {
                        bundles = activeBundles.Split(';');
                    }
                }

                var dbName = database.Value <RavenJObject>("@metadata").Value <string>("@id").Replace("Raven/Databases/", string.Empty);
                return new DatabaseData
                {
                    Name = dbName,
                    Disabled = database.Value <bool>("Disabled"),
                    IndexingDisabled = GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), RavenConfiguration.GetKey(x => x.Indexing.Disabled)),
                    RejectClientsEnabled = GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), RavenConfiguration.GetKey(x => x.Core.RejectClientsMode)),
                    ClusterWide = ClusterManager.IsActive() && !GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), RavenConfiguration.GetKey(x => x.Cluster.NonClusterDatabaseMarker)),
                    Bundles = bundles,
                    IsAdminCurrentTenant = DatabasesLandlord.SystemConfiguration.Core.AnonymousUserAccessMode == AnonymousUserAccessMode.Admin,
                    IsLoaded = DatabasesLandlord.IsDatabaseLoaded(dbName)
                };
            }).ToList());
        }
        protected override TData GetData()
        {
            if (Landlord.IsDatabaseLoaded(DatabaseName))
            {
                return(GetData(Landlord.GetResourceInternal(DatabaseName).Result));
            }

            return(default(TData));
        }
Example #3
0
        protected override TData GetData()
        {
            if (Landlord.IsDatabaseLoaded(DatabaseName))
            {
                return(GetData(Landlord.TryGetOrCreateResourceStore(DatabaseName).Result));
            }

            return(default(TData));
        }
Example #4
0
        private List <DatabaseData> GetDatabasesData(IEnumerable <RavenJToken> databases)
        {
            return(databases
                   .Select(database =>
            {
                var bundles = new string[] {};
                var settings = database.Value <RavenJObject>("Settings");
                if (settings != null)
                {
                    var activeBundles = settings.Value <string>("Raven/ActiveBundles");
                    if (activeBundles != null)
                    {
                        bundles = activeBundles.Split(';');
                    }
                }

                var dbName = database.Value <RavenJObject>("@metadata").Value <string>("@id").Replace("Raven/Databases/", string.Empty);
                var isDatabaseLoaded = DatabasesLandlord.IsDatabaseLoaded(dbName);
                DocumentDatabase.ReducedDatabaseStatistics stats = null;
                if (isDatabaseLoaded)
                {
                    try
                    {
                        var db = DatabasesLandlord.GetResourceInternal(dbName).Result;
                        if (db != null)
                        {
                            stats = db.ReducedStatistics;
                        }
                    }
                    catch (Exception)
                    {
                        //the database is shutting down or locked
                        //we can ignore this
                    }
                }

                return new DatabaseData
                {
                    Name = dbName,
                    Disabled = database.Value <bool>("Disabled"),
                    IndexingDisabled = GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), Constants.IndexingDisabled),
                    RejectClientsEnabled = GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), Constants.RejectClientsModeEnabled),
                    ClusterWide = ClusterManager.IsActive() && !GetBooleanSettingStatus(database.Value <RavenJObject>("Settings"), Constants.Cluster.NonClusterDatabaseMarker),
                    Bundles = bundles,
                    IsAdminCurrentTenant = DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Admin,
                    IsLoaded = isDatabaseLoaded,
                    Stats = stats
                };
            }).ToList());
        }
Example #5
0
        private void AddReplicationDestinationsFromDatabase()
        {
            if (databaseLandlord.IsDatabaseLoaded(databaseName) == false)
            {
                return;
            }

            var database = databaseLandlord.GetResourceInternal(databaseName).Result;

            var replicationDocument = database.ConfigurationRetriever.GetConfigurationDocument <ReplicationDocument <ReplicationDestination.ReplicationDestinationWithConfigurationOrigin> >(Constants.RavenReplicationDestinations);

            if (replicationDocument == null)
            {
                return;
            }

            foreach (var destination in replicationDocument.MergedDocument.Destinations)
            {
                loadedReplicationDestinations.GetOrAdd(destination.Url.ForDatabase(destination.Database), AddReplicationDestination);
            }
        }
Example #6
0
        public SnmpDatabase(DatabasesLandlord databaseLandlord, ObjectStore store, string databaseName, int databaseIndex)
        {
            this.databaseLandlord = databaseLandlord;
            this.store            = store;
            this.databaseName     = databaseName;
            this.databaseIndex    = databaseIndex;

            Initialize();

            databaseLandlord.OnDatabaseLoaded += loadedDatabaseName =>
            {
                if (string.Equals(loadedDatabaseName, databaseName, StringComparison.OrdinalIgnoreCase) == false)
                {
                    return;
                }

                Attach(force: true);
            };

            if (databaseLandlord.IsDatabaseLoaded(databaseName))
            {
                Attach(force: false);
            }
        }
Example #7
0
        private void MarkRequestDuration(IResourceApiController controller, long elapsedMilliseconds)
        {
            switch (controller.ResourceType)
            {
            case ResourceType.Database:
                if (landlord.IsDatabaseLoaded(controller.ResourceName ?? Constants.SystemDatabase))
                {
                    controller.MarkRequestDuration(elapsedMilliseconds);
                }
                break;

            case ResourceType.FileSystem:
                break;

            case ResourceType.TimeSeries:
                break;

            case ResourceType.Counter:
                break;

            default:
                throw new NotSupportedException(controller.ResourceType.ToString());
            }
        }
Example #8
0
        private void FinalizeRequestProcessing(RavenBaseApiController controller, HttpResponseMessage response, Stopwatch sw)
        {
            LogHttpRequestStatsParams logHttpRequestStatsParam = null;

            try
            {
                StringBuilder sb = null;
                if (controller.CustomRequestTraceInfo != null && controller.CustomRequestTraceInfo.Count > 0)
                {
                    sb = new StringBuilder();
                    foreach (var action in controller.CustomRequestTraceInfo)
                    {
                        try
                        {
                            action(sb);
                        }
                        catch (Exception e)
                        {
                            Logger.WarnException("Could not gather information to log request stats custom info, so far got " + sb, e);
                        }
                        sb.AppendLine();
                    }
                    while (sb.Length > 0)
                    {
                        if (!char.IsWhiteSpace(sb[sb.Length - 1]))
                        {
                            break;
                        }
                        sb.Length--;
                    }
                }
                var innerRequest       = controller.InnerRequest;
                var httpRequestHeaders = innerRequest.Headers;
                var httpContentHeaders = innerRequest.Content == null ? null : innerRequest.Content.Headers;
                logHttpRequestStatsParam = new LogHttpRequestStatsParams(
                    sw,
                    new Lazy <HttpHeaders>(() => RavenBaseApiController.CloneRequestHttpHeaders(httpRequestHeaders, httpContentHeaders)),
                    controller.InnerRequest.Method.Method,
                    response != null ? (int)response.StatusCode : 500,
                    controller.InnerRequest.RequestUri.ToString(),
                    sb != null ? sb.ToString() : null,
                    controller.InnerRequestsCount
                    );
            }
            catch (Exception e)
            {
                Logger.WarnException("Could not gather information to log request stats", e);
            }

            if (logHttpRequestStatsParam == null || sw == null)
            {
                return;
            }

            sw.Stop();

            if (landlord.IsDatabaseLoaded(controller.TenantName ?? Constants.SystemDatabase))
            {
                controller.MarkRequestDuration(sw.ElapsedMilliseconds);
            }

            var curReq = Interlocked.Increment(ref reqNum);

            LogHttpRequestStats(controller, logHttpRequestStatsParam, controller.TenantName, curReq);

            if (controller.IsInternalRequest == false)
            {
                TraceTraffic(controller, logHttpRequestStatsParam, controller.TenantName);
            }

            RememberRecentRequests(logHttpRequestStatsParam, controller.TenantName);
        }
		private void CleanupDatabases(DatabasesLandlord databaseLandlord)
		{
			var systemDatabase = databaseLandlord.SystemDatabase;

			int nextStart = 0;
			var databaseDocuments = systemDatabase
				.Documents
				.GetDocumentsWithIdStartingWith(Constants.Database.Prefix, null, null, 0, int.MaxValue, CancellationToken.None, ref nextStart);

			var databaseIds = databaseDocuments
				.Select(x => ((RavenJObject)x)["@metadata"])
				.Where(x => x != null)
				.Select(x => x.Value<string>("@id"))
				.Where(x => x != null && x != Constants.SystemDatabase)
				.ToList();

			foreach (var databaseId in databaseIds)
			{
				try
				{
					var key = databaseId;
					if (key.StartsWith(Constants.Database.Prefix))
						key = key.Substring(Constants.Database.Prefix.Length);

					var shouldCleanup = false;

					DateTime value;
					if (databaseLandlord.IsDatabaseLoaded(key) == false)
						shouldCleanup = true;
					else if (databaseLandlord.LastRecentlyUsed.TryGetValue(key, out value) == false || (SystemTime.UtcNow - value) > maxTimeResourceCanBeIdle)
						shouldCleanup = true;

					if (shouldCleanup == false)
						continue;

					var configuration = databaseLandlord.CreateTenantConfiguration(key, true);

					databaseLandlord.Cleanup(key, maxTimeResourceCanBeIdle, database => false);

					var docKey = Constants.Database.Prefix + key;
					systemDatabase.Documents.Delete(docKey, null, null);

					if (configuration == null)
						continue;

					IOExtensions.DeleteDirectory(configuration.DataDirectory);
					if (configuration.IndexStoragePath != null)
						IOExtensions.DeleteDirectory(configuration.IndexStoragePath);
					if (configuration.Storage.Esent.JournalsStoragePath != null)
						IOExtensions.DeleteDirectory(configuration.Storage.Esent.JournalsStoragePath);
					if (configuration.Storage.Voron.JournalsStoragePath != null)
						IOExtensions.DeleteDirectory(configuration.Storage.Voron.JournalsStoragePath);
				}
				catch (Exception e)
				{
					log.WarnException(string.Format("Failed to cleanup '{0}' database.", databaseId), e);
				}
			}
		}
Example #10
0
        private void CleanupDatabases(DatabasesLandlord databaseLandlord)
        {
            var systemDatabase = databaseLandlord.SystemDatabase;

            int nextStart         = 0;
            var databaseDocuments = systemDatabase
                                    .Documents
                                    .GetDocumentsWithIdStartingWith(Constants.Database.Prefix, null, null, 0, int.MaxValue, CancellationToken.None, ref nextStart);

            var databaseIds = databaseDocuments
                              .Select(x => ((RavenJObject)x)["@metadata"])
                              .Where(x => x != null)
                              .Select(x => x.Value <string>("@id"))
                              .Where(x => x != null && x != Constants.SystemDatabase)
                              .ToList();

            foreach (var databaseId in databaseIds)
            {
                try
                {
                    var key = databaseId;
                    if (key.StartsWith(Constants.Database.Prefix))
                    {
                        key = key.Substring(Constants.Database.Prefix.Length);
                    }

                    var shouldCleanup = false;

                    DateTime value;
                    if (databaseLandlord.IsDatabaseLoaded(key) == false)
                    {
                        shouldCleanup = true;
                    }
                    else if (databaseLandlord.LastRecentlyUsed.TryGetValue(key, out value) == false || (SystemTime.UtcNow - value) > maxTimeResourceCanBeIdle)
                    {
                        shouldCleanup = true;
                    }

                    if (shouldCleanup == false)
                    {
                        continue;
                    }

                    var configuration = databaseLandlord.CreateTenantConfiguration(key, true);

                    databaseLandlord.Cleanup(key, maxTimeResourceCanBeIdle, database => false);

                    var docKey = Constants.Database.Prefix + key;
                    systemDatabase.Documents.Delete(docKey, null, null);

                    if (configuration == null)
                    {
                        continue;
                    }

                    IOExtensions.DeleteDirectory(configuration.DataDirectory);
                    if (configuration.IndexStoragePath != null)
                    {
                        IOExtensions.DeleteDirectory(configuration.IndexStoragePath);
                    }
                    if (configuration.Storage.Esent.JournalsStoragePath != null)
                    {
                        IOExtensions.DeleteDirectory(configuration.Storage.Esent.JournalsStoragePath);
                    }
                    if (configuration.Storage.Voron.JournalsStoragePath != null)
                    {
                        IOExtensions.DeleteDirectory(configuration.Storage.Voron.JournalsStoragePath);
                    }
                }
                catch (Exception e)
                {
                    log.WarnException(string.Format("Failed to cleanup '{0}' database.", databaseId), e);
                }
            }
        }