Example #1
0
        public static bool AmILeader(Server server)
        {
            try
            {
                bool result = helper.RetryOperation(() =>
                {
                    IZooKeeper zookeeper   = _zookeeper;
                    string shardLeaderPath = ZkPath.LeaderPath(ZkPath.CollectionName, server.ShardName);
                    Stat stat = zookeeper.Exists(shardLeaderPath, null);
                    if (stat == null)
                    {
                        throw new NotProcessLeaderElectionException(shardLeaderPath);
                    }

                    Server dataServer = getDataSever(shardLeaderPath);
                    if (dataServer == server)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                });
                return(result);
            }
            catch (KeeperException.SessionExpiredException ex)
            {
                helper.Dispose();
                helper = new LeaderHelper(_zookeeper);
                throw ex;
            }
        }
Example #2
0
        public static List <string> GetSortedChildren(IZooKeeper client, string basePath, string lockName,
                                                      ILockInternalsSorter sorter)
        {
            var children = client.GetChildren(basePath, false);

            return(GetSortedChildren(lockName, sorter, children));
        }
Example #3
0
        private void FindPrefixInChildren(String prefix, IZooKeeper zookeeper, String dir)
        {
            var names = Zookeeper.GetChildren(dir, false);

            foreach (string name in names)
            {
                if (name.StartsWith(prefix))
                {
                    id = name;
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Found id created last time: {0}", id);
                    }
                    break;
                }
            }
            if (id == null)
            {
                id = zookeeper.Create(dir.Combine(prefix), data, Acl, CreateMode.EphemeralSequential);

                if (LOG.IsDebugEnabled)
                {
                    LOG.DebugFormat("Created id: {0}", id);
                }
            }
        }
 public ProtocolSupport(IZooKeeper zookeeper)
 {
     RetryDelay = new TimeSpan(0, 0, 0, 0, 500);
     Acl        = Ids.OPEN_ACL_UNSAFE;
     RetryCount = 10;
     Zookeeper  = zookeeper;
 }
Example #5
0
        public ZooKeeperClient(ZooKeeperConfigSection configuration)
        {
            if (!ValidateHelper.IsPlumpString(configuration.Server))
            {
                throw new ArgumentNullException("zookeeper server can not be empty");
            }

            ConnectionLossTimeout = configuration.SessionTimeOut;
            var timeOut = TimeSpan.FromMilliseconds(configuration.SessionTimeOut);

            if (AppContext.IsRegistered <IWatcher>())
            {
                _zookeeper = new ZooKeeper(
                    configuration.Server,
                    timeOut,
                    AppContext.GetObject <IWatcher>());
            }
            else
            {
                _zookeeper = new ZooKeeper(
                    configuration.Server,
                    timeOut,
                    new DefaultWatcher());
            }
        }
Example #6
0
 public IkZooKeeperClient(string address, TimeSpan sessionTimeOut, IWatcher watcher)
 {
     this._client         = new ZooKeeper(address, sessionTimeOut, watcher);
     this._address        = address;
     this._sessionTimeOut = sessionTimeOut;
     this._watcher        = watcher;
 }
Example #7
0
        /**
         * Return the children of the given path sorted by sequence number
         *
         * @param zookeeper the client
         * @param path      the path
         * @return sorted list of children
         * @throws InterruptedException thread interruption
         * @throws org.apache.zookeeper.KeeperException
         *                              zookeeper errors
         */

        public static List <String> GetSortedChildren(IZooKeeper zookeeper, String path)
        {
            var children   = zookeeper.GetChildren(path, false);
            var sortedList = new List <string>(children.OrderBy(x => x));

            return(sortedList);
        }
Example #8
0
 public WriteLock(IZooKeeper zookeeper, string dir, List <ACL> acl) : base(zookeeper)
 {
     this.dir = dir;
     if (acl != null)
     {
         Acl = acl;
     }
 }
Example #9
0
 private IZooKeeper EnsureAvailableZooKeeperClient()
 {
     if (!this._client.State.IsAlive())
     {
         this._client = new ZooKeeper(_address, _sessionTimeOut, _watcher, this._client.SessionId, this._client.SesionPassword);
     }
     return(this._client);
 }
