Partilhar via


HPC Pack SOA Tutorial I – Write your first SOA service and client

This series of blog posts introduces the SOA programming model for Microsoft HPC Pack 2012. This is the first blog post of the series and provides guidance to write your first SOA service and client. See the accompanying code sample to follow the steps in this article.

Note: Most of the general considerations for SOA for HPC Pack 2012 also apply to earlier versions of HPC Pack.

What is a SOA service?

Service-oriented architecture (SOA) is an approach to building distributed, loosely coupled systems.

With SOA, distinct computational functions are packaged as software modules called services. Unlike traditional HPC applications, an HPC SOA service exposes its functionality through a well-defined service interface which allows any application or process on the network to access the functionality of the service. Developers can implement service functionality by using any programming language, and can write original services or package existing dynamic-link libraries (DLLs) and IT investments as services.

Why SOA services?

HPC applications are typically classified as “message intensive” or “embarrassingly parallel.” Message-intensive applications comprise sequential tasks. Embarrassingly parallel applications can be easily divided into large numbers of parallel tasks, with no dependency or communication between them.

SOA services are designed to solve the embarrassingly parallel problem. Compared with traditional HPC computation models, SOA services provide the following advantages:

- Easier programming model: The interface of the SOA service is just a remote method. It makes the client application able to easily access the service.

- Easier message exchange: Traditional HPC jobs require dealing with input and output files. SOA services allow client applications to easily transfer data between the client application and service.

See https://www.microsoft.com/en-us/download/details.aspx?id=11540 for a technical overview of cluster-enabled SOA application development.

Getting started

To write a SOA program to run on an HPC Pack 2012 cluster, you need the following software:

HPC Pack 2012 Client Utilities

HPC Pack 2012 SDK

.NET 4.0 or later

To test your service you need to set up an HPC Pack Cluster to run your service. You can download the HPC Pack 2012 from here.

Now we can start writing the SOA application. Let’s say we need a service running on the HPC cluster to do math calculations. Then we need a client program for end users, because they cannot directly access the cluster. The client program submits the calculation request to the service and returns the result to the end users. To simplify the code, let’s just use a simple addition calculation as an example.

Here are the steps to write a SOA application:

- Step 1: Implement the service

- Step 2: Deploy the service

- Step 3: Implement the client

- Step 4: Test the service

Step 1: Implement the service

 

Following code can be found in the CalculatorService project in the sample code.

A SOA service is a Windows Communication Foundation (WCF) service running on the HPC cluster. The SOA service is ideal for writing interactive, embarrassingly parallel applications, especially for calculation of complex algorithms.

A SOA service does not require too much extra effort beyond your algorithm. It’s just a standard WCF service. The first step is to define the service contract, like the following code:

 

   [ServiceContract]

    publicinterfaceICalculator

    {

         [OperationContract]

         double Add(double a, doubleb);

    }

 

 

The only extra code is to add the attribute, which can be recognized by WCF, of the interface and its methods. With the service contract the client can understand how to invoke the method and what parameters are required.

The next step is to implement the algorithm of the service:

   public class CalculatorService : ICalculator

    {

         publicdouble Add(double a, doubleb)

         {

             returna + b;

         }

    }

 

Step 2: Deploy the service

We need to deploy the service we just created to the HPC cluster. Before we deploy the service, we need one more step which is creating the SOA service configuration file. Create an XML file like the following:

<?xmlversion="1.0"encoding="utf-8" ?>

 <configuration>

  <configSections>

    <sectionGroup name="microsoft.Hpc.Session.ServiceRegistration"

             type="Microsoft.Hpc.Scheduler.Session.Configuration.ServiceRegistration, Microsoft.Hpc.Scheduler.Session, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

       <section name="service"

  type="Microsoft.Hpc.Scheduler.Session.Configuration.ServiceConfiguration, Microsoft.Hpc.Scheduler.Session, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                allowDefinition="Everywhere"

                allowExeDefinition="MachineToApplication"

                       />

    </sectionGroup>

  </configSections>

   <microsoft.Hpc.Session.ServiceRegistration>

    <!--Change assembly path below-->

<serviceassembly=”\\fileshare\SOAServices\CalculatorService.dll”/>

  </microsoft.Hpc.Session.ServiceRegistration>

 </configuration>

 

Note that the name of the XML file must be as same as the service name. In this case, name it CalculatorService.config.

The only configurable part of the above configuration file is the path of the assembly. Change the highlighted part to the path of the DLL file in your cluster. It is recommended to put the DLL file in a shared folder which can be accessed by all compute nodes. This will save you time manually copying the DLL to all the computer nodes.

Now copy the service configuration file to the configuration file folder, which is under %CCP_HOME%\ServiceRegistration on the head node of the cluster. After copying the file, go to HPC Cluster Manager and navigate to Configuration -> Services to confirm that the CalculatorService is there. Notice that the service name is as same as the file name of the configuration file.

