Jay Paulynice

My thoughts about life, technology, cars, music, business and more.

Author: Jay

Building A Raspberry Pi 3 Cluster

I will write a much longer post about this at some point, but these are some pictures I took while building my raspberry pi 3 cluster.

I ordered a raspberry pi 3 when it came out on February 29, but it took 2 weeks to arrive and in total coincidence arrived home on pi day, 3/14.

I went to the Micro center here in Cambridge to find some more hardware to hook up my pi3, but to my surprise, I found they had several pi3’s at hand. I grabbed a few to build a cluster and this is the end result. I haven’t decided what project I will do with them, but this is just for fun for now.

Up and running:

4-node cluster:

The raspbery pi 3 unboxing:

The supplies:

The original PI:

Fixing Apache Mahout’s Connection Pooling Datasource Warning

I’ve meant to write about this some time ago, but forgot. While using Mahout to build a recommendation engine with a datasource like MySQL or PostgreSQL, one of the warnings one usually encounters is this:

“You are not using ConnectionPoolDataSource. Make sure your DataSource pools connections to the database itself, or database performance will be severely reduced.”

This is a weird warning because even if you’re using a connection pool, the warning will not go away.

Last week, I got very annoyed and decided to fix it. I dug through the source code on github and found this code in AbstractJDBCInMemoryItemSimilarity.java:

In order to create an item similarity based recommender with data stored in a MySQL database, we would do something like this:

MySQLJDBCInMemoryItemSimilarity extends SQL92JDBCInMemoryItemSimilarity which in turn extends the abstract class AbstractJDBCInMemoryItemSimilarity where the data source type is checked to make sure it is a ConnectionPoolDataSource type as above.

The fix is rather simple, but takes some time to dig through the code and figure it out. Here I’m using Spring to autowire my dataSource object, creating the tables and populating them with some initial data SpringDataConfig.java:

This requires commons-dbcp dependency like this:

The difference is instead of returning a dataSource that is an instance of DriverManagerDataSource, we return a new ConnectionPoolDataSource that takes in our normal data source. Mahout then takes care of the connection pooling as follow:

That’s it.

Detecting malicious web attacks with my simple server

Within the last week, I’ve noticed that my blog was frequently down with this error:

My first thought was that I must be getting a lot of visits, but eventually everything would go back to normal. So I restarted the mysql server and apache then all was fine. A couple hours later, I tried accessing my blog again and realized it was down again for the upteenth time.

With the small web server I’ve been working on, I decided to see what’s going on and where the traffic is coming from. I stopped my apache server and installed git, java and gradle on my ubuntu instance.

Once I got the code on the ubuntu instance, I started up my simple web server on port 80 and realized something weird…it seems that someone is running a distributed denial of service attack against my wordpress blog. Instead of using several machines, they’re running on the same IP but different port.

Request signature:


The attacker is using IP and trying to brute force a post request with some xml file to my wordpress xmlrpc.php page. Notice also they’re trying to access my blog IP directly. It could be some targeted attack against Digital Ocean where my ubuntu instance lives or the attacker just has a list of random IP’s they’re trying to attack. The content length also varies but is always in the 250-300 bytes…283 bytes in the case above.

Knowing this info, I modified my code to match the signature and silently drop the request:

Added a request request filter and defined these values:

The filter method:

Check if the signature match:

Then modified the code that handles the request to simply log the IP

The resulting logs from my simple web server showing the logged request and that the response is dropped:

I will add some more details later, but that’s all for now.

Simple Java web server – Part 2

This is a continuation from my last post on creating a web server in Java. The first part went through some basics of the Java NIO API’s…specifically using ServerSocketChannel, Selector, and SelectionKey to setup a thread to asynchronously read and write data to a channel/client.

To try the server, I deployed a sample backbone & marionette app I was working on then using chrome to test it:

Here I have the chrome dev tools opened and we can see all the requests the browser makes to build the html page.

Some server logs

I will add some more details soon. But the goal now is to make the simple web server highly scalable then make it secure through the SSLEngine which has a sample code here: Oracle SSL Engine Demo

Creating a simple web server in Java

This post is part of some ideas I have been working on. Initially, I was just curious about how to create a web server in Java…a simple server that accepts an http request and returns a response. As expected, the Java API is extremely high level…hiding all the nitty gritty details of network programming (TCP, IP, UDP etc…). Fortunately, I found this book: Unix Network Programming which is a definite reference on sockets/networking. While it’s written in C, I found the examples quite easy to read and understand.

As usual, the code is on my repository: simple-java-web-server

Main thread:

Initializing the server:

Implementing the run method:
As long as the server is running, continuously check to see if a client wants to do something: i.e: accept, connect, read or write.

Accepting client connections and registering the client for reading:

Reading from and writing to the client:
This is a rather simple server and reading and writing is done on the same thread. Here we read all the data from the client and based on the request, write back to the client with a response.

Configuring our application with spring annotations:

Finally the Main class responsible for running the server:

Jacoco coverage for multi-project Gradle setup

This post is about setting up a Gradle multi-project build with Jacoco to get an aggregate test code coverage report. This is something I struggled with at first, but with some free time in my hands yesterday, I set out to make it work finally.

The example is part of a Seed Project I have been working on for REST API using Jersey2/Spring.

