Esempio n. 1
0
        public void Read(BinaryReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            Config = ClusterConfig.Read(reader);
        }
        public async Task GetClusterList()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            int readyClusters = 10;
            int deletingCluster = 4;
            int newClusters = 2;
            int removeClusters = 1;

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await this.AddClusters(tx, dictionary, readyClusters, ClusterStatus.Ready);
                await this.AddClusters(tx, dictionary, deletingCluster, ClusterStatus.Deleting);
                await this.AddClusters(tx, dictionary, newClusters, ClusterStatus.New);
                await this.AddClusters(tx, dictionary, removeClusters, ClusterStatus.Remove);
                await tx.CommitAsync();
            }

            IEnumerable<ClusterView> actual = await target.GetClusterListAsync();

            Assert.AreEqual(readyClusters, actual.Count());
        }
Esempio n. 3
0
 // todo : Schedule class
 public void AssignTo(Estimation estimation)
 {
     if (estimation == null)
     {
         this.Time.Estimated = null;
         _assignedTo = new ClusterConfig(null, null);
         _state = TaskState.NotAssigned;
     }
     else
         AssignTo(estimation.ClusterName, estimation.Cores, estimation.Time);
 }
Esempio n. 4
0
        public void Read(BinaryReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            byte stepMode = reader.ReadByte();

            if (!Enum.IsDefined(typeof(StepMode), stepMode))
                throw new InvalidDataException("Unknown step mode: " + stepMode);

            Config = ClusterConfig.GetCompatibility((StepMode)stepMode,
                reader.ReadInt32(),
                reader.ReadByte() > 0);
        }
        public async Task BalanceClusters()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            await target.BalanceClustersAsync(config.MinimumClusterCount);

            ConditionalResult<IReliableDictionary<int, Cluster>> result =
                await stateManager.TryGetAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(config.MinimumClusterCount, await result.Value.GetCountAsync());
            Assert.IsTrue(result.Value.All(x => x.Value.Status == ClusterStatus.New));
        }
        public async Task TestBalanceClusters()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, stateManager)
            {
                Config = config
            };

            await target.BalanceClustersAsync(config.MinimumClusterCount);

            var result = await stateManager.TryGetAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Assert.IsTrue(result.HasValue);
            Assert.AreEqual(config.MinimumClusterCount, await result.Value.GetCountAsync());
            Assert.IsTrue(result.Value.All(x => x.Value.Status == ClusterStatus.New));
        }
Esempio n. 7
0
    public async Task Accepts_RequestVersion_Null()
    {
        var services  = CreateServices();
        var validator = services.GetRequiredService <IConfigValidator>();

        var cluster = new ClusterConfig
        {
            ClusterId   = "cluster1",
            HttpRequest = new ForwarderRequestConfig
            {
                Version = null,
            }
        };

        var errors = await validator.ValidateClusterAsync(cluster);

        Assert.Empty(errors);
    }
Esempio n. 8
0
        public virtual Task ConfigureAsync(ClusterConfig clusterConfig, PoolConfig poolConfig)
        {
            Contract.RequiresNonNull(poolConfig, nameof(poolConfig));

            this.poolConfig    = poolConfig;
            this.clusterConfig = clusterConfig;

            coinProperties = BitcoinProperties.GetCoinProperties(poolConfig.Coin.Type, poolConfig.Coin.Algorithm);

            logger = LogUtil.GetPoolScopedLogger(typeof(BitcoinPayoutHandler), poolConfig);

            var jsonSerializerSettings = ctx.Resolve <JsonSerializerSettings>();

            daemon = new DaemonClient(jsonSerializerSettings);
            daemon.Configure(poolConfig.Daemons);

            return(Task.FromResult(true));
        }
Esempio n. 9
0
        public void Configure(ClusterConfig clusterConfig, PoolConfig poolConfig)
        {
            this.poolConfig    = poolConfig;
            this.clusterConfig = clusterConfig;
            extraConfig        = poolConfig.PaymentProcessing.Extra.SafeExtensionDataAs <EthereumPoolPaymentProcessingConfigExtra>();

            logger = LogUtil.GetPoolScopedLogger(typeof(EthereumPayoutHandler), poolConfig);

            // configure standard daemon
            var jsonSerializerSettings = ctx.Resolve <JsonSerializerSettings>();

            var daemonEndpoints = poolConfig.Daemons
                                  .Where(x => string.IsNullOrEmpty(x.Category))
                                  .ToArray();

            daemon = new DaemonClient(jsonSerializerSettings);
            daemon.Configure(daemonEndpoints);
        }
Esempio n. 10
0
    public async Task SetAvailableDestinationsPolicy_Works(string policy)
    {
        var services  = CreateServices();
        var validator = services.GetRequiredService <IConfigValidator>();

        var cluster = new ClusterConfig
        {
            ClusterId   = "cluster1",
            HealthCheck = new HealthCheckConfig
            {
                AvailableDestinationsPolicy = policy
            }
        };

        var errors = await validator.ValidateClusterAsync(cluster);

        Assert.Empty(errors);
    }
Esempio n. 11
0
    public async Task HttpClient_HeaderEncoding_Valid()
    {
        var services  = CreateServices();
        var validator = services.GetRequiredService <IConfigValidator>();

        var cluster = new ClusterConfig
        {
            ClusterId  = "cluster1",
            HttpClient = new HttpClientConfig
            {
                RequestHeaderEncoding = "utf-8"
            }
        };

        var errors = await validator.ValidateClusterAsync(cluster);

        Assert.Equal(0, errors.Count);
    }
Esempio n. 12
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="CqlConnection" /> class.
        /// </summary>
        /// <param name="connectionString"> The connection string. </param>
        public CqlConnection(string connectionString)
        {
            //get the cluster config, or add one if none exists
            ClusterConfig config = Configs.GetOrAdd(connectionString, connString =>
            {
                //create new config
                var cc = new ClusterConfig(connString);

                //get if a similar already exists, or add it otherwise
                return(Configs.GetOrAdd(cc.ToString(), cc));
            });

            //fetch the cluster, or create one
            Cluster cluster = Clusters.GetOrAdd(config, conf => new Cluster(conf));

            //set the connection provider to the cluster
            _cluster = cluster;
        }
Esempio n. 13
0
    private void ValidateHealthChecks(IList <Exception> errors, ClusterConfig cluster)
    {
        var availableDestinationsPolicy = cluster.HealthCheck?.AvailableDestinationsPolicy;

        if (string.IsNullOrEmpty(availableDestinationsPolicy))
        {
            // The default.
            availableDestinationsPolicy = HealthCheckConstants.AvailableDestinations.HealthyAndUnknown;
        }

        if (!_availableDestinationsPolicies.ContainsKey(availableDestinationsPolicy))
        {
            errors.Add(new ArgumentException($"No matching {nameof(IAvailableDestinationsPolicy)} found for the available destinations policy '{availableDestinationsPolicy}' set on the cluster.'{cluster.ClusterId}'."));
        }

        ValidateActiveHealthCheck(errors, cluster);
        ValidatePassiveHealthCheck(errors, cluster);
    }
