Ejemplo n.º 1
0
        public ZKLock(ZKConfig config)
        {
            this.config = config;

            var tcs = new TaskCompletionSource <bool>();

            lock (lockObj)
            {
                if (zooKeeper == null)
                {
                    zooKeeper = new ZK.ZooKeeper(
                        this.config.ConnectionString,
                        this.config.SessionTimeout,
                        new WaitWatcher(string.Empty, tcs, e => e.getState() == KeeperState.SyncConnected));
                }
            }
            zooKeeper.Connect().Wait();
            if (zooKeeper.getState() != ZK.ZooKeeper.States.CONNECTED &&
                zooKeeper.getState() != ZK.ZooKeeper.States.CONNECTEDREADONLY)
            {
                throw new Exception("ZooKeeper Connected Failed");
            }
            CreatNodeIfNotExist(config.LockPath).Wait();

            Console.WriteLine($"{this.config.Name} Constructed");
        }
Ejemplo n.º 2
0
        public async Task <org.apache.zookeeper.ZooKeeper> Get(String connStr)
        {
            org.apache.zookeeper.ZooKeeper zk = null;
            bool isExists = MappedZooKeepers.ContainsKey(connStr);

            if (isExists)
            {
                zk = MappedZooKeepers[connStr];
                var zkState = zk.getState();
                if (zkState == org.apache.zookeeper.ZooKeeper.States.CLOSED
                    ||
                    zkState == org.apache.zookeeper.ZooKeeper.States.NOT_CONNECTED
                    )
                {
                    await Remove(connStr);

                    zk = new org.apache.zookeeper.ZooKeeper(connStr, SESSION_TIMEOUT, NoneWatcher.Instance);
                    MappedZooKeepers.Add(connStr, zk);
                }
                return(zk);
            }
            zk = new org.apache.zookeeper.ZooKeeper(connStr, SESSION_TIMEOUT, NoneWatcher.Instance);
            MappedZooKeepers.Add(connStr, zk);
            return(zk);
        }
Ejemplo n.º 3
0
        public async Task DeleteValue(string path)
        {
            var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 60000, null);
            await zk.deleteAsync(path);

            await zk.closeAsync();
        }
        public async Task <bool> StartSessionAsync(TimeSpan sessionTimeout, TimeSpan connectTimeout, CancellationToken token)
        {
            this.token = token;
            var sw = new Stopwatch();

            sw.Start();

            if (this.zookeeper != null)
            {
                await this.zookeeper.closeAsync();
            }

            this.zookeeper = new org.apache.zookeeper.ZooKeeper(
                this.zookeeperHosts,
                (int)sessionTimeout.TotalMilliseconds,
                this);

            while (this.keeperState != Event.KeeperState.SyncConnected && sw.Elapsed <= connectTimeout)
            {
                await Task.Delay(50);
            }

            var connected = this.keeperState == Event.KeeperState.SyncConnected;

            this.sessionExpired = !connected;

            return(connected);
        }
Ejemplo n.º 5
0
        private async Task LoadNode(org.apache.zookeeper.ZooKeeper zk, Node node)
        {
            var result = await zk.getChildrenAsync(node.Path);

            if (result != null)
            {
                node.NodeState = NodeState.Build(result.Stat);
                foreach (var child in result.Children)
                {
                    var childNode = new Node
                    {
                        Text = child
                    };

                    if (node.Path != "/")
                    {
                        childNode.Path = node.Path + "/" + child;
                    }
                    else
                    {
                        childNode.Path = node.Path + child;
                    }
                    await LoadNode(zk, childNode);

                    if (node.Nodes == null)
                    {
                        node.Nodes = new List <Node>();
                    }
                    node.Nodes.Add(childNode);
                }
            }
        }
Ejemplo n.º 6
0
 public static async Task Connect(this ZK.ZooKeeper zooKeeper)
 {
     while (zooKeeper.getState() == ZK.ZooKeeper.States.CONNECTING)
     {
         await Task.Delay(15);
     }
 }
