예제 #1
0
        OnOpenPhysicalLogAsync(
            string pathToCommonContainer,
            Guid physicalLogId,
            bool isStagingLog,
            CancellationToken cancellationToken)
        {
            Requires.Argument("pathToCommonContainer", pathToCommonContainer).NotNull();

            IPhysicalLog result = null;

            await _Lock.WaitUntilSetAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                PhysicalLog subjectLog;

                if (_Logs.TryGetValue(physicalLogId, out subjectLog) == false)
                {
                    // P.Log has not been opened yet- attempt to open its underpinnings
                    var underlyingContainer = await _AppDomainPhysLogManager.OpenLogContainerAsync(
                        pathToCommonContainer,
                        isStagingLog?Guid.Empty : physicalLogId,
                        cancellationToken).ConfigureAwait(false);

                    Exception caughtException = null;
                    try
                    {
                        subjectLog = new PhysicalLog(physicalLogId, underlyingContainer);
                        _Logs.Add(physicalLogId, subjectLog);
                        subjectLog.IsOpen = true;
                        result            = await subjectLog.OnCreateHandle(cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        // Cause clean up failed create
                        caughtException = ex;
                    }

                    if (caughtException != null)
                    {
                        // clean up failed create and forward the causing exception
                        if (subjectLog != null)
                        {
                            subjectLog.IsOpen = false;
                            _Logs.Remove(physicalLogId);
                        }

                        try
                        {
                            await underlyingContainer.CloseAsync(cancellationToken).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            AppTrace.TraceSource.WriteInfo(TraceType, "OnOpenPhysicalLogAsync - CloseAsync: Exception {0}", ex);
                        }

                        throw caughtException;
                    }
                }
                else
                {
                    // Already open - just alias with another handle
                    result = await subjectLog.OnCreateHandle(cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                _Lock.Set();
            }

            return(result);
        }
예제 #2
0
        OnCreatePhysicalLogAsync(
            string pathToCommonContainer,
            Guid physicalLogId,
            long containerSize,
            uint maximumNumberStreams,
            uint maximumLogicalLogBlockSize,
            LogManager.LogCreationFlags CreationFlags,
            CancellationToken cancellationToken)
        {
            Requires.Argument("pathToCommonContainer", pathToCommonContainer).NotNull();

            IPhysicalLog result = null;

            await _Lock.WaitUntilSetAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                PhysicalLog subjectLog;

                if (_Logs.TryGetValue(physicalLogId, out subjectLog) == false)
                {
                    // P.Log has not been opened or created - attempt to create it
                    var underlyingContainer = await _AppDomainPhysLogManager.CreateContainerAsync(
                        pathToCommonContainer,
                        physicalLogId,
                        containerSize,
                        maximumNumberStreams,
                        maximumLogicalLogBlockSize,
                        CreationFlags,
                        cancellationToken).ConfigureAwait(false);

                    Exception caughtException = null;
                    try
                    {
                        // Make and "open" PhysicalLog representing the underlying IPhysicalLogContainer
                        subjectLog = new PhysicalLog(physicalLogId, underlyingContainer);
                        _Logs.Add(physicalLogId, subjectLog);
                        subjectLog.IsOpen = true;
                        result            = await subjectLog.OnCreateHandle(cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        // Cause clean up failed create
                        caughtException = ex;
                    }

                    if (caughtException != null)
                    {
                        // clean up failed - create and forward the causing exception
                        if (subjectLog != null)
                        {
                            subjectLog.IsOpen = false;
                            _Logs.Remove(physicalLogId);
                        }

                        try
                        {
                            await underlyingContainer.CloseAsync(cancellationToken).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            AppTrace.TraceSource.WriteInfo(TraceType, "OnCreatePhysicalLogAsync - CloseAsync: Exception {0}", ex);
                        }

                        try
                        {
                            await underlyingContainer.DeletePhysicalLogStreamAsync(physicalLogId, cancellationToken).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            AppTrace.TraceSource.WriteInfo(TraceType, "OnCreatePhysicalLogAsync - DeletePhysicalLogStreamAsync: Exception {0}", ex);
                        }

                        throw caughtException;
                    }
                }
                else
                {
                    // Have an existing physical log with this id already - fault
                    throw new UnauthorizedAccessException(SR.Error_PhysicalLog_Exists);
                }
            }
            finally
            {
                _Lock.Set();
            }

            return(result);
        }