Esempio n. 14
0
        public override async Task ConfigureAsync(ClusterConfig clusterConfig, PoolConfig poolConfig, CancellationToken ct)
        {
            await base.ConfigureAsync(clusterConfig, poolConfig, ct);

            poolExtraConfig = poolConfig.Extra.SafeExtensionDataAs <EquihashPoolConfigExtra>();

            // detect network
            var blockchainInfoResponse = await rpcClient.ExecuteAsync <BlockchainInfo>(logger, BitcoinCommands.GetBlockchainInfo, ct);

            network = Network.GetNetwork(blockchainInfoResponse.Response.Chain.ToLower());

            chainConfig = poolConfig.Template.As <EquihashCoinTemplate>().GetNetwork(network.ChainName);

            // detect z_shieldcoinbase support
            var response = await rpcClient.ExecuteAsync <JObject>(logger, EquihashCommands.ZShieldCoinbase, ct);

            supportsNativeShielding = response.Error.Code != (int)BitcoinRPCErrorCode.RPC_METHOD_NOT_FOUND;
        }
    public virtual Task ConfigureAsync(ClusterConfig cc, PoolConfig pc, CancellationToken ct)
    {
        Contract.RequiresNonNull(pc, nameof(pc));

        poolConfig    = pc;
        clusterConfig = cc;

        extraPoolConfig = pc.Extra.SafeExtensionDataAs <BitcoinDaemonEndpointConfigExtra>();
        extraPoolPaymentProcessingConfig = pc.PaymentProcessing.Extra.SafeExtensionDataAs <BitcoinPoolPaymentProcessingConfigExtra>();

        logger = LogUtil.GetPoolScopedLogger(typeof(BitcoinPayoutHandler), pc);

        var jsonSerializerSettings = ctx.Resolve <JsonSerializerSettings>();

        rpcClient = new RpcClient(pc.Daemons.First(), jsonSerializerSettings, messageBus, pc.Id);

        return(Task.FromResult(true));
    }
Esempio n. 16
0
        public virtual async Task ConfigureAsync(ClusterConfig clusterConfig, PoolConfig poolConfig, CancellationToken ct)
        {
            Contract.RequiresNonNull(poolConfig, nameof(poolConfig));

            logger = LogUtil.GetPoolScopedLogger(typeof(ErgoPayoutHandler), poolConfig);

            this.poolConfig    = poolConfig;
            this.clusterConfig = clusterConfig;

            extraPoolPaymentProcessingConfig = poolConfig.PaymentProcessing.Extra.SafeExtensionDataAs <ErgoPaymentProcessingConfigExtra>();

            ergoClient = ErgoClientFactory.CreateClient(poolConfig, clusterConfig, null);

            // detect chain
            var info = await ergoClient.GetNodeInfoAsync(ct);

            network = ErgoConstants.RegexChain.Match(info.Name).Groups[1].Value.ToLower();
        }
Esempio n. 17
0
 /// <summary>
 /// Converts the <see cref="sourceValue" /> parameter to the <see cref="destinationType" /> parameter using <see cref="formatProvider"
 /// /> and <see cref="ignoreCase" />
 /// </summary>
 /// <param name="sourceValue">the value to convert into an instance of <see cref="ClusterConfig" />.</param>
 /// <returns>
 /// an instance of <see cref="ClusterConfig" />, or <c>null</c> if there is no suitable conversion.
 /// </returns>
 public static object ConvertFrom(dynamic sourceValue)
 {
     if (null == sourceValue)
     {
         return(null);
     }
     try
     {
         ClusterConfig.FromJsonString(typeof(string) == sourceValue.GetType() ? sourceValue : sourceValue.ToJsonString());
     }
     catch
     {
         // Unable to use JSON pattern
     }
     try
     {
         return(new ClusterConfig
         {
             AuthorizedPublicKeyList = sourceValue.AuthorizedPublicKeyList,
             Build = BuildInfoTypeConverter.ConvertFrom(sourceValue.Build),
             CaCertificateList = sourceValue.CaCertificateList,
             CertificationSigningInfo = CertificationSigningInfoTypeConverter.ConvertFrom(sourceValue.CertificationSigningInfo),
             ClientAuth = ClientAuthTypeConverter.ConvertFrom(sourceValue.ClientAuth),
             ClusterArch = sourceValue.ClusterArch,
             EnabledFeatureList = sourceValue.EnabledFeatureList,
             EncryptionStatus = sourceValue.EncryptionStatus,
             ExternalConfigurations = ExternalConfigurationsTypeConverter.ConvertFrom(sourceValue.ExternalConfigurations),
             GpuDriverVersion = sourceValue.GpuDriverVersion,
             IsAvailable = sourceValue.IsAvailable,
             ManagementServerList = sourceValue.ManagementServerList,
             OperationMode = sourceValue.OperationMode,
             RedundancyFactor = sourceValue.RedundancyFactor,
             ServiceList = sourceValue.ServiceList,
             SoftwareMap = sourceValue.SoftwareMap,
             SslKey = SslKeyTypeConverter.ConvertFrom(sourceValue.SslKey),
             SupportedInformationVerbosity = sourceValue.SupportedInformationVerbosity,
             Timezone = sourceValue.Timezone,
         });
     }
     catch
     {
     }
     return(null);
 }
Esempio n. 18
0
        public virtual void Init(TBlockTemplate blockTemplate, string jobId,
                                 PoolConfig poolConfig, ClusterConfig clusterConfig, IMasterClock clock,
                                 IDestination poolAddressDestination, BitcoinNetworkType networkType,
                                 BitcoinExtraNonceProvider extraNonceProvider, bool isPoS, double shareMultiplier,
                                 IHashAlgorithm coinbaseHasher, IHashAlgorithm headerHasher, IHashAlgorithm blockHasher)
        {
            Contract.RequiresNonNull(blockTemplate, nameof(blockTemplate));
            Contract.RequiresNonNull(poolConfig, nameof(poolConfig));
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));
            Contract.RequiresNonNull(clock, nameof(clock));
            Contract.RequiresNonNull(poolAddressDestination, nameof(poolAddressDestination));
            Contract.RequiresNonNull(extraNonceProvider, nameof(extraNonceProvider));
            Contract.RequiresNonNull(coinbaseHasher, nameof(coinbaseHasher));
            Contract.RequiresNonNull(headerHasher, nameof(headerHasher));
            Contract.RequiresNonNull(blockHasher, nameof(blockHasher));
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(jobId), $"{nameof(jobId)} must not be empty");

            this.poolConfig             = poolConfig;
            this.clusterConfig          = clusterConfig;
            this.clock                  = clock;
            this.poolAddressDestination = poolAddressDestination;
            this.networkType            = networkType;
            BlockTemplate               = blockTemplate;
            JobId      = jobId;
            Difficulty = new Target(new NBitcoin.BouncyCastle.Math.BigInteger(BlockTemplate.Target, 16)).Difficulty;

            extraNoncePlaceHolderLength = extraNonceProvider.PlaceHolder.Length;
            this.isPoS           = isPoS;
            this.shareMultiplier = shareMultiplier;

            this.coinbaseHasher = coinbaseHasher;
            this.headerHasher   = headerHasher;
            this.blockHasher    = blockHasher;

            blockTargetValue = BigInteger.Parse(BlockTemplate.Target, NumberStyles.HexNumber);

            previousBlockHashReversedHex = BlockTemplate.PreviousBlockhash
                                           .HexToByteArray()
                                           .ReverseByteOrder()
                                           .ToHexString();

            BuildMerkleBranches();
            BuildCoinbase();
        }
