private static List <ZooKeeperInstance> CreateInstances(ZooKeeperEnsembleSettings settings, ILog log)
        {
            var instances = new List <ZooKeeperInstance>(settings.Size);

            for (var i = 0; i < settings.Size; i++)
            {
                var clientPort   = settings.InstancesPorts?[i] ?? FreeTcpPortFinder.GetFreePort();
                var peerPort     = FreeTcpPortFinder.GetFreePort();
                var electionPort = FreeTcpPortFinder.GetFreePort();
                var index        = settings.StartingId + i;

                var instanceDirectoryPath = "ZK-" + index;

                if (!string.IsNullOrEmpty(settings.BaseDirectory))
                {
                    instanceDirectoryPath = Path.Combine(settings.BaseDirectory, instanceDirectoryPath);
                }

                instanceDirectoryPath = Path.GetFullPath(instanceDirectoryPath);

                instances.Add(new ZooKeeperInstance(index, instanceDirectoryPath, clientPort, peerPort, electionPort, log));
            }

            log.Info("Created instances: \n\t" + string.Join("\n\t", instances.Select(i => i.ToString())));

            return(instances);
        }
        public static ZooKeeperEnsemble DeployNew([NotNull] ZooKeeperEnsembleSettings settings, [NotNull] ILog log, bool startInstances = true)
        {
            var ensemble = new ZooKeeperEnsemble(settings, log);

            ensemble.Deploy(startInstances);

            return(ensemble);
        }
        private ZooKeeperEnsemble([NotNull] ZooKeeperEnsembleSettings settings, [NotNull] ILog log)
        {
            this.log = (log ?? throw new ArgumentNullException(nameof(log))).ForContext <ZooKeeperEnsemble>();

            if (settings.Size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.Size), "Ensemble must have at least one instance.");
            }

            Instances = CreateInstances(settings, this.log);
        }
        private ZooKeeperEnsemble([NotNull] ZooKeeperEnsembleSettings settings, [NotNull] ILog log)
        {
            this.log = (log ?? throw new ArgumentNullException(nameof(log))).ForContext <ZooKeeperEnsemble>();

            if (settings.Size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.Size), "Ensemble must have at least one instance.");
            }

            if (settings.InstancesPorts != null && settings.InstancesPorts.Count != settings.Size)
            {
                throw new ArgumentException(
                          "You should either specify port for every instance or specify none",
                          nameof(settings.InstancesPorts));
            }

            Instances = CreateInstances(settings, this.log);
        }
        public static ZooKeeperEnsemble DeployNew([NotNull] ZooKeeperEnsembleSettings settings, [NotNull] ILog log, bool startInstances = true)
        {
            ZooKeeperEnsemble ensemble = null;

            Retrier.RetryOnException(() =>
            {
                ensemble = new ZooKeeperEnsemble(settings, log);

                ensemble.Deploy(startInstances);
            },
                                     3,
                                     "Unable to start Zookeeper ensemble",
                                     () =>
            {
                log.Warn("Retrying Zookeeper.LocalEnsemble deployment...");
                ensemble?.Dispose();
            });

            return(ensemble);
        }