יום שישי, 9 במאי 2014

How to run ZeroMQ with Java in windows7

In order to run 0MQ with java in windows follow the following steps:
1.Compile the libzmq dll 
Capture52

Set the output lib of the libzmq project
Capture51

Don’t forget to compile for x64 Bit

The compilation result should be:
1> Creating library libzmq.lib and object libzmq.exp

 

1> libzmq.vcxproj -> H:\deleteme100\zeromq-4.0.4\builds\msvc\libzmq\../../../bin/x64/libzmq_d.dll

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

 

2.Compile the jzmq dll.
Capture53
Set the libzmq library in the project additional dependencies
Capture54
Set the jdk and the zero mq header files pathes in the c++ include directory .
Capture55
Compile the project in x64 bit .
The output should be :

1>------ Rebuild All started: Project: jzmq, Configuration: Release x64 ------

1> The syntax of the command is incorrect.

1> Note: Some input files use or override a deprecated API.

1> Note: Recompile with -Xlint:deprecation for details.

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Context.cpp

1> Poller.cpp

1>..\..\..\src\main\c++\Poller.cpp(76): warning C4244: '=' : conversion from 'jint' to 'short', possible loss of data

1> Socket.cpp

1>..\..\..\src\main\c++\Socket.cpp(266): warning C4267: 'argument' : conversion from 'size_t' to 'jsize', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(272): warning C4267: 'argument' : conversion from 'size_t' to 'jsize', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(834): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(862): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

1> util.cpp

1> ZMQ.cpp

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppBuild.targets(1137,5): warning MSB8012: TargetPath(H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.dll) does not match the Linker's OutputFile property value (H:\deleteme101\jzmq-master\lib\jzmq.dll). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).

1> Creating library H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.lib and object H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.exp

1> Generating code

1> Finished generating code

1> jzmq.vcxproj -> H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.dll

1> added manifest

1> adding: org/zeromq/App.class(in = 2433) (out= 1352)(deflated 44%)

1> adding: org/zeromq/EmbeddedLibraryTools.class(in = 4442) (out= 2473)(deflated 44%)

1> adding: org/zeromq/ZContext.class(in = 2621) (out= 1333)(deflated 49%)