Esempio n. 19
0
        public async Task BalancedStrategyManyRequests()
        {
            using (ShimsContext.Create())
            {
                //create cluster
                var config = new ClusterConfig {
                    NewConnectionTreshold = 20
                };

                var cluster = new Cluster(config);

                //create nodes
                var n     = new Node(IPAddress.Parse("127.0.0.1"), cluster);
                var n2    = new Node(IPAddress.Parse("127.0.0.2"), cluster);
                var n3    = new Node(IPAddress.Parse("127.0.0.3"), cluster);
                var n4    = new Node(IPAddress.Parse("127.0.0.4"), cluster);
                var nodes = new Ring(new List <Node> {
                    n, n2, n3, n4
                }, "RandomPartitioner");

                ShimAllConnections();


                var logger = cluster.LoggerManager.GetLogger("BalancedStrategyManyRequestsTest");

                IConnectionStrategy strategy = new BalancedConnectionStrategy(nodes, config);

                const int nr = 80;

                for (int i = 0; i < nr; i++)
                {
                    Connection connection;

                    using (logger.ThreadBinding())
                        connection = strategy.GetOrCreateConnection(PartitionKey.None);

                    await connection.SendRequestAsync(new QueryFrame("", CqlConsistency.Any), logger, 10);
                }

                Assert.AreEqual(nodes.Sum(nd => nd.ConnectionCount), 8);
                Assert.IsTrue(nodes.All(nd => nd.ConnectionCount == 2));
                Assert.IsTrue(nodes.SelectMany(nd => nd).All(c => c.Load == (80 * 10) / 4 / 2));
            }
        }
Esempio n. 20
0
        public void SelectServer_ReturnsServerFroMappingTable()
        {
            var first = new ServerConfig
            {
                Ip   = "192.168.0.1",
                Port = 5000
            };

            ClusterConfig config = new ClusterConfig
            {
                Servers = new[] { first }
            };


            ServerProvider provider       = new ServerProvider(config);
            ServerConfig   selectedServer = provider.SelectServer(GetHashCode());

            Assert.Equal(selectedServer, first);
        }
Esempio n. 21
0
        // Note this performs all validation steps without short circuiting in order to report all possible errors.
        public ValueTask <IList <Exception> > ValidateClusterAsync(ClusterConfig cluster)
        {
            _ = cluster ?? throw new ArgumentNullException(nameof(cluster));
            var errors = new List <Exception>();

            if (string.IsNullOrEmpty(cluster.ClusterId))
            {
                errors.Add(new ArgumentException("Missing Cluster Id."));
            }

            errors.AddRange(_transformBuilder.ValidateCluster(cluster));
            ValidateLoadBalancing(errors, cluster);
            ValidateSessionAffinity(errors, cluster);
            ValidateProxyHttpClient(errors, cluster);
            ValidateProxyHttpRequest(errors, cluster);
            ValidateHealthChecks(errors, cluster);

            return(new ValueTask <IList <Exception> >(errors));
        }
Esempio n. 22
0
        public NotificationService(
            ClusterConfig clusterConfig,
            IEnumerable <Meta <INotificationSender, NotificationSenderMetadataAttribute> > notificationSenders)
        {
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));
            Contract.RequiresNonNull(notificationSenders, nameof(notificationSenders));

            this.clusterConfig       = clusterConfig;
            this.notificationSenders = notificationSenders;

            adminEmail = clusterConfig.Notifications?.Admin?.EmailAddress;
            adminPhone = null;

            queueSub = queue.GetConsumingEnumerable()
                       .ToObservable(TaskPoolScheduler.Default)
                       .Select(notification => Observable.FromAsync(() => SendNotificationAsync(notification)))
                       .Concat()
                       .Subscribe();
        }
Esempio n. 23
0
        /// <summary>
        /// Adds BFF access token metadata to a cluster configuration
        /// </summary>
        /// <param name="config"></param>
        /// <param name="tokenType"></param>
        /// <returns></returns>
        public static ClusterConfig WithAccessToken(this ClusterConfig config, TokenType tokenType)
        {
            Dictionary <string, string> metadata;

            if (config.Metadata != null)
            {
                metadata = new Dictionary <string, string>(config.Metadata);
            }
            else
            {
                metadata = new();
            }

            metadata.TryAdd(Constants.Yarp.TokenTypeMetadata, tokenType.ToString());

            return(config with {
                Metadata = metadata
            });
        }
