TerrapinAdmin { @VisibleForTesting protected static int selectFileSetRollbackVersion(FileSetInfo fileSetInfo, InputStream inputStream) { int size = fileSetInfo.oldServingInfoList.size(); if (fileSetInfo.numVersionsToKeep < 2 || fileSetInfo.oldServingInfoList.size() < 1) { throw new IllegalArgumentException("no available version for rollback"); } System.out.println("available versions for rollback:"); for (int i = 0; i < size; i++) { FileSetInfo.ServingInfo servingInfo = fileSetInfo.oldServingInfoList.get(i); System.out.println(String.format("[%d] %s", i, servingInfo.hdfsPath)); } if (size > 1) { System.out.print(String.format("choose a version [0-%d]: ", size - 1)); } else { System.out.print("choose a version [0]: "); } Scanner scanner = new Scanner(inputStream); if (scanner.hasNextLine()) { String input = scanner.nextLine(); if (input.length() > 0) { int index = Integer.valueOf(input); if (index >= 0 && index < size) { return index; } throw new IllegalArgumentException( String.format("version index should between 0 and %d", size - 1) ); } } throw new IllegalArgumentException("version index not specified"); } TerrapinAdmin(PropertiesConfiguration configuration); void setNamenode(String[] args); void setDfsReplication(String[] args); void checkClusterHealth(String[] args); void rollbackFileSet(String[] args); void deleteFileSet(String[] args); static FileSetInfo lockFileSet(ZooKeeperManager zkManager,
                                        String fileSet,
                                        CreateMode mode); static FileSetInfo lockFileSet(ZooKeeperManager zkManager, String fileSet); static void unlockFileSet(ZooKeeperManager zkManager, String fileSet); static void rollbackFileSet(ZooKeeperManager zkManager, String fileSet,
                                     FileSetInfo fileSetInfo, int versionIndex); static void deleteFileSet(ZooKeeperManager zkManager, String fileSet); static void main(String[] args);  }
@Test public void testSelectFileSetRollbackVersion() { FileSetInfo fileSetInfo = new FileSetInfo(); fileSetInfo.numVersionsToKeep = 4; fileSetInfo.oldServingInfoList = ImmutableList.of( new FileSetInfo.ServingInfo("/hdfs/path/1", "resource", 1000, PartitionerType.MODULUS), new FileSetInfo.ServingInfo("/hdfs/path/2", "resource", 1000, PartitionerType.MODULUS), new FileSetInfo.ServingInfo("/hdfs/path/3", "resource", 1000, PartitionerType.MODULUS) ); int expectedVersionIndex = 1; ByteArrayInputStream inputStream = new ByteArrayInputStream((String.valueOf(expectedVersionIndex) + "\n").getBytes()); int versionIndex = TerrapinAdmin.selectFileSetRollbackVersion(fileSetInfo, inputStream); assertEquals(expectedVersionIndex, versionIndex); }
@Test(expected = IllegalArgumentException.class) public void testSelectFileSetRollbackVersionWithEmptyInput() { FileSetInfo fileSetInfo = new FileSetInfo(); fileSetInfo.numVersionsToKeep = 4; fileSetInfo.oldServingInfoList = ImmutableList.of( new FileSetInfo.ServingInfo("/hdfs/path/1", "resource", 1000, PartitionerType.MODULUS), new FileSetInfo.ServingInfo("/hdfs/path/2", "resource", 1000, PartitionerType.MODULUS), new FileSetInfo.ServingInfo("/hdfs/path/3", "resource", 1000, PartitionerType.MODULUS) ); ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); TerrapinAdmin.selectFileSetRollbackVersion(fileSetInfo, inputStream); }
@Test(expected = IllegalArgumentException.class) public void testSelectFileSetRollbackVersionWhenNoAvailableVersions() { FileSetInfo fileSetInfo = new FileSetInfo(); fileSetInfo.numVersionsToKeep = 2; fileSetInfo.oldServingInfoList = ImmutableList.of(); ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); TerrapinAdmin.selectFileSetRollbackVersion(fileSetInfo, inputStream); }