YARN-6004. Refactor TestResourceLocalizationService#testDownloadingResourcesOnContainer so that it is less than 150 lines. (Chris Trezzo via mingma)

This commit is contained in:
Ming Ma 2017-04-04 18:05:09 -07:00
parent 1938f97c0b
commit 7507ccd38a
1 changed files with 224 additions and 176 deletions

View File

@ -1124,7 +1124,6 @@ public class TestResourceLocalizationService {
} }
@Test(timeout = 20000) @Test(timeout = 20000)
@SuppressWarnings("unchecked")
public void testDownloadingResourcesOnContainerKill() throws Exception { public void testDownloadingResourcesOnContainerKill() throws Exception {
List<Path> localDirs = new ArrayList<Path>(); List<Path> localDirs = new ArrayList<Path>();
String[] sDirs = new String[1]; String[] sDirs = new String[1];
@ -1132,13 +1131,6 @@ public class TestResourceLocalizationService {
sDirs[0] = localDirs.get(0).toString(); sDirs[0] = localDirs.get(0).toString();
conf.setStrings(YarnConfiguration.NM_LOCAL_DIRS, sDirs); conf.setStrings(YarnConfiguration.NM_LOCAL_DIRS, sDirs);
DrainDispatcher dispatcher = new DrainDispatcher();
dispatcher.init(conf);
dispatcher.start();
EventHandler<ApplicationEvent> applicationBus = mock(EventHandler.class);
dispatcher.register(ApplicationEventType.class, applicationBus);
EventHandler<ContainerEvent> containerBus = mock(EventHandler.class);
dispatcher.register(ContainerEventType.class, containerBus);
DummyExecutor exec = new DummyExecutor(); DummyExecutor exec = new DummyExecutor();
LocalDirsHandlerService dirsHandler = new LocalDirsHandlerService(); LocalDirsHandlerService dirsHandler = new LocalDirsHandlerService();
@ -1149,6 +1141,7 @@ public class TestResourceLocalizationService {
delService.init(new Configuration()); delService.init(new Configuration());
delService.start(); delService.start();
DrainDispatcher dispatcher = getDispatcher(conf);
ResourceLocalizationService rawService = new ResourceLocalizationService( ResourceLocalizationService rawService = new ResourceLocalizationService(
dispatcher, exec, delService, dirsHandler, nmContext); dispatcher, exec, delService, dirsHandler, nmContext);
ResourceLocalizationService spyService = spy(rawService); ResourceLocalizationService spyService = spy(rawService);
@ -1191,12 +1184,41 @@ public class TestResourceLocalizationService {
spyService.init(conf); spyService.init(conf);
spyService.start(); spyService.start();
final Application app = mock(Application.class); doLocalization(spyService, dispatcher, exec, delService);
final ApplicationId appId =
BuilderUtils.newApplicationId(314159265358979L, 3); } finally {
String user = "user0"; spyService.stop();
when(app.getUser()).thenReturn(user); dispatcher.stop();
when(app.getAppId()).thenReturn(appId); delService.stop();
}
}
private DrainDispatcher getDispatcher(Configuration config) {
DrainDispatcher dispatcher = new DrainDispatcher();
dispatcher.init(config);
dispatcher.start();
return dispatcher;
}
@SuppressWarnings("unchecked")
private EventHandler<ApplicationEvent> getApplicationBus(
DrainDispatcher dispatcher) {
EventHandler<ApplicationEvent> applicationBus = mock(EventHandler.class);
dispatcher.register(ApplicationEventType.class, applicationBus);
return applicationBus;
}
@SuppressWarnings("unchecked")
private EventHandler<ContainerEvent> getContainerBus(
DrainDispatcher dispatcher) {
EventHandler<ContainerEvent> containerBus = mock(EventHandler.class);
dispatcher.register(ContainerEventType.class, containerBus);
return containerBus;
}
private void initApp(ResourceLocalizationService spyService,
EventHandler<ApplicationEvent> applicationBus, Application app,
final ApplicationId appId, DrainDispatcher dispatcher) {
spyService.handle(new ApplicationLocalizationEvent( spyService.handle(new ApplicationLocalizationEvent(
LocalizationEventType.INIT_APPLICATION_RESOURCES, app)); LocalizationEventType.INIT_APPLICATION_RESOURCES, app));
ArgumentMatcher<ApplicationEvent> matchesAppInit = ArgumentMatcher<ApplicationEvent> matchesAppInit =
@ -1210,30 +1232,29 @@ public class TestResourceLocalizationService {
}; };
dispatcher.await(); dispatcher.await();
verify(applicationBus).handle(argThat(matchesAppInit)); verify(applicationBus).handle(argThat(matchesAppInit));
}
// Initialize localizer. private void doLocalization(ResourceLocalizationService spyService,
Random r = new Random(); DrainDispatcher dispatcher, DummyExecutor exec,
long seed = r.nextLong(); DeletionService delService)
System.out.println("SEED: " + seed); throws IOException, URISyntaxException, InterruptedException {
r.setSeed(seed); final Application app = mock(Application.class);
final ApplicationId appId =
BuilderUtils.newApplicationId(314159265358979L, 3);
String user = "user0";
when(app.getUser()).thenReturn(user);
when(app.getAppId()).thenReturn(appId);
List<LocalResource> resources = initializeLocalizer(appId);
LocalResource resource1 = resources.get(0);
LocalResource resource2 = resources.get(1);
LocalResource resource3 = resources.get(2);
final Container c1 = getMockContainer(appId, 42, "user0"); final Container c1 = getMockContainer(appId, 42, "user0");
final Container c2 = getMockContainer(appId, 43, "user0"); final Container c2 = getMockContainer(appId, 43, "user0");
FSDataOutputStream out =
new FSDataOutputStream(new DataOutputBuffer(), null); EventHandler<ApplicationEvent> applicationBus =
doReturn(out).when(spylfs).createInternal(isA(Path.class), getApplicationBus(dispatcher);
isA(EnumSet.class), isA(FsPermission.class), anyInt(), anyShort(), EventHandler<ContainerEvent> containerBus = getContainerBus(dispatcher);
anyLong(), isA(Progressable.class), isA(ChecksumOpt.class), initApp(spyService, applicationBus, app, appId, dispatcher);
anyBoolean());
final LocalResource resource1 = getPrivateMockedResource(r);
LocalResource resource2 = null;
do {
resource2 = getPrivateMockedResource(r);
} while (resource2 == null || resource2.equals(resource1));
LocalResource resource3 = null;
do {
resource3 = getPrivateMockedResource(r);
} while (resource3 == null || resource3.equals(resource1)
|| resource3.equals(resource2));
// Send localization requests for container c1 and c2. // Send localization requests for container c1 and c2.
final LocalResourceRequest req1 = new LocalResourceRequest(resource1); final LocalResourceRequest req1 = new LocalResourceRequest(resource1);
@ -1250,13 +1271,13 @@ public class TestResourceLocalizationService {
rsrcs.put(LocalResourceVisibility.PRIVATE, privateResourceList); rsrcs.put(LocalResourceVisibility.PRIVATE, privateResourceList);
spyService.handle(new ContainerLocalizationRequestEvent(c1, rsrcs)); spyService.handle(new ContainerLocalizationRequestEvent(c1, rsrcs));
final LocalResourceRequest req1_1 = new LocalResourceRequest(resource2); final LocalResourceRequest req11 = new LocalResourceRequest(resource2);
Map<LocalResourceVisibility, Collection<LocalResourceRequest>> rsrcs1 = Map<LocalResourceVisibility, Collection<LocalResourceRequest>> rsrcs1 =
new HashMap<LocalResourceVisibility, new HashMap<LocalResourceVisibility,
Collection<LocalResourceRequest>>(); Collection<LocalResourceRequest>>();
List<LocalResourceRequest> privateResourceList1 = List<LocalResourceRequest> privateResourceList1 =
new ArrayList<LocalResourceRequest>(); new ArrayList<LocalResourceRequest>();
privateResourceList1.add(req1_1); privateResourceList1.add(req11);
rsrcs1.put(LocalResourceVisibility.PRIVATE, privateResourceList1); rsrcs1.put(LocalResourceVisibility.PRIVATE, privateResourceList1);
spyService.handle(new ContainerLocalizationRequestEvent(c2, rsrcs1)); spyService.handle(new ContainerLocalizationRequestEvent(c2, rsrcs1));
@ -1265,28 +1286,8 @@ public class TestResourceLocalizationService {
exec.waitForLocalizers(2); exec.waitForLocalizers(2);
LocalizerRunner locC1 = LocalizerRunner locC1 =
spyService.getLocalizerRunner(c1.getContainerId().toString()); spyService.getLocalizerRunner(c1.getContainerId().toString());
final String containerIdStr = c1.getContainerId().toString();
// Heartbeats from container localizer
LocalResourceStatus rsrc1success = mock(LocalResourceStatus.class);
LocalResourceStatus rsrc2pending = mock(LocalResourceStatus.class);
LocalizerStatus stat = mock(LocalizerStatus.class);
when(stat.getLocalizerId()).thenReturn(containerIdStr);
when(rsrc1success.getResource()).thenReturn(resource1);
when(rsrc2pending.getResource()).thenReturn(resource2);
when(rsrc1success.getLocalSize()).thenReturn(4344L);
URL locPath = getPath("/some/path");
when(rsrc1success.getLocalPath()).thenReturn(locPath);
when(rsrc1success.getStatus()).
thenReturn(ResourceStatusType.FETCH_SUCCESS);
when(rsrc2pending.getStatus()).
thenReturn(ResourceStatusType.FETCH_PENDING);
when(stat.getResources()) LocalizerStatus stat = mockLocalizerStatus(c1, resource1, resource2);
.thenReturn(Collections.<LocalResourceStatus>emptyList())
.thenReturn(Collections.singletonList(rsrc1success))
.thenReturn(Collections.singletonList(rsrc2pending))
.thenReturn(Collections.singletonList(rsrc2pending))
.thenReturn(Collections.<LocalResourceStatus>emptyList());
// First heartbeat which schedules first resource. // First heartbeat which schedules first resource.
LocalizerHeartbeatResponse response = spyService.heartbeat(stat); LocalizerHeartbeatResponse response = spyService.heartbeat(stat);
@ -1296,15 +1297,15 @@ public class TestResourceLocalizationService {
// Second resource is scheduled. // Second resource is scheduled.
response = spyService.heartbeat(stat); response = spyService.heartbeat(stat);
assertEquals(LocalizerAction.LIVE, response.getLocalizerAction()); assertEquals(LocalizerAction.LIVE, response.getLocalizerAction());
final String locPath1 = response.getResourceSpecs().get(0). final String locPath1 =
getDestinationDirectory().getFile(); response.getResourceSpecs().get(0).getDestinationDirectory().getFile();
// Third heartbeat which reports second resource as pending. // Third heartbeat which reports second resource as pending.
// Third resource is scheduled. // Third resource is scheduled.
response = spyService.heartbeat(stat); response = spyService.heartbeat(stat);
assertEquals(LocalizerAction.LIVE, response.getLocalizerAction()); assertEquals(LocalizerAction.LIVE, response.getLocalizerAction());
final String locPath2 = response.getResourceSpecs().get(0). final String locPath2 =
getDestinationDirectory().getFile(); response.getResourceSpecs().get(0).getDestinationDirectory().getFile();
// Container c1 is killed which leads to cleanup // Container c1 is killed which leads to cleanup
spyService.handle(new ContainerLocalizationCleanupEvent(c1, rsrcs)); spyService.handle(new ContainerLocalizationCleanupEvent(c1, rsrcs));
@ -1337,8 +1338,8 @@ public class TestResourceLocalizationService {
Thread.sleep(50); Thread.sleep(50);
} }
// Verify if downloading resources were submitted for deletion. // Verify if downloading resources were submitted for deletion.
verify(delService).delete(eq(user), verify(delService).delete(eq(user), (Path) eq(null),
(Path) eq(null), argThat(new DownloadingPathsMatcher(paths))); argThat(new DownloadingPathsMatcher(paths)));
LocalResourcesTracker tracker = spyService.getLocalResourcesTracker( LocalResourcesTracker tracker = spyService.getLocalResourcesTracker(
LocalResourceVisibility.PRIVATE, "user0", appId); LocalResourceVisibility.PRIVATE, "user0", appId);
@ -1360,11 +1361,58 @@ public class TestResourceLocalizationService {
// other container, hence its removed. // other container, hence its removed.
LocalizedResource rsrc3 = tracker.getLocalizedResource(req3); LocalizedResource rsrc3 = tracker.getLocalizedResource(req3);
assertNull(rsrc3); assertNull(rsrc3);
} finally {
spyService.stop();
dispatcher.stop();
delService.stop();
} }
private LocalizerStatus mockLocalizerStatus(Container c1,
LocalResource resource1, LocalResource resource2) {
final String containerIdStr = c1.getContainerId().toString();
// Heartbeats from container localizer
LocalResourceStatus rsrc1success = mock(LocalResourceStatus.class);
LocalResourceStatus rsrc2pending = mock(LocalResourceStatus.class);
LocalizerStatus stat = mock(LocalizerStatus.class);
when(stat.getLocalizerId()).thenReturn(containerIdStr);
when(rsrc1success.getResource()).thenReturn(resource1);
when(rsrc2pending.getResource()).thenReturn(resource2);
when(rsrc1success.getLocalSize()).thenReturn(4344L);
URL locPath = getPath("/some/path");
when(rsrc1success.getLocalPath()).thenReturn(locPath);
when(rsrc1success.getStatus()).thenReturn(ResourceStatusType.FETCH_SUCCESS);
when(rsrc2pending.getStatus()).thenReturn(ResourceStatusType.FETCH_PENDING);
when(stat.getResources())
.thenReturn(Collections.<LocalResourceStatus> emptyList())
.thenReturn(Collections.singletonList(rsrc1success))
.thenReturn(Collections.singletonList(rsrc2pending))
.thenReturn(Collections.singletonList(rsrc2pending))
.thenReturn(Collections.<LocalResourceStatus> emptyList());
return stat;
}
@SuppressWarnings("unchecked")
private List<LocalResource> initializeLocalizer(ApplicationId appId)
throws IOException {
// Initialize localizer.
Random r = new Random();
long seed = r.nextLong();
System.out.println("SEED: " + seed);
r.setSeed(seed);
FSDataOutputStream out =
new FSDataOutputStream(new DataOutputBuffer(), null);
doReturn(out).when(spylfs).createInternal(isA(Path.class),
isA(EnumSet.class), isA(FsPermission.class), anyInt(), anyShort(),
anyLong(), isA(Progressable.class), isA(ChecksumOpt.class),
anyBoolean());
final LocalResource resource1 = getPrivateMockedResource(r);
LocalResource resource2 = null;
do {
resource2 = getPrivateMockedResource(r);
} while (resource2 == null || resource2.equals(resource1));
LocalResource resource3 = null;
do {
resource3 = getPrivateMockedResource(r);
} while (resource3 == null || resource3.equals(resource1)
|| resource3.equals(resource2));
return Arrays.asList(resource1, resource2, resource3);
} }
@Test @Test