Example #10
0
        public static IEnumerable <string> GetParticipantNodes(IZooKeeper client, string basePath, string lockName,
                                                               ILockInternalsSorter sorter)
        {
            var names       = GetSortedChildren(client, basePath, lockName, sorter);
            var transformed = names.Select(x => ZKPaths.MakePath(basePath, x));

            return(transformed);
        }
Example #11
0
        /**
         * Kill the given ZK session
         *
         * @param client the client to kill
         * @param connectString server connection string
         * @param maxMs max time ms to wait for kill
         * @throws Exception errors
         */
        public static void kill(IZooKeeper client, String connectString, int maxMs)
        {
            System.Diagnostics.Debug.WriteLine("Kill Start");
            long startTicks = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds;

            var      sessionLostLatch = new AutoResetEvent(false);
            IWatcher sessionLostWatch = new CuratorWatcher((e) => { if (e.State == KeeperState.Expired)
                                                                    {
                                                                        sessionLostLatch.Set();
                                                                    }
                                                           });

            client.Exists("/___CURATOR_KILL_SESSION___" + System.DateTime.Now.Ticks, sessionLostWatch);

            var connectionLatch   = new AutoResetEvent(false);
            var connectionWatcher = new CuratorWatcher((e) => {
                if (e.State == KeeperState.SyncConnected)
                {
                    connectionLatch.Set();
                }
            });


            IZooKeeper zk = new ZooKeeper(connectString, TimeSpan.FromMilliseconds(maxMs), connectionWatcher, client.SessionId, client.SesionPassword);

            try
            {
                if (!connectionLatch.WaitOne(maxMs))
                {
                    throw new Exception("KillSession could not establish duplicate session");
                }
                try
                {
                    zk.Dispose();
                }
                finally
                {
                    zk = null;
                }

                while (client.State.IsAlive() && !sessionLostLatch.WaitOne(100))
                {
                    long elapsed = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds - startTicks;
                    if (elapsed > maxMs)
                    {
                        throw new Exception("KillSession timed out waiting for session to expire");
                    }
                }
            }
            finally
            {
                if (zk != null)
                {
                    zk.Dispose();
                }
            }
            System.Diagnostics.Debug.WriteLine("Kill End");
        }
Example #12
0
 public LeaderWatcher(IZooKeeper zooKeeper, LeaderElection election, string penultimatePath, ILeaderWatcher watcher, string path, string id)
 {
     this.zooKeeper       = zooKeeper;
     this.election        = election;
     this.penultimatePath = penultimatePath;
     this.watcher         = watcher;
     this.path            = path;
     this.id = id;
 }
Example #13
0
        private static Server getDataSever(string shardLeaderPath)
        {
            IZooKeeper zookeeper = _zookeeper;
            var        data      = zookeeper.GetData(shardLeaderPath, false, null);
            string     dataStr   = EncodeHelper.ByteArrayToString(data);
            Server     ser       = JsonConvert.DeserializeObject <Server>(dataStr);

            return(ser);
        }
        public PredicateResults GetsTheLock(IZooKeeper client, IEnumerable <string> children, string sequenceNodeName, int maxLeases)
        {
            var scannableChildren = children as string[] ?? children.ToArray();
            var ourIndex          = Array.IndexOf(scannableChildren, sequenceNodeName);

            ValidateOurIndex(sequenceNodeName, ourIndex);

            var    getsTheLock = ourIndex < maxLeases;
            String pathToWatch = getsTheLock ? null : scannableChildren[ourIndex - maxLeases];

            return(new PredicateResults(pathToWatch, getsTheLock));
        }
Example #15
0
 private void Update()
 {
     if (zooKeeper == null)
     {
         zooKeeper = new ZooKeeper(
             zooKeeperConnection,
             TimeSpan.FromSeconds(10), this);
     }
     state = SolrCloudStateParser.Parse(
         Encoding.Default.GetString(
             zooKeeper.GetData("/clusterstate.json", true, null)));
 }
Example #16
0
        public LockInternals(IZooKeeper client, ILockInternalsDriver driver, string path, string lockName,
                             int maxLeases)
        {
            _driver    = driver;
            _lockName  = lockName;
            _maxLeases = maxLeases;
            PathUtils.ValidatePath(path);

            _client   = client;
            _basePath = path;
            _path     = ZKPaths.MakePath(path, lockName);
        }