Ejemplo n.º 7
0
        private async Task SetupConnectionAsync()
        {
            zooKeeper = new org.apache.zookeeper.ZooKeeper(connectionString, 100000, new ZookeeperWatcher());
            org.apache.zookeeper.ZooKeeper.LogToFile  = false;
            org.apache.zookeeper.ZooKeeper.LogToTrace = true;

            var root = await zooKeeper.existsAsync("/");

            if (root == null)
            {
                if (!createStoreIfNeeded)
                {
                    throw new InvalidOperationException("Store does not exist");
                }

                await zooKeeper.createAsync("/", null, Z.ZooDefs.Ids.OPEN_ACL_UNSAFE, Z.CreateMode.PERSISTENT);
            }

            await CreateIfNotExist(zooKeeper, prefix);

            if (useWatchdog)
            {
                await CreateIfNotExist(zooKeeper, watchdogPrefix);
            }

            watcher = new FeatureWatcher(zooKeeper, OnZookeeperEntryChanged);
            if (useWatchdog)
            {
                await SetupWatchdog();
            }
            localView.Clear();
        }
Ejemplo n.º 8
0
        public async Task ChangeValue(string path, string value)
        {
            var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 60000, null);
            await zk.setDataAsync(path, Encoding.UTF8.GetBytes(value));

            await zk.closeAsync();
        }
        static void Main(string[] args)
        {
            var client = new org.apache.zookeeper.ZooKeeper(
                "10.217.9.184:2181,10.217.6.124:2181,10.217.6.140:2181,10.217.6.222:2181,10.217.9.47:2181",
                5000,
                null);

            Thread.Sleep(1000);
            var path         = "/LinuxMustDie";
            var data         = "LINUX IS ALIVE!!";
            var createResult = client.createAsync(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            Console.WriteLine($"Create result = {createResult}");

            var setDataResult = client.setDataAsync(path, Encoding.UTF8.GetBytes(data)).GetAwaiter().GetResult();

            Console.WriteLine($"SetData result version = {setDataResult.getVersion()}");


            var getDataResult = client.getDataAsync(path).GetAwaiter().GetResult();

            Console.WriteLine($"GetData result = {Encoding.UTF8.GetString(getDataResult.Data)}");

            Console.ReadKey();
            client.deleteAsync(path).Wait();
        }
Ejemplo n.º 10
0
        public async Task AddValue(string path, string value)
        {
            var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 60000, null);
            await zk.createAsync(path, Encoding.UTF8.GetBytes(value), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                 CreateMode.PERSISTENT);

            await zk.closeAsync();
        }
 public async Task CloseSessionAsync()
 {
     if (this.zookeeper != null)
     {
         await this.zookeeper.closeAsync();
     }
     this.zookeeper = null;
 }
        protected static void CheckVersions(org.apache.zookeeper.ZooKeeper client, string rootNode, int version, int cVersion)
        {
            var currentStat = client.existsAsync(rootNode).GetAwaiter().GetResult();

            currentStat.getVersion().Should().Be(version);
            currentStat.getCversion().Should().Be(cVersion);
            //currentStat.Version.Should().Be(version);
            //currentStat.Cversion.Should().Be(cVersion);
        }
        protected static void SetData(string path, string data, org.apache.zookeeper.ZooKeeper client)
        {
            var setDataResult = client.setDataAsync(path, Encoding.UTF8.GetBytes(data)).GetAwaiter().GetResult();

            setDataResult.Should().NotBeNull();
            //setDataResult.EnsureSuccess();
            //setDataResult.Status.Should().Be(ZooKeeperStatus.Ok);
            //setDataResult.Path.Should().Be(path);
        }
Ejemplo n.º 14
0
        public async Task CloseSessionAsync()
        {
            if (zookeeper != null)
            {
                await zookeeper.closeAsync();
            }

            zookeeper = null;
        }
        protected static void EnsureNodeDoesNotExist(string path, org.apache.zookeeper.ZooKeeper anotherClient)
        {
            var existResult = anotherClient.existsAsync(path).GetAwaiter().GetResult();

            existResult.Should().BeNull();
            //existResult.EnsureSuccess();
            //existResult.Path.Should().Be(path);
            //existResult.Status.Should().Be(ZooKeeperStatus.Ok);
            //existResult.Payload.Should().BeNull();
        }
        protected static void EnsureChildrenExists(org.apache.zookeeper.ZooKeeper client, string rootNode, string[] children)
        {
            var getChildrenResult = client.getChildrenAsync(rootNode).GetAwaiter().GetResult();

            getChildrenResult.Children.Should().BeEquivalentTo(children);
            //getChildrenResult.EnsureSuccess();
            //getChildrenResult.Status.Should().Be(ZooKeeperStatus.Ok);
            //getChildrenResult.Path.Should().Be(rootNode);
            //getChildrenResult.Payload.Should().BeEquivalentTo(children);
        }
