/** * Creates a ZooKeeperServer instance. Nothing is setup, use the setX * methods to prepare the instance (eg datadir, datalogdir, ticktime, * builder, etc...) * * @throws IOException */ public ZooKeeperServer() { serverStats = new ServerStats(this); listener = new ZooKeeperServerListenerImpl(this); }
/** * Creates a ZooKeeperServer instance. Nothing is setup, use the setX * methods to prepare the instance (eg datadir, datalogdir, ticktime, * builder, etc...) * * @throws IOException */ public ZooKeeperServer() { serverStats = new ServerStats(this); listener = new ZooKeeperServerListenerImpl(this); }
@Before public void setUp() { outputWriter = new StringWriter(); ServerCnxn serverCnxnMock = mock(ServerCnxn.class); LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class); when(zks.isRunning()).thenReturn(true); providerMock = mock(ServerStats.Provider.class); when(zks.serverStats()).thenReturn(new ServerStats(providerMock)); ZKDatabase zkDatabaseMock = mock(ZKDatabase.class); when(zks.getZKDatabase()).thenReturn(zkDatabaseMock); Leader leaderMock = mock(Leader.class); when(leaderMock.getProposalStats()).thenReturn(new BufferStats()); when(zks.getLeader()).thenReturn(leaderMock); ServerCnxnFactory serverCnxnFactory = mock(ServerCnxnFactory.class); ServerCnxn serverCnxn = mock(ServerCnxn.class); List<ServerCnxn> connections = new ArrayList<>(); connections.add(serverCnxn); when(serverCnxnFactory.getConnections()).thenReturn(connections); statCommand = new StatCommand(new PrintWriter(outputWriter), serverCnxnMock, FourLetterCommands.statCmd); statCommand.setZkServer(zks); statCommand.setFactory(serverCnxnFactory); }
/** * Creates a ZooKeeperServer instance. It sets everything up, but doesn't * actually start listening for clients until run() is invoked. * * @param dataDir the directory to put the data */ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, DataTreeBuilder treeBuilder, ZKDatabase zkDb) { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.txnLogFactory.setServerStats(this.serverStats); this.zkDb = zkDb; this.tickTime = tickTime; this.minSessionTimeout = minSessionTimeout; this.maxSessionTimeout = maxSessionTimeout; listener = new ZooKeeperServerListenerImpl(this); LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir()); }
/** * Creates a ZooKeeperServer instance. It sets everything up, but doesn't * actually start listening for clients until run() is invoked. * * @param dataDir the directory to put the data */ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb) { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.txnLogFactory.setServerStats(this.serverStats); this.zkDb = zkDb; this.tickTime = tickTime; setMinSessionTimeout(minSessionTimeout); setMaxSessionTimeout(maxSessionTimeout); listener = new ZooKeeperServerListenerImpl(this); readResponseCache = new ResponseCache(); connThrottle = new BlueThrottle(); LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir()); }
@Test public void testFsyncThresholdExceedMetrics() { // Given ... ServerStats serverStats = new ServerStats(providerMock); int incrementCount = 30; // When increment ... for (int i = 0; i < incrementCount; i++) { serverStats.incrementFsyncThresholdExceedCount(); } // Then ... Assert.assertEquals(incrementCount, serverStats.getFsyncThresholdExceedCount()); // When reset ... serverStats.resetFsyncThresholdExceedCount(); // Then ... assertFsyncThresholdExceedCountZero(serverStats); }
public void testSyncThresholdExceedCount() throws IOException { // Given ... // Set threshold to -1, as after the first commit it takes 0ms to commit to disk. java.lang.System.setProperty(FileTxnLog.ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY, "-1"); ServerStats.Provider providerMock = mock(ServerStats.Provider.class); ServerStats serverStats = new ServerStats(providerMock); File logDir = ClientBase.createTmpDir(); FileTxnLog fileTxnLog = new FileTxnLog(logDir); fileTxnLog.setServerStats(serverStats); // Verify serverStats is 0 before any commit Assert.assertEquals(0L, serverStats.getFsyncThresholdExceedCount()); // When ... for (int i = 0; i < 50; i++) { fileTxnLog.append(new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.create), new CreateTxn("/testFsyncThresholdCountIncreased", new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0)); fileTxnLog.commit(); // only 1 commit, otherwise it will be flaky // Then ... verify serverStats is updated to the number of commits (as threshold is set to 0) Assert.assertEquals((long) i + 1 , serverStats.getFsyncThresholdExceedCount()); } }
@Test public void testLatencyMetrics() { // Given ... ServerStats serverStats = new ServerStats(providerMock); // When incremented... Request fakeRequest = new Request(0, 0, 0, null, null, 0); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 2000); // Then ... assertThat("Max latency check", 2000L, lessThanOrEqualTo(serverStats.getMaxLatency())); assertThat("Min latency check", 1000L, lessThanOrEqualTo(serverStats.getMinLatency())); Assert.assertEquals((double)1500, serverStats.getAvgLatency(), (double)200); // When reset... serverStats.resetLatency(); // Then ... assertAllLatencyZero(serverStats); }
@Test public void testReset() { // Given ... ServerStats serverStats = new ServerStats(providerMock); assertAllPacketsZero(serverStats); assertAllLatencyZero(serverStats); // When ... Request fakeRequest = new Request(0, 0, 0, null, null, 0); serverStats.incrementPacketsSent(); serverStats.incrementPacketsReceived(); serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); serverStats.reset(); // Then ... assertAllPacketsZero(serverStats); assertAllLatencyZero(serverStats); }
@Test public void testPacketsMetrics() { // Given ... ServerStats serverStats = new ServerStats(providerMock); int incrementCount = 20; // When increment ... for (int i = 0; i < incrementCount; i++) { serverStats.incrementPacketsSent(); serverStats.incrementPacketsReceived(); serverStats.incrementPacketsReceived(); } // Then ... Assert.assertEquals(incrementCount, serverStats.getPacketsSent()); Assert.assertEquals(incrementCount*2, serverStats.getPacketsReceived()); // When reset ... serverStats.resetRequestCounters(); // Then ... assertAllPacketsZero(serverStats); }
/** * Creates a ZooKeeperServer instance. Nothing is setup, use the setX * methods to prepare the instance (eg datadir, datalogdir, ticktime, * builder, etc...) * * @throws IOException */ public ZooKeeperServer() { serverStats = new ServerStats(this); }
/** * Creates a ZooKeeperServer instance. It sets everything up, but doesn't * actually start listening for clients until run() is invoked. * * @param dataDir the directory to put the data * @throws IOException */ public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, DataTreeBuilder treeBuilder, ZKDatabase zkDb) throws IOException { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.zkDb = zkDb; this.tickTime = tickTime; this.minSessionTimeout = minSessionTimeout; this.maxSessionTimeout = maxSessionTimeout; LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir()); }