Example #17
0
        /**
         * Kill the given ZK session
         *
         * @param client the client to kill
         * @param connectString server connection string
         * @param maxMs max time ms to wait for kill
         * @throws Exception errors
         */
        public static void kill(IZooKeeper client, String connectString, int maxMs) 
        {
			System.Diagnostics.Debug.WriteLine ("Kill Start");
            long startTicks = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds;

			var sessionLostLatch = new AutoResetEvent(false);
			IWatcher sessionLostWatch = new CuratorWatcher((e)=> { if(e.State == KeeperState.Expired) sessionLostLatch.Set();});
            client.Exists("/___CURATOR_KILL_SESSION___" + System.DateTime.Now.Ticks, sessionLostWatch);

			var connectionLatch = new AutoResetEvent(false);
			var connectionWatcher = new CuratorWatcher((e)=> {
				if(e.State == KeeperState.SyncConnected){
					connectionLatch.Set();
				}
			});

             
            IZooKeeper zk = new ZooKeeper(connectString, TimeSpan.FromMilliseconds(maxMs), connectionWatcher, client.SessionId, client.SesionPassword);
            try
            {
                if ( !connectionLatch.WaitOne(maxMs) )
                {
                    throw new Exception("KillSession could not establish duplicate session");
                }
                try
                {
                        zk.Dispose();
                }
                finally
                {
                    zk = null;
                }

				while ( client.State.IsAlive() && !sessionLostLatch.WaitOne(100) )
                {
                    long elapsed = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds - startTicks;
                    if ( elapsed > maxMs )
                    {
                        throw new Exception("KillSession timed out waiting for session to expire");
                    }
                }
            }
            finally
            {
                if ( zk != null )
                {
                    zk.Dispose();
                }
            }
			System.Diagnostics.Debug.WriteLine ("Kill End");
    }
Example #18
0
            public IZooKeeper GetZooKeeper()
            {
                lock (this)
                {
                    if (zooKeeperHandle == null)
                    {
                        connectionString = parent.ensembleProvider.GetConnectionString();
                        zooKeeperHandle  = parent.zookeeperFactory.NewZooKeeper(connectionString, parent.sessionTimeout, parent.watcher, parent.canBeReadOnly);
                    }

                    parent.helper = new SyncHelper.Helper(this);
                    return(zooKeeperHandle);
                }
            }
Example #19
0
        public static Server ShardLeader(string shardName)
        {
            IZooKeeper zookeeper       = _zookeeper;
            string     shardLeaderPath = ZkPath.LeaderPath(ZkPath.CollectionName, shardName);
            Stat       stat            = zookeeper.Exists(shardLeaderPath, null);

            if (stat == null)
            {
                return(null);
            }
            Server dataServer = getDataSever(shardLeaderPath);

            return(dataServer);
        }
        private static int Dequeue(IZooKeeper zk, string path, IReadOnlyList<string> nodes)
        {
            Stat stat = null;

            //Get minimum node
            var minstr = GetMinimumNode(nodes);

            var b = zk.GetData(path + "/element" + minstr, false, stat);

            zk.Delete(path + "/element" + minstr, 0);

            var buffer = Convert.ToInt32(b.First());
            var retvalue = buffer;
            return retvalue;
        }
Example #21
0
        private static int Dequeue(IZooKeeper zk, string path, IReadOnlyList <string> nodes)
        {
            Stat stat = null;

            //Get minimum node
            var minstr = GetMinimumNode(nodes);

            var b = zk.GetData(path + "/element" + minstr, false, stat);

            zk.Delete(path + "/element" + minstr, 0);

            var buffer   = Convert.ToInt32(b.First());
            var retvalue = buffer;

            return(retvalue);
        }
Example #22
0
 private bool Update()
 {
     try {
         if (zooKeeper == null)
         {
             zooKeeper = new ZooKeeper(zooKeeperConnection, TimeSpan.FromSeconds(10), this);
         }
         Collections = SolrClusterStateParser.ParseJsonToCollections(
             Encoding.Default.GetString(
                 zooKeeper.GetData("/clusterstate.json", true, null)));
         return(true);
     } catch (Exception exception) {
         exceptionHandlers.Handle(exception);
     }
     return(false);
 }