Ejemplo n.º 17
0
        public override async Task <CreateResult> Execute(ZooKeeperNetExClient client)
        {
            if (!NodeHelper.ValidateDataSize(Request.Data))
            {
                return(CreateUnsuccessfulResult(ZooKeeperStatus.BadArguments, NodeHelper.DataSizeLimitExceededException(Request.Data)));
            }

            var newPath = await client.createAsync(Request.Path, Request.Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, Request.CreateMode.ToInnerCreateMode()).ConfigureAwait(false);

            return(CreateResult.Successful(Request.Path, newPath));
        }
        protected static void EnsureChildrenExistWithCorrectStat(org.apache.zookeeper.ZooKeeper client, string rootNode, string[] children, int nodeVersion = 0, int childVersion = 0)
        {
            var getChildrenWithStatResult = client.getChildrenAsync(rootNode).GetAwaiter().GetResult();

            getChildrenWithStatResult.Children.Should().BeEquivalentTo(children);
            //getChildrenWithStatResult.Path.Should().Be(rootNode);
            //getChildrenWithStatResult.Status.Should().Be(ZooKeeperStatus.Ok);
            //getChildrenWithStatResult.Payload.Item1.Should().BeEquivalentTo(children);
            //getChildrenWithStatResult.Payload.Item2.Version.Should().Be(nodeVersion);
            //getChildrenWithStatResult.Payload.Item2.Cversion.Should().Be(childVersion);
        }
Ejemplo n.º 19
0
        public PessimisticLockV2(org.apache.zookeeper.ZooKeeper zooKeeper)
        {
            _zooKeeper = zooKeeper;
            var node = _zooKeeper.existsAsync("/PessimisticLockV2").Result;

            if (node == null)
            {
                Task.WaitAll(_zooKeeper.createAsync("/PessimisticLockV2", null, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                    CreateMode.PERSISTENT));
            }
        }
        protected static void EnsureDataExists(string path, org.apache.zookeeper.ZooKeeper client, string expectedData, int expectedVersion = 0)
        {
            var expectedDataBytes = Encoding.UTF8.GetBytes(expectedData);
            var getDataResult     = client.getDataAsync(path).GetAwaiter().GetResult();

            getDataResult.Data.Should().BeEquivalentTo(expectedDataBytes);
            //getDataResult.EnsureSuccess();
            //getDataResult.Path.Should().Be(path);
            //getDataResult.Status.Should().Be(ZooKeeperStatus.Ok);
            //getDataResult.Payload.Item1.Should().BeEquivalentTo(expectedDataBytes);
            //getDataResult.Payload.Item2.Version.Should().Be(expectedVersion);
        }
        protected org.apache.zookeeper.ZooKeeper CreateNewClient(ILog logForClient = null)
        {
            //10.217.9.184:2181,10.217.6.124:2181,10.217.6.140:2181,10.217.6.222:2181,10.217.9.47:2181
            var client = new org.apache.zookeeper.ZooKeeper(
                //"10.217.9.184:2181,10.217.6.124:2181,10.217.6.140:2181,10.217.6.222:2181,10.217.9.47:2181",
                ensemble.ConnectionString,
                5000,
                null);

            Thread.Sleep(1.Seconds());
            return(client);
        }
 protected static void DeleteNonexistentNode(string path, org.apache.zookeeper.ZooKeeper client)
 {
     try
     {
         client.deleteAsync(path).GetAwaiter().GetResult();
     }
     catch (Exception e)
     {
         e.Should().BeAssignableTo <KeeperException.NoNodeException>();
     }
     //deleteResult.Status.Should().Be(ZooKeeperStatus.NoNode);
     //deleteResult.Path.Should().Be(path);
 }
Ejemplo n.º 23
0
        public void Connect(Watcher watcher)
        {
            lock (_connectLock)
            {
                if (_zooKeeper != null)
                {
                    this.LogInfo("ZooKeeper client has been started");
                }

                this._zooKeeper = new org.apache.zookeeper.ZooKeeper(_address, (int)_sessionTimeout.TotalMilliseconds, watcher);
                this.LogInfo($"ZooKeeper client has been created and connect to {this._address}");
            }
        }
Ejemplo n.º 24
0
        private org.apache.zookeeper.ZooKeeper GetSession()
        {
            var session = new org.apache.zookeeper.ZooKeeper(_connectionString, 30000, new StubWatcher());

            return(session);

            //if (!_sessions.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
            //{
            //    var session = new org.apache.zookeeper.ZooKeeper(_connectionString, 60000, new StubWatcher());
            //    _sessions.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, session);
            //}
            //return _sessions[System.Threading.Thread.CurrentThread.ManagedThreadId];
        }
