/// <summary>Startup: start ZK.</summary> /// <remarks> /// Startup: start ZK. It is only after this that /// the binding information is valid. /// </remarks> /// <exception cref="System.Exception"/> protected override void ServiceStart() { SetupSecurity(); ZooKeeperServer zkServer = new ZooKeeperServer(); FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, dataDir); zkServer.SetTxnLogFactory(ftxn); zkServer.SetTickTime(tickTime); Log.Info("Starting Local Zookeeper service"); factory = ServerCnxnFactory.CreateFactory(); factory.Configure(GetAddress(port), -1); factory.Startup(zkServer); string connectString = GetConnectionString(); Log.Info("In memory ZK started at {}\n", connectString); if (Log.IsDebugEnabled()) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); zkServer.DumpConf(pw); pw.Flush(); Log.Debug(sw.ToString()); } binding = new BindingInformation(); binding.ensembleProvider = new FixedEnsembleProvider(connectString); binding.description = GetName() + " reachable at \"" + connectString + "\""; AddDiagnostics(binding.description); // finally: set the binding information in the config GetConfig().Set(KeyRegistryZkQuorum, connectString); }
/// <exception cref="System.Exception"/> public virtual void TestHandleSessionExpiration() { ActiveStandbyElector.ActiveStandbyElectorCallback cb = cbs[0]; byte[] appData = appDatas[0]; ActiveStandbyElector elector = electors[0]; // Let the first elector become active elector.EnsureParentZNode(); elector.JoinElection(appData); ZooKeeperServer zks = GetServer(serverFactory); ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, appData); Org.Mockito.Mockito.Verify(cb, Org.Mockito.Mockito.Timeout(1000)).BecomeActive(); CheckFatalsAndReset(); Log.Info("========================== Expiring session"); zks.CloseSession(elector.GetZKSessionIdForTests()); // Should enter neutral mode when disconnected Org.Mockito.Mockito.Verify(cb, Org.Mockito.Mockito.Timeout(1000)).EnterNeutralMode (); // Should re-join the election and regain active ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, appData); Org.Mockito.Mockito.Verify(cb, Org.Mockito.Mockito.Timeout(1000)).BecomeActive(); CheckFatalsAndReset(); Log.Info("========================== Quitting election"); elector.QuitElection(false); ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, null); // Double check that we don't accidentally re-join the election // due to receiving the "expired" event. Thread.Sleep(1000); Org.Mockito.Mockito.Verify(cb, Org.Mockito.Mockito.Never()).BecomeActive(); ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, null); CheckFatalsAndReset(); }
/// <summary> /// The open callback /// </summary> /// <param name="cancellationToken">the Cancellation Token</param> /// <returns>A <see cref="Task"/> that tracks completion of this method</returns> public Task <string> OpenAsync(CancellationToken cancellationToken) { RingMasterServiceEventSource.Log.ListenerOpenAsync(this.uriPublished); this.server = new ZooKeeperServer(this.protocol, this.instrumentation, cancellationToken: cancellationToken); this.server.RegisterTransport(this.transport); this.server.OnInitSession = this.OnInitSession; this.transport.StartServer(this.port); return(Task.FromResult(this.uriPublished)); }
/// <exception cref="System.Exception"/> public override void SetUp() { base.SetUp(); zkServer = GetServer(serverFactory); for (int i = 0; i < NumElectors; i++) { cbs[i] = Org.Mockito.Mockito.Mock <ActiveStandbyElector.ActiveStandbyElectorCallback >(); appDatas[i] = Ints.ToByteArray(i); electors[i] = new ActiveStandbyElector(hostPort, 5000, ParentDir, ZooDefs.Ids.OpenAclUnsafe , Collections.EmptyList <ZKUtil.ZKAuthInfo>(), cbs[i], CommonConfigurationKeys.HaFcElectorZkOpRetriesDefault ); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> internal static ServerCnxnFactory CreateNewServerInstance(FilePath dataDir, ServerCnxnFactory factory, string hostPort, int maxCnxns) { ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); int Port = GetPort(hostPort); if (factory == null) { factory = ServerCnxnFactory.CreateFactory(Port, maxCnxns); } factory.Startup(zks); Assert.True("waiting for server up", ClientBaseWithFixes.WaitForServerUp ("127.0.0.1:" + Port, ConnectionTimeout)); return(factory); }
public MiniZKFCCluster(Configuration conf, ZooKeeperServer zks) { this.conf = conf; // Fast check interval so tests run faster conf.SetInt(CommonConfigurationKeys.HaHmCheckIntervalKey, 50); conf.SetInt(CommonConfigurationKeys.HaHmConnectRetryIntervalKey, 50); conf.SetInt(CommonConfigurationKeys.HaHmSleepAfterDisconnectKey, 50); svcs = new DummyHAService[2]; svcs[0] = new DummyHAService(HAServiceProtocol.HAServiceState.Initializing, new IPEndPoint ("svc1", 1234)); svcs[0].SetSharedResource(sharedResource); svcs[1] = new DummyHAService(HAServiceProtocol.HAServiceState.Initializing, new IPEndPoint ("svc2", 1234)); svcs[1].SetSharedResource(sharedResource); this.ctx = new MultithreadedTestUtil.TestContext(); this.zks = zks; }
public static void SetupZooKeeper() { Log.Info("Starting ZK server"); zkTmpDir = FilePath.CreateTempFile("zookeeper", "test"); zkTmpDir.Delete(); zkTmpDir.Mkdir(); try { zks = new ZooKeeperServer(zkTmpDir, zkTmpDir, ZooKeeperDefaultPort); serverFactory = new NIOServerCnxnFactory(); serverFactory.Configure(new IPEndPoint(ZooKeeperDefaultPort), 10); serverFactory.Startup(zks); } catch (Exception e) { Log.Error("Exception while instantiating ZooKeeper", e); } bool b = LocalBookKeeper.WaitForServerUp(Hostport, ConnectionTimeout); Log.Debug("ZooKeeper server up: " + b); }
public virtual void SetUp() { Runtime.SetProperty("zookeeper.preAllocSize", "100"); FileTxnLog.SetPreallocSize(100 * 1024); if (!Basetest.Exists()) { Basetest.Mkdirs(); } FilePath dataDir = CreateTmpDir(Basetest); zks = new ZooKeeperServer(dataDir, dataDir, 3000); int Port = System.Convert.ToInt32(hostPort.Split(":")[1]); if (factory == null) { factory = new NIOServerCnxnFactory(); factory.Configure(new IPEndPoint(Port), maxCnxns); } factory.Startup(zks); NUnit.Framework.Assert.IsTrue("waiting for server up", WaitForServerUp("127.0.0.1:" + Port, ConnectionTimeout)); }
internal static void ShutdownServerInstance(ServerCnxnFactory factory, string hostPort ) { if (factory != null) { ZKDatabase zkDb; { ZooKeeperServer zs = GetServer(factory); zkDb = zs.GetZKDatabase(); } factory.Shutdown(); try { zkDb.Close(); } catch (IOException ie) { Log.Warn("Error closing logs ", ie); } int Port = GetPort(hostPort); Assert.True("waiting for server down", ClientBaseWithFixes.WaitForServerDown ("127.0.0.1:" + Port, ConnectionTimeout)); } }
/// <exception cref="System.Exception"/> public virtual void TestHandleSessionExpirationOfStandby() { // Let elector 0 be active electors[0].EnsureParentZNode(); electors[0].JoinElection(appDatas[0]); ZooKeeperServer zks = GetServer(serverFactory); ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, appDatas [0]); Org.Mockito.Mockito.Verify(cbs[0], Org.Mockito.Mockito.Timeout(1000)).BecomeActive (); CheckFatalsAndReset(); // Let elector 1 be standby electors[1].JoinElection(appDatas[1]); ActiveStandbyElectorTestUtil.WaitForElectorState(null, electors[1], ActiveStandbyElector.State .Standby); Log.Info("========================== Expiring standby's session"); zks.CloseSession(electors[1].GetZKSessionIdForTests()); // Should enter neutral mode when disconnected Org.Mockito.Mockito.Verify(cbs[1], Org.Mockito.Mockito.Timeout(1000)).EnterNeutralMode (); // Should re-join the election and go back to STANDBY ActiveStandbyElectorTestUtil.WaitForElectorState(null, electors[1], ActiveStandbyElector.State .Standby); CheckFatalsAndReset(); Log.Info("========================== Quitting election"); electors[1].QuitElection(false); // Double check that we don't accidentally re-join the election // by quitting elector 0 and ensuring elector 1 doesn't become active electors[0].QuitElection(false); // due to receiving the "expired" event. Thread.Sleep(1000); Org.Mockito.Mockito.Verify(cbs[1], Org.Mockito.Mockito.Never()).BecomeActive(); ActiveStandbyElectorTestUtil.WaitForActiveLockData(null, zks, ParentDir, null); CheckFatalsAndReset(); }
/// <exception cref="System.Exception"/> public static void WaitForActiveLockData(MultithreadedTestUtil.TestContext ctx, ZooKeeperServer zks, string parentDir, byte[] activeData) { long st = Time.Now(); long lastPrint = st; while (true) { if (ctx != null) { ctx.CheckException(); } try { Stat stat = new Stat(); byte[] data = zks.GetZKDatabase().GetData(parentDir + "/" + ActiveStandbyElector. LockFilename, stat, null); if (activeData != null && Arrays.Equals(activeData, data)) { return; } if (Time.Now() > lastPrint + LogIntervalMs) { Log.Info("Cur data: " + StringUtils.ByteToHexString(data)); lastPrint = Time.Now(); } } catch (KeeperException.NoNodeException) { if (activeData == null) { return; } if (Time.Now() > lastPrint + LogIntervalMs) { Log.Info("Cur data: no node"); lastPrint = Time.Now(); } } Thread.Sleep(50); } }
protected internal static ZooKeeperServer GetServer(ServerCnxnFactory fac) { ZooKeeperServer zs = ServerCnxnFactoryAccessor.GetZkServer(fac); return(zs); }