Example #23
0
        public ZooKeeperClient(ZooKeeperConfigSection configuration, IWatcher watcher = null)
        {
            if (!ValidateHelper.IsPlumpString(configuration.Server))
            {
                throw new ArgumentNullException("zookeeper server can not be empty");
            }

            ConnectionLossTimeout = configuration.SessionTimeOut;
            var timeOut = TimeSpan.FromMilliseconds(configuration.SessionTimeOut);


            _zookeeper = new ZooKeeper(
                configuration.Server,
                timeOut,
                watcher ?? new DefaultWatcher());
        }
Example #24
0
 private void InternalClose()
 {
     try
     {
         IZooKeeper zooKeeper = (helper != null) ? helper.GetZooKeeper() : null;
         if (zooKeeper != null)
         {
             IWatcher dummyWatcher = new CuratorWatcher();
             zooKeeper.Register(dummyWatcher);                       // clear the default watcher so that no new events get processed by mistake
             zooKeeper.Dispose();
         }
     }
     catch (ThreadInterruptedException dummy)
     {
         Thread.CurrentThread.Interrupt();
     }
 }
Example #25
0
        public void TestExpiredSession()
        {
            var timing  = new Timing();
            var latch   = new AutoResetEvent(false);
            var watcher = new CuratorWatcher((e) => {
                if (e.State == KeeperState.Expired)
                {
                    latch.Set();
                }
            });

            using (var client = new CuratorZookeeperClient(server.GetConnectionString(), timing.session(), timing.connection(), watcher, new RetryOneTime(TimeSpan.FromMilliseconds(2))))
            {
                client.Start();

                bool firstTime = true;
                RetryLoop.CallWithRetry <bool>(client, () =>
                {
                    if (firstTime)
                    {
                        try
                        {
                            var stat = client.GetZooKeeper().Exists("/foo", false);
                            if (stat == null)
                            {
                                client.GetZooKeeper().Create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                            }
                        }
                        catch (KeeperException.NodeExistsException ignore)
                        {
                        }

                        KillSession.kill(client.GetZooKeeper(), server.GetConnectionString());
                        Assert.IsFalse(timing.awaitLatch(latch));
                    }

                    IZooKeeper zooKeeper = client.GetZooKeeper();
                    client.BlockUntilConnectedOrTimedOut();
                    Assert.IsNotNull(zooKeeper.Exists("/foo", false));

                    return(true);
                });
            }
        }
Example #26
0
        public UtilsBase(IZooKeeper zooKeeper, IZkCache zkCache, IZkSerialization zkSerialization, TimeSpan syncInterval, string filePath) : base(zooKeeper)
        {
            if (zooKeeper == null)
            {
                throw new ArgumentNullException("zookeeper");
            }

            ZkCache = zkCache ?? throw new ArgumentNullException("zkCache");

            ZkSerialization = zkSerialization;

            this.LocalZkFilePath = filePath;

            SyncInterval   = syncInterval;
            timer          = new Timer(SyncInterval.TotalSeconds);
            timer.Elapsed += Timer_Elapsed;

            Init();
        }
Example #27
0
        /**
         * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish
         * between directories and files. So, every node in the path is created. The data for each node is an empty blob
         *
         * @param zookeeper the client
         * @param path      path to ensure
         * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created
         * @throws InterruptedException thread interruption
         * @throws org.apache.zookeeper.KeeperException
         *                              Zookeeper errors
         */

        public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode)
        {
            PathUtils.ValidatePath(path);

            var pos = 1; // skip first slash, root is guaranteed to exist

            do
            {
                pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1);

                if (pos == -1)
                {
                    if (makeLastNode)
                    {
                        pos = path.Length;
                    }
                    else
                    {
                        break;
                    }
                }

                var subPath = path.Substring(0, pos);
                if (zookeeper.Exists(subPath, false) == null)
                {
                    try
                    {
                        zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                    }
                    catch (KeeperException.NodeExistsException e)
                    {
                        // ignore... someone else has created it since we checked
                    }
                }
            }while (pos < path.Length);
        }
Example #28
0
 public Helper(SyncHelper parent)
 {
     this.parent      = parent;
     zooKeeperHandle  = parent.zooKeeperHandle;
     connectionString = parent.connectionString;
 }