Esempio n. 24
0
        public virtual Task ConfigureAsync(ClusterConfig clusterConfig, PoolConfig poolConfig)
        {
            Contract.RequiresNonNull(poolConfig, nameof(poolConfig));

            this.poolConfig    = poolConfig;
            this.clusterConfig = clusterConfig;

            extraPoolConfig = poolConfig.Extra.SafeExtensionDataAs <BitcoinDaemonEndpointConfigExtra>();
            extraPoolPaymentProcessingConfig = poolConfig.PaymentProcessing.Extra.SafeExtensionDataAs <BitcoinPoolPaymentProcessingConfigExtra>();

            logger = LogUtil.GetPoolScopedLogger(typeof(BitcoinPayoutHandler), poolConfig);

            var jsonSerializerSettings = ctx.Resolve <JsonSerializerSettings>();

            daemon = new DaemonClient(jsonSerializerSettings, messageBus, clusterConfig.ClusterName ?? poolConfig.PoolName, poolConfig.Id);
            daemon.Configure(poolConfig.Daemons);

            return(Task.FromResult(true));
        }
        public HealthProbeWorkerTests()
        {
            // Set up all the parameter needed for healthProberWorker.
            Provide <IMonotonicTimer, MonotonicTimer>();
            Provide <ILogger, Logger <HealthProbeWorker> >();
            Mock <IProxyHttpClientFactoryFactory>()
            .Setup(f => f.CreateFactory())
            .Returns(new Mock <IProxyHttpClientFactory>().Object);

            // Set up clusters. We are going to fake multiple service for us to probe.
            _clusterManager = Provide <IClusterManager, ClusterManager>();
            _clusterConfig  = new ClusterConfig(
                healthCheckOptions: new ClusterConfig.ClusterHealthCheckOptions(
                    enabled: true,
                    interval: TimeSpan.FromSeconds(1),
                    timeout: TimeSpan.FromSeconds(1),
                    port: 8000,
                    path: "/example"),
                loadBalancingOptions: default,
    public async Task HttpClient_HeaderEncoding_Invalid()
    {
        var services  = CreateServices();
        var validator = services.GetRequiredService <IConfigValidator>();

        var cluster = new ClusterConfig
        {
            ClusterId  = "cluster1",
            HttpClient = new HttpClientConfig
            {
                RequestHeaderEncoding = "base64"
            }
        };

        var errors = await validator.ValidateClusterAsync(cluster);

        Assert.Equal(1, errors.Count);
        Assert.Equal("Invalid header encoding 'base64'.", errors[0].Message);
    }
    public async Task EnableSessionAffinity_Works()
    {
        var services  = CreateServices();
        var validator = services.GetRequiredService <IConfigValidator>();

        var cluster = new ClusterConfig
        {
            ClusterId       = "cluster1",
            SessionAffinity = new SessionAffinityConfig
            {
                Enabled         = true,
                AffinityKeyName = "SomeKey"
            }
        };

        var errors = await validator.ValidateClusterAsync(cluster);

        Assert.Empty(errors);
    }
Esempio n. 28
0
        public void ConfigureAsync(ClusterConfig clusterConfig, PoolConfig poolConfig)
        {
            this.poolConfig    = poolConfig;
            this.clusterConfig = clusterConfig;
            extraConfig        = poolConfig.PaymentProcessing.Extra.SafeExtensionDataAs <AionPoolPaymentExtraConfig>();

            logger = LogUtil.GetPoolScopedLogger(typeof(AionPayoutHandler), poolConfig);

            // configure standard daemon
            var jsonSerializerSettings = ctx.Resolve <JsonSerializerSettings>();

            var daemonEndpoints = poolConfig.Daemons
                                  .Where(x => string.IsNullOrEmpty(x.Category))
                                  .ToArray();

            daemon = new DaemonClient(jsonSerializerSettings, messageBus, clusterConfig.ClusterName ?? poolConfig.PoolName, poolConfig.Id);
            daemon.Configure(daemonEndpoints);
            rewardsCalculator = new AionRewardsCalculator();
        }
Esempio n. 29
0
        public ICluster GetCluster(ClusterConfig clusterConfig)
        {
            clusterConfig.CheckArgumentNotNull("clusterConfig");
            clusterConfig.Endpoints.CheckArgumentNotNull("clusterConfig.Endpoints");

            TransportConfig  transportConfig = clusterConfig.Transport ?? new TransportConfig();
            IRecoveryService recoveryService = GetRecoveryService(transportConfig.Recoverable);
            KeyspaceConfig   keyspaceConfig  = clusterConfig.DefaultKeyspace ?? new KeyspaceConfig();

            // create endpoints
            IEndpointSnitch snitch = ServiceActivator <Factory> .Create <IEndpointSnitch>(clusterConfig.Endpoints.Snitch, _logger);

            IEnumerable <IPAddress> endpoints = clusterConfig.Endpoints.Servers.Select(Network.Find).Where(x => null != x).ToArray();

            if (!endpoints.Any())
            {
                throw new ArgumentException("Expecting at least one valid endpoint");
            }

            // create required services
            IEndpointStrategy endpointsManager = ServiceActivator <EndpointStrategy.Factory> .Create <IEndpointStrategy>(clusterConfig.Endpoints.Strategy,
                                                                                                                         endpoints, snitch,
                                                                                                                         _logger, clusterConfig.Endpoints);

            IConnectionFactory connectionFactory = ServiceActivator <Transport.Factory> .Create <IConnectionFactory>(transportConfig.Type, transportConfig, keyspaceConfig, _logger,
                                                                                                                     _instrumentation);

            IPartitioner partitioner = ServiceActivator <Partitioner.Factory> .Create <IPartitioner>(clusterConfig.Partitioner);

            // create the cluster now
            ICluster cluster = ServiceActivator <Cluster.Factory> .Create <ICluster>(clusterConfig.Type, endpointsManager, _logger, connectionFactory,
                                                                                     recoveryService, partitioner, clusterConfig);

            IDiscoveryService discoveryService = ServiceActivator <Discovery.Factory> .Create <IDiscoveryService>(clusterConfig.Endpoints.Discovery.Type,
                                                                                                                  clusterConfig.Endpoints.Discovery,
                                                                                                                  _logger,
                                                                                                                  cluster);

            discoveryService.OnTopologyUpdate += endpointsManager.Update;
            cluster.OnClosed += discoveryService.SafeDispose;

            return(cluster);
        }
Esempio n. 30
0
    public void GetAvailableDestinations_HealthChecksDisabled_ReturnAll(HealthCheckConfig config)
    {
        var cluster = new ClusterConfig()
        {
            ClusterId = "cluster1", HealthCheck = config
        };
        var allDestinations = new[]
        {
            new DestinationState("d1")
            {
                Health = { Active = DestinationHealth.Healthy }
            },
            new DestinationState("d2")
            {
                Health = { Active = DestinationHealth.Unhealthy, Passive = DestinationHealth.Healthy }
            },
            new DestinationState("d3")
            {
                Health = { Passive = DestinationHealth.Healthy }
            },
            new DestinationState("d4"),
            new DestinationState("d5")
            {
                Health = { Active = DestinationHealth.Healthy, Passive = DestinationHealth.Unhealthy }
            },
            new DestinationState("d6")
            {
                Health = { Active = DestinationHealth.Unhealthy, Passive = DestinationHealth.Unhealthy }
            }
        };
        var policy = new HealthyAndUnknownDestinationsPolicy();

        var availableDestinations = policy.GetAvailalableDestinations(cluster, allDestinations);

        Assert.Equal(6, availableDestinations.Count);
        Assert.Same(allDestinations[0], availableDestinations[0]);
        Assert.Same(allDestinations[1], availableDestinations[1]);
        Assert.Same(allDestinations[2], availableDestinations[2]);
        Assert.Same(allDestinations[3], availableDestinations[3]);
        Assert.Same(allDestinations[4], availableDestinations[4]);
        Assert.Same(allDestinations[5], availableDestinations[5]);
    }
Esempio n. 31
0
        public static (ClusterConfig, GrpcCoreRemoteConfig) CreateClusterConfig(IClusterSettings clusterSettings, IClusterProvider clusterProvider, IIdentityLookup identityLookup, IDescriptorProvider descriptorProvider, ILogger _logger)
        {
            //var portStr = Environment.GetEnvironmentVariable("PROTOPORT") ?? $"{RemoteConfigBase.AnyFreePort}";

            var clusterName = clusterSettings.ClusterName;

            var hostip         = Environment.GetEnvironmentVariable("PROTOHOST");
            var host           = clusterSettings.ClusterHost;
            var advertisedHost = clusterSettings.ClusterHost;
            var port           = clusterSettings.ClusterPort;

            if ("protohost".Equals(host))
            {
                host           = hostip;
                advertisedHost = hostip;
                _logger.LogDebug($"Using PROTOHOST");
            }
            _logger.LogDebug($"BindTo to {host} port {port}");
            _logger.LogDebug($"WithAdvertisedHost to {advertisedHost}");

            FileDescriptor[] descriptors = descriptorProvider.GetDescriptors();


            // TOOD: This doesn't seem to work. Why?
            List <ChannelOption> options = new List <ChannelOption>()
            {
                new ChannelOption(ChannelOptions.MaxSendMessageLength, (100 * 1024 * 1024)),
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, (100 * 1024 * 1024))
            };


            GrpcCoreRemoteConfig remoteConfig = GrpcCoreRemoteConfig.BindTo(host, port)
                                                .WithEndpointWriterMaxRetries(0)
                                                .WithRemoteDiagnostics(true)
                                                .WithAdvertisedHost(advertisedHost)
                                                .WithProtoMessages(descriptors)
                                                .WithChannelOptions(options);

            var clusterConfig = ClusterConfig.Setup(clusterName, clusterProvider, identityLookup);

            return(clusterConfig, remoteConfig);
        }
        private ClusterInfo GetClusterInfo(string id, int destinationCount, int?failureThreshold = null)
        {
            var metadata = failureThreshold != null
                ? new Dictionary <string, string> {
                { ConsecutiveFailuresHealthPolicyOptions.ThresholdMetadataName, failureThreshold.ToString() }
            }
                : null;
            var clusterConfig = new ClusterConfig(
                new Cluster
            {
                Id          = id,
                HealthCheck = new HealthCheckOptions()
                {
                    Active = new ActiveHealthCheckOptions
                    {
                        Enabled = true,
                        Policy  = "policy",
                        Path    = "/api/health/",
                    },
                },
                Metadata = metadata,
            },
                null);
            var clusterInfo = new ClusterInfo(id);

            clusterInfo.Config = clusterConfig;
            for (var i = 0; i < destinationCount; i++)
            {
                var destinationConfig = new DestinationConfig(new Destination {
                    Address = $"https://localhost:1000{i}/{id}/", Health = $"https://localhost:2000{i}/{id}/"
                });
                var destinationId = $"destination{i}";
                clusterInfo.Destinations.GetOrAdd(destinationId, id => new DestinationInfo(id)
                {
                    Config = destinationConfig
                });
            }

            clusterInfo.ProcessDestinationChanges();

            return(clusterInfo);
        }
Esempio n. 33
0
        private void StartAdminApi(ClusterConfig clusterConfig)
        {
            var address = clusterConfig.Api?.ListenAddress != null
                ? (clusterConfig.Api.ListenAddress != "*" ? IPAddress.Parse(clusterConfig.Api.ListenAddress) : IPAddress.Any)
                : IPAddress.Parse("127.0.0.1");

            var port = clusterConfig.Api?.AdminPort ?? 4001;

            webHostAdmin = new WebHostBuilder()
                           .Configure(app =>
            {
                app.Run(HandleRequestAdmin);
            })
                           .UseKestrel(options => { options.Listen(address, port); })
                           .Build();

            webHostAdmin.Start();

            logger.Info(() => $"Admin API Online @ {address}:{port}");
        }
Esempio n. 34
0
        public NotificationService(
            ClusterConfig clusterConfig,
            JsonSerializerSettings serializerSettings)
        {
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));

            this.clusterConfig      = clusterConfig;
            this.serializerSettings = serializerSettings;

            poolConfigs = clusterConfig.Pools.ToDictionary(x => x.Id, x => x);

            adminEmail = clusterConfig.Notifications?.Admin?.EmailAddress;
            adminPhone = null;

            queueSub = queue.GetConsumingEnumerable()
                       .ToObservable(TaskPoolScheduler.Default)
                       .Select(notification => Observable.FromAsync(() => SendNotificationAsync(notification)))
                       .Concat()
                       .Subscribe();
        }