Ejemplo n.º 25
0
 private void CreateZooKeeperClient()
 {
     ZooKeeper?.closeAsync();
     if (ZooKeeperSessionId == 0)
     {
         ZooKeeper = new org.apache.zookeeper.ZooKeeper(ZooKeeperClientConfigure.ConnectionString,
                                                        ZooKeeperClientConfigure.SessionTimeout, ZooKeeperWatcher, ZooKeeperClientConfigure.CanBeReadOnly);
     }
     else
     {
         ZooKeeper = new org.apache.zookeeper.ZooKeeper(ZooKeeperClientConfigure.ConnectionString,
                                                        ZooKeeperClientConfigure.SessionTimeout, ZooKeeperWatcher, ZooKeeperSessionId, null, ZooKeeperClientConfigure.CanBeReadOnly);
     }
 }
Ejemplo n.º 26
0
        public static async Task DeleteRecursive(org.apache.zookeeper.ZooKeeper zk, string path = "", string key = "")
        {
            try
            {
                var correctedPath = path + "/" + key;
                var a             = await zk.getChildrenAsync(correctedPath);

                foreach (var child in a.Children)
                {
                    await DeleteRecursive(zk, correctedPath == "/"? "" : correctedPath, child);
                }
                await zk.deleteAsync(correctedPath);
            }
            catch (KeeperException.NoNodeException) { }
        }
Ejemplo n.º 27
0
        public async Task Setup()
        {
            StartZooKeeper();
            var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 100000, null);

            await DeleteRecursive(zk, "/FeatureFlags", "features");
            await CreateIfNotExist(zk, "/FeatureFlags/features/featureA", "true");
            await CreateIfNotExist(zk, "/FeatureFlags/features/featureB", "false");
            await CreateIfNotExist(zk, "/FeatureFlags/features/featureC", "true");
            await CreateIfNotExist(zk, "/FeatureFlags/features/featureD", "true");

            await zk.closeAsync();

            dynamicFeatureStore = new ZooKeeperFeatureStore("127.0.0.1:2181/FeatureFlags");
            featureStore        = new CachingFeatureStore(dynamicFeatureStore);
        }
Ejemplo n.º 28
0
        private static async Task Main(string[] args)
        {
            org.apache.zookeeper.ZooKeeper zooKeeper = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 5000, new WatcherSample());

            var res = await zooKeeper.createAsync("/node1", Encoding.UTF8.GetBytes("1"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

            Console.WriteLine(res);

            res = await zooKeeper.createAsync("/node2", Encoding.UTF8.GetBytes("1"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

            Console.WriteLine(res);



            PessimisticLock lLock = new PessimisticLock(zooKeeper);

            Console.WriteLine(await lLock.GetLock("4"));

            Console.WriteLine(await lLock.ReleaseLock("4"));

            List <Task> tasks = new List <Task>(5);

            for (int i = 0; i < 5; i++)
            {
                tasks.Add(new Task(() =>
                {
                    var lockRes = lLock.GetLock("4").Result;
                    Console.WriteLine($"{Thread.CurrentThread.Name} get  lock {lockRes}");
                }));
            }

            foreach (var task in tasks)
            {
                task.Start();
            }
            Task.WaitAll(tasks.ToArray());


            PessimisticLockV2 lockV2 = new PessimisticLockV2(zooKeeper);
            var lockV2res            = await lockV2.GetLock("111");

            Console.WriteLine(lockV2res);

            await Task.CompletedTask;
        }
Ejemplo n.º 29
0
        public void Close()
        {
            lock (_connectLock)
            {
                if (_zooKeeper == null)
                {
                    return;
                }
                this.LogInfo($"Closing ZooKeeper connected to {this._address}");


                Task.Run(async() =>
                {
                    await _zooKeeper.closeAsync().ConfigureAwait(false);
                }).ConfigureAwait(false).GetAwaiter().GetResult();

                _zooKeeper = null;
            }
        }
Ejemplo n.º 30
0
        public static async Task <string> CreateIfNotExist(org.apache.zookeeper.ZooKeeper zk, string path, string data = null)
        {
            var    p   = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var    s   = "";
            string ret = null;

            for (int i = 0; i < p.Length; i++)
            {
                s = s + "/" + p[i];
                try
                {
                    ret = await zk.createAsync(s, i == p.Length - 1?Encoding.UTF8.GetBytes(data) : null, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                               CreateMode.PERSISTENT);
                }
                catch (KeeperException.NodeExistsException)
                {
                }
            }
            return(ret);
        }