Example #29
0
        /**
         * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish
         * between directories and files. So, every node in the path is created. The data for each node is an empty blob
         *
         * @param zookeeper the client
         * @param path      path to ensure
         * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created
         * @throws InterruptedException thread interruption
         * @throws org.apache.zookeeper.KeeperException
         *                              Zookeeper errors
         */
        public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode)
        {
            PathUtils.ValidatePath(path);

            var pos = 1; // skip first slash, root is guaranteed to exist
            do
            {
                pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1);

                if (pos == -1)
                {
                    if (makeLastNode)
                    {
                        pos = path.Length;
                    }
                    else
                    {
                        break;
                    }
                }

                var subPath = path.Substring(0, pos);
                if (zookeeper.Exists(subPath, false) == null)
                {
                    try
                    {
                        zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                    }
                    catch (KeeperException.NodeExistsException e)
                    {
                        // ignore... someone else has created it since we checked
                    }
                }

            }
            while (pos < path.Length);
        }
Example #30
0
 public ConfigCenterWatcher(IZooKeeper zooKeeper, IZkCache zkCache)
 {
     this.zkCache   = zkCache;
     this.Zookeeper = zooKeeper;
 }
Example #31
0
 /**
  * Kill the given ZK session
  *
  * @param client the client to kill
  * @param connectString server connection string
  * @throws Exception errors
  */
 public static void kill(IZooKeeper client, String connectString) 
 {
     kill(client, connectString, new Timing().forWaiting().milliseconds());
 }
 private void Update()
 {
     if (zooKeeper == null)
         zooKeeper = new ZooKeeper(
             zooKeeperConnection,
             TimeSpan.FromSeconds(10), this);
     state = SolrCloudStateParser.Parse(
         Encoding.Default.GetString(
             zooKeeper.GetData("/clusterstate.json", true, null)));
 }
Example #33
0
 /**
  * Kill the given ZK session
  *
  * @param client the client to kill
  * @param connectString server connection string
  * @throws Exception errors
  */
 public static void kill(IZooKeeper client, String connectString)
 {
     kill(client, connectString, new Timing().forWaiting().milliseconds());
 }
Example #34
0
 public InterProcessMutex(IZooKeeper client, string path)
     : this(client, path, LOCK_NAME, 1, new StandardLockInternalsDriver())
 {
 }
Example #35
0
 public InterProcessMutex(IZooKeeper client, string path, string lockName, int maxLeases,
                          ILockInternalsDriver driver)
 {
     _basePath  = path;
     _internals = new LockInternals(client, driver, path, lockName, maxLeases);
 }
Example #36
0
 /**
  * Return the children of the given path sorted by sequence number
  *
  * @param zookeeper the client
  * @param path      the path
  * @return sorted list of children
  * @throws InterruptedException thread interruption
  * @throws org.apache.zookeeper.KeeperException
  *                              zookeeper errors
  */
 public static List<String> GetSortedChildren(IZooKeeper zookeeper, String path)
 {
     var children = zookeeper.GetChildren(path, false);
     var sortedList = new List<string>(children.OrderBy(x => x));
     return sortedList;
 }
Example #37
0
			public IZooKeeper GetZooKeeper() 
			{
                lock (this)
                {

                    if (zooKeeperHandle == null)
                    {
                        connectionString = parent.ensembleProvider.GetConnectionString();
                        zooKeeperHandle =  parent.zookeeperFactory.NewZooKeeper(connectionString, parent.sessionTimeout, parent.watcher, parent.canBeReadOnly);
                    }

                    parent.helper = new SyncHelper.Helper(this);
                    return zooKeeperHandle;
                }
			}
        public PredicateResults GetsTheLock(IZooKeeper client, IEnumerable<string> children, string sequenceNodeName, int maxLeases)
        {
            var scannableChildren = children as string[] ?? children.ToArray();
            var ourIndex = Array.IndexOf(scannableChildren, sequenceNodeName);
            ValidateOurIndex(sequenceNodeName, ourIndex);

            var getsTheLock = ourIndex < maxLeases;
            String pathToWatch = getsTheLock ? null : scannableChildren[ourIndex - maxLeases];

            return new PredicateResults(pathToWatch, getsTheLock);
        }
Example #39
0
 public Helper(SyncHelper parent)
 {
     this.parent = parent;
     zooKeeperHandle = parent.zooKeeperHandle;
     connectionString = parent.connectionString;
 }