Exemple #1
0
        /// <summary>
        /// Creates instance of <see cref="IFileFsClient"/>.
        /// </summary>
        /// <param name="fileFsStoragePath">Path to FileFS storage file.</param>
        /// <param name="options">Options for FileFS client.</param>
        /// <param name="logger">Logger instance.</param>
        /// <returns>Instance of <see cref="IFileFsClient"/>.</returns>
        public static IFileFsClient Create(string fileFsStoragePath, FileFsClientOptions options, ILogger logger)
        {
            ITransactionWrapper CreateTransactionWrapper(bool enableTransactions) =>
            enableTransactions
                    ? (ITransactionWrapper) new TransactionWrapper(fileFsStoragePath, logger)
                    : (ITransactionWrapper) new NullTransactionWrapper();

            FileFsClientOptionsValidator.Validate(options);

            var storageStreamProvider = new StorageStreamProvider(fileFsStoragePath, logger);
            var connection            = new StorageConnection(storageStreamProvider, options.ByteBufferSize, logger);

            var filesystemDescriptorSerializer = new FilesystemDescriptorSerializer(logger);
            var filesystemDescriptorAccessor   = new FilesystemDescriptorAccessor(connection, filesystemDescriptorSerializer, logger);

            var entryDescriptorSerializer = new EntryDescriptorSerializer(filesystemDescriptorAccessor, logger);
            var entryDescriptorRepository = new EntryDescriptorRepository(connection, filesystemDescriptorAccessor, entryDescriptorSerializer, logger);

            var operationLocker = new StorageOperationLocker();
            var optimizer       = new StorageOptimizer(connection, entryDescriptorRepository, filesystemDescriptorAccessor, operationLocker, logger);
            var extender        = new StorageExtender(connection, filesystemDescriptorAccessor, operationLocker, logger);
            var allocator       = new FileAllocator(connection, filesystemDescriptorAccessor, entryDescriptorRepository, optimizer, extender, operationLocker, logger);

            var entryRepository     = new EntryRepository(filesystemDescriptorAccessor, entryDescriptorRepository, logger);
            var fileRepository      = new FileRepository(connection, allocator, filesystemDescriptorAccessor, entryDescriptorRepository, logger);
            var directoryRepository = new DirectoryRepository(filesystemDescriptorAccessor, entryDescriptorRepository, logger);

            var externalFileManager = new ExternalFileManager(logger);
            var transactionWrapper  = CreateTransactionWrapper(options.EnableTransactions);

            var client = new FileFsClient(fileRepository, directoryRepository, entryRepository, externalFileManager, optimizer, transactionWrapper, operationLocker);

            return(client);
        }
        public void Update_ShouldReturnUpdatedDescriptor(int bufferSize, int fileDescriptorLength)
        {
            // Arrange
            var buffer = new byte[bufferSize];
            var storageStreamProvider = StorageStreamProviderMockFactory.Create(buffer);
            var logger               = new LoggerConfiguration().CreateLogger();
            var storageConnection    = new StorageConnection(storageStreamProvider, 4096, logger);
            var filesystemSerializer = new FilesystemDescriptorSerializer(logger);

            var expectedDescriptor         = new FilesystemDescriptor(0, 0, fileDescriptorLength);
            var filesystemDescriptorBytes  = filesystemSerializer.ToBytes(expectedDescriptor);
            var filesystemDescriptorCursor = new Cursor(-FilesystemDescriptor.BytesTotal, SeekOrigin.End);

            storageConnection.PerformWrite(filesystemDescriptorCursor, filesystemDescriptorBytes);

            var filesystemDescriptorAccessor =
                new FilesystemDescriptorAccessor(storageConnection, filesystemSerializer, logger);

            // Act
            filesystemDescriptorAccessor.Update(_ => 0, _ => 0, _ => fileDescriptorLength);

            // Assert
            var returnedDescriptorBytes =
                storageConnection.PerformRead(filesystemDescriptorCursor, FilesystemDescriptor.BytesTotal);
            var returnedDescriptor = filesystemSerializer.FromBytes(returnedDescriptorBytes);

            Assert.Equal(expectedDescriptor, returnedDescriptor);
        }
 /// <summary>
 /// 获取存储节点的响应
 /// </summary>
 /// <returns></returns>
 public override byte[] GetStorageResponse()
 {
     if (StorageConnection == null)
     {
         throw new Exception("");
     }
     this.Message = string.Empty;
     byte[] body = null;
     try
     {
         StorageConnection.OpenConnection();
     }
     catch (Exception ex)
     {
         Message = ex.Message;
         Console.WriteLine($"GetStorageResponse.OpenConnection => {ex.Message}");
         throw ex;
     }
     try
     {
         body = ReadNetStream();
     }
     catch (Exception ex)
     {
         Message = ex.Message;
         Console.WriteLine($"GetStorageResponse.ReadStream => {ex.Message}");
         StorageConnection.CloseConnection();
     }
     return(body);
 }
        // 网络流中读取字节码,当前stream不能关闭
        private byte[] ReadNetStream()
        {
            var stream       = StorageConnection.GetStream();
            var headerBuffer = Header.ToByte();

            stream.Write(headerBuffer, 0, headerBuffer.Length);
            stream.Write(Body, 0, Body.Length);
            var header = new FDFSHeader(stream);

            if (header.Status != 0)
            {
                throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status));
            }

            long remind = header.Length;

            byte[] body = null;
            if (stream.CanRead)
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    do
                    {
                        byte[] buff  = new byte[256 * 1024];
                        var    index = stream.Read(buff, 0, buff.Length);
                        outStream.Write(buff, 0, index);
                        remind = remind - index;
                    }while (remind > 0);
                    body = outStream.ToArray();
                }
            }
            return(body);
        }
