예제 #1
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");
    }
예제 #2
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();
     }
 }
예제 #3
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 ();

			}
		}
예제 #4
0
		public void SessionTermination(){

			var latch = new AutoResetEvent (false);
			var sxl = new AutoResetEvent (false);
			var conlatch = new AutoResetEvent (false);
			var w1 = new CuratorWatcher ((e) => {
				if(e.State == KeeperState.SyncConnected){
					latch.Set();
				}
				else if(e.State == KeeperState.Expired){
					sxl.Set();
				}
			});
			var zookeeper = new ZooKeeper(connectionString,TimeSpan.FromMilliseconds(10000),w1);
			latch.WaitOne (5000);

			using (var zk = new ZooKeeper (connectionString, TimeSpan.FromMilliseconds (2000), new CuratorWatcher ((e) => {
				if (e.State == KeeperState.SyncConnected)
					conlatch.Set ();
			}), zookeeper.SessionId, zookeeper.SesionPassword)) {

				if (!conlatch.WaitOne (5000)) {
					Assert.Fail ();
				} 
			};

			if (!sxl.WaitOne (20000)) {
				Assert.Fail ();
			} 

			try{
				var stat = zookeeper.Exists ("/test", false);
				if (stat == null) {
					System.Diagnostics.Debug.WriteLine ("Node does not exits");

				}
			}
			catch(KeeperException e){

				System.Diagnostics.Debug.WriteLine ("Session Expired");

			}

		}
예제 #5
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;
                });

            }
                


        }