Exemplo n.º 1
0
        public byte *Get(int size, out NativeMemory.ThreadStats thread)
        {
            size = Bits.NextPowerOf2(size);

            if (size > Constants.Size.Megabyte * 16)
            {
                // We don't want to pool large buffers
                return(NativeMemory.Allocate4KbAlignedMemory(size, out thread));
            }

            var index = Bits.MostSignificantBit(size);

            if (_items[index].TryPop(out var allocation))
            {
                thread = allocation.AllocatingThread;
                return((byte *)allocation.Ptr);
            }

            return(NativeMemory.Allocate4KbAlignedMemory(size, out thread));
        }
Exemplo n.º 2
0
        private bool TryReadAndValidateHeader(StorageEnvironmentOptions options, out TransactionHeader *current)
        {
            if (_readAt4Kb > _journalPagerNumberOfAllocated4Kb)
            {
                current = null;
                return(false); // end of jouranl
            }

            const int pageTo4KbRatio     = Constants.Storage.PageSize / (4 * Constants.Size.Kilobyte);
            var       pageNumber         = _readAt4Kb / pageTo4KbRatio;
            var       positionInsidePage = (_readAt4Kb % pageTo4KbRatio) * (4 * Constants.Size.Kilobyte);

            current = (TransactionHeader *)
                      (_journalPager.AcquirePagePointer(this, pageNumber) + positionInsidePage);

            // due to the reuse of journals we no longer can assume we have zeros in the end of the journal
            // we might have there random garbage or old transactions we can ignore, so we have the following scenarios:
            // * TxId <= current Id      ::  we can ignore old transaction of the reused journal and continue
            // * TxId == current Id + 1  ::  valid, but if hash is invalid. Transaction hasn't been committed
            // * TxId >  current Id + 1  ::  if hash is invalid we can ignore reused/random, but if hash valid then we might missed TXs

            if (current->HeaderMarker != Constants.TransactionHeaderMarker)
            {
                // not a transaction page,

                // if the header marker is zero or garbage, we are probably in the area at the end of the log file, and have no additional log records
                // to read from it. This can happen if the next transaction was too big to fit in the current log file. We stop reading
                // this log file and move to the next one, or it might have happened becuase of reuse of journal file

                // note : we might encounter a "valid" TransactionHeaderMarker which is still garbage, so we will test that later on

                RequireHeaderUpdate = false;
                return(false);
            }

            if (current->TransactionId < 0)
            {
                return(false);
            }

            current = EnsureTransactionMapped(current, pageNumber, positionInsidePage);

            if (options.EncryptionEnabled)
            {
                // We use temp buffers to hold the transaction before decrypting, and release the buffers afterwards.
                var pagesSize = current->CompressedSize != -1 ? current->CompressedSize : current->UncompressedSize;
                var size      = (4 * Constants.Size.Kilobyte) * GetNumberOf4KbFor(sizeof(TransactionHeader) + pagesSize);

                var ptr    = NativeMemory.Allocate4KbAlignedMemory(size, out var thread);
                var buffer = new EncryptionBuffer
                {
                    Pointer          = ptr,
                    Size             = size,
                    AllocatingThread = thread
                };

                _encryptionBuffers.Add(buffer);
                Memory.Copy(buffer.Pointer, (byte *)current, size);
                current = (TransactionHeader *)buffer.Pointer;

                try
                {
                    DecryptTransaction((byte *)current, options);
                }
                catch (InvalidOperationException ex)
                {
                    RequireHeaderUpdate = true;
                    options.InvokeRecoveryError(this, "Transaction " + current->TransactionId + " was not committed", ex);
                    return(false);
                }
            }
            bool hashIsValid = ValidatePagesHash(options, current);

            if (LastTransactionHeader == null)
            {
                return(hashIsValid);
            }

            var txIdDiff = current->TransactionId - LastTransactionHeader->TransactionId;

            // 1 is a first storage transaction which does not increment transaction counter after commit
            if (current->TransactionId != 1)
            {
                if (txIdDiff < 0)
                {
                    return(false);
                }

                if (txIdDiff > 1 || txIdDiff == 0)
                {
                    if (hashIsValid)
                    {
                        // TxId is bigger then the last one by nore the '1' but has valid hash which mean we lost transactions in the middle
                        throw new InvalidDataException(
                                  $"Transaction has valid(!) hash with invalid transaction id {current->TransactionId}, the last valid transaction id is {LastTransactionHeader->TransactionId}." +
                                  $" Journal file {_journalPager.FileName} might be corrupted");
                    }
                }

                // if (txIdDiff == 1) :
                if (current->LastPageNumber <= 0)
                {
                    throw new InvalidDataException("Last page number after committed transaction must be greater than 0");
                }
            }

            if (hashIsValid == false)
            {
                RequireHeaderUpdate = true;
                options.InvokeRecoveryError(this, "Transaction " + current->TransactionId + " was not committed",
                                            null);
                return(false);
            }

            return(true);
        }