Esempio n. 35
0
        public override void Configure(PoolConfig poolConfig, ClusterConfig clusterConfig)
        {
            poolAddressBase58Prefix = LibCryptonote.DecodeAddress(poolConfig.Address);
            if (poolAddressBase58Prefix == 0)
            {
                logger.ThrowLogPoolStartupException("Unable to decode pool-address)", LogCat);
            }

            // extract standard daemon endpoints
            daemonEndpoints = poolConfig.Daemons
                              .Where(x => string.IsNullOrEmpty(x.Category))
                              .ToArray();

            // extract wallet daemon endpoints
            walletDaemonEndpoints = poolConfig.Daemons
                                    .Where(x => x.Category?.ToLower() == MoneroConstants.WalletDaemonCategory)
                                    .ToArray();

            base.Configure(poolConfig, clusterConfig);
        }
Esempio n. 36
0
        private void StartMetrics(ClusterConfig clusterConfig)
        {
            var address = clusterConfig.Api?.ListenAddress != null
                ? (clusterConfig.Api.ListenAddress != "*" ? IPAddress.Parse(clusterConfig.Api.ListenAddress) : IPAddress.Any)
                : IPAddress.Parse("127.0.0.1");

            var port = clusterConfig.Api?.MetricsPort ?? 4002;

            webHostMetrics = new WebHostBuilder()
                             .Configure(app =>
            {
                app.UseMetricServer();
            })
                             .UseKestrel(options => { options.Listen(address, port); })
                             .Build();

            webHostMetrics.Start();

            logger.Info(() => $"Prometheus Metrics Online @ {address}:{port}/metrics");
        }
        public async Task JoinClusterSuccessful()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(
                null,
                new MockMailer(),
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            int id = 5;
            string email = "*****@*****.**";
            Cluster cluster = new Cluster("test", ClusterStatus.Ready, 0, 0, "", new[] {80}, new ClusterUser[0], DateTimeOffset.UtcNow);

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.AddAsync(tx, id, cluster);
                await tx.CommitAsync();
            }

            await target.JoinClusterAsync(id, email);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                Cluster actual = (await dictionary.TryGetValueAsync(tx, id)).Value;

                Assert.AreEqual(1, actual.Users.Count(x => x.Email == email));
            }
        }
        private void RaiseConfigChanged(ClusterConfig config)
        {
            var ccc = this.ClusterConfigChanged;

            // we got a new config, notify the pool to reload itself
            if (ccc != null)
                ccc(config);

            // trigger the event so Start stops blocking
            if (this.mre != null)
                this.mre.Set();
        }
        public async Task TargetClusterCapacityDecreaseAtMinCount()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            int clusterCount = config.MinimumClusterCount + 1;
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await this.AddClusters(
                    tx,
                    dictionary,
                    clusterCount,
                    () => this.CreateCluster(
                        ClusterStatus.Ready,
                        new List<ClusterUser>(
                            Enumerable.Repeat(
                                new ClusterUser(),
                                (int) Math.Floor((double) config.MaximumUsersPerCluster*config.UserCapacityLowPercentThreshold)))));

                await tx.CommitAsync();
            }

            int expected = config.MinimumClusterCount;
            int actual = await target.GetTargetClusterCapacityAsync();

            Assert.AreEqual(expected, actual);
        }
        public async Task ProcessClustersAsyncDelete()
        {
            int key = 1;
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterConfig config = new ClusterConfig();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = (name) => Task.FromResult(ClusterOperationStatus.ClusterNotFound)
            };

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Cluster original = this.CreateCluster(ClusterStatus.Deleting);
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.SetAsync(tx, key, original);
            }

            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            await target.ProcessClustersAsync();

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                ConditionalResult<Cluster> actual = await dictionary.TryGetValueAsync(tx, key);

                Assert.IsFalse(actual.HasValue);
            }
        }
