/// <summary>
        /// Build a driver instance using the current configuration set with the builder.
        /// </summary>
        ///
        /// <returns>A newly created driver.</returns>
        public QldbDriver Build()
        {
            if (this.SessionConfig == null)
            {
                this.SessionConfig = new AmazonQLDBSessionConfig();
            }

            this.SessionConfig.MaxErrorRetry = 0;
            this.sessionClient = this.Credentials == null ? new AmazonQLDBSessionClient(this.SessionConfig)
                : new AmazonQLDBSessionClient(this.Credentials, this.SessionConfig);
            this.sessionClient.BeforeRequestEvent += SetUserAgent;

            ValidationUtils.AssertStringNotEmpty(this.LedgerName, "ledgerName");

            if (this.maxConcurrentTransactions == 0)
            {
                this.maxConcurrentTransactions = this.SessionConfig.GetType().GetProperty("MaxConnectionsPerServer") == null ?
                                                 int.MaxValue : this.GetMaxConn();
            }

            return(new QldbDriver(
                       new SessionPool(
                           () => Session.StartSession(this.LedgerName, this.sessionClient, this.Logger),
                           CreateDefaultRetryHandler(this.logRetries ? this.Logger : null),
                           this.maxConcurrentTransactions,
                           this.Logger)));
        }