See build.gradle for full example. The end results:

Coveralls integration:

Simple Html reports:

Building A Simple Movie Recommendation Engine

The goal of this blog post is to build a simple movie recommendation engine using Apache Mahout.

The code is on my github here: Movie Recommendation Engine.

I first came across Apache Mahout a couple years ago while researching machine learning libraries for a music application I was working on. Mahout’s goal, according to the official Apache web page, is “to build an environment for quickly creating scalable performant machine learning applications.” The latest version 0.10 seems to offer just that and more combined with Hadoop and several new features.

But today, I just want to look at a simple use case of building a movie recommendation API. The Use case:


  • A list of users
  • A list of movies
  • Similarities between movies…movie 1 and movie 2, etc.
  • Users preferences for each movie

For a given user recommend a number of movies.

This is similar to the feature we see all the time on Amazon after buying a book: “You might also be interested in that book”. This falls under the collaborative-filtering algorithms families whereby we have a database of user preferences for certain items like movies, books, etc and similarity between the items. Using that data, we can predict what a user would prefer.

Data Model:

The data model is somewhat simple:

  • users table stores user info
  • items table stores movie details
  • taste_preferences stores user preference for a movie
  • taste_item_similarity table stores similarity between movies

To start, I created an interface for the recommendation engine. While the data can be stored in a MySQL database, it can also live in a CSV file. Also recommendations can be user based or item based. With user based recommendations, the idea is to look for similar users and what items they like. Item based recommendation on the other hand mean given several user preferences for items, find similar items. Therefore, we can have multiple implementations.


MySQL Item Based Recommendation Implementation:

Initializing the recommender:

Recommending movies:

Getting movie details:

Example Response:

Android- Change A Shape’s Color Dynamically.

This post is part of a small android jigsaw puzzle game that I have been working on for some time. I originally created the project 2 years ago when I worked on a similar python jigsaw puzzle game.

The project is on my github here: Android-Jigsaw-Puzzle

The idea is very simple: a user draws a picture then creates a jigsaw puzzle with the drawing. The application slices the image into n x m pieces which are then randomly rearranged. The user tries to reconstruct the image by dragging the slices into the correct positions.

I thought this would be a fun idea because after drawing the picture, the user would need a lot of attention to detail to recreate it from the randomly generated and rearranged pieces.

These are some example drawings:

A boat with green color selected:

Boat with orange color selected:

Looking at the lower rows, we see 6 colors and 4 brush sizes. When a user selects a color, all 4 brushes change to show the selected color.

With Android, a developer can create an image by specifying an xml resource. For example this is the small brush which resides under the Android-Jigsaw-Puzzle/app/src/main/res/drawables folder.

small.xml file:

This image can then be referenced as the source code of an image button using the @drawable/id where ‘id’ is the shape created above…@drawable/small in our case.

One problem I came across was how to dynamically change the color of the brushes when a user selects a color. I googled for a few hours and none of the solutions seemed to work for me…for example: stack overflow. Finally, I thought hard about it and came up with this solution:

It could be that the android API has changed, but this is quite simple. However, it took me a long time to get it. So I wrote this quick post in the hope that someone looking to change a shape’s color dynamically in android can find it.

Configuring Backbone+Marionette Application Layout With Database persistence using MySQL+Hibernate

This post is part of a larger code repository search application that I have been working on as a side project. The complete code is on my github here. In order to keep the post short and sweet, it assumes some familiarity with a lot of technologies from the front-end: backbone+marionette+requirejs to the middle and service tiers: java+spring+cxf+elasticsearch+hibernate+mysql.

The focus of this post is primarily this use case:

  • as a user, I want to be able to configure my application layout so that I can have my search results on the left side and my document preview on the right side or vice versa.


1. Results on the left and preview on the right

2. Preview on the left and results on the right

My complete project setup on github:

  • owlpad-webapp: web application
  • owlpad-service: public api
  • owlpad-domain: domain objects shared by the api, service and the client
  • owlpad-service-impl: service implementation

To start, I created 4 classes under the domain project:





Under the owlpad-service project, I created an interface:


Under the owlpad-service-impl, I then created my dao/dao implementations as well as service implementations:



applicationContext.xml for owlpad-service-impl:

There is rather a bit more involved with setting up the REST service, MySQL, and Hibernate, but this is left to the user to experiment with and figure out since the complete code is here

When the service starts, Hibernate creates the tables:

then import some default configuration data located under:

Now that we’ve setup the REST Service, we go into our webapp and setup the spring controller and repository:

Under the owlpad-webapp project, we create:


servlet-context for webapp:


Now that all the backend and middle tiers have been setup. We can look at the fun javascript, backbone, marionette parts. Again, this project assumes that the user is familiar with requirejs, backbone and marionette and has a good understanding already.

In order to make a call to get the application configuration, we create an ajax entity:

then a configMediator:


Then our marionette application:

The important part here is the call to load the application configuration:
configMediator.loadConfig() which returns the json:

Then using underscore’s built in functions _.object and _.map, we create a nicer object for the regions:

Configuring the application is a matter of changing which selector goes with what region in the database. In this case, we have the preview region on the left (r1) and results on the right (r2).

I will add a part 2 later to explain in more details…but for now, the github repo is a good source…

© 2017 Jay Paulynice