Esempio n. 41
0
		private InternalState InitBasic(ClusterConfig config, ISaslAuthenticationProvider auth)
		{
			if (log.IsInfoEnabled) log.Info("No vbucket. Server count: " + (config.nodes == null ? 0 : config.nodes.Length));

			// no vbucket config, use the node list and the ports
			var portType = this.configuration.Port;

			var tmp = config == null
					? Enumerable.Empty<IMemcachedNode>()
						: (from node in config.nodes
						   let ip = new IPEndPoint(IPAddress.Parse(node.hostname),
													(portType == BucketPortType.Proxy
														? node.ports.proxy
														: node.ports.direct))
						   where node.status == "healthy"
						   select (IMemcachedNode)(new BinaryNode(ip, this.configuration.SocketPool, auth)));

			return new InternalState
			{
				CurrentNodes = tmp.ToArray(),
				Locator = this.configuration.CreateNodeLocator() ?? new KetamaNodeLocator(),
				OpFactory = new Enyim.Caching.Memcached.Protocol.Binary.BinaryOperationFactory()
			};
		}
        public async Task JoinClusterUserAlreadyExists()
        {
            ClusterConfig config = new ClusterConfig() {MaximumUsersPerCluster = 2};
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockMailer mockMailer = new MockMailer();
            ClusterService target = new ClusterService(
                null,
                mockMailer,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            int idWithUser = 5;
            string email = "*****@*****.**";
            Cluster clusterWithUser = new Cluster(
                "test",
                ClusterStatus.Ready,
                0,
                0,
                "",
                new[] {80, 81},
                new[] {new ClusterUser(email, 80)},
                DateTimeOffset.UtcNow);

            int idWithoutUser = 6;
            Cluster clusterWithoutUser = new Cluster(
                "test2",
                ClusterStatus.Ready,
                0,
                0,
                "",
                new[] {80, 81},
                new ClusterUser[0],
                DateTimeOffset.UtcNow);


            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.AddAsync(tx, idWithUser, clusterWithUser);
                await dictionary.AddAsync(tx, idWithoutUser, clusterWithoutUser);
                await tx.CommitAsync();
            }

            try
            {
                await target.JoinClusterAsync(idWithoutUser, email);
                Assert.Fail("JoinClusterFailedException not thrown.");
            }
            catch (JoinClusterFailedException result)
            {
                Assert.AreEqual(JoinClusterFailedReason.UserAlreadyJoined, result.Reason);
            }
        }
        public async Task JoinClusterNotReady()
        {
            ClusterConfig config = new ClusterConfig() {MaximumUsersPerCluster = 2};
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            int id = 5;
            Cluster cluster = new Cluster(
                "test",
                ClusterStatus.Creating,
                0,
                0,
                "",
                new[] {80},
                new ClusterUser[0],
                DateTimeOffset.UtcNow);

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.AddAsync(tx, id, cluster);
                await tx.CommitAsync();
            }

            try
            {
                await target.JoinClusterAsync(id, "email");
                Assert.Fail("JoinClusterFailedException not thrown.");
            }
            catch (JoinClusterFailedException result)
            {
                Assert.AreEqual(JoinClusterFailedReason.ClusterNotReady, result.Reason);
            }
        }
        public async Task BalanceClustersIncreaseBelowMin()
        {
            ClusterConfig config = new ClusterConfig();
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            int readyCount = (int) Math.Floor(config.MinimumClusterCount/5D);
            int newCount = readyCount;
            int creatingCount = readyCount;

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await this.AddClusters(tx, dictionary, readyCount, ClusterStatus.Ready);
                await this.AddClusters(tx, dictionary, newCount, ClusterStatus.New);
                await this.AddClusters(tx, dictionary, creatingCount, ClusterStatus.Creating);
                await this.AddClusters(tx, dictionary, config.MinimumClusterCount, ClusterStatus.Deleting);
                await tx.CommitAsync();
            }

            await target.BalanceClustersAsync(readyCount*4);

            Assert.AreEqual(
                config.MinimumClusterCount,
                dictionary.Count(
                    x =>
                        x.Value.Status == ClusterStatus.Ready ||
                        x.Value.Status == ClusterStatus.New ||
                        x.Value.Status == ClusterStatus.Creating));
        }
        public async Task JoinClusterEmailData()
        {
            ClusterConfig config = new ClusterConfig();
            DateTimeOffset clusterCreated = DateTimeOffset.UtcNow;

            string expectedEmail = "*****@*****.**";
            string expectedClusterAddress = "test.azure.com";
            int expectedUserPort = 543;
            string expectedLinkAddress = "http://domain.com/app";
            string expectedLinkText = "title";
            string expectedLinkDesc = "desc";

            string actualEmail = null;
            string actualClusterAddress = null;
            int actualUserPort = 0;
            HyperlinkView actualExplorerLink = null;
            HyperlinkView actualAppLink = null;

            MockMailer mailer = new MockMailer();
            MockApplicationDeployService appDeploy = new MockApplicationDeployService()
            {
                GetApplicationDeploymentsAsyncFunc = (c, p) => Task.FromResult<IEnumerable<ApplicationView>>(new[]
                {
                     new ApplicationView(new HyperlinkView(expectedLinkAddress, expectedLinkText, expectedLinkDesc))
                })
            };

            mailer.SendJoinMailFunc = (receipientAddress, clusterAddress, userPort, timeRemaining, clusterExpiration, links) =>
            {
                actualEmail = receipientAddress;
                actualClusterAddress = clusterAddress;
                actualUserPort = userPort;
                actualExplorerLink = links.ElementAt(0);
                actualAppLink = links.ElementAt(1);

                return Task.FromResult(true);
            };

            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(
                null,
                mailer,
                appDeploy,
                stateManager,
                this.CreateServiceParameters(),
                config);

            int id = 5;
            Cluster cluster = new Cluster("test", ClusterStatus.Ready, 0, 0, expectedClusterAddress, new[] { expectedUserPort }, new ClusterUser[0], DateTimeOffset.UtcNow);
            
            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.AddAsync(tx, id, cluster);
                await tx.CommitAsync();
            }

            await target.JoinClusterAsync(id, expectedEmail);
            
            Assert.AreEqual(expectedEmail, actualEmail);
            Assert.AreEqual(expectedClusterAddress + ":19000", actualClusterAddress);
            Assert.AreEqual(expectedUserPort, actualUserPort);
            Assert.AreEqual(expectedLinkAddress, actualAppLink.Address);
            Assert.AreEqual(expectedLinkText, actualAppLink.Text);
            Assert.AreEqual(expectedLinkDesc, actualAppLink.Description);
            Assert.AreEqual("http://" + expectedClusterAddress + ":19080/Explorer/index.html", actualExplorerLink.Address);

        }
