Exemplo n.º 1
0
        /// <summary>
        /// Writes a value in znode
        /// </summary>
        public void Write()
        {
            _path = GetPath(_input1);

            try
            {
                var stat = Zk.Exists(_path, false);
                if (stat == null)
                {
                    Zk.Create(_path, Encoding.Default.GetBytes(_input2), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                    AppendLineShow(String.Format("Znode {0} does not exist! Creating one..", _input1));
                }
                else
                {
                    Zk.SetData(_path, Encoding.Default.GetBytes(_input2), -1);
                    AppendLineShow(String.Format("{0} set value {1}", _input1, _input2));
                }
            }
            catch (KeeperException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
            catch (ArgumentNullException e)
            {
                AppendLineShow("Insert in textboxes!");
            }
        }
Exemplo n.º 2
0
        internal void UploadFonts()
        {
            _path = "/Fonts";

            // Get fonts from local machine
            InstalledFontCollection installedFontCollection = new InstalledFontCollection();

            FontFamily[]  fontFamilies  = installedFontCollection.Families;
            List <string> localFontList = fontFamilies.Select(t => t.Name).ToList();

            if (Zk.Exists(_path, false) == null)
            {
                try
                {
                    var createdPath = Zk.Create(_path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                    AppendLineShow("Created: " + createdPath);
                }
                catch (KeeperException e)
                {
                    AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
                }
                catch (InvalidOperationException e)
                {
                    AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
                }
            }

            //Upload to zookeeper
            AppendLineShow("\n+++ Write in ZK +++");
            foreach (string font in localFontList)
            {
                _input1 = "Fonts/" + font;
                Create(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deletes a znode
        /// </summary>
        public void Delete()
        {
            _path = GetPath(_input1);

            try
            {
                IEnumerable <String> children = Zk.GetChildren(_path, false);
                foreach (var child in children)
                {
                    Zk.Delete(_path + "/" + child, -1);
                }

                Zk.Delete(_path, -1);
                AppendLineShow(String.Format("Znode {0} was deleted successfully\n", _input1));
            }
            catch (KeeperException.NoNodeException e)
            {
                AppendLineShow(String.Format("Znode {0} does not exist!", _input1));
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
            catch (Exception e)
            {
                AppendLineShow("Exception " + e);
            }
        }
Exemplo n.º 4
0
        private ZookeeperRedialManager()
        {
            _lockerFilePath = $"{_root}/redialer.lock";

            if (Zk.Exists(_root, false) == null)
            {
                Zk.Create(_root, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            }

            AtomicExecutor = new ZookeeperAtomicExecutor(this);
        }
Exemplo n.º 5
0
 public void Delete(string path, int version)
 {
     try
     {
         Zk.Delete(path, version);
     }
     catch (Exception e)
     {
         log.Error(String.Format("An error occured while trying to delete. Exception: {0}", e.ToString()));
     }
 }
Exemplo n.º 6
0
        internal void UploadConfig()
        {
            var keys = ConfigurationManager.AppSettings;

            _path = "/FirmID-ClientNames";

            // Create parent folder if != exist
            if (Zk.Exists(_path, false) == null)
            {
                try
                {
                    var createdPath = Zk.Create(_path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                    AppendLineShow("Created: " + createdPath);
                }
                catch (KeeperException e)
                {
                    AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
                }
                catch (InvalidOperationException e)
                {
                    AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
                }
            }

            // Upload to zookeeper
            AppendLineShow("\n+++ Write in ZK +++");
            foreach (var key in keys)
            {
                var value = keys.GetValues(key.ToString());
                if (value != null)
                {
                    _input1 = "FirmID-ClientNames/" + (string)value.GetValue(0);
                }
                _input2 = key.ToString();
                Write();
            }


            // Show the config
            AppendLineShow("\n+++ Keys and values in App.config +++");
            foreach (var key in keys)
            {
                var strings = keys.GetValues(key.ToString());
                if (strings == null)
                {
                    continue;
                }

                var value1 = strings.GetValue(0);
                AppendLineShow(key + " - " + value1.ToString());
            }
        }
Exemplo n.º 7
0
        public override RedialResult Redial()
        {
            if (Skip)
            {
                return(RedialResult.Skip);
            }

            if (Zk.Exists(_lockerFilePath, false) != null)
            {
                while (true)
                {
                    Thread.Sleep(50);
                    if (Zk.Exists(_lockerFilePath, false) == null)
                    {
                        return(RedialResult.OtherRedialed);
                    }
                }
            }
            else
            {
                try
                {
                    using (var zk = ZookeeperUtil.GetShortSessionZk(120))
                    {
                        zk.Create(_lockerFilePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);

                        // wait all operation stop.
                        Thread.Sleep(5000);

                        Logger.Warn("Wait atomic action to finish...");

                        AtomicExecutor.WaitAtomicAction();

                        Logger.Warn("Try to redial network...");

                        RedialInternet();

                        Logger.Warn("Redial finished.");
                        return(RedialResult.Sucess);
                    }
                }
                catch (IOException)
                {
                    // 有极小可能同时调用File.Open时抛出异常
                    return(Redial());
                }
                catch (Exception)
                {
                    return(RedialResult.Failed);
                }
            }
        }
        public void WaitAtomicAction()
        {
            // 等待数据库等操作完成
            while (true)
            {
                if (Zk.GetChildren(_root, false).Count() == 1)
                {
                    break;
                }

                Thread.Sleep(1);
            }
        }
Exemplo n.º 9
0
 public byte[] GetData(string path, bool watch, Stat stat)
 {
     byte[] returnBytes = null;
     try
     {
         returnBytes = Zk.GetData(path, watch, stat);
     }
     catch (Exception e)
     {
         log.Error(String.Format("An error occured while trying to get data. Exception: {0}", e.ToString()));
     }
     return(returnBytes);
 }
Exemplo n.º 10
0
        public string Create(string path, byte[] data, List <ACL> acl, CreateMode createMode)
        {
            string pathValue = null;

            try
            {
                pathValue = Zk.Create(path, data, acl, createMode);
            }
            catch (Exception e)
            {
                log.Error(String.Format("An error occured while trying to create. Exception: {0}", e.ToString()));
            }
            return(pathValue);
        }
Exemplo n.º 11
0
        public List <string> GetChildren(string path, bool watch)
        {
            List <string> children = null;

            try
            {
                children = Zk.GetChildren(path, watch).ToList();
            }
            catch (Exception e)
            {
                log.Error(String.Format("An error occured while trying to get children. Exception: {0}", e.ToString()));
            }
            return(children);
        }
Exemplo n.º 12
0
        public Stat SetData(string path, byte[] data, int version)
        {
            Stat returnBytes = null;

            try
            {
                returnBytes = Zk.SetData(path, data, version);
            }
            catch (Exception e)
            {
                log.Error(String.Format("An error occured while trying to set data. Exception: {0}", e.ToString()));
            }
            return(returnBytes);
        }
Exemplo n.º 13
0
        public Stat Exists(string path, bool watch)
        {
            Stat stat = null;

            try
            {
                stat = Zk.Exists(path, watch);
            }
            catch (Exception e)
            {
                log.Error(String.Format("An error occured while trying to check if exists. Exception: {0}", e.ToString()));
            }
            return(stat);
        }
Exemplo n.º 14
0
        public T Execute <T>(string name, Func <T> func)
        {
            WaitforRedial.WaitforRedialFinish();
            string id = GetPath(name);

            try
            {
                Zk.Create(id, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
                return(func());
            }
            finally
            {
                Zk.Delete(id, -1);
            }
        }
Exemplo n.º 15
0
        public void Execute(string name, Action <object> action, object obj)
        {
            WaitforRedial.WaitforRedialFinish();

            string id = GetPath(name);

            try
            {
                Zk.Create(id, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
                action(obj);
            }
            finally
            {
                Zk.Delete(id, -1);
            }
        }
Exemplo n.º 16
0
        public void Listen()
        {
            const string pdfPath = "/PDFConverter";

            try
            {
                Zk.GetChildren(pdfPath, true);
                AppendLineShow(string.Format("A watch on {0}'s children has been placed.", pdfPath));
            }
            catch (KeeperException.NoNodeException e)
            {
                AppendLineShow(String.Format("Znode {0} does not exist!", _input1));
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
        }
Exemplo n.º 17
0
        public void Watch()
        {
            _path = GetPath(_input1);

            try
            {
                Zk.Exists(_path, this);
                AppendLineShow(String.Format("Watch added to {0}", _input1));
            }
            catch (KeeperException)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
        }
Exemplo n.º 18
0
        public override void WaitforRedialFinish()
        {
            if (Skip)
            {
                return;
            }

            if (Zk.Exists(_lockerFilePath, false) != null)
            {
                for (int i = 0; i < RedialTimeout; ++i)
                {
                    Thread.Sleep(50);
                    if (Zk.Exists(_lockerFilePath, false) == null)
                    {
                        break;
                    }
                }
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Creates a znode
        /// </summary>
        /// <param name="eph"> eph stands for Ephemeral</param>
        public void Create(bool eph)
        {
            _path = GetPath(eph ? _input1 + "/" + _input2 : _input1);

            try
            {
                var createdPath = Zk.Create(_path, null, Ids.OPEN_ACL_UNSAFE,
                                            eph ? CreateMode.EphemeralSequential : CreateMode.Persistent);
                AppendLineShow("Created: " + createdPath);
            }
            catch (KeeperException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// Read value from znode
 /// </summary>
 public void Read()
 {
     _path = GetPath(_input1);
     try
     {
         var data = Zk.GetData(_path, this, null /*stat*/);
         AppendLineShow(String.Format("Value of {0} is {1}", _input1, Encoding.UTF8.GetString(data)));
     }
     catch (KeeperException.NoNodeException)
     {
         AppendLineShow("Znode does not exist!");
     }
     catch (KeeperException)
     {
         AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
     }
     catch (InvalidOperationException)
     {
         AppendLineShow(string.Format("Znode {0} isn't valid!", _input1));
     }
 }
Exemplo n.º 21
0
        /// <summary>
        /// Lists the znodes
        /// </summary>
        public void List()
        {
            _path = GetPath(_input1);

            try
            {
                var children = Zk.GetChildren(_path, false);
                IEnumerable <string> enumerable = children as string[] ?? children.ToArray();

                if (enumerable.IsEmpty())
                {
                    AppendLineShow("No members in znode: " + _input1);
                }
                else
                {
                    AppendLineShow("\n======= Z =======");
                    foreach (var child in enumerable)
                    {
                        Output.Append(child + "   ");
                    }
                    AppendLineShow("\n");
                }
            }
            catch (KeeperException.NoNodeException e)
            {
                AppendLineShow("Znode does not exist!");
            }
            catch (KeeperException.SessionExpiredException e)
            {
                AppendLineShow("The session expired!");
            }
            catch (InvalidOperationException e)
            {
                AppendLineShow("Too many '/'!");
            }
        }