|
39 | 39 | import org.junit.Before;
|
40 | 40 | import org.junit.Test;
|
41 | 41 |
|
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.FileWriter; |
| 45 | +import java.io.BufferedWriter; |
| 46 | +import java.nio.file.Files; |
| 47 | +import java.util.Arrays; |
| 48 | +import java.util.ArrayList; |
42 | 49 | import java.util.Collections;
|
43 | 50 | import java.util.HashMap;
|
44 | 51 | import java.util.Map;
|
@@ -747,4 +754,157 @@ public void testSyncSnapshotTimeStampChecking() throws Exception {
|
747 | 754 | }
|
748 | 755 | Assert.assertTrue(threwException);
|
749 | 756 | }
|
| 757 | + |
| 758 | + private void initData10(Path dir) throws Exception { |
| 759 | + final Path staging = new Path(dir, ".staging"); |
| 760 | + final Path stagingF1 = new Path(staging, "f1"); |
| 761 | + final Path data = new Path(dir, "data"); |
| 762 | + final Path dataF1 = new Path(data, "f1"); |
| 763 | + |
| 764 | + DFSTestUtil.createFile(dfs, stagingF1, BLOCK_SIZE, DATA_NUM, 0L); |
| 765 | + DFSTestUtil.createFile(dfs, dataF1, BLOCK_SIZE, DATA_NUM, 0L); |
| 766 | + } |
| 767 | + |
| 768 | + private void changeData10(Path dir) throws Exception { |
| 769 | + final Path staging = new Path(dir, ".staging"); |
| 770 | + final Path prod = new Path(dir, "prod"); |
| 771 | + dfs.rename(staging, prod); |
| 772 | + } |
| 773 | + |
| 774 | + private java.nio.file.Path generateFilterFile(String fileName) |
| 775 | + throws IOException { |
| 776 | + java.nio.file.Path tmpFile = Files.createTempFile(fileName, "txt"); |
| 777 | + String str = ".*\\.staging.*"; |
| 778 | + try (BufferedWriter writer = new BufferedWriter( |
| 779 | + new FileWriter(tmpFile.toString()))) { |
| 780 | + writer.write(str); |
| 781 | + } |
| 782 | + return tmpFile; |
| 783 | + } |
| 784 | + |
| 785 | + private void deleteFilterFile(java.nio.file.Path filePath) |
| 786 | + throws IOException { |
| 787 | + Files.delete(filePath); |
| 788 | + } |
| 789 | + |
| 790 | + @Test |
| 791 | + public void testSync10() throws Exception { |
| 792 | + java.nio.file.Path filterFile = null; |
| 793 | + try { |
| 794 | + Path sourcePath = new Path(dfs.getWorkingDirectory(), "source"); |
| 795 | + initData10(sourcePath); |
| 796 | + dfs.allowSnapshot(sourcePath); |
| 797 | + dfs.createSnapshot(sourcePath, "s1"); |
| 798 | + filterFile = generateFilterFile("filters"); |
| 799 | + final DistCpOptions.Builder builder = new DistCpOptions.Builder( |
| 800 | + new ArrayList<>(Arrays.asList(sourcePath)), |
| 801 | + target) |
| 802 | + .withFiltersFile(filterFile.toString()) |
| 803 | + .withSyncFolder(true); |
| 804 | + new DistCp(conf, builder.build()).execute(); |
| 805 | + verifySync(dfs.getFileStatus(sourcePath), |
| 806 | + dfs.getFileStatus(target), false, ".staging"); |
| 807 | + |
| 808 | + dfs.allowSnapshot(target); |
| 809 | + dfs.createSnapshot(target, "s1"); |
| 810 | + changeData10(sourcePath); |
| 811 | + dfs.createSnapshot(sourcePath, "s2"); |
| 812 | + |
| 813 | + final DistCpOptions.Builder diffBuilder = new DistCpOptions.Builder( |
| 814 | + new ArrayList<>(Arrays.asList(sourcePath)), |
| 815 | + target) |
| 816 | + .withUseDiff("s1", "s2") |
| 817 | + .withFiltersFile(filterFile.toString()) |
| 818 | + .withSyncFolder(true); |
| 819 | + new DistCp(conf, diffBuilder.build()).execute(); |
| 820 | + verifyCopy(dfs.getFileStatus(sourcePath), |
| 821 | + dfs.getFileStatus(target), false); |
| 822 | + } finally { |
| 823 | + deleteFilterFile(filterFile); |
| 824 | + } |
| 825 | + } |
| 826 | + |
| 827 | + private void initData11(Path dir) throws Exception { |
| 828 | + final Path staging = new Path(dir, "prod"); |
| 829 | + final Path stagingF1 = new Path(staging, "f1"); |
| 830 | + final Path data = new Path(dir, "data"); |
| 831 | + final Path dataF1 = new Path(data, "f1"); |
| 832 | + |
| 833 | + DFSTestUtil.createFile(dfs, stagingF1, BLOCK_SIZE, DATA_NUM, 0L); |
| 834 | + DFSTestUtil.createFile(dfs, dataF1, BLOCK_SIZE, DATA_NUM, 0L); |
| 835 | + } |
| 836 | + |
| 837 | + private void changeData11(Path dir) throws Exception { |
| 838 | + final Path staging = new Path(dir, "prod"); |
| 839 | + final Path prod = new Path(dir, ".staging"); |
| 840 | + dfs.rename(staging, prod); |
| 841 | + } |
| 842 | + |
| 843 | + private void verifySync(FileStatus s, FileStatus t, boolean compareName, |
| 844 | + String deletedName) |
| 845 | + throws Exception { |
| 846 | + Assert.assertEquals(s.isDirectory(), t.isDirectory()); |
| 847 | + if (compareName) { |
| 848 | + Assert.assertEquals(s.getPath().getName(), t.getPath().getName()); |
| 849 | + } |
| 850 | + if (!s.isDirectory()) { |
| 851 | + // verify the file content is the same |
| 852 | + byte[] sbytes = DFSTestUtil.readFileBuffer(dfs, s.getPath()); |
| 853 | + byte[] tbytes = DFSTestUtil.readFileBuffer(dfs, t.getPath()); |
| 854 | + Assert.assertArrayEquals(sbytes, tbytes); |
| 855 | + } else { |
| 856 | + FileStatus[] slist = dfs.listStatus(s.getPath()); |
| 857 | + FileStatus[] tlist = dfs.listStatus(t.getPath()); |
| 858 | + int minFiles = tlist.length; |
| 859 | + if (slist.length < tlist.length) { |
| 860 | + minFiles = slist.length; |
| 861 | + } |
| 862 | + for (int i = 0; i < minFiles; i++) { |
| 863 | + if (slist[i].getPath().getName().contains(deletedName)) { |
| 864 | + if (tlist[i].getPath().getName().contains(deletedName)) { |
| 865 | + throw new Exception("Target is not synced as per exclusion filter"); |
| 866 | + } |
| 867 | + continue; |
| 868 | + } |
| 869 | + verifySync(slist[i], tlist[i], true, deletedName); |
| 870 | + } |
| 871 | + } |
| 872 | + } |
| 873 | + |
| 874 | + @Test |
| 875 | + public void testSync11() throws Exception { |
| 876 | + java.nio.file.Path filterFile = null; |
| 877 | + try { |
| 878 | + Path sourcePath = new Path(dfs.getWorkingDirectory(), "source"); |
| 879 | + initData11(sourcePath); |
| 880 | + dfs.allowSnapshot(sourcePath); |
| 881 | + dfs.createSnapshot(sourcePath, "s1"); |
| 882 | + filterFile = generateFilterFile("filters"); |
| 883 | + final DistCpOptions.Builder builder = new DistCpOptions.Builder( |
| 884 | + new ArrayList<>(Arrays.asList(sourcePath)), |
| 885 | + target) |
| 886 | + .withFiltersFile(filterFile.toString()) |
| 887 | + .withSyncFolder(true); |
| 888 | + new DistCp(conf, builder.build()).execute(); |
| 889 | + verifyCopy(dfs.getFileStatus(sourcePath), |
| 890 | + dfs.getFileStatus(target), false); |
| 891 | + |
| 892 | + dfs.allowSnapshot(target); |
| 893 | + dfs.createSnapshot(target, "s1"); |
| 894 | + changeData11(sourcePath); |
| 895 | + dfs.createSnapshot(sourcePath, "s2"); |
| 896 | + |
| 897 | + final DistCpOptions.Builder diffBuilder = new DistCpOptions.Builder( |
| 898 | + new ArrayList<>(Arrays.asList(sourcePath)), |
| 899 | + target) |
| 900 | + .withUseDiff("s1", "s2") |
| 901 | + .withFiltersFile(filterFile.toString()) |
| 902 | + .withSyncFolder(true); |
| 903 | + new DistCp(conf, diffBuilder.build()).execute(); |
| 904 | + verifySync(dfs.getFileStatus(sourcePath), |
| 905 | + dfs.getFileStatus(target), false, ".staging"); |
| 906 | + } finally { |
| 907 | + deleteFilterFile(filterFile); |
| 908 | + } |
| 909 | + } |
750 | 910 | }
|
0 commit comments