Now the next step is to enable HPC Pack to locate the dll file. Copy the DLL file to the folder you specified in the configuration file. (Note: If you specified a local path, you need to copy the DLL file to the corresponding path on all the compute nodes.)

Now you have successfully deployed the service. To verify that the deployment is successful, you can use a diagnostic tool in HPC Cluster Manager console to test the service. Right click CalculatorService, and then select Run SOA Service Loading Diagnostic Test. You can check the diagnostic result. Click Diagnostics, select the test result, and you can see the test result and the detailed information.

clip_image004[9]

A successful diagnostic result looks like this:

clip_image006[8]

Step 3: Implement the client

Following code can be found in the Clientproject in the sample code.

 

Now we need a client program to submit the calculation request. The client needs to invoke the service we just created remotely, so we need to leverage WCF to generate the proxy class for us. Visual Studio can simplify the whole process by using its service references functionality.

The suggested project organization in Visual Studio is to create different projects for the service and client programs, as shown in the following figure:

clip_image007[7]

We need to generate the proxy class for the client program based on the CalculatorService. Make sure the service project has been successfully built then click Add Service Reference, click Discover, and you should see the service you just created. Select the service and click Advanced.

clip_image009[7]

Important: In the advanced Service Reference Settings dialog box, ensure that Always generate message contracts is selected.

clip_image011[7]

Now we can write the HPC client code:

a. Prepare the session info, which includes the head node address and the service name. Let’s assume the head node host name is head.contoso.com and we are using the CalculatorService.

SessionStartInfo info = new SessionStartInfo(“head.contoso.com”, “CalculatorService”);

 

b. With the SessionStartInfo object we can create a session to connect to the head node.

using (Session session = Session.CreateSession(info))

 {

   ……

 }

 

c. To be able to send requests and receive responses, you need to create a BrokerClient object.

using (Session session = Session.CreateSession(info))

 {

 using (BrokerClient<ICalculator>client = new BrokerClient<ICalculator>(session))

  {

        ……

  }

 }

 

d.With the BrokerClient, you can send requests and receive responses.

using (BrokerClient<ICalculator>client = new BrokerClient<ICalculator>(session))

 {

  //send request

  AddRequest request = newAddRequest(1, 2);

  client.SendRequest<AddRequest>(request);

  client.EndRequests();

  //get response

 foreach (BrokerResponse<AddResponse>response in client.GetResponses<AddResponse>())

  {

   doubleresult = response.Result.AddResult;

   Console.WriteLine("Add 1 and 2, and we get {0}", result);

  }

 }

 

Step 4: Test the service

Now you can test your service using your client program.

Run the client in the Visual Studio. If everything is working fine, you should see output like the following:

clip_image015[6]

Open HPC Cluster Manager, navigate to Job Management, and you can see the corresponding job.

Congratulations! You have successfully created and run your fisrt SOA service. In our next blog post, we will introduce way of writing a service to handle a batch of client requests.

Comments

  • Anonymous
    January 01, 2003
    Hi M from ZJU,

    What is the error message of the SessionException? Could you paste the full stack? Please first check if the HpcSession service is running fine on the HN and you specified the correct HN name in the SessionStartInfo.

    Regards,
    Yutong

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    Hi M from ZJU

    You are welcome, M. Error message in Chinese is not a problem, however I suggest you to post your detailed questions to the HPC Forum (http://social.microsoft.com/Forums/en-US/home?category=windowshpc ) for quicker responses.

    With regard to the username and password, they are not mandatory in SessionStartInfo. If you don't set them and never saved the credential before, it would prompt for username/password when creating the session. For more SOA sample code, you may want to check out the HPC Pack 2012 R2 SDK and Sample Code @ http://www.microsoft.com/zh-cn/download/details.aspx?id=41633.

    Hope it helps.

    Regards,
    Yutong

  • Anonymous
    March 27, 2013
    Can i call the service from asp.net page as client? I searched lot of blog but did not find any example. Is there any issues with web client? Thanks in advance

  • Anonymous
    June 09, 2014
    After I implemented and deployed the service and the client, I ran the client, but it always had the error unhandled exception “Microsoft.Hpc.Scheduler.Session.SessionException " occurs in Microsoft.Hpc.Scheduler.Session.dll. And the code row where there was an error is using (Session session = Session.CreateSession(info)). How can I solve this problem?
    Thanks in advance

  • Anonymous
    June 09, 2014
    why my program always failed to connect to HPC session service after i implemented and deployed the service and the client according to the introduction?

  • Anonymous
    June 18, 2014
    Thank you very much for responding me! The error message in my program is Chinese, should I paste it? moreover, I have checked the service,it's running fine on the HN and I specified the HN name which is correct. I was thinking should I supply the Username and Password of HN in the program. Such as
    SessionStartInfo info = new SessionStartInfo("DC", "CalculatorService");
    info.Username = "Administrator";
    info.Password = "*******";