Esempio n. 46
0
		private void ReconfigurePool(ClusterConfig config)
		{
			// kill the timer first
			this.isTimerActive = false;
			if (this.resurrectTimer != null)
				this.resurrectTimer.Change(Timeout.Infinite, Timeout.Infinite);

			if (config == null)
			{
				if (log.IsInfoEnabled) log.Info("Config is empty, all nodes are down.");

				Interlocked.Exchange(ref this.state, InternalState.Empty);

				return;
			}

			// these should be disposed after we've been reinitialized
			var oldNodes = this.state == null ? null : this.state.CurrentNodes;

			// default bucket does not require authentication
			// membase 1.6 tells us if a bucket needs authentication,
			// so let's try to use the config's password
			var password = config.authType == "sasl"
										? config.saslPassword
										: this.bucketPassword;

			var authenticator = this.bucketName == null
						   ? null
						   : new PlainTextAuthenticator(null, this.bucketName, password);

			try
			{
				var state = (config == null || config.vBucketServerMap == null)
								? this.InitBasic(config, authenticator)
								: this.InitVBucket(config, authenticator);

				var nodes = state.CurrentNodes;

				state.Locator.Initialize(nodes);

				// we need to subscribe the failed event, 
				// so we can periodically check the dead 
				// nodes, since we do not get a config 
				// update every time a node dies
				for (var i = 0; i < nodes.Length; i++) nodes[i].Failed += this.NodeFail;

				Interlocked.Exchange(ref this.state, state);
			}
			catch (Exception e)
			{
				log.Error("Failed to initialize the pool.", e);

				Interlocked.Exchange(ref this.state, InternalState.Empty);
			}

			// kill the old nodes
			if (oldNodes != null)
				for (var i = 0; i < oldNodes.Length; i++)
					try
					{
						oldNodes[i].Failed -= this.NodeFail;
						oldNodes[i].Dispose();
					}
					catch { }
		}
Esempio n. 47
0
		private InternalState InitVBucket(ClusterConfig config, ISaslAuthenticationProvider auth)
		{
			// we have a vbucket config, which has its own server list
			// it's supposed to be the same as the cluster config's list,
			// but the order is significicant (because of the bucket indexes),
			// so we we'll use this for initializing the locator
			var vbsm = config.vBucketServerMap;

			if (log.IsInfoEnabled) log.Info("Has vbucket. Server count: " + (vbsm.serverList == null ? 0 : vbsm.serverList.Length));

			var epa = (from server in vbsm.serverList
					   select ConfigurationHelper.ResolveToEndPoint(server)).ToArray();

			var epaLength = epa.Length;

			for (var i = 0; i < vbsm.vBucketMap.Length; i++)
			{
				var vb = vbsm.vBucketMap[i];
				if (vb == null || vb.Length == 0)
					throw new InvalidOperationException("Server sent an empty vbucket definition at index " + i);
				if (vb[0] >= epaLength || vb[0] < 0)
					throw new InvalidOperationException(String.Format("VBucket line {0} has a master index {1} out of range of the server list ({2})", i, vb[0], epaLength));
			}

			var buckets = vbsm.vBucketMap.Select(a => new VBucket(a[0], a.Skip(1).ToArray())).ToArray();
			var bucketNodeMap = buckets.ToLookup(vb =>
			{
				try
				{
					return epa[vb.Master];
				}
				catch (Exception e)
				{
					log.Error(e);

					throw;
				}
			}
			);
			var vbnl = new VBucketNodeLocator(vbsm.hashAlgorithm, buckets);

			return new InternalState
			{
				CurrentNodes = epa.Select(ip => (IMemcachedNode)new BinaryNode(ip, this.configuration.SocketPool, auth)).ToArray(),
				Locator = vbnl,
				OpFactory = new VBucketAwareOperationFactory(vbnl)
			};
		}
        public async Task ProcessRemove()
        {
            bool calledActual = false;
            string nameActual = null;

            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                DeleteClusterAsyncFunc = name =>
                {
                    nameActual = name;
                    calledActual = true;
                    return Task.FromResult(true);
                }
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.Remove);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.IsTrue(calledActual);
            Assert.AreEqual(ClusterStatus.Deleting, actual.Status);
        }
Esempio n. 49
0
		//public VBucketNodeLocator ForwardLocator { get { return this.state.ForwardLocator; } }

		private void InitNodes(ClusterConfig config)
		{
			if (log.IsInfoEnabled) log.Info("Received new configuration.");

			// we cannot overwrite the config while the timer is is running
			lock (this.DeadSync)
				this.ReconfigurePool(config);
		}
        public async Task ProcessCreatingClusterFailed()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = name => Task.FromResult(ClusterOperationStatus.CreateFailed)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);
            Cluster cluster = this.CreateCluster(ClusterStatus.Creating);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Remove, actual.Status);
            Assert.AreEqual(0, actual.Ports.Count());
            Assert.AreEqual(0, actual.Users.Count());
        }
        public async Task ProcessClustersAsyncSaveChanges()
        {
            int key = 1;
            string nameTemplate = "Test:{0}";
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterConfig config = new ClusterConfig();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                CreateClusterAsyncFunc = name => { return Task.FromResult(String.Format(nameTemplate, name)); }
            };

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Cluster original = this.CreateCluster(ClusterStatus.New);
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.SetAsync(tx, key, original);
            }

            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            await target.ProcessClustersAsync();

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                Cluster actual = (await dictionary.TryGetValueAsync(tx, key)).Value;

                Assert.AreNotEqual(original, actual);
            }
        }
        public async Task ProcessCreatingClusterSuccess()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = name => Task.FromResult(ClusterOperationStatus.Ready)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);
            Cluster cluster = this.CreateCluster(ClusterStatus.Creating);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Ready, actual.Status);
            Assert.IsTrue(actual.CreatedOn.ToUniversalTime() <= DateTimeOffset.UtcNow);
            actual.Ports.SequenceEqual(await clusterOperator.GetClusterPortsAsync(""));
        }
        /// <summary>
        /// When cluster config changes, update the static config
        /// list of bootstrap URIs with known working nodes from cluster config
        /// </summary>
        public void UpdateNodes(ClusterConfig config)
        {
            var currentUri = this.urls[this.urlIndex]; //last known good Uri
            var serverConfigUris = config.nodes.Where(n => n.Status == "healthy") //healthy nodes from config
                                      .Select(n => new UriBuilder(
                                                    currentUri.Scheme, n.HostName,
                                                    currentUri.Port, currentUri.PathAndQuery).Uri).ToList();

            serverConfigUris.ForEach(u => statusPool[u] = true);
            var newConfigUris = serverConfigUris.Union(this.urls).ToArray(); //allow any client supplied URIs to remain

            Interlocked.Exchange(ref this.urls, newConfigUris);
            Interlocked.Exchange(ref this.urlIndex, 0);
            Interlocked.Exchange(ref this.realUrls, this.urls.Distinct().ToDictionary(u => u, u => (Uri)null));
        }
        public async Task ProcessNewCluster()
        {
            bool calledActual = false;
            string nameTemplate = "Test:{0}";
            string nameActual = null;

            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                CreateClusterAsyncFunc = name =>
                {
                    nameActual = name;
                    calledActual = true;
                    return Task.FromResult(String.Format(nameTemplate, name));
                }
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.New);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.IsTrue(calledActual);
            Assert.AreEqual(ClusterStatus.Creating, actual.Status);
            Assert.AreEqual(String.Format(nameTemplate, nameActual), actual.Address);
        }
