Exemple #1
0
 public DocumentDatabase(string name, RavenConfiguration configuration, ServerStore serverStore)
 {
     StartTime                 = SystemTime.UtcNow;
     Name                      = name;
     ResourceName              = "db/" + name;
     Configuration             = configuration;
     _logger                   = LoggingSource.Instance.GetLogger <DocumentDatabase>(Name);
     Notifications             = new DocumentsNotifications();
     DocumentsStorage          = new DocumentsStorage(this);
     IndexStore                = new IndexStore(this);
     TransformerStore          = new TransformerStore(this);
     SqlReplicationLoader      = new SqlReplicationLoader(this);
     DocumentReplicationLoader = new DocumentReplicationLoader(this);
     DocumentTombstoneCleaner  = new DocumentTombstoneCleaner(this);
     SubscriptionStorage       = new SubscriptionStorage(this);
     Operations                = new DatabaseOperations(this);
     Metrics                   = new MetricsCountersManager();
     IoMetrics                 = serverStore?.IoMetrics ?? new IoMetrics(256, 256);
     Patch                     = new PatchDocument(this);
     TxMerger                  = new TransactionOperationsMerger(this, DatabaseShutdown);
     HugeDocuments             = new HugeDocuments(configuration.Databases.MaxCollectionSizeHugeDocuments,
                                                   configuration.Databases.MaxWarnSizeHugeDocuments);
     ConfigurationStorage = new ConfigurationStorage(this, serverStore);
     DatabaseInfoCache    = serverStore?.DatabaseInfoCache;
 }
Exemple #2
0
        public DocumentDatabase(string name, RavenConfiguration configuration, ServerStore serverStore)
        {
            Scripts       = new ScriptRunnerCache(this);
            _logger       = LoggingSource.Instance.GetLogger <DocumentDatabase>(Name);
            _serverStore  = serverStore;
            StartTime     = SystemTime.UtcNow;
            Name          = name;
            Configuration = configuration;

            try
            {
                using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext ctx))
                    using (ctx.OpenReadTransaction())
                    {
                        MasterKey = serverStore.GetSecretKey(ctx, Name);

                        var databaseRecord = _serverStore.Cluster.ReadDatabase(ctx, Name);
                        if (databaseRecord != null)
                        {
                            // can happen when we are in the process of restoring a database
                            if (databaseRecord.Encrypted && MasterKey == null)
                            {
                                throw new InvalidOperationException($"Attempt to create encrypted db {Name} without supplying the secret key");
                            }
                            if (databaseRecord.Encrypted == false && MasterKey != null)
                            {
                                throw new InvalidOperationException($"Attempt to create a non-encrypted db {Name}, but a secret key exists for this db.");
                            }
                        }
                    }

                QueryMetadataCache       = new QueryMetadataCache();
                IoChanges                = new IoChangesNotifications();
                Changes                  = new DocumentsChanges();
                DocumentTombstoneCleaner = new DocumentTombstoneCleaner(this);
                DocumentsStorage         = new DocumentsStorage(this);
                IndexStore               = new IndexStore(this, serverStore, _indexAndTransformerLocker);
                EtlLoader                = new EtlLoader(this, serverStore);
                ReplicationLoader        = new ReplicationLoader(this, serverStore);
                SubscriptionStorage      = new SubscriptionStorage(this, serverStore);
                Metrics                  = new MetricsCountersManager();
                TxMerger                 = new TransactionOperationsMerger(this, DatabaseShutdown);
                HugeDocuments            = new HugeDocuments(configuration.PerformanceHints.HugeDocumentsCollectionSize,
                                                             configuration.PerformanceHints.HugeDocumentSize.GetValue(SizeUnit.Bytes));
                ConfigurationStorage            = new ConfigurationStorage(this);
                NotificationCenter              = new NotificationCenter.NotificationCenter(ConfigurationStorage.NotificationsStorage, Name, _databaseShutdown.Token);
                Operations                      = new Operations.Operations(Name, ConfigurationStorage.OperationsStorage, NotificationCenter, Changes);
                DatabaseInfoCache               = serverStore.DatabaseInfoCache;
                RachisLogIndexNotifications     = new RachisLogIndexNotifications(DatabaseShutdown);
                CatastrophicFailureNotification = new CatastrophicFailureNotification(e =>
                {
                    serverStore.DatabasesLandlord.UnloadResourceOnCatastrophicFailure(name, e);
                });
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }
        internal static IEnumerable <ReplayProgress> Replay(DocumentDatabase database, Stream replayStream)
        {
            DocumentsOperationContext txCtx   = null;
            IDisposable          txDisposable = null;
            DocumentsTransaction previousTx   = null;

            using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (context.GetManagedBuffer(out var buffer))
                    using (var gZipStream = new GZipStream(replayStream, CompressionMode.Decompress, leaveOpen: true))
                    {
                        var peepingTomStream = new PeepingTomStream(gZipStream, context);
                        var state            = new JsonParserState();
                        var parser           = new UnmanagedJsonParser(context, state, "file");

                        var commandsProgress = 0;
                        var readers          = UnmanagedJsonParserHelper.ReadArrayToMemory(context, peepingTomStream, parser, state, buffer);
                        using (var readersItr = readers.GetEnumerator())
                        {
                            ReadStartRecordingDetails(readersItr, context, peepingTomStream);
                            while (readersItr.MoveNext())
                            {
                                using (readersItr.Current)
                                {
                                    if (readersItr.Current.TryGet(nameof(RecordingDetails.Type), out string strType) == false)
                                    {
                                        throw new ReplayTransactionsException($"Can't read {nameof(RecordingDetails.Type)} of replay detail", peepingTomStream);
                                    }

                                    if (Enum.TryParse <TxInstruction>(strType, true, out var type))
                                    {
                                        switch (type)
                                        {
                                        case TxInstruction.BeginTx:
                                            txDisposable = database.DocumentsStorage.ContextPool.AllocateOperationContext(out txCtx);
                                            txCtx.OpenWriteTransaction();
                                            break;

                                        case TxInstruction.Commit:
                                            txCtx.Transaction.Commit();
                                            break;

                                        case TxInstruction.DisposeTx:
                                            txDisposable.Dispose();
                                            break;

                                        case TxInstruction.BeginAsyncCommitAndStartNewTransaction:
                                            previousTx        = txCtx.Transaction;
                                            txCtx.Transaction = txCtx.Transaction.BeginAsyncCommitAndStartNewTransaction(txCtx);
                                            txDisposable      = txCtx.Transaction;
                                            break;

                                        case TxInstruction.EndAsyncCommit:
                                            previousTx.EndAsyncCommit();
                                            break;

                                        case TxInstruction.DisposePrevTx:
                                            previousTx.Dispose();
                                            break;
                                        }
                                        continue;
                                    }

                                    try
                                    {
                                        var cmd = DeserializeCommand(context, database, strType, readersItr.Current, peepingTomStream);
                                        commandsProgress += cmd.ExecuteDirectly(txCtx);
                                        TransactionOperationsMerger.UpdateGlobalReplicationInfoBeforeCommit(txCtx);
                                    }
                                    catch (Exception)
                                    {
                                        //TODO To accept exceptions that was thrown while recording
                                        txDisposable.Dispose();
                                        throw;
                                    }

                                    yield return(new ReplayProgress
                                    {
                                        CommandsProgress = commandsProgress
                                    });
                                }
                            }
                        }
                    }
        }
 public BeforeEnabledRecordingState(TransactionOperationsMerger txOpMerger)
 {
     _txOpMerger = txOpMerger;
 }