Exemple #5
0
        public DsmsStorageAccountFactory(StorageConnection connection) : base(connection)
        {
            this.traceSource = new FabricEvents.ExtensionsEvents(FabricEvents.Tasks.FabricDCA);

            var sourceLocation = connection.DsmsSourceLocation;

            this.traceSource.WriteInfo(TraceType, "Invoking ClientInitialize and creating DsmsStorageCredentials instance for source location: {0} and EndPointSuffix: {1}", sourceLocation, connection.DsmsEndpointSuffix);

            try
            {
                if (connection.DsmsAutopilotServiceName == null)
                {
                    CredentialManagementClient.Instance.ClientInitialize();
                }
                else
                {
                    CredentialManagementClient.Instance.ClientInitializeForAp(connection.DsmsAutopilotServiceName);
                }

                this.dsmsCredentials = DsmsStorageCredentials.GetStorageCredentials(sourceLocation);
            }
            catch (Exception ex)
            {
                this.traceSource.WriteExceptionAsError(TraceType, ex);
                throw;
            }
        }
 public StorageConnectionShould()
 {
     this.AssumeConnectionStringProviderIsInitialised();
     this.AssumeAppSettingProviderIsInitialised();
     this.AssumeAccountProviderIsInitialised();
     this.target = new StorageConnection(this.connectionStringProvider,
                                         this.appSettingProvider,
                                         this.accountProvider);
 }
Exemple #7
0
        public void Dispose_DoesNotDisposeTheConnection()
        {
            var sqlConnection = ConnectionUtils.GetConnectionProvider();
            var connection    = new StorageConnection(sqlConnection, _queue.Object, _options);

            connection.Dispose();

            // Assert.Equal(sqlConnection.); TODO
        }
Exemple #8
0
 private void UseConnection(Action <StorageConnection> action)
 {
     using (var connection = new StorageConnection(
                ConnectionUtils.GetConnectionProvider(),
                _queue.Object,
                _options))
     {
         action(connection);
     }
 }
Exemple #9
0
        private static StorageConnection ValidateParse(string testString, string expectedAccountName)
        {
            var parseErrorMessage = string.Empty;
            var connection        = new StorageConnection();
            var result            = ConnectionStringParser.ParseConnectionString(testString.ToCharArray(), (error) => { parseErrorMessage = error; }, ref connection);

            Assert.IsTrue(result, parseErrorMessage);
            Assert.IsNotNull(connection);
            Assert.AreEqual(expectedAccountName, connection.AccountName);
            return(connection);
        }
Exemple #10
0
        private void UseConnections(Action <NpgsqlConnection, StorageConnection> action)
        {
            var provider = ConnectionUtils.GetConnectionProvider();

            using (var connection = new StorageConnection(provider, _queue.Object, _options))
            {
                using (var con = provider.AcquireConnection())
                {
                    action(con.Connection, connection);
                }
            }
        }
