1818package org .apache .hadoop .hdfs .server .federation .router ;
1919
2020import org .apache .hadoop .conf .Configuration ;
21+ import org .apache .hadoop .fs .ContentSummary ;
22+ import org .apache .hadoop .fs .FSDataOutputStream ;
23+ import org .apache .hadoop .fs .FileSystem ;
24+ import org .apache .hadoop .fs .Path ;
2125import org .apache .hadoop .hdfs .protocol .ClientProtocol ;
2226import org .apache .hadoop .hdfs .protocol .HdfsFileStatus ;
2327import org .apache .hadoop .hdfs .server .federation .MiniRouterDFSCluster .RouterContext ;
2428import org .apache .hadoop .hdfs .server .federation .RouterConfigBuilder ;
2529import org .apache .hadoop .hdfs .server .federation .StateStoreDFSCluster ;
2630import org .apache .hadoop .hdfs .server .federation .resolver .MountTableManager ;
2731import org .apache .hadoop .hdfs .server .federation .resolver .MountTableResolver ;
32+ import org .apache .hadoop .hdfs .server .federation .resolver .RemoteLocation ;
2833import org .apache .hadoop .hdfs .server .federation .resolver .RouterResolveException ;
2934import org .apache .hadoop .hdfs .server .federation .store .protocol .AddMountTableEntryRequest ;
3035import org .apache .hadoop .hdfs .server .federation .store .protocol .AddMountTableEntryResponse ;
4045
4146import java .io .IOException ;
4247import java .util .Collections ;
48+ import java .util .List ;
49+ import java .util .Map ;
4350
4451import static org .junit .Assert .assertEquals ;
4552import static org .junit .Assert .assertNotNull ;
4653import static org .junit .Assert .assertTrue ;
54+ import static org .junit .Assert .fail ;
4755
4856/**
4957 * Test a router end-to-end including the MountTable without default nameservice.
@@ -53,6 +61,8 @@ public class TestRouterMountTableWithoutDefaultNS {
5361 private static RouterContext routerContext ;
5462 private static MountTableResolver mountTable ;
5563 private static ClientProtocol routerProtocol ;
64+ private static FileSystem nnFs0 ;
65+ private static FileSystem nnFs1 ;
5666
5767 @ BeforeClass
5868 public static void globalSetUp () throws Exception {
@@ -71,6 +81,8 @@ public static void globalSetUp() throws Exception {
7181 cluster .waitClusterUp ();
7282
7383 // Get the end points
84+ nnFs0 = cluster .getNamenode ("ns0" , null ).getFileSystem ();
85+ nnFs1 = cluster .getNamenode ("ns1" , null ).getFileSystem ();
7486 routerContext = cluster .getRandomRouter ();
7587 Router router = routerContext .getRouter ();
7688 routerProtocol = routerContext .getClient ().getNamenode ();
@@ -144,4 +156,113 @@ public void testGetFileInfoWithoutSubMountPoint() throws Exception {
144156 LambdaTestUtils .intercept (RouterResolveException .class ,
145157 () -> routerContext .getRouter ().getRpcServer ().getFileInfo ("/testdir2" ));
146158 }
159+
160+ /**
161+ * Verify that RBF that disable default nameservice should support
162+ * get information about ancestor mount points.
163+ */
164+ @ Test
165+ public void testGetContentSummaryWithSubMountPoint () throws IOException {
166+ MountTable addEntry = MountTable .newInstance ("/testdir/1/2" ,
167+ Collections .singletonMap ("ns0" , "/testdir/1/2" ));
168+ assertTrue (addMountTable (addEntry ));
169+
170+ try {
171+ writeData (nnFs0 , new Path ("/testdir/1/2/3" ), 10 * 1024 * 1024 );
172+
173+ RouterRpcServer routerRpcServer = routerContext .getRouterRpcServer ();
174+ ContentSummary summaryFromRBF = routerRpcServer .getContentSummary ("/testdir" );
175+ assertNotNull (summaryFromRBF );
176+ assertEquals (1 , summaryFromRBF .getFileCount ());
177+ assertEquals (10 * 1024 * 1024 , summaryFromRBF .getLength ());
178+ } finally {
179+ nnFs0 .delete (new Path ("/testdir" ), true );
180+ }
181+ }
182+
183+ @ Test
184+ public void testGetAllLocations () throws IOException {
185+ // Add mount table entry.
186+ MountTable addEntry = MountTable .newInstance ("/testA" ,
187+ Collections .singletonMap ("ns0" , "/testA" ));
188+ assertTrue (addMountTable (addEntry ));
189+ addEntry = MountTable .newInstance ("/testA/testB" ,
190+ Collections .singletonMap ("ns1" , "/testA/testB" ));
191+ assertTrue (addMountTable (addEntry ));
192+ addEntry = MountTable .newInstance ("/testA/testB/testC" ,
193+ Collections .singletonMap ("ns2" , "/testA/testB/testC" ));
194+ assertTrue (addMountTable (addEntry ));
195+
196+ RouterClientProtocol protocol = routerContext .getRouterRpcServer ().getClientProtocolModule ();
197+ Map <String , List <RemoteLocation >> locations = protocol .getAllLocations ("/testA" );
198+ assertEquals (3 , locations .size ());
199+ }
200+
201+ @ Test
202+ public void testGetLocationsForContentSummary () throws Exception {
203+ // Add mount table entry.
204+ MountTable addEntry = MountTable .newInstance ("/testA/testB" ,
205+ Collections .singletonMap ("ns0" , "/testA/testB" ));
206+ assertTrue (addMountTable (addEntry ));
207+ addEntry = MountTable .newInstance ("/testA/testB/testC" ,
208+ Collections .singletonMap ("ns1" , "/testA/testB/testC" ));
209+ assertTrue (addMountTable (addEntry ));
210+
211+ RouterClientProtocol protocol = routerContext .getRouterRpcServer ().getClientProtocolModule ();
212+ List <RemoteLocation > locations = protocol .getLocationsForContentSummary ("/testA" );
213+ assertEquals (2 , locations .size ());
214+
215+ for (RemoteLocation location : locations ) {
216+ String nsId = location .getNameserviceId ();
217+ if ("ns0" .equals (nsId )) {
218+ assertEquals ("/testA/testB" , location .getDest ());
219+ } else if ("ns1" .equals (nsId )) {
220+ assertEquals ("/testA/testB/testC" , location .getDest ());
221+ } else {
222+ fail ("Unexpected NS " + nsId );
223+ }
224+ }
225+
226+ LambdaTestUtils .intercept (NoLocationException .class ,
227+ () -> protocol .getLocationsForContentSummary ("/testB" ));
228+ }
229+
230+ @ Test
231+ public void testGetContentSummary () throws Exception {
232+ try {
233+ // Add mount table entry.
234+ MountTable addEntry = MountTable .newInstance ("/testA" ,
235+ Collections .singletonMap ("ns0" , "/testA" ));
236+ assertTrue (addMountTable (addEntry ));
237+ addEntry = MountTable .newInstance ("/testA/testB" ,
238+ Collections .singletonMap ("ns0" , "/testA/testB" ));
239+ assertTrue (addMountTable (addEntry ));
240+ addEntry = MountTable .newInstance ("/testA/testB/testC" ,
241+ Collections .singletonMap ("ns1" , "/testA/testB/testC" ));
242+ assertTrue (addMountTable (addEntry ));
243+
244+ writeData (nnFs0 , new Path ("/testA/testB/file1" ), 1024 * 1024 );
245+ writeData (nnFs1 , new Path ("/testA/testB/testC/file2" ), 1024 * 1024 );
246+ writeData (nnFs1 , new Path ("/testA/testB/testC/file3" ), 1024 * 1024 );
247+
248+ RouterRpcServer routerRpcServer = routerContext .getRouterRpcServer ();
249+ ContentSummary summary = routerRpcServer .getContentSummary ("/testA" );
250+ assertEquals (3 , summary .getFileCount ());
251+ assertEquals (1024 * 1024 * 3 , summary .getLength ());
252+
253+ LambdaTestUtils .intercept (NoLocationException .class ,
254+ () -> routerRpcServer .getContentSummary ("/testB" ));
255+ } finally {
256+ nnFs0 .delete (new Path ("/testA" ), true );
257+ nnFs1 .delete (new Path ("/testA" ), true );
258+ }
259+ }
260+
261+ void writeData (FileSystem fs , Path path , int fileLength ) throws IOException {
262+ try (FSDataOutputStream outputStream = fs .create (path )) {
263+ for (int writeSize = 0 ; writeSize < fileLength ; writeSize ++) {
264+ outputStream .write (writeSize );
265+ }
266+ }
267+ }
147268}
0 commit comments