@Test public void shouldDeleteLocalTaskStoreWhenItHasNoOffsetFile() throws Exception { localStoreMonitor.monitor(); assertTrue("Task store directory should not exist.", !taskStoreDir.exists()); assertEquals(taskStoreSize, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); assertEquals(1, localStoreMonitorMetrics.noOfDeletedTaskPartitionStores.getCount()); }
@Test public void shouldDeleteInActiveLocalStoresOfTheJob() throws Exception { File inActiveStoreDir = new File(jobDir, "inActiveStore"); FileUtils.forceMkdir(inActiveStoreDir); File inActiveTaskDir = new File(inActiveStoreDir, "test-task"); FileUtils.forceMkdir(inActiveTaskDir); long inActiveTaskDirSize = inActiveTaskDir.getTotalSpace(); localStoreMonitor.monitor(); assertTrue("Inactive task store directory should not exist.", !inActiveTaskDir.exists()); assertEquals(taskStoreSize + inActiveTaskDirSize, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); assertEquals(2, localStoreMonitorMetrics.noOfDeletedTaskPartitionStores.getCount()); FileUtils.deleteDirectory(inActiveStoreDir); }
@Test public void shouldDeleteTaskStoreWhenTaskPreferredStoreIsNotLocalHost() throws Exception { Task task = new Task("notLocalHost", "test-task", "0", new ArrayList<>(), ImmutableList.of("test-store")); Mockito.when(jobsClientMock.getTasks(Mockito.any())).thenReturn(ImmutableList.of(task)); localStoreMonitor.monitor(); assertTrue("Task store directory should not exist.", !taskStoreDir.exists()); assertEquals(taskStoreSize, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); assertEquals(1, localStoreMonitorMetrics.noOfDeletedTaskPartitionStores.getCount()); }
@Test public void shouldDoNothingWhenTheJobIsRunning() throws Exception { Mockito.when(jobsClientMock.getJobStatus(Mockito.any())).thenReturn(JobStatus.STARTED); File offsetFile = createOffsetFile(taskStoreDir); localStoreMonitor.monitor(); assertTrue("Offset file should exist.", offsetFile.exists()); assertEquals(0, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); }
@Test public void shouldDeleteLocalStoreWhenLastModifiedTimeOfOffsetFileIsGreaterThanOffsetTTL() throws Exception { File offsetFile = createOffsetFile(taskStoreDir); offsetFile.setLastModified(0); localStoreMonitor.monitor(); assertTrue("Offset file should not exist.", !offsetFile.exists()); assertEquals(0, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); }
@Test public void shouldDoNothingWhenLastModifiedTimeOfOffsetFileIsLessThanOffsetTTL() throws Exception { File offsetFile = createOffsetFile(taskStoreDir); localStoreMonitor.monitor(); assertTrue("Offset file should exist.", offsetFile.exists()); assertEquals(0, localStoreMonitorMetrics.diskSpaceFreedInBytes.getCount()); }
@Test public void testFirstTimeSuccessPut() throws Exception { String tableId = "testFirstTimeSuccessPut"; TableRetryPolicy policy = new TableRetryPolicy(); policy.withFixedBackoff(Duration.ofMillis(100)); TableWriteFunction<String, String> writeFn = mock(TableWriteFunction.class); doReturn(true).when(writeFn).isRetriable(any()); doReturn(CompletableFuture.completedFuture("bar")).when(writeFn).putAsync(anyString(), anyString()); RetriableWriteFunction<String, String> retryIO = new RetriableWriteFunction<>(policy, writeFn, schedExec); retryIO.setMetrics(getMetricsUtil(tableId)); retryIO.putAsync("foo", "bar").get(); verify(writeFn, times(1)).putAsync(anyString(), anyString()); Assert.assertEquals(0, retryIO.retryMetrics.retryCount.getCount()); Assert.assertEquals(1, retryIO.retryMetrics.successCount.getCount()); Assert.assertEquals(0, retryIO.retryMetrics.retryTimer.getSnapshot().getMax()); }
@Test public void testFirstTimeSuccessGet() throws Exception { String tableId = "testFirstTimeSuccessGet"; TableRetryPolicy policy = new TableRetryPolicy(); policy.withFixedBackoff(Duration.ofMillis(100)); TableReadFunction<String, String> readFn = mock(TableReadFunction.class); doReturn(true).when(readFn).isRetriable(any()); doReturn(CompletableFuture.completedFuture("bar")).when(readFn).getAsync(anyString()); RetriableReadFunction<String, String> retryIO = new RetriableReadFunction<>(policy, readFn, schedExec); retryIO.setMetrics(getMetricsUtil(tableId)); Assert.assertEquals("bar", retryIO.getAsync("foo").get()); verify(readFn, times(1)).getAsync(anyString()); Assert.assertEquals(0, retryIO.retryMetrics.retryCount.getCount()); Assert.assertEquals(1, retryIO.retryMetrics.successCount.getCount()); Assert.assertEquals(0, retryIO.retryMetrics.retryTimer.getSnapshot().getMax()); }
@Test public void testIgnoreVersionConficts() throws Exception { ArgumentCaptor<BulkProcessor.Listener> listenerCaptor = ArgumentCaptor.forClass(BulkProcessor.Listener.class); when(BULK_PROCESSOR_FACTORY.getBulkProcessor(eq(CLIENT), listenerCaptor.capture())) .thenReturn(processorOne); producer.register(SOURCE_ONE); BulkResponse response = getRespWithFailedDocument(RestStatus.CONFLICT); listenerCaptor.getValue().afterBulk(0, null, response); assertEquals(1, metrics.conflicts.getCount()); producer.flush(SOURCE_ONE); }
@Test public void testMetrics() { ReadableMetricsRegistry registry = new MetricsRegistryMap(); ElasticsearchSystemProducerMetrics metrics = new ElasticsearchSystemProducerMetrics("es", registry); metrics.bulkSendSuccess.inc(29L); metrics.inserts.inc(); metrics.updates.inc(7L); metrics.conflicts.inc(3L); Set<String> groups = registry.getGroups(); assertEquals(1, groups.size()); assertEquals(GRP_NAME, groups.toArray()[0]); Map<String, Metric> metricMap = registry.getGroup(GRP_NAME); assertEquals(4, metricMap.size()); assertEquals(29L, ((Counter) metricMap.get("es-bulk-send-success")).getCount()); assertEquals(1L, ((Counter) metricMap.get("es-docs-inserted")).getCount()); assertEquals(7L, ((Counter) metricMap.get("es-docs-updated")).getCount()); assertEquals(3L, ((Counter) metricMap.get("es-version-conflicts")).getCount()); }
@Test public void testShouldRemoveBufferedEventsInDebounceQueueOnSessionExpiration() { ZkKeyBuilder keyBuilder = Mockito.mock(ZkKeyBuilder.class); ZkClient mockZkClient = Mockito.mock(ZkClient.class); when(keyBuilder.getJobModelVersionBarrierPrefix()).thenReturn(TEST_BARRIER_ROOT); ZkUtils zkUtils = Mockito.mock(ZkUtils.class); when(zkUtils.getKeyBuilder()).thenReturn(keyBuilder); when(zkUtils.getZkClient()).thenReturn(mockZkClient); when(zkUtils.getJobModel(TEST_JOB_MODEL_VERSION)).thenReturn(new JobModel(new MapConfig(), new HashMap<>())); ScheduleAfterDebounceTime mockDebounceTimer = Mockito.mock(ScheduleAfterDebounceTime.class); ZkJobCoordinator zkJobCoordinator = Mockito.spy(new ZkJobCoordinator("TEST_PROCESSOR_ID", new MapConfig(), new NoOpMetricsRegistry(), zkUtils)); zkJobCoordinator.debounceTimer = mockDebounceTimer; zkJobCoordinator.zkSessionMetrics = new ZkSessionMetrics(new MetricsRegistryMap()); final ZkSessionStateChangedListener zkSessionStateChangedListener = zkJobCoordinator.new ZkSessionStateChangedListener(); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.Expired); verify(zkUtils).incGeneration(); verify(mockDebounceTimer).cancelAllScheduledActions(); verify(mockDebounceTimer).scheduleAfterDebounceTime(Mockito.eq("ZK_SESSION_EXPIRED"), Mockito.eq(0L), Mockito.any(Runnable.class)); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSessionExpirations.getCount()); }
@Test public void testZookeeperSessionMetricsAreUpdatedCoorrectly() { ZkKeyBuilder keyBuilder = Mockito.mock(ZkKeyBuilder.class); ZkClient mockZkClient = Mockito.mock(ZkClient.class); when(keyBuilder.getJobModelVersionBarrierPrefix()).thenReturn(TEST_BARRIER_ROOT); ZkUtils zkUtils = Mockito.mock(ZkUtils.class); when(zkUtils.getKeyBuilder()).thenReturn(keyBuilder); when(zkUtils.getZkClient()).thenReturn(mockZkClient); when(zkUtils.getJobModel(TEST_JOB_MODEL_VERSION)).thenReturn(new JobModel(new MapConfig(), new HashMap<>())); ScheduleAfterDebounceTime mockDebounceTimer = Mockito.mock(ScheduleAfterDebounceTime.class); ZkJobCoordinator zkJobCoordinator = Mockito.spy(new ZkJobCoordinator("TEST_PROCESSOR_ID", new MapConfig(), new NoOpMetricsRegistry(), zkUtils)); zkJobCoordinator.debounceTimer = mockDebounceTimer; zkJobCoordinator.zkSessionMetrics = new ZkSessionMetrics(new MetricsRegistryMap()); final ZkSessionStateChangedListener zkSessionStateChangedListener = zkJobCoordinator.new ZkSessionStateChangedListener(); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.Disconnected); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.SyncConnected); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.AuthFailed); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSessionErrors.getCount()); zkSessionStateChangedListener.handleSessionEstablishmentError(new SamzaException("Test exception")); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSessionDisconnects.getCount()); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSyncConnected.getCount()); Assert.assertEquals(2, zkJobCoordinator.zkSessionMetrics.zkSessionErrors.getCount()); }
@Test public void testGetAll() throws Exception { ReadWriteTable table = createTable(false); Assert.assertEquals(values, table.getAll(keys)); Assert.assertEquals(values, table.getAllAsync(keys).get()); verify(kvStore, times(2)).getAll(any()); Assert.assertEquals(Collections.emptyMap(), table.getAll(Collections.emptyList())); Assert.assertEquals(2, numMissedLookups.getCount()); Assert.assertEquals(3, numGetAlls.getCount()); Assert.assertTrue(getAllNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, numGets.getCount()); Assert.assertEquals(0, getNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, getCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testGet() throws Exception { ReadWriteTable table = createTable(false); Assert.assertEquals("v1", table.get("k1")); Assert.assertEquals("v2", table.getAsync("k2").get()); Assert.assertNull(table.get("k3")); verify(kvStore, times(3)).get(any()); Assert.assertEquals(3, numGets.getCount()); Assert.assertEquals(1, numMissedLookups.getCount()); Assert.assertTrue(getNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, numGetAlls.getCount()); Assert.assertEquals(0, getAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, getCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testDeleteAll() throws Exception { ReadWriteTable table = createTable(false); table.deleteAll(Collections.emptyList()); table.deleteAllAsync(Collections.emptyList()).get(); verify(kvStore, times(2)).deleteAll(any()); Assert.assertEquals(2, numDeleteAlls.getCount()); Assert.assertTrue(deleteAllNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, putNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, putAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, flushNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, numPuts.getCount()); Assert.assertEquals(0, numPutAlls.getCount()); Assert.assertEquals(0, numDeletes.getCount()); Assert.assertEquals(0, numFlushes.getCount()); Assert.assertEquals(0, putCallbackNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testTimerDisabled() throws Exception { ReadWriteTable table = createTable(true); table.get(""); table.getAsync("").get(); table.getAll(keys); table.getAllAsync(keys).get(); Assert.assertEquals(2, numGets.getCount()); Assert.assertEquals(4, numMissedLookups.getCount()); Assert.assertEquals(2, numGetAlls.getCount()); Assert.assertEquals(0, getNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, getAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, getCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testPut() throws Exception { ReadWriteTable table = createTable(false); table.put("k1", "v1"); table.putAsync("k2", "v2").get(); table.putAsync("k3", null).get(); verify(kvStore, times(2)).put(any(), any()); verify(kvStore, times(1)).delete(any()); Assert.assertEquals(2, numPuts.getCount()); Assert.assertEquals(1, numDeletes.getCount()); Assert.assertTrue(putNs.getSnapshot().getAverage() > 0); Assert.assertTrue(deleteNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, putAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, flushNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, numPutAlls.getCount()); Assert.assertEquals(0, numDeleteAlls.getCount()); Assert.assertEquals(0, numFlushes.getCount()); Assert.assertEquals(0, putCallbackNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testPutAll() throws Exception { ReadWriteTable table = createTable(false); List<Entry> entries = Arrays.asList(new Entry("k1", "v1"), new Entry("k2", null)); table.putAll(entries); table.putAllAsync(entries).get(); verify(kvStore, times(2)).putAll(any()); verify(kvStore, times(2)).deleteAll(any()); Assert.assertEquals(2, numPutAlls.getCount()); Assert.assertEquals(2, numDeleteAlls.getCount()); Assert.assertTrue(putAllNs.getSnapshot().getAverage() > 0); Assert.assertTrue(deleteAllNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, putNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, flushNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, numPuts.getCount()); Assert.assertEquals(0, numDeletes.getCount()); Assert.assertEquals(0, numFlushes.getCount()); Assert.assertEquals(0, putCallbackNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testDelete() throws Exception { ReadWriteTable table = createTable(false); table.delete(""); table.deleteAsync("").get(); verify(kvStore, times(2)).delete(any()); Assert.assertEquals(2, numDeletes.getCount()); Assert.assertTrue(deleteNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, putNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, putAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, flushNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, numPuts.getCount()); Assert.assertEquals(0, numPutAlls.getCount()); Assert.assertEquals(0, numDeleteAlls.getCount()); Assert.assertEquals(0, numFlushes.getCount()); Assert.assertEquals(0, putCallbackNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteCallbackNs.getSnapshot().getAverage(), 0.001); }
@Test public void testFlush() { ReadWriteTable table = createTable(false); table.flush(); table.flush(); // Note: store.flush() is NOT called here verify(kvStore, times(0)).flush(); Assert.assertEquals(2, numFlushes.getCount()); Assert.assertTrue(flushNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, putNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, putAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, numPuts.getCount()); Assert.assertEquals(0, numPutAlls.getCount()); Assert.assertEquals(0, numDeletes.getCount()); Assert.assertEquals(0, numDeleteAlls.getCount()); Assert.assertEquals(0, putCallbackNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, deleteCallbackNs.getSnapshot().getAverage(), 0.001); }