Exemple #11
0
        public Form1()
        {
            InitializeComponent();
            var loginService = new LoginService();

            _connection        = loginService.Login();
            _storageConnection = new StorageConnection(_connection.Storage.ConnectionParameters);

            Text = string.Format(@"{0} - {1}", Text, _connection.ConnectionUri.Host);
            _btnDownloadLatest.Enabled   = false;
            _btnDownloadSelected.Enabled = false;
        }
        /// <summary>
        /// Creates instance of <see cref="IStorageInitializer"/>.
        /// </summary>
        /// <param name="fileFsStoragePath">Path to FileFS storage file.</param>
        /// <param name="logger">Logger instance.</param>
        /// <returns>Instance of <see cref="IStorageInitializer"/>.</returns>
        public static IStorageInitializer Create(string fileFsStoragePath, ILogger logger)
        {
            var storageStreamProvider          = new StorageStreamProvider(fileFsStoragePath, logger);
            var connection                     = new StorageConnection(storageStreamProvider, 4096, logger);
            var filesystemDescriptorSerializer = new FilesystemDescriptorSerializer(logger);
            var filesystemDescriptorAccessor   = new FilesystemDescriptorAccessor(connection, filesystemDescriptorSerializer, logger);
            var entryDescriptorSerializer      = new EntryDescriptorSerializer(filesystemDescriptorAccessor, logger);
            var entryDescriptorRepository      = new EntryDescriptorRepository(connection, filesystemDescriptorAccessor, entryDescriptorSerializer, logger);
            var operationLocker                = new StorageOperationLocker();
            var storageInitializer             =
                new StorageInitializer(connection, operationLocker, filesystemDescriptorAccessor, entryDescriptorRepository, logger);

            return(storageInitializer);
        }
Exemple #13
0
        public StorageConnection GetConnection(bool skipInitiatedCheck = false)
        {
            if (!skipInitiatedCheck && !storageInitiated)
            {
                logger.Error("Cannot created connection to database: storage not initialized");
                statusService.Post("Помилка: база даний не ініційована, продовження роботи неможливе");
            }
            string fullDbFilePath = FilenameService.GetDBPath();
            string connString     = GetConnectionString();

            StorageConnection connection = new StorageConnection(connString);

            if (connection != null && (connection.State == System.Data.ConnectionState.Connecting || connection.State == System.Data.ConnectionState.Open))
            {
                return(connection);
            }
            else
            {
                throw new Exception(string.Format("Database is not available with connection string {0}", connString));
            }
        }
Exemple #14
0
        private bool disposedValue = false; // 要检测冗余调用

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    if (Connection != null)
                    {
                        Connection.ReleaseConnection();
                    }
                    if (StorageConnection != null)
                    {
                        StorageConnection.ReleaseConnection();
                    }
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }
        public async Task <bool> DeleteProductSubType(ProductSubTypeModel subType, StorageConnection connection = null)
        {
            bool result = false;

            try
            {
                using (var db = connection ?? dbManager.GetConnection())
                    using (var command = db.Connection.CreateCommand())
                    {
                        command.CommandText = string.Format("DELETE FROM PRODUCTSUBTYPE WHERE Id = {0}", subType.Id);
                        await command.ExecuteNonQueryAsync().ConfigureAwait(false);

                        result = true;
                    }
            }
            catch (Exception ex)
            {
                result = false;
                logger.Error("Exception during execution method \"DeleteProductSubType\": {0}", ex.Message);
            }

            return(result);
        }
        public void CanWriteAndReadBackMessagesOnQueue()
        {
            var connection =
                new StorageConnection(
                    new ConfigFileConnectionStringProvider(),
                    new ConfigFileAppSettingProvider(),
                    new AccountProvider()).Open("PcgStorageAccount");
            var writer = connection.CreateQueueWriter("testing");

            writer.Write(new QueueMessage("message 1"));
            writer.Write(new QueueMessage("message 2"));
            writer.Write(new QueueMessage("message 3"));

            var reader  = connection.CreateQueueReader("testing");
            var counter = 0;

            while (reader.Read())
            {
                reader.Current.Should()
                .Be("message " + ++counter);
            }
            counter.Should()
            .Be(3);
        }
Exemple #17
0
 public StorageNodeClient(StorageConnection storageConnection)
 {
     ApiUrl = storageConnection.RestApiUrl;
     HeaderCredentialsVal = $"NemAddress={storageConnection.NemAddress}; Bearer {storageConnection.BearerToken}";
 }
