Example #1
0
        /// <inheritdoc />
        public async Task ReprepareOnAllNodesWithExistingConnections(
            IInternalSession session, InternalPrepareRequest request, PrepareResult prepareResult)
        {
            var pools        = session.GetPools();
            var hosts        = session.InternalCluster.AllHosts();
            var poolsByHosts = pools.Join(
                hosts, po => po.Key,
                h => h.Address,
                (pair, host) => new { host, pair.Value }).ToDictionary(k => k.host, k => k.Value);

            if (poolsByHosts.Count == 0)
            {
                PrepareHandler.Logger.Warning("Could not prepare query on all hosts because there are no connection pools.");
                return;
            }

            using (var semaphore = new SemaphoreSlim(64, 64))
            {
                var tasks = new List <Task>(poolsByHosts.Count);
                foreach (var poolKvp in poolsByHosts)
                {
                    if (poolKvp.Key.Address.Equals(prepareResult.HostAddress))
                    {
                        continue;
                    }

                    if (prepareResult.TriedHosts.ContainsKey(poolKvp.Key.Address))
                    {
                        PrepareHandler.Logger.Warning(
                            $"An error occured while attempting to prepare query on {{0}}:{Environment.NewLine}{{1}}",
                            poolKvp.Key.Address,
                            prepareResult.TriedHosts[poolKvp.Key.Address]);
                        continue;
                    }

                    await semaphore.WaitAsync().ConfigureAwait(false);

                    tasks.Add(ReprepareOnSingleNodeAsync(poolKvp, prepareResult.PreparedStatement, request, semaphore, false));
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
        }
Example #2
0
        /// <summary>
        /// Sends the prepare request to all nodes have have an existing open connection. Will not attempt to send the request to hosts that were tried before (successfully or not).
        /// </summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <param name="prepareResult">The result of the prepare request on the first node.</param>
        /// <returns></returns>
        private async Task SendRequestToAllNodesWithExistingConnections(IInternalSession session, InternalPrepareRequest request, PrepareResult prepareResult)
        {
            var pools = session.GetPools().ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            if (pools.Count == 0)
            {
                PrepareHandler.Logger.Warning("Could not prepare query on all hosts because there are no connection pools.");
                return;
            }

            using (var semaphore = new SemaphoreSlim(64, 64))
            {
                var tasks = new List <Task>(pools.Count);
                foreach (var poolKvp in pools)
                {
                    if (poolKvp.Key.Equals(prepareResult.HostAddress))
                    {
                        continue;
                    }

                    if (prepareResult.TriedHosts.ContainsKey(poolKvp.Key))
                    {
                        PrepareHandler.Logger.Warning(
                            $"An error occured while attempting to prepare query on {{0}}:{Environment.NewLine}{{1}}",
                            poolKvp.Key,
                            prepareResult.TriedHosts[poolKvp.Key]);
                        continue;
                    }

                    await semaphore.WaitAsync().ConfigureAwait(false);

                    tasks.Add(SendSingleRequest(poolKvp, request, semaphore));
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
        }