func main() { var err error // Create YarnConfiguration conf, _ := conf.NewYarnConfiguration() // Create YarnClient yarnClient, _ := yarn_client.CreateYarnClient(conf) // Create new application to get ApplicationSubmissionContext _, asc, _ := yarnClient.CreateNewApplication() // Some useful information queue := "default" appName := "simple-go-yarn-app" appType := "GO_HADOOP_UNMANAGED" unmanaged := true clc := hadoop_yarn.ContainerLaunchContextProto{} // Setup ApplicationSubmissionContext for the application asc.AmContainerSpec = &clc asc.ApplicationName = &appName asc.Queue = &queue asc.ApplicationType = &appType asc.UnmanagedAm = &unmanaged // Submit! err = yarnClient.SubmitApplication(asc) if err != nil { log.Fatal("yarnClient.SubmitApplication ", err) } log.Println("Successfully submitted unmanaged application: ", asc.ApplicationId) time.Sleep(1 * time.Second) appReport, err := yarnClient.GetApplicationReport(asc.ApplicationId) if err != nil { log.Fatal("yarnClient.GetApplicationReport ", err) } appState := appReport.GetYarnApplicationState() for appState != hadoop_yarn.YarnApplicationStateProto_ACCEPTED { log.Println("Application in state ", appState) time.Sleep(1 * time.Second) appReport, err = yarnClient.GetApplicationReport(asc.ApplicationId) appState = appReport.GetYarnApplicationState() if appState == hadoop_yarn.YarnApplicationStateProto_FAILED || appState == hadoop_yarn.YarnApplicationStateProto_KILLED { log.Fatal("Application in state ", appState) } } log.Println("Application in state ", appState) // Create AMRMClient var attemptId int32 attemptId = 1 applicationAttemptId := hadoop_yarn.ApplicationAttemptIdProto{ApplicationId: asc.ApplicationId, AttemptId: &attemptId} rmClient, _ := yarn_client.CreateAMRMClient(conf, &applicationAttemptId) log.Println("Created RM client: ", rmClient) // Wait for ApplicationAttempt to be in Launched state appAttemptReport, err := yarnClient.GetApplicationAttemptReport(&applicationAttemptId) appAttemptState := appAttemptReport.GetYarnApplicationAttemptState() for appAttemptState != hadoop_yarn.YarnApplicationAttemptStateProto_APP_ATTEMPT_LAUNCHED { log.Println("ApplicationAttempt in state ", appAttemptState) time.Sleep(1 * time.Second) appAttemptReport, err = yarnClient.GetApplicationAttemptReport(&applicationAttemptId) appAttemptState = appAttemptReport.GetYarnApplicationAttemptState() } log.Println("ApplicationAttempt in state ", appAttemptState) // Register with ResourceManager log.Println("About to register application master.") err = rmClient.RegisterApplicationMaster("", -1, "") if err != nil { log.Fatal("rmClient.RegisterApplicationMaster ", err) } log.Println("Successfully registered application master.") // Add resource requests const numContainers = int32(1) memory := int32(128) resource := hadoop_yarn.ResourceProto{Memory: &memory} rmClient.AddRequest(1, "*", &resource, numContainers) // Now call ResourceManager.allocate allocateResponse, err := rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } log.Println("#containers allocated: ", len(allocateResponse.AllocatedContainers)) numAllocatedContainers := int32(0) allocatedContainers := make([]*hadoop_yarn.ContainerProto, numContainers, numContainers) for numAllocatedContainers < numContainers { // Sleep for a while log.Println("Sleeping...") time.Sleep(3 * time.Second) log.Println("Sleeping... done!") // Try to get containers now... allocateResponse, err = rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } for _, container := range allocateResponse.AllocatedContainers { allocatedContainers[numAllocatedContainers] = container numAllocatedContainers++ } log.Println("#containers allocated: ", len(allocateResponse.AllocatedContainers)) log.Println("Total #containers allocated so far: ", numAllocatedContainers) } log.Println("Final #containers allocated: ", numAllocatedContainers) // Now launch containers containerLaunchContext := hadoop_yarn.ContainerLaunchContextProto{Command: []string{"/bin/date"}} log.Println("containerLaunchContext: ", containerLaunchContext) for _, container := range allocatedContainers { log.Println("Launching container: ", *container, " ", container.NodeId.Host, ":", container.NodeId.Port) nmClient, err := yarn_client.CreateAMNMClient(*container.NodeId.Host, int(*container.NodeId.Port)) if err != nil { log.Fatal("hadoop_yarn.DialContainerManagementProtocolService: ", err) } log.Println("Successfully created nmClient: ", nmClient) err = nmClient.StartContainer(container, &containerLaunchContext) if err != nil { log.Fatal("nmClient.StartContainer: ", err) } } // Wait for Containers to complete numCompletedContainers := int32(0) for numCompletedContainers < numContainers { // Sleep for a while log.Println("Sleeping...") time.Sleep(3 * time.Second) log.Println("Sleeping... done!") allocateResponse, err = rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } for _, containerStatus := range allocateResponse.CompletedContainerStatuses { log.Println("Completed container: ", *containerStatus, " exit-code: ", *containerStatus.ExitStatus) numCompletedContainers++ } } log.Println("Containers complete: ", numCompletedContainers) // Unregister with ResourceManager log.Println("About to unregister application master.") finalStatus := hadoop_yarn.FinalApplicationStatusProto_APP_SUCCEEDED err = rmClient.FinishApplicationMaster(&finalStatus, "done", "") if err != nil { log.Fatal("rmClient.RegisterApplicationMaster ", err) } log.Println("Successfully unregistered application master.") }
//allocates one "fat" container. func (yarnScheduler *YARNScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) { rmClient := yarnScheduler.rmClient // Add resource requests // Even though we only allocate one container for now, this could change in the future. const numContainers = int32(1) memory := int32(128) resource := hadoop_yarn.ResourceProto{Memory: &memory} numAllocatedContainers := int32(0) const maxAttempts = int(5) allocationAttempts := 0 //allocatedContainers := make([]*hadoop_yarn.ContainerProto, numContainers, numContainers) rmClient.AddRequest(1, "*", &resource, numContainers) for numAllocatedContainers < numContainers && allocationAttempts < maxAttempts { var allocatedContainers []*hadoop_yarn.ContainerProto select { case allocatedContainers = <-yarnScheduler.handler.allocatedContainers: break case <-time.After(3 * time.Second): // Sleep for a while before trying again allocationAttempts++ log.Println("Sleeping...") time.Sleep(3 * time.Second) log.Println("Sleeping... done!") continue } for _, container := range allocatedContainers { allocatedContainers[numAllocatedContainers] = container numAllocatedContainers++ log.Println("#containers allocated so far: ", numAllocatedContainers) log.Printf("pod=%s YARN container=%v", pod.Name, container.GetId()) yarnScheduler.podsToContainersMap[pod.Name] = container.GetId() host := *container.NodeId.Host port := *container.NodeId.Port log.Println("allocated container on: ", host) //launch a "sleep" container so that the "loop" is completed and YARN doesn't deallocate the container containerLaunchContext := hadoop_yarn.ContainerLaunchContextProto{Command: []string{"while :; do sleep 1; done"}} nmClient, err := yarn_client.CreateAMNMClient(host, int(port)) if err != nil { log.Fatal("Failed to create AMNMClient! ", err) } log.Printf("Successfully created nmClient: %v", nmClient) log.Printf("Attempting to start container on %s", host) err = nmClient.StartContainer(container, &containerLaunchContext) if err != nil { log.Fatal("failed to start container! ", err) } //We have the hostname available. return from here. //This many change in case we allocate more than one container in the future. return findMinionForHost(host, minionLister) } log.Println("#containers allocated: ", len(allocatedContainers)) log.Println("Total #containers allocated so far: ", numAllocatedContainers) } log.Println("Final #containers allocated: ", numAllocatedContainers) return "<invalid_host>", errors.New("unable to schedule pod! YARN didn't allocate a container") }
func main() { var err error // Get applicationAttemptId from environment applicationAttemptId, err := parseAppAttemptId() if err != nil { log.Fatal("parseAppAttemptId: ", err) } // Create YarnConfiguration conf, _ := conf.NewYarnConfiguration() // Create AMRMClient rmClient, _ := yarn_client.CreateAMRMClient(conf, applicationAttemptId) log.Println("Created RM client: ", rmClient) // Register with ResourceManager log.Println("About to register application master.") err = rmClient.RegisterApplicationMaster("", -1, "") if err != nil { log.Fatal("rmClient.RegisterApplicationMaster ", err) } log.Println("Successfully registered application master.") // Add resource requests const numContainers = int32(1) memory := int32(128) resource := hadoop_yarn.ResourceProto{Memory: &memory} rmClient.AddRequest(1, "*", &resource, numContainers) // Now call ResourceManager.allocate allocateResponse, err := rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } log.Println("#containers allocated: ", len(allocateResponse.AllocatedContainers)) numAllocatedContainers := int32(0) allocatedContainers := make([]*hadoop_yarn.ContainerProto, numContainers, numContainers) for numAllocatedContainers < numContainers { // Sleep for a while log.Println("Sleeping...") time.Sleep(3 * time.Second) log.Println("Sleeping... done!") // Try to get containers now... allocateResponse, err = rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } for _, container := range allocateResponse.AllocatedContainers { allocatedContainers[numAllocatedContainers] = container numAllocatedContainers++ } log.Println("#containers allocated: ", len(allocateResponse.AllocatedContainers)) log.Println("Total #containers allocated so far: ", numAllocatedContainers) } log.Println("Final #containers allocated: ", numAllocatedContainers) // Now launch containers containerLaunchContext := hadoop_yarn.ContainerLaunchContextProto{Command: []string{"/bin/date"}} log.Println("containerLaunchContext: ", containerLaunchContext) for _, container := range allocatedContainers { log.Println("Launching container: ", *container, " ", container.NodeId.Host, ":", container.NodeId.Port) nmClient, err := yarn_client.CreateAMNMClient(*container.NodeId.Host, int(*container.NodeId.Port)) if err != nil { log.Fatal("hadoop_yarn.DialContainerManagementProtocolService: ", err) } log.Println("Successfully created nmClient: ", nmClient) err = nmClient.StartContainer(container, &containerLaunchContext) if err != nil { log.Fatal("nmClient.StartContainer: ", err) } } // Wait for Containers to complete numCompletedContainers := int32(0) for numCompletedContainers < numContainers { // Sleep for a while log.Println("Sleeping...") time.Sleep(3 * time.Second) log.Println("Sleeping... done!") allocateResponse, err = rmClient.Allocate() if err == nil { log.Println("allocateResponse: ", *allocateResponse) } for _, containerStatus := range allocateResponse.CompletedContainerStatuses { log.Println("Completed container: ", *containerStatus, " exit-code: ", *containerStatus.ExitStatus) numCompletedContainers++ } } log.Println("Containers complete: ", numCompletedContainers) // Unregister with ResourceManager log.Println("About to unregister application master.") finalStatus := hadoop_yarn.FinalApplicationStatusProto_APP_SUCCEEDED err = rmClient.FinishApplicationMaster(&finalStatus, "done", "") if err != nil { log.Fatal("rmClient.RegisterApplicationMaster ", err) } log.Println("Successfully unregistered application master.") }