Exemple #18
0
        public void ParseDsmsConnectionStringTest()
        {
            // Test cases with expected results in parsing
            var connectionStrings = new[] {
                // Testing parsing success case with SourceLocation before EndpointSuffix
                new {
                    ConnectionString  = $"dsms:SourceLocation={DsmsSourceLocation};EndpointSuffix={DsmsEndpointSuffix}",
                    StorageConnection = new StorageConnection(DsmsSourceLocation, DsmsEndpointSuffix, null),
                    Valid             = true
                },
                // Testing parsing success case with EndpointSuffix before SourceLocation
                new {
                    ConnectionString  = $"dsms:EndpointSuffix={DsmsEndpointSuffix};SourceLocation={DsmsSourceLocation}",
                    StorageConnection = new StorageConnection(DsmsSourceLocation, DsmsEndpointSuffix, null),
                    Valid             = true
                },
                // Testing parsing success case with AutopilotServiceName after the required parameters
                new {
                    ConnectionString  = $"dsms:EndpointSuffix={DsmsEndpointSuffix};SourceLocation={DsmsSourceLocation};AutopilotServiceName={DsmsAutopilotServiceName}",
                    StorageConnection = new StorageConnection(DsmsSourceLocation, DsmsEndpointSuffix, DsmsAutopilotServiceName),
                    Valid             = true
                },
                // Testing parsing success case with AutopilotServiceName before the required parameters
                new {
                    ConnectionString  = $"dsms:AutopilotServiceName={DsmsAutopilotServiceName};EndpointSuffix={DsmsEndpointSuffix};SourceLocation={DsmsSourceLocation}",
                    StorageConnection = new StorageConnection(DsmsSourceLocation, DsmsEndpointSuffix, DsmsAutopilotServiceName),
                    Valid             = true
                },
                // Testing parsing failure case missing EndpointSuffix
                new {
                    ConnectionString  = $"dsms:SourceLocation={DsmsSourceLocation}",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case missing SourceLocation
                new {
                    ConnectionString  = $"dsms:EndpointSuffix={DsmsEndpointSuffix}",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case missing both EndpointSuffix and SourceLocation
                new {
                    ConnectionString  = $"dsms:",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case repeated SourceLocation
                new {
                    ConnectionString  = $"dsms:SourceLocation={DsmsSourceLocation};SourceLocation={DsmsSourceLocation}",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case repeated EndpointSuffix
                new {
                    ConnectionString  = $"dsms:EndpointSuffix={DsmsEndpointSuffix};EndpointSuffix={DsmsEndpointSuffix}",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case where SourceLocation is empty
                new {
                    ConnectionString  = $"dsms:SourceLocation=;EndpointSuffix={DsmsEndpointSuffix}",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case where EndpointSuffix is empty
                new {
                    ConnectionString  = $"dsms:SourceLocation={DsmsSourceLocation};EndpointSuffix=",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case where both SourceLocation and EndpointSuffix are empty
                new {
                    ConnectionString  = $"dsms:SourceLocation=;EndpointSuffix=",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                },
                // Testing parsing failure case AutopilotServiceName is empty
                new {
                    ConnectionString  = $"dsms:SourceLocation={DsmsSourceLocation};EndpointSuffix={DsmsEndpointSuffix};AutopilotServiceName=",
                    StorageConnection = new StorageConnection(),
                    Valid             = false
                }
            };

            bool   testSuccess   = true;
            string parseErrorMsg = string.Empty;

            // go over each test case and append error message in case test fails
            foreach (var connection in connectionStrings)
            {
                try
                {
                    StorageConnection storageConnection = new StorageConnection();

                    Action <string> errorLogger = (connection.Valid) ? new Action <string>(error => { Console.WriteLine(error); }) : new Action <string>(error => { });
                    bool            parseRes    = ConnectionStringParser.ParseConnectionString(
                        connection.ConnectionString.ToCharArray(),
                        errorLogger,
                        ref storageConnection);

                    if (
                        parseRes != connection.Valid ||
                        storageConnection.IsDsms != connection.Valid ||
                        storageConnection.DsmsEndpointSuffix != connection.StorageConnection.DsmsEndpointSuffix ||
                        storageConnection.DsmsSourceLocation != connection.StorageConnection.DsmsSourceLocation ||
                        storageConnection.DsmsAutopilotServiceName != connection.StorageConnection.DsmsAutopilotServiceName)
                    {
                        parseErrorMsg += $"TestCase with ConnectionString=\"{connection.ConnectionString}\" failed to parse.\n" +
                                         $"Parsing Result = {parseRes}\n" +
                                         $"Expected:\n\tParseResult = {connection.Valid}\n\tIsDsms = { connection.StorageConnection.IsDsms},\n\tSourceLocation = { connection.StorageConnection.DsmsSourceLocation}\n\tEndpointSuffix = { connection.StorageConnection.DsmsEndpointSuffix}\n\tAutopilotServiceName = {connection.StorageConnection.DsmsAutopilotServiceName}\n" +
                                         $"Read    :\n\tParseResult = {parseRes        }\n\tIsDsms = { storageConnection.IsDsms           },\n\tSourceLocation = { storageConnection.DsmsSourceLocation           }\n\tEndpointSuffix = { storageConnection.DsmsEndpointSuffix           }\n\tAutopilotServiceName = {storageConnection.DsmsAutopilotServiceName}\n";

                        testSuccess = false;
                    }
                }
                catch (Exception e)
                {
                    // test failed
                    parseErrorMsg += $"TestCase with ConnectionString=\"{connection.ConnectionString}\" failed with Exception: {e}";
                    testSuccess    = false;
                }
            }

            Assert.IsTrue(testSuccess, parseErrorMsg);
        }