public override string UpdateDatabaseRecord(DatabaseRecord record, long etag)
        {
            DeletionInProgressStatus deletionStatus = DeletionInProgressStatus.No;

            record.DeletionInProgress?.TryGetValue(NodeTag, out deletionStatus);

            record.Topology.RemoveFromTopology(NodeTag);
            record.DeletionInProgress?.Remove(NodeTag);

            if (DatabaseId == null)
            {
                return(null);
            }

            if (deletionStatus == DeletionInProgressStatus.HardDelete)
            {
                if (record.UnusedDatabaseIds == null)
                {
                    record.UnusedDatabaseIds = new HashSet <string>();
                }

                record.UnusedDatabaseIds.Add(DatabaseId);
            }

            return(null);
        }
        public override void UpdateDatabaseRecord(DatabaseRecord record, long etag)
        {
            DeletionInProgressStatus deletionStatus = DeletionInProgressStatus.No;

            record.DeletionInProgress?.TryGetValue(NodeTag, out deletionStatus);
            if (deletionStatus == DeletionInProgressStatus.No)
            {
                return;
            }

            record.Topology.RemoveFromTopology(NodeTag);
            record.DeletionInProgress?.Remove(NodeTag);

            if (DatabaseId == null)
            {
                return;
            }

            if (deletionStatus == DeletionInProgressStatus.HardDelete)
            {
                if (record.UnusedDatabaseIds == null)
                {
                    record.UnusedDatabaseIds = new HashSet <string>();
                }

                record.UnusedDatabaseIds.Add(DatabaseId);
            }

            if (record.RollingIndexes == null)
            {
                return;
            }

            foreach (var rollingIndex in record.RollingIndexes)
            {
                if (rollingIndex.Value.ActiveDeployments.ContainsKey(NodeTag))
                {
                    // we use a dummy command to update the record as if the indexing on the removed node was completed
                    var dummy = new PutRollingIndexCommand(DatabaseName, rollingIndex.Key, NodeTag, finishedAt: null, "dummy update");
                    dummy.UpdateDatabaseRecord(record, etag);
                    rollingIndex.Value.ActiveDeployments.Remove(NodeTag);
                }
            }
        }
        public void DeleteDatabase(string dbName, DeletionInProgressStatus deletionInProgress, DatabaseRecord record)
        {
            IDisposable removeLockAndReturn = null;
            string      databaseId;

            try
            {
                try
                {
                    removeLockAndReturn = DatabasesCache.RemoveLockAndReturn(dbName, CompleteDatabaseUnloading, out var database);
                    databaseId          = database?.DbBase64Id;
                }
                catch (AggregateException ae) when(nameof(DeleteDatabase).Equals(ae.InnerException.Data["Source"]))
                {
                    // this is already in the process of being deleted, we can just exit and let another thread handle it
                    return;
                }
                catch (DatabaseConcurrentLoadTimeoutException e)
                {
                    if (e.Data.Contains(nameof(DeleteDatabase)))
                    {
                        // This case is when a deletion request was issued during the loading of the database.
                        // The DB will be deleted after actually finishing the loading process
                        return;
                    }

                    throw;
                }

                if (deletionInProgress == DeletionInProgressStatus.HardDelete)
                {
                    RavenConfiguration configuration;
                    try
                    {
                        configuration = CreateDatabaseConfiguration(dbName, ignoreDisabledDatabase: true, ignoreBeenDeleted: true, ignoreNotRelevant: true,
                                                                    databaseRecord: record);
                    }
                    catch (Exception ex)
                    {
                        configuration = null;
                        if (_logger.IsInfoEnabled)
                        {
                            _logger.Info("Could not create database configuration", ex);
                        }
                    }

                    // this can happen if the database record was already deleted
                    if (configuration != null)
                    {
                        DatabaseHelper.DeleteDatabaseFiles(configuration);
                    }
                }

                // At this point the db record still exists but the db was effectively deleted
                // from this node so we can also remove its secret key from this node.
                if (record.Encrypted)
                {
                    using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                        using (var tx = context.OpenWriteTransaction())
                        {
                            _serverStore.DeleteSecretKey(context, dbName);
                            tx.Commit();
                        }
                }

                // delete the cache info
                DeleteDatabaseCachedInfo(dbName, _serverStore);
            }
            finally
            {
                removeLockAndReturn?.Dispose();
            }
            NotifyLeaderAboutRemoval(dbName, databaseId);
        }