HadoopArchiveLogs implements Tool { @VisibleForTesting void checkMaxEligible() { if (maxEligible > 0 && eligibleApplications.size() > maxEligible) { if (verbose) { LOG.info("Too many applications (" + eligibleApplications .size() + " > " + maxEligible + ")"); } List<AppInfo> sortedApplications = new ArrayList<AppInfo>(eligibleApplications); Collections.sort(sortedApplications, new Comparator< AppInfo>() { @Override public int compare(AppInfo o1, AppInfo o2) { int lCompare = Long.compare(o1.getFinishTime(), o2.getFinishTime()); if (lCompare == 0) { return o1.getAppId().compareTo(o2.getAppId()); } return lCompare; } }); for (int i = maxEligible; i < sortedApplications.size(); i++) { if (verbose) { LOG.info("Removing " + sortedApplications.get(i)); } eligibleApplications.remove(sortedApplications.get(i)); } } } HadoopArchiveLogs(Configuration conf); static void main(String[] args); @Override int run(String[] args); @Override void setConf(Configuration conf); @Override Configuration getConf();  }
@Test(timeout = 10000) public void testCheckMaxEligible() throws Exception { Configuration conf = new Configuration(); HadoopArchiveLogs.AppInfo app1 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 1).toString(), USER); app1.setFinishTime(CLUSTER_TIMESTAMP - 5); HadoopArchiveLogs.AppInfo app2 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 2).toString(), USER); app2.setFinishTime(CLUSTER_TIMESTAMP - 10); HadoopArchiveLogs.AppInfo app3 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 3).toString(), USER); HadoopArchiveLogs.AppInfo app4 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 4).toString(), USER); app4.setFinishTime(CLUSTER_TIMESTAMP + 5); HadoopArchiveLogs.AppInfo app5 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 5).toString(), USER); app5.setFinishTime(CLUSTER_TIMESTAMP + 10); HadoopArchiveLogs.AppInfo app6 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 6).toString(), USER); HadoopArchiveLogs.AppInfo app7 = new HadoopArchiveLogs.AppInfo( ApplicationId.newInstance(CLUSTER_TIMESTAMP, 7).toString(), USER); app7.setFinishTime(CLUSTER_TIMESTAMP); HadoopArchiveLogs hal = new HadoopArchiveLogs(conf); Assert.assertEquals(0, hal.eligibleApplications.size()); hal.eligibleApplications.add(app1); hal.eligibleApplications.add(app2); hal.eligibleApplications.add(app3); hal.eligibleApplications.add(app4); hal.eligibleApplications.add(app5); hal.eligibleApplications.add(app6); hal.eligibleApplications.add(app7); Assert.assertEquals(7, hal.eligibleApplications.size()); hal.maxEligible = -1; hal.checkMaxEligible(); Assert.assertEquals(7, hal.eligibleApplications.size()); hal.maxEligible = 6; hal.checkMaxEligible(); Assert.assertEquals(6, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app5)); hal.maxEligible = 5; hal.checkMaxEligible(); Assert.assertEquals(5, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app4)); hal.maxEligible = 4; hal.checkMaxEligible(); Assert.assertEquals(4, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app7)); hal.maxEligible = 3; hal.checkMaxEligible(); Assert.assertEquals(3, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app1)); hal.maxEligible = 2; hal.checkMaxEligible(); Assert.assertEquals(2, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app2)); hal.maxEligible = 1; hal.checkMaxEligible(); Assert.assertEquals(1, hal.eligibleApplications.size()); Assert.assertFalse(hal.eligibleApplications.contains(app6)); Assert.assertTrue(hal.eligibleApplications.contains(app3)); }