Tutorial 3: Request a Resource Allocation in a Cluster
This tutorial describes how to create a registered EGO client that requests a resource allocation in a cluster and starts an activity on it.
Using this tutorial, you will ...
- Open a connection to the EGO Web Service endpoint
- Retrieve and print out cluster info
- Retrieve and print out resource info
- Register the client with Platform EGO and print out the registration response
- Request a resource allocation
- Check for notification of resource allocation
- Create an activity that will run on the requested resource
- Locate the client and print out the client info
- Unregister the client
Step 1: Import class references
Import the necessary classes and interfaces that are required by the client to invoke the Web Service.
Step 2: Retrieve cluster and Resource information
Refer to Tutorial1: Step 2: Retrieve cluster information and Step 3: Retrieve resource information.
Step 3: Register the client
Refer to Tutorial 2: Step 3: Register the client.
Step 4: Make a resource allocation request
The RequestAllocation operation is used by an EGO client to make a request for resources.
Call the allocate method and pass the number of resources requested as the input argument. In this case, the number of resources is 1.
Check if the client can connect to the AllocationPortTypeStub endpoint. If successful, create a resource specification (resourceSpecifications). The resource specification provides a means for specifying a request for consumable resources and any constraints on resource selection.
Create an allocation specification (alocSpec). This allocation specification describes a request to Platform EGO for an allocation of resources. The only required variables for the allocation specification are the consumer name to charge this allocation to, and the resource specification (resourceSpecifications) of what is being requested.
Create an allocation request object (alocReq) and link it with the client name and allocation specification (alocSpec).
Create the security document. The wrappers generated for Java have signatures that provide for multiple types of security information to be included. As in the case of client registration, we are using just one security document (logonDoc). Pass it along with along with alocReqDoc to the RequestAllocation method.
Create an allocated resource object (alocResources). The AllocatedResources class models the resources that the application has requested and tracks them as it obtains and uses them. The constructor takes the allocation ID and the number of resources requested.
The allocation ID is then added to a list of allocation IDs. The allocidToResourcesMap object is used to map allocation ID to the AllocatedResources class that tracks it. So given an allocation ID, one can find out the details about it from the AllocatedResources instance. The alocIds.add and allocidToResourceMap.put methods update the list of IDs and the map, respectively.
public void allocate(int numberOfResources) { if(allocPort == null) { try { allocPort = new AllocationPortTypeStub(null, targetURL); } catch(Exception e) { e.printStackTrace(); return; } } resourceSpecifications = createResourceSpecification(numberOfResources); AllocationSpecificationDocument alocSpecDoc = AllocationSpecificationDocument.Factory.newInstance(); AllocationSpecification alocSpec = alocSpecDoc.addNewAllocationSpecification(); alocSpec.setAllocationName("Sample3Allocation"); alocSpec.setConsumerName("SampleApplications/EclipseSamples"); alocSpec.setResourceSpecificationArray(resourceSpecifications); RequestAllocationRequestDocument alocReqDoc = RequestAllocationRequestDocument.Factory.newInstance(); RequestAllocationRequest alocReq = alocReqDoc.addNewRequestAllocationRequest(); alocReq.setClientName(clientId); alocReq.setAllocationSpecification(alocSpec); RequestAllocationResponseDocument alocResDoc; RequestAllocationResponse alocRes; try { alocResDoc = allocPort.RequestAllocation(alocReqDoc, logonDoc); alocRes = alocResDoc.getRequestAllocationResponse(); AllocatedResources alocResources = new AllocatedResources(alocRes.getAllocationID(), numberOfResources); alocIds.add(alocRes.getAllocationID()); allocidToResourcesMap.put(alocRes.getAllocationID(), alocResources); }catch (RemoteException rex) { System.err.println("Failed to Allocate"); rex.printStackTrace(); return; }}
protected ResourceSpecification[] createResourceSpecification(int numberOfResources) { ResourceSpecification [] resSpecs = new ResourceSpecification [1]; ResourceSpecification resSpec = ResourceSpecification.Factory.newInstance(); resSpec.setMaxResources(new BigInteger(Integer.toString(numberOfResources))); resSpec.setMinResources(new BigInteger("1")); resSpec.setResourceGroupName("ComputeHosts"); resSpec.setResourceRequirement("LINUX86"); // NTX86 resSpecs[0] = resSpec; return resSpecs; }Step 5: Check the allocation status
This sample implements a notification service that runs as a separate thread in the background and listens continuously for allocation notification events. When the checkAddResourceNotification() method is called, the main thread "waits" on the Notification.notification object, blocking the thread. When a notification arrives, the thread that handles the notification, "notifies" the Notification.notification object. This wakes up the main thread that was blocked. A while loop is used so that if the thread is interrupted, the execution goes back to waiting. The synchronized keyword ensures that the code block is executed by only one thread at a time.
Once the resource is allocated, print out the resource request details and add them to the AllocatedResources instance.
public int checkAddResourceNotification() { // wait until we hear about allocation boolean done = false; AddResourceRequest arReq = null; synchronized(Notification.notification) { while(!done){ try { Notification.notification.wait(); arReq = Notification.notification.getAddResourceRequest(); done = match(arReq); // true } catch (InterruptedException ie) { ie.printStackTrace(); } } } // received soap call at notification port print(arReq); //TODO Handle multiple AllocationIds // For multiple resources requested in a single allocation // the addResource may come individually at different times // Append to the already recieved ones. // incrementally use the ones as we obtain them. AllocatedResources allocatedResources = allocidToResourcesMap.get(arReq.getAllocationID()); allocatedResources.add(arReq.getResourceArray()); // sendResponse(); // handled by the NotificationReceiver return arReq.getResourceArray().length; }Step 6: Create and start an activity on a resource
The StartActivity operation is used by an EGO client to request the execution of an activity on a resource. Usually resources are allocated to the client via an EGO allocation request prior to starting an activity . Thus the AllocationID within the request message is the one returned from a previous RequestAllocation call.
Call the createActivity method and pass the allocation ID and the number of activities as input arguments. In this case, the number of activities is 1.
Check if the client can connect to the ActivityPortTypeStub endpoint. If successful, create an activity specification (actSpec). The activity specification describes the execution parameters for an activity, such as the command. In this case, the sleep command is used to simulate activity on the resource.
Create an activity start request object (sactReq). The sactReq object links the activity specification with the allocation ID and client ID.
Call the StartActivity() method with the start activity request document (sactReqDoc) and the logon document (logonDoc) as input arguments. The response message for StartActivity (sactRes) contains the ActivityID assigned by Platform EGO to this activity. The ActivityID can then be used in other operations to manage and query the state of the activity.
Create an activity resources object (activityResources). The ActivityResources class models the resources that the activity is consuming and tracks them as they are used. The constructor takes the activity ID, allocation ID, and resource name array.
The activityidToResourcesMap object is used to map activity ID to the ActivityResources class that tracks it. So given an activity ID, you can find out the details about it from the ActivityResources instance. The activityidToResourceMap.put method updates the list of IDs and the map.
Program execution returns to the main method and waits until the activity, which is sleep for 120 seconds, is finished so that various notifications about the activity can be displayed
public void createActivity(String allocationId, int numActivities) { this.numActivities = numActivities; if (activityPort == null) { try { activityPort = new ActivityPortTypeStub(null, targetURL); } catch(Exception e) { e.printStackTrace(); return; } } AllocatedResources allocatedResources = allocidToResourcesMap.get(allocationId); String [] resourceNames = allocatedResources.consume(); EnvironmentVariable [] env = new EnvironmentVariable [] {}; Rlimit [] rlimits = new Rlimit[] {}; ActivitySpecificationDocument actSpecDoc = ActivitySpecificationDocument.Factory.newInstance(); ActivitySpecification actSpec = actSpecDoc.addNewActivitySpecification(); actSpec.setActivityName("Sample3Job"); actSpec.setCommand("/bin/sleep 120"); actSpec.setEnvironmentVariableArray(env); actSpec.setExecutionUser("lsfadmin"); actSpec.setWorkingDirectory("/tmp"); actSpec.setUmask("0777"); actSpec.setRlimitArray(rlimits); StartActivityRequestDocument sactReqDoc = StartActivityRequestDocument.Factory.newInstance(); StartActivityRequest sactReq = sactReqDoc.addNewStartActivityRequest(); sactReq.setActivitySpecification(actSpec); sactReq.setAllocationID(allocationId); sactReq.setClientName(clientId); sactReq.setResourceNameArray(resourceNames); sactReq.setOptionArray(null); SecurityDocument sdoc1 = SecurityDocument.Factory.newInstance(); SecurityHeaderType sec1 = sdoc1.addNewSecurity(); StartActivityResponseDocument sactResDoc; StartActivityResponse sactRes;
for (int i=0; i<numActivities; i++) { try { sactResDoc = activityPort.StartActivity(sactReqDoc, logonDoc /* sdoc1 */); sactRes = sactResDoc.getStartActivityResponse(); //TODO check if activity started correctly activities.add(sactRes.getActivityID()); // add to the map ActivityResources activityResources = new ActivityResources( sactRes.getActivityID(), allocationId, resourceNames); activityidToResourcesMap.put(sactRes.getActivityID(), activityResources); // monitorActivity(activities); } catch(RemoteException rex) { rex.printStackTrace(); //TODO Release Resource releaseResources(allocationId); return; } } }Step 7: Locate the client
Refer to Tutorial 2: Step 4: Locate the client.
Step 8: Unregister the client
Refer to Tutorial 2: Step 5: Unregister the client.
Run the client application
- Select Run > Run.
The Run dialog appears.
- In the Configurations list, either select a Java Application or click New for a new configuration.
For a new configuration, enter the configuration name.
- Enter the project name and Main class.
- Click the Arguments tab and enter the following arguments in the given order:
- URL of the web service gateway
- Port number (string) for the notification interface
- Client ID (string)
- Client description (string).
note:
Arguments must be separated by a space.
![]()
- Click Apply and then Run.
Sample Output
![]()
![]()
[ Top ]
[ Platform Documentation ]
Date Modified: July 12, 2006
Platform Computing: www.platform.com
Platform Support: support@platform.com
Platform Information Development: doc@platform.com
Copyright © 1994-2006 Platform Computing Corporation. All rights reserved.