#pragma warning restore SA1600 // Elements should be documented

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseQldbDriver"/> class.
        /// </summary>
        ///
        /// <param name="ledgerName">The ledger to create sessions to.</param>
        /// <param name="sessionClient">QLDB session client.</param>
        /// <param name="retryLimit">The amount of retries sessions created by this driver will attempt upon encountering a non-fatal error.</param>
        /// <param name="logger">Logger to be used by this.</param>
        internal BaseQldbDriver(string ledgerName, AmazonQLDBSessionClient sessionClient, int retryLimit, ILogger logger)
        {
            this.ledgerName    = ledgerName;
            this.sessionClient = sessionClient;
            this.retryLimit    = retryLimit;
            this.logger        = logger;
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Session"/> class to a specific ledger.
 /// </summary>
 ///
 /// <param name="ledgerName">The name of the ledger to create a session to.</param>
 /// <param name="sessionClient">The low-level session used for communication with QLDB.</param>
 /// <param name="sessionToken">The unique identifying token for this session to QLDB.</param>
 /// <param name="sessionId">The initial request ID for this session to QLDB.</param>
 /// <param name="logger">The logger to inject any logging framework.</param>
 internal Session(
     string ledgerName,
     AmazonQLDBSessionClient sessionClient,
     string sessionToken,
     string sessionId,
     ILogger logger)
 {
     this.LedgerName    = ledgerName;
     this.SessionClient = sessionClient;
     this.sessionToken  = sessionToken;
     this.SessionId     = sessionId;
     this.logger        = logger;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PooledQldbDriver"/> class.
        /// </summary>
        ///
        /// <param name="ledgerName">The ledger to create sessions to.</param>
        /// <param name="sessionClient">QLDB session client.</param>
        /// <param name="retryLimit">The amount of retries sessions created by this driver will attempt upon encountering a non-fatal error.</param>
        /// <param name="poolLimit">The maximum number of sessions that can be created from the pool at any one time.</param>
        /// <param name="timeout">The maximum amount of time to wait, in milliseconds.</param>
        /// <param name="logger">Logger to be used by this.</param>
        internal PooledQldbDriver(
            string ledgerName,
            AmazonQLDBSessionClient sessionClient,
            int retryLimit,
            int poolLimit,
            int timeout,
            ILogger logger)
            : base(ledgerName, sessionClient, retryLimit, logger)
        {
            this.timeout = timeout;

            this.poolPermits = new SemaphoreSlim(poolLimit, poolLimit);
            this.sessionPool = new BlockingCollection <QldbSession>(poolLimit);
        }
Esempio n. 5
0
        protected IAmazonQLDBSession CreateClient(AWSCredentials credentials, RegionEndpoint region)
        {
            var config = new AmazonQLDBSessionConfig {
                RegionEndpoint = region
            };

            Amazon.PowerShell.Utils.Common.PopulateConfig(this, config);
            this.CustomizeClientConfig(config);
            var client = new AmazonQLDBSessionClient(credentials, config);

            client.BeforeRequestEvent += RequestEventHandler;
            client.AfterResponseEvent += ResponseEventHandler;
            return(client);
        }
        /// <summary>
        /// Build a driver instance using the current configuration set with the builder.
        /// </summary>
        ///
        /// <returns>A newly created driver.</returns>
        public TDriver Build()
        {
            if (this.SessionConfig == null)
            {
                this.SessionConfig = new AmazonQLDBSessionConfig();
            }

            this.SessionConfig.MaxErrorRetry = 0;
            this.sessionClient = this.Credentials == null ? new AmazonQLDBSessionClient(this.SessionConfig)
                : new AmazonQLDBSessionClient(this.Credentials, this.SessionConfig);
            this.sessionClient.BeforeRequestEvent += SetUserAgent;

            ValidationUtils.AssertStringNotEmpty(this.LedgerName, "ledgerName");
            return(this.ConstructDriver());
        }
        /// <summary>
        /// Set defaults and verify the current configuration set with the builder.
        /// </summary>
        private protected void PrepareBuild()
        {
            this.SessionConfig ??= new AmazonQLDBSessionConfig();

            // Set SDK retry to 0 in order to let driver handle retry logic.
            this.SessionConfig.MaxErrorRetry = 0;
            this.sessionClient = this.Credentials == null ? new AmazonQLDBSessionClient(this.SessionConfig)
                : new AmazonQLDBSessionClient(this.Credentials, this.SessionConfig);
            this.sessionClient.BeforeRequestEvent += this.SetUserAgent;

            ValidationUtils.AssertStringNotEmpty(this.LedgerName, "ledgerName");

            if (this.maxConcurrentTransactions == 0)
            {
                this.maxConcurrentTransactions = this.SessionConfig.GetType().GetProperty("MaxConnectionsPerServer") ==
                                                 null ? int.MaxValue : this.GetMaxConn();
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Factory method for constructing a new Session, creating a new session to QLDB on construction.
        /// </summary>
        ///
        /// <param name="ledgerName">The name of the ledger to create a session to.</param>
        /// <param name="sessionClient">The low-level session used for communication with QLDB.</param>
        /// <param name="logger">The logger to inject any logging framework.</param>
        ///
        /// <returns>A newly created <see cref="Session"/>.</returns>
        internal static Session StartSession(string ledgerName, AmazonQLDBSessionClient sessionClient, ILogger logger)
        {
            var startSessionRequest = new StartSessionRequest
            {
                LedgerName = ledgerName,
            };
            var request = new SendCommandRequest
            {
                StartSession = startSessionRequest,
            };

            logger.LogDebug("Sending start session request: {}", request);
            var response = sessionClient.SendCommandAsync(request).GetAwaiter().GetResult();

            return(new Session(
                       ledgerName,
                       sessionClient,
                       response.StartSession.SessionToken,
                       response.ResponseMetadata.RequestId,
                       logger));
        }
        /// <summary>
        /// Factory method for constructing a new Session, creating a new session to QLDB on construction.
        /// </summary>
        ///
        /// <param name="ledgerName">The name of the ledger to create a session to.</param>
        /// <param name="sessionClient">The low-level session used for communication with QLDB.</param>
        /// <param name="logger">The logger to inject any logging framework.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        ///
        /// <returns>A newly created <see cref="Session"/>.</returns>
        internal static async Task <Session> StartSession(string ledgerName, AmazonQLDBSessionClient sessionClient, ILogger logger, CancellationToken cancellationToken = default)
        {
            var startSessionRequest = new StartSessionRequest
            {
                LedgerName = ledgerName,
            };
            var request = new SendCommandRequest
            {
                StartSession = startSessionRequest,
            };

            logger.LogDebug("Sending start session request: {}", request);
            var response = await sessionClient.SendCommandAsync(request, cancellationToken);

            return(new Session(
                       ledgerName,
                       sessionClient,
                       response.StartSession.SessionToken,
                       response.ResponseMetadata.RequestId,
                       logger));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="QldbDriver"/> class.
 /// </summary>
 ///
 /// <param name="ledgerName">The ledger to create sessions to.</param>
 /// <param name="sessionClient">QLDB session client.</param>
 /// <param name="retryLimit">The amount of retries sessions created by this driver will attempt upon encountering a non-fatal error.</param>
 /// <param name="logger">Logger to be used by this.</param>
 internal QldbDriver(string ledgerName, AmazonQLDBSessionClient sessionClient, int retryLimit, ILogger logger)
     : base(ledgerName, sessionClient, retryLimit, logger)
 {
 }