Beispiel #1
0
        static IEtcdWatchStatus MonitorEtcd(IEtcdWatchStatus status)
        {
            IEtcdClient client = new EtcdClient();
            status = client.Watch(status, null, (a, b) =>
            {
                Console.WriteLine("{0}-{1}", b.Node.Key, b.Node.Value);
                return true;
            },
            (a,b) =>
            {
                if (b.PrevErrorsCount%5 == 0){
                    Console.WriteLine("Another 5 errors:" + b.Exception.Message);
                }

                if (a.AllFailed && a.Clients.Select(c => c.LastError.PrevErrorsCount).All(num => num > 15))
                {
                    Console.WriteLine("Fatal Error in etcd cluster, Reinitializing...");
                    a.AbortAll(); //implicit not running
                    MonitorEtcd(status);
                }

                return true;
            },true);

            return status;
        }
Beispiel #2
0
        public EtcdDiscoveryMode (string topKey, params string[] discoveryServers)
        {
            if (topKey.StartsWith("/"))
                topKey.Substring(1, topKey.Length - 1);

            _etcdTopKey = topKey;
            foreach (var etcdUrl in discoveryServers)
            {
                var etcdClient = new EtcdClient(new Uri(string.Format("{0}/v2/keys", etcdUrl)));
                try
                {
                    etcdClient.Statistics.Leader();
                    _etcdClient = etcdClient;
                }
                catch (Exception e)
                {
                    Logger.Error(e.Message);
                    Logger.Error("Invalid Etcd Host: {0}", etcdUrl);
                    continue;
                }
            }

            if (_etcdClient == null)
                throw new Exception("Not valid EtcdClient");
        }
Beispiel #3
0
 static void Main(string[] args)
 {
     var etcdUrl = new Uri("http://localhost:4001");
     _client = new EtcdClient(etcdUrl);
     GetOne();
     GetAllConfigss();;
     PrintAndWatch();
     Console.ReadKey();
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            IEtcdClient client = new EtcdClient();
            //Console.Write(client.Get("assets/special_referrers").Node.Value);

            IEtcdWatchStatus watchStatus = MonitorEtcd(null);
            Console.Read();
            watchStatus.AbortAll();
            Console.Read();
        }
Beispiel #5
0
        public static void Configure(ServiceConfiguration config)
        {
            //ServiceEndpoint se = new ServiceEndpoint(new ContractDescription("IService1"), new BasicHttpBinding(), new EndpointAddress("http://localhost/testservice/service1.svc"));
            //se.Behaviors.Add(new MyEndpointBehavior());
            //config.AddServiceEndpoint(se);

            config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
            config.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });

            var etcd = new EtcdClient(new Uri("http://localhost:4001/v2/keys/"));
            etcd.Set("local/EtcdPrototype.IService1/url", config.BaseAddresses.First().ToString());
            //            etcd.Set("local/EtcdPrototype.IService1/binding",config.)
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            var etcdUrl = new Uri("http://localhost:4001");
            _client = new EtcdClient(etcdUrl);
            InsertSomeTestData();
            GetOne();
            GetAllConfigs();;
            PrintAndWatchApiKey();

            while (true)
            {
                Thread.Sleep(3000);
                InsertSomeRandomApiKey();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="timeout">Seconds we will try to get the lock for.</param>
        /// <param name="ttl">Seconds until lock times out. Governs how frequently we extend the lock as well. Minimum of 10 seconds.</param>
        public DistributedLock(string name, int timeout = 10, int ttl = 30)
        {

            if (string.IsNullOrEmpty(name))
                throw new Exception("Distributed Lock Requires a Name");

            _ttl = ttl;
            if (_ttl < 10) throw new Exception("Minimum TTL is 10 seconds");

            _timeout = DateTime.UtcNow.AddSeconds(timeout);
            _client = new EtcdClient(Configuration.Instance.Hostnames[0]);
            _name = name;
            _handle = new ManualResetEventSlim(false);

            // get the unique ID of this locking instance
            _guid = Guid.NewGuid();

            // 1. try to create the node
            //    -> if already exists set up a watch to lock after it
            //       is given back.
            //       watch should also have the ID of the modifyIndex so 
            //       it will catch up if something happens in between

            // 2. extend the lock while it's active automatically
            //    by updating the TTL
            //    

            // this will attempt to get the lock and re-call itself
            // if there are multiple threads/servers attempting to 
            // get the same lock
            GetLock();

            // use the reset event to block this thread
            _handle.Wait(timeout * 1000);
            _handle.Dispose();

            if (_index == 0)
            {
                // we didn't get the lock
                throw new DistributedLockException("Could not aquire lock after retries");
            }
        }
Beispiel #8
0
 public void Init()
 {
     etcd = new EtcdClient(new Uri("http://localhost:4001/v2/keys/"));
     
 }