1> adding: org/zeromq/ZDispatcher$1.class(in = 206) (out= 161)(deflated 21%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$1.class(in = 861) (out= 413)(deflated 52%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$2.class(in = 2034) (out= 829)(deflated 59%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$ZMessageBuffer.class(in = 1395) (out= 653)(deflated 53%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher.class(in = 4184) (out= 1790)(deflated 57%)

1> adding: org/zeromq/ZDispatcher$ZMessageHandler.class(in = 340) (out= 213)(deflated 37%)

1> adding: org/zeromq/ZDispatcher$ZSender.class(in = 780) (out= 436)(deflated 44%)

1> adding: org/zeromq/ZDispatcher.class(in = 2778) (out= 1254)(deflated 54%)

1> adding: org/zeromq/ZFrame.class(in = 4765) (out= 2642)(deflated 44%)

1> adding: org/zeromq/ZMQ$Context.class(in = 1262) (out= 711)(deflated 43%)

1> adding: org/zeromq/ZMQ$Error.class(in = 2545) (out= 1433)(deflated 43%)

1> adding: org/zeromq/ZMQ$Event.class(in = 1444) (out= 732)(deflated 49%)

1> adding: org/zeromq/ZMQ$Poller.class(in = 4092) (out= 2007)(deflated 50%)

1> adding: org/zeromq/ZMQ$PollItem.class(in = 1651) (out= 736)(deflated 55%)

1> adding: org/zeromq/ZMQ$Socket.class(in = 11809) (out= 4589)(deflated 61%)

1> adding: org/zeromq/ZMQ.class(in = 3529) (out= 1717)(deflated 51%)

1> adding: org/zeromq/ZMQException.class(in = 771) (out= 485)(deflated 37%)

1> adding: org/zeromq/ZMQForwarder.class(in = 1464) (out= 870)(deflated 40%)

1> adding: org/zeromq/ZMQQueue.class(in = 1860) (out= 1041)(deflated 44%)

1> adding: org/zeromq/ZMQStreamer.class(in = 424) (out= 260)(deflated 38%)

1> adding: org/zeromq/ZMsg.class(in = 10214) (out= 4286)(deflated 58%)

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

3.Copy the generated dlls into the system 32 directory
Capture56

Note:
Copy the libzmq_d.dll into libzmq.dll

4.Add the zmq jar to your project
found in zmq-master\lib
Capture57 

5.Enjoy 0mq.

References:
http://stackoverflow.com/questions/11455588/how-to-run-zeromq-with-java

Camel asyncRequestBody

A simple sample of using Camel asyncRequestBody method .
The sample is based on the Camel: Camel :: Example :: Cafe 2.12.1 sample.
The main :

 public void runCafeTableFinder () throws Exception {      
        DefaultCamelContext camelContext = new DefaultCamelContext();
        camelContext.setRegistry(createRegistry());
        camelContext.addRoutes(this);
        camelContext.start();
        
        ProducerTemplate template = camelContext.createProducerTemplate();
       
        Guest guest = new Guest (4);       
        
        Future<CafeTable> future = template.asyncRequestBody("direct:cafeEnterence", guest , CafeTable.class);
        
        CafeTable response = future.get(1, TimeUnit.MINUTES);
        
        System.out.println ("The table :"  + response.toString());
        
        camelContext.stop();
     }
The sample use JNDI in order to cache beans.
The hostes that finds table code:
public class hostess {
    public CafeTable getTable (Guest pGuest)
    {
       System.out.println ("No of people  :" + Integer.toString(pGuest.getmNoOfP()));
        
        return new CafeTable ("Not in the smoking Zone");
    }
}

The registration in the JNDI :

  protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = new JndiRegistry();     
        jndi.bind("hostess", new hostess());
        return jndi;
    }

The table and the guests classes are simple pojos:

public class Guest {
    
    private int mNoOfP;
    public int getmNoOfP() {
        return mNoOfP;
    }
    public void setmNoOfP(int mNoOfP) {
        this.mNoOfP = mNoOfP;
    }
    
    public Guest(int pNoOfP) {
        mNoOfP = pNoOfP;
    }
    
}

and  the CafeTable pojo

public class CafeTable {
    private String mLocation = "";
    public CafeTable (String pLocation)
    {
        mLocation = pLocation;
    }
    @Override
    public String toString() {
        return "CafeTable{" + "mLocation=" + mLocation + '}';
    }
}

The result:
No of people  :4
The table :CafeTable{mLocation=Not in the smoking Zone}

יום חמישי, 8 במאי 2014

Writing custom openstack wighter

Two options are available in order to guide the OpenStack cloud infrastructure to schedule  the next Virtual machine on a host with minimum benchmark execution time left :
1.Write a custom Nova scheduling filter.
2.Write a custom Nova scheduling  weigher   .
Due to the fact that the filter logic in this case is complicated a wighter class is more appropriated for this task.

The wighter classes location :
The scheduling implementation in Nova is located in \nova\scheduler directory
The weights folder contains build in Weighers for example the RAMWeigher that returns host score  according to its available ram .

Open stack Weigher classes relationships 

Capture4
A concrete Weigher implements the weights.BaseHostWeigher interface (its abc python interface ).
The BaseWeigher interface declares the following methods:
The def _weigh_object(self, host_state, weight_properties):
method returns the host un normalized score.
The def weigh_objects(self, weighed_obj_list, weight_properties):
method may used to calculate the weight of an host when information from all hosted is required  .(Not our case ).

We need to create a new concrete Weighter class that's overrides the _weigh_object method .

class BenchMarkResourcesWeigher(weights.BaseHostWeigher):
    def _weight_multiplier(self):
        """the default weight multiplier."""
        return 1
    def _weigh_object(self, host_state, weight_properties):
        """Higher weights win.  """
        return -1 * host_state.BenchMarks_Total_Work_Left


In the next Post we will add the new member : BenchMarks_Total_Work_left  to the host_state class 

יום שלישי, 6 במאי 2014

Spring inheritance tree

With Spring you can collect classes and there inherits tree into list .
For an example :
Use the autowire spring context declaration:
@Configuration
@ComponentScan("com.model.cars")
public class SpringContext {
}

We can declare a ctor in a @component Road class
 @Autowired
public Road(List<Car> pCar) {
 
}
The auto wired process scans the  com.model.cars directory for all the components that inherit from Car class (and the car class itself if exists in the scanning directory )  and construct the car Ctor parameter pCar list accordingly .
So if the com.model.cars path contains the following component
@Component
Class sosita extends Car
and
@Component
Class Subaru extends
sosita
The  Road ctor will be called with a 2 items list of sosita and Subaru

Reference:
http://lkrnac.net/blog/2014/04/30/load-inheritance-tree/

Simple 0MQ request replay client and server

The biggest problem of 0MQ in my opinion is it’s installation process.
In contradiction to Rabbit , MSMQ and ActiveMQ its very hard to install it and use it.
But there are a lot of samples in any language.
The project:
Capture50
The main

package test0mq2;
import java.lang.reflect.Field;
public class Test0Mq2 {
    public static void main(String[] args) throws Exception {
 
        SetLibraryPath();
 
        StartServer();
        
        SendMsgToServer();
       
    }
    private static void StartServer()
    {
        ZeroMQRepServer theZeroMQRepServer = new ZeroMQRepServer();
    
        theZeroMQRepServer.Startlisten();
    }
    
    private static void SendMsgToServer()
    {
        ZeroMQREQClient theZeroMQREQClient = new ZeroMQREQClient();
        
        theZeroMQREQClient.SendMsg ("Hello Zvika");
    }
    private static void SetLibraryPath() throws SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        String libPathProperty = System.getProperty("java.library.path");
        
        System.out.println(libPathProperty);
        
        System.setProperty( "java.library.path", "H:\\deleteme100\\zeromq-4.0.4\\bin\\Win32" );
        
        Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
        fieldSysPath.setAccessible( true );
        fieldSysPath.set( null, null );
    
        libPathProperty = System.getProperty("java.library.path");
        System.out.println(libPathProperty);
    }
}

Note:
The SetLibraryPath method is used to set the path of the jzmq
The jzmq.dll is a jni native extension used for the 0MQ
The Server


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQRepServer extends Thread{
    
     public void Startlisten ()
     {
         (new ZeroMQRepServer()).start();
     }
     @Override
     public void run() {
          
        ZMQ.Context context = ZMQ.context(1);
        //  Socket to talk to clients
        ZMQ.Socket responder = context.socket(ZMQ.REP);
        responder.bind("tcp://*:5555");
        while (!Thread.currentThread().isInterrupted()) {
            // Wait for next request from the client
            byte[] request = responder.recv(0);
           
            // Send reply back to client
            String theReplay = "Get:" + new String(request);
            responder.send(theReplay.getBytes(), 0);
        }
        responder.close();
        context.term();
     }
}

 The Client


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQREQClient {
    
    public void SendMsg (String pMsg)
    {
        ZMQ.Context context = ZMQ.context(1);
       //Connecting to server with REQ mode 
        ZMQ.Socket requester = context.socket(ZMQ.REQ);
        
        requester.connect("tcp://localhost:5555");
        requester.send(pMsg.getBytes(), 0);
        byte[] reply = requester.recv(0);
        
        System.out.println("Received:" + new String(reply));
        
        requester.close();
        
        context.term();
    }
}

 

 

יום שני, 5 במאי 2014

Inspecting executing commands for cancelation

Sometimes a New Command Execution should cancel current commands execution or remove older commands from a commands Queue .
For an example:
When a Command is issue to Self driving car to follow a route while other route command is currently executing , the new Route command should cancel the execution of the old one.
There should be a mechanism in the software that its duty is to decide is there a need for canceling a commands before executing the new command ?
The  mechanism should determent:
1.Which commands to cancel ?
2.Can the cancelation process may be invoked in parallel to the execution of the new  command or the new command execution should be blocked until the cancelation process finished .
3.Maybe instead of cancelation of a command execution an execution blocking is needed (in case of resource sharing between the commands and there is a need to boost the current command execution ).
The algorithm for selecting the command should be based on the following parameter's:
1.Commands that are currently executing or in queue .
2.Priority of the current executing commands .
3.Internal concrete state of the current executing commands.
4.Shared resources that are consumed by the current executing commands
5.The period of time left for the old commands to finish it’s duties.
6.System overall state.

One of the architecture dilemmas is where to implement the mechanism  for choosing the commands to be canceled.
Should it be part of the Command ?
Capture46

List <ICommand> GetListOfCommandsToCancel (List <ICommand> pCurrentExecutingCommands).

There is a Single responsibility principle violation in this case I think .And this attitude may lead to DRY violation were each command may implement a similar mechanism .

May be the following implementation :

Capture47

Here there is a decoupling between the command and the commands that should ne canceled.

I think that the return value of the GetListOfCommandsToCancel should be extend to use custom return type

Capture48

The IComamndCancelationInspector may have properties used to query the current domain overall state in order to determent its result.
The recommendation may although contain if the cancel of just postponed the command .

The execution sequence of the process :

Capture49 

Note:
The cancelation process is out of this post scope.

יום ראשון, 4 במאי 2014

OpenCL Simple Sample

The following sample use OpenCL in order to calculate the sum of two distinct vectors.
I use Nvidia video card so I installed the Cuda SDK in order to get the OpenCL headers and libs.
The C++ Calling program:

#include "stdafx.h"
#define PROGRAM_FILE "vecSuming.cl"
#define KERNEL_FUNC "vec_sum"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include\CL\opencl.h"
int _tmain(int argc, _TCHAR* argv[]) {
	cl_platform_id platform;
	cl_device_id device;
	cl_context context;
	cl_command_queue queue;
	cl_int i, err;
	cl_program program;
	FILE *program_handle;
	char *program_buffer, *program_log;
	size_t program_size, log_size;
	cl_kernel kernel;
	size_t work_units_per_kernel;
	float vec1[4], vec2[4], result[4];
	cl_mem vec1_buff, vec2_buff, res_buff;
	for(i=0; i<4; i++)
	{
		   vec1[i] = i ;
		   vec2[i] = i * 2 ;
	}
	 
	clGetPlatformIDs(1, &platform, NULL);
	clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1,
	&device, NULL);
	context = clCreateContext(NULL, 1, &device, NULL,
	NULL, &err);
	program_handle = fopen("C:\\Learn\\OpenCL\\MeshamemLiMavetOpenCL\\matvec.cl", "r");
	fseek(program_handle, 0, SEEK_END);
	program_size = ftell(program_handle);
	rewind(program_handle);
	program_buffer = (char*)malloc(program_size + 1);
	program_buffer[program_size] = '\0';
	fread(program_buffer, sizeof(char), program_size,
	program_handle);
	fclose(program_handle);
	program_buffer[program_size - 6 ] = '\0';
	program = clCreateProgramWithSource(context, 1,
	(const char**)&program_buffer, &program_size, &err);
	free(program_buffer);
	cl_int theBuildResult =  clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
	if (theBuildResult != 0) {
		// Determine the size of the log
		size_t log_size;
		clGetProgramBuildInfo(program, device , CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
		// Allocate memory for the log
		char *log = (char *) malloc(log_size);
		// Get the log
		clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
		// Print the log
		printf("%s\n", log);
		
		return -1;
	}
	kernel = clCreateKernel(program, KERNEL_FUNC, &err);
	queue = clCreateCommandQueue(context, device, 0, &err);
	vec1_buff = clCreateBuffer(context, CL_MEM_READ_ONLY |
		   CL_MEM_COPY_HOST_PTR, sizeof(float)*4, vec1, &err);
	vec2_buff = clCreateBuffer(context, CL_MEM_READ_ONLY |
		   CL_MEM_COPY_HOST_PTR, sizeof(float)*4, vec2 , &err);
	res_buff = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
		sizeof(float)*4, NULL, &err);
	clSetKernelArg(kernel, 0, sizeof(cl_mem), &vec1_buff);
	clSetKernelArg(kernel, 1, sizeof(cl_mem), &vec2_buff);
	clSetKernelArg(kernel, 2, sizeof(cl_mem), &res_buff);
	work_units_per_kernel = 4;
	clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &work_units_per_kernel, NULL, 0, NULL, NULL);
	clEnqueueReadBuffer(queue, res_buff, CL_TRUE, 0,sizeof(float)*4, result, 0, NULL, NULL);
	for ( i = 0 ; i < 4 ; i ++ )
	{
		printf ( "result of Index %d = %f " ,i,  result[i]);
	}
	clReleaseMemObject(vec1_buff);
	clReleaseMemObject(vec2_buff);
	clReleaseMemObject(res_buff);
	clReleaseKernel(kernel);
	clReleaseCommandQueue(queue);
	clReleaseProgram(program);
	clReleaseContext(context);
	return 0;
}

  The kernel function program :


__kernel void vec_sum(__global float* vector1,__global float* vector2,
	__global float* result)
{
	int i = get_global_id(0);
	result[i] = vector1[i] + vector2[i];
}
The result :
Capture45