ClusterRepairScheduler { public void scheduleRepairs(Cluster cluster) throws ReaperException { List<String> excludedClusters = context .config .getAutoScheduling() .getExcludedClusters() .stream() .map(Cluster::toSymbolicName) .collect(Collectors.toList()); if (excludedClusters.contains(cluster.getName())) { LOG.debug("Not creating schedules for excluded cluster {}.", cluster.getName()); return; } AtomicInteger scheduleIndex = new AtomicInteger(); ScheduledRepairDiffView schedulesDiff = ScheduledRepairDiffView.compareWithExistingSchedules(context, cluster); schedulesDiff.keyspacesDeleted().forEach(keyspace -> deleteRepairSchedule(cluster, keyspace)); schedulesDiff .keyspacesWithoutSchedules() .stream() .filter(keyspace -> keyspaceCandidateForRepair(cluster, keyspace)) .forEach( keyspace -> createRepairSchedule(cluster, keyspace, nextActivationStartDate(scheduleIndex.getAndIncrement()))); } ClusterRepairScheduler(AppContext context); void scheduleRepairs(Cluster cluster);  }
@Test public void schedulesRepairForAllKeyspacesInAllClusters() throws Exception { context.config.getAutoScheduling().setExcludedKeyspaces(null); context.storage.addCluster(cluster); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1", "keyspace3")); when(jmxProxy.getTablesForKeyspace("keyspace1")).thenReturn( Sets.newHashSet(Table.builder().withName("table1").withCompactionStrategy(STCS).build())); when(jmxProxy.getTablesForKeyspace("keyspace3")).thenReturn( Sets.newHashSet(Table.builder().withName("table1").withCompactionStrategy(STCS).build())); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(2); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(2) .repairScheduleForKeyspace("keyspace1") .hasSameConfigItemsAs(context.config) .hasOwner("auto-scheduling") .hasNextActivationDateCloseTo(timeOfFirstSchedule()) .andThen() .repairScheduleForKeyspace("keyspace3") .hasSameConfigItemsAs(context.config) .hasOwner("auto-scheduling") .hasNextActivationDateCloseTo(timeOfFirstSchedule()); }
@Test public void removeSchedulesForKeyspaceThatNoLongerExists() throws Exception { context.storage.addCluster(cluster); context.storage.addRepairSchedule(aRepairSchedule(cluster, "keyspace1", TWO_HOURS_AGO)); context.storage.addRepairSchedule(aRepairSchedule(cluster, "keyspace2", TWO_HOURS_AGO)); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1")); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(1); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(1) .repairScheduleForKeyspace("keyspace1").hasCreationTimeCloseTo(TWO_HOURS_AGO); }
@Test public void addSchedulesForNewKeyspace() throws Exception { context.storage.addCluster(cluster); context.storage.addRepairSchedule(aRepairSchedule(cluster, "keyspace1", TWO_HOURS_AGO)); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1", "keyspace2")); when(jmxProxy.getTablesForKeyspace("keyspace1")).thenReturn( Sets.newHashSet(Table.builder().withName("table1").withCompactionStrategy(STCS).build())); when(jmxProxy.getTablesForKeyspace("keyspace2")).thenReturn( Sets.newHashSet(Table.builder().withName("table2").withCompactionStrategy(STCS).build())); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(2); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(2) .repairScheduleForKeyspace("keyspace1").hasCreationTime(TWO_HOURS_AGO) .andThen() .repairScheduleForKeyspace("keyspace2").hasCreationTimeCloseTo(DateTime.now()); }
@Test public void doesNotScheduleRepairForSystemKeyspaces() throws Exception { context.storage.addCluster(cluster); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("system", "system_auth", "system_traces", "keyspace2")); when(jmxProxy.getTablesForKeyspace("keyspace2")).thenReturn( Sets.newHashSet(Table.builder().withName("table1").withCompactionStrategy(STCS).build())); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(1); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(1) .repairScheduleForKeyspace("keyspace2"); }
@Test public void doesNotScheduleRepairWhenKeyspaceHasNoTable() throws Exception { context.storage.addCluster(cluster); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1")); when(jmxProxy.getTablesForKeyspace("keyspace1")).thenReturn(Sets.newHashSet()); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(0); }
@Test public void spreadsKeyspaceScheduling() throws Exception { context.config = TestRepairConfiguration.defaultConfigBuilder() .withAutoScheduling(TestRepairConfiguration.defaultAutoSchedulingConfigBuilder() .thatIsEnabled() .withTimeBeforeFirstSchedule(DELAY_BEFORE_SCHEDULE) .withScheduleSpreadPeriod(Duration.ofHours(6)) .build()) .build(); context.storage.addCluster(cluster); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1", "keyspace2", "keyspace3", "keyspace4")); when(jmxProxy.getTablesForKeyspace("keyspace1")).thenReturn( Sets.newHashSet(Table.builder().withName("sometable").withCompactionStrategy(STCS).build())); when(jmxProxy.getTablesForKeyspace("keyspace2")).thenReturn( Sets.newHashSet(Table.builder().withName("sometable").withCompactionStrategy(STCS).build())); when(jmxProxy.getTablesForKeyspace("keyspace4")).thenReturn( Sets.newHashSet(Table.builder().withName("sometable").withCompactionStrategy(STCS).build())); clusterRepairAuto.scheduleRepairs(cluster); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(3) .repairScheduleForKeyspace("keyspace1") .hasNextActivationDateCloseTo(timeOfFirstSchedule()) .andThen() .repairScheduleForKeyspace("keyspace2") .hasNextActivationDateCloseTo(timeOfFirstSchedule().plusHours(6)) .andThen() .repairScheduleForKeyspace("keyspace4") .hasNextActivationDateCloseTo(timeOfFirstSchedule().plusHours(12)); }
@Test public void doesNotScheduleRepairWhenClusterExcluded() throws Exception { context.config.getAutoScheduling().setExcludedClusters(Arrays.asList(cluster.getName())); context.storage.addCluster(cluster); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(0); }
@Test public void scheduleRepairsWhenClusterNotExcluded() throws Exception { context.config.getAutoScheduling().setExcludedClusters( Arrays.asList(cluster.getName() + "-1", cluster.getName() + "-2") ); context.storage.addCluster(cluster); context.storage.addRepairSchedule(aRepairSchedule(cluster, "keyspace1", TWO_HOURS_AGO)); when(jmxProxy.getKeyspaces()).thenReturn(Lists.newArrayList("keyspace1")); clusterRepairAuto.scheduleRepairs(cluster); assertThat(context.storage.getAllRepairSchedules()).hasSize(1); assertThatClusterRepairSchedules(context.storage.getRepairSchedulesForCluster(cluster.getName())) .hasScheduleCount(1) .repairScheduleForKeyspace("keyspace1").hasCreationTimeCloseTo(TWO_HOURS_AGO); }