Esempio n. 55
0
 public void AssignTo(string clusterName, int[] cores, TimeSpan time)
 {
     this.Time.Estimated = time;
     _assignedTo = new ClusterConfig(clusterName, cores);
     _state = TaskState.Assigned;
 }
Esempio n. 56
0
        private void ReconfigurePool(ClusterConfig config)
        {
            if (log.IsDebugEnabled)
            {
                if (Thread.CurrentThread.Name == null)
                {
                    Thread.CurrentThread.Name = "cbp_thread";
                }
            }

            // kill the timer first
            this.isTimerActive = false;
            if (this.resurrectTimer != null)
                this.resurrectTimer.Change(Timeout.Infinite, Timeout.Infinite);

            if (config == null)
            {
                if (log.IsInfoEnabled) log.Info("Config is empty, all nodes are down.");
                return; //continue to use current state and wait for config update
            }

            var currentState = this.state;

            // these should be disposed after we've been reinitialized
            var oldNodes = currentState == null ? null : currentState.CurrentNodes;

            // default bucket does not require authentication
            // Couchbase 1.6 tells us if a bucket needs authentication,
            // so let's try to use the config's password
            var password = config.authType == "sasl"
                            ? config.saslPassword
                            : this.configuration.BucketPassword;

            var authenticator = this.configuration.Bucket == null
                                   ? null
                                   : new PlainTextAuthenticator(null, this.configuration.Bucket, password);

            try
            {
                var state = (config == null || config.vBucketServerMap == null)
                                ? this.InitBasic(config, authenticator)
                                : this.InitVBucket(config, authenticator);

                var nodes = state.CurrentNodes;
                state.Locator.Initialize(nodes);

                // we need to subscribe the failed event,
                // so we can periodically check the dead
                // nodes, since we do not get a config
                // update every time a node dies
                foreach (IMemcachedNode node in nodes)
                {
                    node.Failed += this.NodeFail;
                }

                Interlocked.Exchange(ref this.state, state);
            }
            catch (Exception e)
            {
                log.Error("Failed to initialize the pool.", e);

                Interlocked.Exchange(ref this.state, InternalState.Empty);
            }

            //Clean up the old nodes
            CloseNodes(oldNodes);
        }
        private InternalState InitBasic(ClusterConfig config, ISaslAuthenticationProvider auth)
        {
            if (log.IsInfoEnabled) log.Info("No vbucket. Server count: " + (config.nodes == null ? 0 : config.nodes.Length));

            // the cluster can return host names in the server list, so
            // we ha ve to make sure they are converted to IP addresses
            var nodes = config == null
                        ? Enumerable.Empty<IMemcachedNode>()
                            : (from node in config.nodes
                               let ip = new IPEndPoint(GetFirstAddress(node.HostName), node.Port)
                               where node.Status == "healthy"
                               select CreateNode(ip, auth, node.ConfigurationData));

            return new InternalState
            {
                CurrentNodes = nodes.ToArray(),
                Locator = configuration.CreateNodeLocator() ?? new KetamaNodeLocator(),
                OpFactory = BasicCouchbaseOperationFactory.Instance
            };
        }
        private InternalState InitVBucket(ClusterConfig config, ISaslAuthenticationProvider auth)
        {
            // we have a vbucket config, which has its own server list
            // it's supposed to be the same as the cluster config's list,
            // but the order is significicant (because of the bucket indexes),
            // so we we'll use this for initializing the locator
            var vbsm = config.vBucketServerMap;

            if (log.IsInfoEnabled) log.Info("Has vbucket. Server count: " + (vbsm.serverList == null ? 0 : vbsm.serverList.Length));

            // parse the ip addresses of the servers in the vbucket map
            // make sure we have a propert vbucket map
            ValidateVBucketMap(vbsm, vbsm.serverList.Length);

            // create vbuckets from the int[][] arrays
            var buckets = vbsm.vBucketMap.Select(a => new VBucket(a[0], a.Skip(1).ToArray())).ToArray();

            var locator = new VBucketNodeLocator(vbsm.hashAlgorithm, buckets);

            // create a (host=>node) lookup from the node info objects,
            // so we can pass the extra config data to the factory method
            // (the vbucket map only contains 'host:port' strings)
            // this expects that all nodes listed in the vbucket map are listed in the config.nodes member as well
            var realNodes = config.nodes.ToDictionary(node => node.HostName + ":" + node.Port);
            var nodes = new List<IMemcachedNode>();

            foreach (var hostSpec in vbsm.serverList)
            {
                ClusterNode node;

                if (!realNodes.TryGetValue(hostSpec, out node))
                    throw new InvalidOperationException(String.Format("VBucket map contains a node {0} whihc was not found in the cluster info's node list.", hostSpec));

                var ip = GetFirstAddress(node.HostName);
                var endpoint = new IPEndPoint(ip, node.Port);

                nodes.Add(this.CreateNode(endpoint, auth, node.ConfigurationData));
            }

            return new InternalState
            {
                CurrentNodes = nodes.ToArray(),
                Locator = locator,
                OpFactory = new VBucketAwareOperationFactory(locator),
                IsVbucket = true
            };
        }
        public async Task ProcessDeletingSuccessful()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = domain => Task.FromResult(ClusterOperationStatus.ClusterNotFound)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceParameters(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.Deleting);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Deleted, actual.Status);
        }
        public async Task ProcessRemoveTimeLimit()
        {
            ClusterConfig config = new ClusterConfig()
            {
                MaximumClusterUptime = TimeSpan.FromHours(2)
            };

            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterService target = new ClusterService(null, null, new MockApplicationDeployService(), stateManager, this.CreateServiceParameters(), config);

            Cluster cluster = new Cluster(
                "test",
                ClusterStatus.Ready,
                0,
                0,
                String.Empty,
                new int[0],
                new ClusterUser[0],
                DateTimeOffset.UtcNow - config.MaximumClusterUptime);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Remove, actual.Status);
        }