Found this command to be pretty handy to list all the authors user names for a git project:
git log --format='%aN <%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | cut -d\ -f2-
Found it on this site: http://www.commandlinefu.com/commands/view/4519/list-all-authors-of-a-particular-git-project
Tuesday, June 11, 2013
Thursday, November 22, 2012
Fix a bad fstab file in CentOS
If you ever screw up your fstab file in CentOS the OS will refuse to boot. It will however allow you to enter the root password to try and fix the problem. This would be nice if the file system was mounted correctly because you could then edit the /etc/fstab file and be on your way. Alas, since the filesystem is not mounted correctly because you messed up the fstab file you can only open up files in read only mode. To fix this issue this little command and then edit your fstab file.
mount -n -o remount,rw /
Done! Now go grab a beer and be happy.
Wednesday, December 28, 2011
Wednesday, July 6, 2011
Using log4j to Print Hibernate SQL and Parameters
Here are some handy log4j entries to print out what hibernate is doing behind the scenes:
log4j.logger.org.hibernate.SQL=DEBUG -- This will output the SQL hibernate generates. log4j.logger.org.hibernate.type=TRACE -- This will show the bind variable values hibernate is using.
log4j.logger.org.hibernate.SQL=DEBUG -- This will output the SQL hibernate generates. log4j.logger.org.hibernate.type=TRACE -- This will show the bind variable values hibernate is using.
Thursday, June 2, 2011
Hibernate Criteria with Complex Object Restrictions
So here is the deal, you want to create a hibernate criteria statement with some restrictions. These are no ordinary restrictions though, these are properties of other properties in your class; So something like the following:
You want to load a Person object by setting a restriction on the username property of the UserInfo class. So at first you might try something like this:
What happens when you do that is hibernate throws up all over itself. Here is the way to make it work:
So all that really needs to be done is create an alias for the UserInfo entity and reference that in your restriction statement, DONE!
public class Person{
private UserInfo userInfo;
//Getters and setters omitted
}
public class UserInfo {
private String username;
}
You want to load a Person object by setting a restriction on the username property of the UserInfo class. So at first you might try something like this:
Criteria personCriteria = getCurrentSession().createCriteria(Person.class)
.add(Restrictions.ilike("userInfo.username", "someUserNameGoesHere"));
What happens when you do that is hibernate throws up all over itself. Here is the way to make it work:
Criteria countCriteria = getCurrentSession().createCriteria(Person.class)
.createAlias("userInfo", "ui")
.add(Restrictions.ilike("ui.username", "someUserNameGoesHere"));
So all that really needs to be done is create an alias for the UserInfo entity and reference that in your restriction statement, DONE!
Friday, April 29, 2011
Spring 3 @Async with Hibernate and Spring Transactions
The other day we ran into a fun little problem while trying to use the Spring 3 @Async annotation. We had the following scenario:
A web service was invoked by a client app to update some things in the database. Based on what was updated several other calls had to be made to the database to update even more data. When all was said and done this process could take upwards of 15 minutes to complete which is pretty bad considering the SLA was 10-30 seconds. The obvious problem here is this class is doing WAY too much, but that is what the user's wanted so that is how things had to be.
The call stack looked something like this
->Call in to web service from client - (a few calls happen within this service besides the call listed below)
--->Web service calls service facade to run calculations - (This method is transactional since it reads and writes to the database, this method is also called twice in the web service.)
------>Service facade calls a DAO object (backed by hibernate) many many times.
<---Return to client after an eternity.
Our solution to this was to make the slow part asynchronous with Spring 3's new, awesome @Async annotation. So we annotated the service facade with the @Async annotation and thought our super
slow process would run in the background. We put some log statements in the beginning and end of the slow process so we could be sure the method was actually executing in it's own thread after the web service
call returned, it blew up big time. After searching on Google for a while I found out that having @Async and @Transactional on the same method can cause issues from time to time. So we created another service
facade to call the old service facade. We annotated the new facade with @Async and left @Transactional on the old facade. This approach got us further but failed on the second call to the service facades.
You will notice above that the service facade is called two times from within the web service, the first call worked perfectly. The second call would fail with a org.hibernate.exception.GenericJDBCException: Cannot open connection exception when trying to access a member variable attahed to a parameter in the service facade. Once I dug into this I noticed the parameter object being passed to the service facades was actually a hibernate backed domain object which was populated inside the web service. The member
variable we were trying to access within the service facade was actually a collection of objects that was set to be lazy loaded. When this object was passed to the asyncronus service facade it was not fully
populated, just backed by hibernate. That being said when the async service tried to load up that collection of objects backed by hibernate the hibernate session the object was loaded on was killed
when the first thread exited, the web service thread, thus causing hibernate to fail. By moving the loading logic for that object into the ansync method everything started working!
A web service was invoked by a client app to update some things in the database. Based on what was updated several other calls had to be made to the database to update even more data. When all was said and done this process could take upwards of 15 minutes to complete which is pretty bad considering the SLA was 10-30 seconds. The obvious problem here is this class is doing WAY too much, but that is what the user's wanted so that is how things had to be.
The call stack looked something like this
->Call in to web service from client - (a few calls happen within this service besides the call listed below)
--->Web service calls service facade to run calculations - (This method is transactional since it reads and writes to the database, this method is also called twice in the web service.)
------>Service facade calls a DAO object (backed by hibernate) many many times.
<---Return to client after an eternity.
Our solution to this was to make the slow part asynchronous with Spring 3's new, awesome @Async annotation. So we annotated the service facade with the @Async annotation and thought our super
slow process would run in the background. We put some log statements in the beginning and end of the slow process so we could be sure the method was actually executing in it's own thread after the web service
call returned, it blew up big time. After searching on Google for a while I found out that having @Async and @Transactional on the same method can cause issues from time to time. So we created another service
facade to call the old service facade. We annotated the new facade with @Async and left @Transactional on the old facade. This approach got us further but failed on the second call to the service facades.
You will notice above that the service facade is called two times from within the web service, the first call worked perfectly. The second call would fail with a org.hibernate.exception.GenericJDBCException: Cannot open connection exception when trying to access a member variable attahed to a parameter in the service facade. Once I dug into this I noticed the parameter object being passed to the service facades was actually a hibernate backed domain object which was populated inside the web service. The member
variable we were trying to access within the service facade was actually a collection of objects that was set to be lazy loaded. When this object was passed to the asyncronus service facade it was not fully
populated, just backed by hibernate. That being said when the async service tried to load up that collection of objects backed by hibernate the hibernate session the object was loaded on was killed
when the first thread exited, the web service thread, thus causing hibernate to fail. By moving the loading logic for that object into the ansync method everything started working!
Monday, January 17, 2011
Extracting Movies Times from Google with Mule 3
Hey everyone, this is going to be a multipart series on what it takes to stand up a mule 3 service with the latest features to communicate with CityGrid web services and pull movie times from google and expose them using REST/JSON. To start with I am going to open up all the source code of the mule service then walk through each facet of the system over the next couple weeks. The system, affectionately named UDoin (more on the sweet name later) currently has the following features:
- UDoin takes in a set of latitude and longitude coordinates via a REST/JSON service and retrieves restaurants, bars/clubs, movie times/theaters within a radius of the given location.
- The coordinates are fed to CityGrid along with a few other arguments and this is where the restaurants, bars, and clubs come from. This is done using Mule's new style of multi-casting router the "All" router.
- After results come back from CityGrid movie times for the current location and time are extracted from Google using the open source HtmlParser project and put on the UDoin response.
- UDoin comes complete with JMX to monitor the system health in real time.
- UDoin uses maven for dependency management and packaging to allow for seamless integration with a CI server such as Hudson.
Saturday, December 4, 2010
Unit Testing CXF Web Services
Here is a quick little unit test setup to test your CXF service class and your client class. This is nice if you are testing marshaling issues.
@Test
public void testMyWebService() {
MyWebServiceImpl endpoint = new MyWebServiceImpl ();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(MyWebServiceInterface.class);
svrFactory.setAddress("http://localhost:9000/myService");
svrFactory.setServiceBean(endpoint);
svrFactory.create();
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(MyWebServiceInterface.class);
factory.setAddress("http://localhost:9000/myService");
MyWebServiceInterface client = (MyWebServiceInterface) factory.create();
List details = client.performSomeWebServiceAction("11");
Assert.assertNotNull(details);
}
That's it! This will bootstrap an actual JAXWS version of your service within your JVM and hit it with a real CXF client. One thing you have to make sure of, if you are running tests on a CI server be sure to use a port which is not in use on that build box.
@Test
public void testMyWebService() {
MyWebServiceImpl endpoint = new MyWebServiceImpl ();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(MyWebServiceInterface.class);
svrFactory.setAddress("http://localhost:9000/myService");
svrFactory.setServiceBean(endpoint);
svrFactory.create();
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(MyWebServiceInterface.class);
factory.setAddress("http://localhost:9000/myService");
MyWebServiceInterface client = (MyWebServiceInterface) factory.create();
List
Assert.assertNotNull(details);
}
That's it! This will bootstrap an actual JAXWS version of your service within your JVM and hit it with a real CXF client. One thing you have to make sure of, if you are running tests on a CI server be sure to use a port which is not in use on that build box.
Friday, December 3, 2010
Fixing Eclipse JDK Issue
If you have ever had the issue of eclipse complaining it was not running in a jdk here is how to fix it. Open the eclipse.ini file at the root of the eclipse installation. Add this line to the top of the file, change it to point to your jdk...
-vm
C:\Program Files\Java\jdk1.6.0_11\jre\bin\javaw
Restart Eclipse and you are done!
-vm
C:\Program Files\Java\jdk1.6.0_11\jre\bin\javaw
Restart Eclipse and you are done!
Thursday, December 2, 2010
CXF Interface Extending Another Interface
Just a fun little finding for the day. In the newer versions of CXF you cannot have an interface you define as a WebService extend another interface. This tends to throw a SOAP Fault Exception "Unexpected Wrapper Element". The solution for this is to create another service for the interface you are extending or just merge all the methods into one interface.
Wednesday, December 1, 2010
Spring JDBC Template Tuning
Had a fun little issue the other day with spring's JDBC template. I was querying a fairly large data set, about 65 million records, and realized the job was running slower than molasses going uphill. After debugging the code I found the query was returning very fast, so the database was not the problem. The issue ended up being JDBC template itself, namely the fetch size. If you do not configure the fetch size explicitly the template will use the driver default, which for Oracle is 10, the database I was working with.
The rows returned from my test query was about 53,000, so with a fetch size of 10 this would take 5,300 network trips to retrieve all the data and store it in memory. Before tuning this operation took about 2.5 minutes to run.
One thing to keep in mind when tuning the fetch size is memory consumption. You should be sure you will have enough memory available to store the result set. For my example I set the fetch size to 10,000 as my result sets were always very large. I was also working with a batch job which ran during off-peak hours, so I knew my available memory would be plentiful.
After setting the fetch size the operation took 15 seconds to run, way better than before. Nice!
Sample code:
On a dao class extending JdbcDaoSupport I stuck this in an overridden initDao() method.
simpleJdbcTemplate.
Here is another good post about JDBC fetch size: http://webmoli.com/2009/02/01/jdbc-performance-tuning-with-optimal-fetch-size/
The rows returned from my test query was about 53,000, so with a fetch size of 10 this would take 5,300 network trips to retrieve all the data and store it in memory. Before tuning this operation took about 2.5 minutes to run.
One thing to keep in mind when tuning the fetch size is memory consumption. You should be sure you will have enough memory available to store the result set. For my example I set the fetch size to 10,000 as my result sets were always very large. I was also working with a batch job which ran during off-peak hours, so I knew my available memory would be plentiful.
After setting the fetch size the operation took 15 seconds to run, way better than before. Nice!
Sample code:
On a dao class extending JdbcDaoSupport I stuck this in an overridden initDao() method.
simpleJdbcTemplate.
setFetchSize(1000); Here is another good post about JDBC fetch size: http://webmoli.com/2009/02/01/jdbc-performance-tuning-with-optimal-fetch-size/
Dust off the old blog
Been a couple months since I have added anything to this blog, I was too busy with work. I have a new job now that is structured a little different, I will have some time to add to this every once in a while, here it goes...
Friday, March 26, 2010
Streaming large LOBs from MySQL to flash clients blew up HARD!
Had an interesting run in the other day with a fundamental feature in one of my web applications. What is supposed to happen is a user logs into a flash client app capable of streaming video and playing back an swf version of a powerpoint which is streamed from a database. The flash client defines an movie loader object which pulls a byte stream from a spring MVC controller which in turn pulls these bytes from MySQL using a spring DefaultLobHandler implementation. This is where I think things went horribly wrong, here is the issue:
During a web cast we had many people logging on at one time, about 100. As stated earlier when a user successfully logs in they pull an swf file from the database and load that into their client app. Usually this works perfectly fine with small files, < 1Mb in size, and has even worked with a much larger audience. On this occasion we had a 10Mb swf file to stream from the database, all hell broke loose. Once many people began signing on and downloading this file we began receiving MANY Broken Pipe exceptions from this controller. Right now it is not clear if the database was breaking the connection, the DBCP 1.2.2 connection pool was breaking the connection, the spring MVC controller was breaking it OR the flash client apps were breaking it. I am leaning towards either the database or the connection pool as the culprit here. One last clue was tomcat totally hung when too many of these errors occurred, it refused to serve any more pages. Even after starting and stopping the tomcat service in Fedora it refused to work, I had to hard reboot the servers.
This continued to occur after hard rebooting the servers, I still received Broken Pipe exceptions and Tomcat continued to hang.
I guess the next step is to try and recreate the situation. I am going to take my Uber jar approach and build out a JUnit test which spawns off about 30 threads each calling the download ppt method from the web app. Using JUnit Perf I am going to make this available from the command line, install it on several servers on Amazon Cloud EC2, then nail the main web app with this test and simulate around 120 users downloading a massive powerpoint file at the same time.
Possible Solutions:
If it is found that streaming a large BLOB directly from the database to many clients will never work I have come up with a few different solutions.
During a web cast we had many people logging on at one time, about 100. As stated earlier when a user successfully logs in they pull an swf file from the database and load that into their client app. Usually this works perfectly fine with small files, < 1Mb in size, and has even worked with a much larger audience. On this occasion we had a 10Mb swf file to stream from the database, all hell broke loose. Once many people began signing on and downloading this file we began receiving MANY Broken Pipe exceptions from this controller. Right now it is not clear if the database was breaking the connection, the DBCP 1.2.2 connection pool was breaking the connection, the spring MVC controller was breaking it OR the flash client apps were breaking it. I am leaning towards either the database or the connection pool as the culprit here. One last clue was tomcat totally hung when too many of these errors occurred, it refused to serve any more pages. Even after starting and stopping the tomcat service in Fedora it refused to work, I had to hard reboot the servers.
This continued to occur after hard rebooting the servers, I still received Broken Pipe exceptions and Tomcat continued to hang.
I guess the next step is to try and recreate the situation. I am going to take my Uber jar approach and build out a JUnit test which spawns off about 30 threads each calling the download ppt method from the web app. Using JUnit Perf I am going to make this available from the command line, install it on several servers on Amazon Cloud EC2, then nail the main web app with this test and simulate around 120 users downloading a massive powerpoint file at the same time.
Possible Solutions:
If it is found that streaming a large BLOB directly from the database to many clients will never work I have come up with a few different solutions.
- Continue storing the BLOB in the database AND add a filePath field to the database which points to a location on disk where this file is also stored. This would give me the confidence that I always have a copy of the file sitting in the database in case I need it. This is nice if for some reason the file on disk is deleted I can always restore it. NOW, when a user requests this ppt file the system will first check to see if the file is available on disk, if it is just stream it to the users using regular file io. If the file is NOT on disk, syncronize the call, then stream the file from the database to disk, then stream to the client from the disk.
- Pretty much the same solution as option 1, but instead of streaming the file from disk store it in Amazon S3 storage.
Tuesday, March 9, 2010
SimpleJdbcInsert Sometimes Fails with Java 1.6.0_18
This one is pretty awesome, I am getting an InvalidParameter exception for the SimpleJdbcInsert.executeAndReturnKey(). This only showed up when upgrading from Java 1.6.0_17 to 1.6.0_18. And the extra fun part is this does not occur on all calls to this method, only in isolated cases. It is totally awesome and I have no idea what is causing it....yet.
REVISION:
Actually this was due to the fact I was passing a poorly formatted date string to the SimpleJdbcInsert object. I guess jdk1.6.0_18 is pickier than it's predecessor.
Friday, March 5, 2010
Design Patterns Quick Reference
Here is a handy little reference for design patterns. Nice and concise.
http://www.mcdonaldland.info/files/designpatterns/designpatternscard.pdf
http://www.mcdonaldland.info/files/designpatterns/designpatternscard.pdf
Running Remote Desktop Without a Shortcut
Just a handy little tip I picked up today. If you are ever on a machine and want to run remote desktop, but the shortcut is gone, just issue this command from run:
mstsc
YES!
mstsc
YES!
Friday, February 26, 2010
Creating an Uber Jar with Maven 2
There may come a time in your life as a developer when you need to create a massive 'uber-jar' for your whole project. What do I mean by Uber-Jar, well, this is a jar that has your project, all its dependencies, AND all it's test files packaged and exploded into one massive jar file, the Uber-Jar. You may be thinking to yourself, why would I ever need to do this? Your thinking is correct, this does sound stupid to include test classes next to regular classes, but consider this use case. You may want to create a jar to run tests on a remote machine, say a dev box, to do performance testing. Ok, enough talk, on with the meat of this thing!
Create a Maven Assembly descriptor for your project to include all dependencies and your test classes. Here is an example:
<assembly>
<id>performance-test</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
</assembly>
Ok, so now you have an assembly file. Now we need to instruct maven to run this only when you want it run. For that we need to create a maven build profile. Check it:
<profiles>
<profile>
<id>package.test</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/performanceTest.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Pretty easy so far right? Now we just need to tell maven to run this profile AND generate test classes. Here is a command to do so.
mvn -e clean jar:test-jar assembly:assembly -Ppackage.test
That's it! You now have a massive jar with tons of dependencies in it including all your source and test files side by side. Enjoy!
Create a Maven Assembly descriptor for your project to include all dependencies and your test classes. Here is an example:
<assembly>
<id>performance-test</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
</assembly>
Ok, so now you have an assembly file. Now we need to instruct maven to run this only when you want it run. For that we need to create a maven build profile. Check it:
<profiles>
<profile>
<id>package.test</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/performanceTest.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Pretty easy so far right? Now we just need to tell maven to run this profile AND generate test classes. Here is a command to do so.
mvn -e clean jar:test-jar assembly:assembly -Ppackage.test
That's it! You now have a massive jar with tons of dependencies in it including all your source and test files side by side. Enjoy!
Monday, February 22, 2010
Tomcat Sucks at auto deploying multiple WAR files.
Had an issue over the weekend trying to deploy a few new war files on a tomcat server. The server is set to run with a max heap space of 1G and does not even get close to this while running. Also, said server, on amazon cloud, has the following stats:
Java 6
Tomcat 6
7.5 GB memory
4 EC2 Compute Units (2 virtual cores with 2 EC2 Compute Units each)
850 GB instance storage (2×420 GB plus 10 GB root partition)
64-bit platform
I/O Performance: High
API name: m1.large
When starting the tomcat service all the WARs would be autodeployed and exploded just fine. After humming along for a few minutes the entire server would freeze and had to be hard restarted from the EC2 admin console.
So to fix this I just disabled auto-deploy in the server.xml file for tomcat. I'm wondering what the true cause of this issue was. I'll have to play around and find out.
Java 6
Tomcat 6
7.5 GB memory
4 EC2 Compute Units (2 virtual cores with 2 EC2 Compute Units each)
850 GB instance storage (2×420 GB plus 10 GB root partition)
64-bit platform
I/O Performance: High
API name: m1.large
When starting the tomcat service all the WARs would be autodeployed and exploded just fine. After humming along for a few minutes the entire server would freeze and had to be hard restarted from the EC2 admin console.
So to fix this I just disabled auto-deploy in the server.xml file for tomcat. I'm wondering what the true cause of this issue was. I'll have to play around and find out.
Subscribe to:
Posts (Atom)