Tuesday, June 28, 2011

Advanced Java Multi-threading - Gotchas around thread-safety!

Went through the initial pages from the book Java Concurreny in Practice - Brian Goetz and found it awesome. Thought of sharing some key learnings...

1. A Java Class is said to be threadsafe, if it behaves the same way in both single and multi threaded environment.

2. A Java Class should always be designed by keeping "multi-threaded issues" in mind, as these days, the Java Class hardly work in Single threaded environment. When a class is being called by a framework, it is most likely invoked by a thread. A typical example is a TimerTask invoked by Timer.

3. Stateless objects are always thread safe. By stateless objects we mean those objects of classes which do not have any instance variables defined to hold the state of the object. The objects of these classes are always thread-safe, because there is no data to share between threads.

4. What are class invariants?
The states of objects that have some relationship to each other all the time. Say, for example, if there are two instance variables inside an object which are related like dateOfBirth and Age, then whenever dateOfBirth gets updated, the age should be updated atomically. The class invariants here are dateOfBirth and age and these must always be protected within a critical section.

5. What is reentrancy?
The intrinsic locks (the object locks) is acquired on a per-thread basis, as opposed to, a per-invocation basis in posix threads. The default locking behavior of pthreads (posix threads) mutexes are on per-invocation basis. The JVM associates a counter with the thread and whenever the thread which already acquired the lock tries to grab the same lock again, the JVM allows the thread to proceed further, after incrementing the associated counter. This is defined as reentrant.

6. Significance of volatile variables.
In multi-processor/multi-core CPU hardware, there are optimizations to temporarily store data in CPU cache or registers for a while, before updating the in-memory location. For a thread to always make sure that, it reads the most recent value of a shared data, mark it as volatile. So that, the underlying optimizations on locality of references are turned off.

7. Immutable objects are always thread safe. The objects whose state cannot be changed after construction are said to be immutable. When a state cannot be modified, it is always thread-safe.

Tuesday, June 21, 2011

Fundamentals of Telecom and Voice - Made easy

Have started to work on a new domain - "The Voice Technology" and eventually started logging all the relevant technical stuffs that are found useful.

1. What is frequency?

According to International System of Units, the frequency is measured in hertz and is defined as the number of cycles per second of a periodic phenomenon.

2. What is an Audio Frequency (Audible frequency)?

An audible frequency is a periodic vibration that is audible to average human ears. It is always between 20hz and 20KHz.

3. What is (voice) signal Processing?

When a system or a process is operating or analysing the (voice) signals in either discrete or continuous time to perform useful operations on those signals, it can be called as (voice) signal processing.

4. What is Bandwidth?

Bandwidth has a significance difference in definition and understanding in analog and digital world. Let's define as below.

5. What is Analog Bandwidth?

It is a measure of the range of signal frequencies that a system can process or a transmission line can carry. It is measured in hertz.

6. What is Digital Bandwidth?

It is the rate of data (binary digits) transfer or throughput of the communication endpoint, measured in bits per second.

7. What is a Voice signal & the significance of Nyquist Rate in voice processing?

In physical forms, the voice is defined as vibration of air on the continuous-time plane. This signal can be sampled into discrete-time plane for processing or storing in digitized form. The digitized signal can be "fully" converted back to continuous-time plane for signal reproduction as "original". This is possible only when the sampling rate exceeds twice the maximum frequency in the "original" signal on continuous-time plane. This signal is said to be a band limiting signal and the associated sampling rate is defined as Nyquist Rate.

8. What is a voice channel?

The analog bandwidth of a Voice signal is roughly 4khz, and those used in telephony systems varies between 300hz and 3300hz. So, when we convert these signals into the discrete-time plane, we should ideally sample them at 8khz (as per the Nyquist Rate). This means every sample is taken at 125 micro secs (1/8000) in the continuous-time plane. So, if 256 level is used to represent a sample, i.e. 8 bits, then 8000 samples per second will result in transmitting 64000 bits per second. A voice channel on the telecommunications domain requires a channel bandwidth of 64kbps.

9. What is multiplexing?

It is a concept of using a single/same carrier for multiple (voice) channels. In the analog world, multiplexing is performed by dividing the channel based on frequencies (FDM). On the other hand, multiplexing in the digitized world is performed by divided the carrier into multiple channels by time (TDM) .

10. What is a T1 carrier line?

As an example, for a 1.5Mbps telephone carrier, 24 voice channels (1500/64 = 24) can be multiplexed. This is nothing but a classical T1 line. A typical T1 frame has 193 bits (24 channels * 8bit) / 125uSec. A TDM network, in general, will be implemented with transmission technologies like X.25, Frame Relay, IDN, ISDN and SONET.

Thanks for reading Part 1

330
.
The subsequent posts will have more in-detail information about various aspects of the voice technology... Thank you!

Friday, June 10, 2011

Low disk space in windows (having weblogic server)...

Of late, I found that - suddenly my windows operating system started warning me about the low disk space. I had atleast 50Gig space free for sure. I had no clue of what is clogging my hard drive and the only clue is it's a development box. When I was badly in need for a tool, which can walk through my hard drive and give me a easy report of what is occupying my disc ,the most space, I stumbled upon this open source tool - WinDirStat

The tool gives exactly what I needed. It runs over the hard drive and computes a clear report of which file/folder is occupying the most disc space. Check the below snapshot.


Problem on my PC: The data source configured on my weblogic server was pointing to a Oracle DB server which is heavily loaded and not responding. The server log on which the data source was targeted started growing slowly due to this issue's stack trace and bloated so high - 50Gig in a matter of 2 days (very surprising) . After bringing down the server and pruning the log files, the issue got resolved.

Thursday, June 9, 2011

Issue with linux softlink which is part of a running JVM's classpath...

We observed a strange problem with Weblogic JVM in 10.3.2 version:

1) We're adding the following as classpath during the cluster/server startup. Please note that, "config" below is a symbolic link and this gets into the Weblogic JVM's classpath.

/mydrive/myfolder/myprojects/project/config

config -> project-1.1/config

2) The application is deployed and it works fine.

3) During the next release of the application, the "config" symbolic link used in the JVM classpath has been changed as below and the old project folder was removed. (THIS IS THE ISSUE).
config -> project-1.2/config

4) Now the newly deployed application is trying to come up to ACTIVE state. As part of initializing the app, it reads a properties file from the classpath (which is supposed to be referenced through the config soft link in the classpath).

5) At this stage, the Weblogic JVM is still holding the softlink reference to the previous deployment folder which NO MORE EXISTS. I verified this with a sample program and have described about it further below.

6) As a result, the application throws FileNotFoundException.

More experiments:
I tried a simple experiment to reproduce this issue with a standalone JVM...

1) Create a folder: /mydrive/myfolder/myprojects/project.
2) Create two subfolders: project1 and project2.
3) Each folder must have config directory within itself and config folder should contain test.props with their own contents.
4) Create a config softlink at the base folder (/mydrive/myfolder/myprojects/project) to either project1/config or project2/config.
5) Create a Java file which will try to read the property file from the classpath ("test.props") and wait for a user input. And after user input is read, it should again read the same properties file.
6) After successfully compiling the program, run the program like: java -cp "/mydrive/myfolder/myprojects/project/config:." JavaFile
7) You will see the program reads the correct property file as pointed out through the softlink and waits for user input.
8) Now, on another terminal switch the config softlink to point to the other project's config folder, while the JVM is running on another terminal waiting for user input.
9) If you give 'Enter' on the waiting java process, it will READ again the test.props file AFRESH. But this time you can SEE IT STILL READS THE SAME OLD PROPERTY FILE AND NOT THE NEW ONE POINTED BY SOFTLINK.
10) If you had deleted the old projectX folder, instead of just changing the softlink as above, you will get null for the resource - test.props, which encounters just the same scenario as we observed in our application on Weblogic.


Bottom Line:
Never change a softlink that is part of a running JVM's classpath. This includes not just the soft link (versioned or non-versioned), but also the softlink contents. Instead add the PARENT FOLDER of the softlink in the JVM classpath, so that any changes to the softlink or contents of the softlink are decoupled from the running JVM.

Difference in compiled binary between Flex Mojo Build & Eclipse Build

Problem:
I've been working on a Flex Project for sometime and came across this issue after maveniz'ing our project with Flex-Mojos. The issue was, for some reasons, the UI misbehaved with maven build - the Panels were not laid out as intended and the UI seems to have broken in the maven build. However, the same code works perfectly fine when built through Eclipse.

Analysis and Solution:
We first verified whether the SWC dependencies used in both the build methods are just same or not. Once this step was through, as a next step, we wanted to make sure whether the compiler arguments/options are the same. But surprisingly they were not the same.

The sonatype flexmojo-site gave conclusive documentation about adding the various possible maven flex mojo compiler configurations.

Our eclipse flex compiler configuration looks like below:


We later found that adding the following under <configuration> section made our eclipse configs and maven configs looking same and that has resolved the issue.


<keepAllTypeSelectors>true</keepAllTypeSelectors>
<accessible>true</accessible>
<strict>true</strict>

Wednesday, June 8, 2011

Unlocker 1.9.1 - FileHippo.com

I personally found the Unlocker tool very useful while performing the day-to-day software development/debugging activities...

When we have multiple applications running in parallel and when we have to delete/rename a particular file, we often hit the famous windows error:

"It is being used by another person or a program.
Close any programs that might be using the file and try again" !!

At times, we will not be able to figure the exact application that's having the handle to the file being modified. It's so frustrating that sometimes even after closing all the foreground applications/processes, we still get this error. The reason can be quite obvious that there is some process still running in the background with an open file handle to this file. In such scenarios, this tool comes very handy and as the icon of this tool describes, this will work like a magic wand. The moment we hit the error, the Unlocker freeware automatically catches the error and displays a meaning message and some interactive controls to take decisions. We can either unlock the handle through this tool or just ignore it as the situation demands...

A must tool for developers working on Windows Operating System - Download Unlocker 1.9.1 - FileHippo.com

tail -f for windows

Have often found people using TextPad, Notepad++ or EditPlus to view the running logs on Windows Platform. The approach lurks, as the application drives the page refresh in a user interactive manner. The user has to confirm everytime to reload the file when it gets updated.

There is a project at sourceforge called Tail for Win32 that comes to our rescue.

It's a win32 application and has a nice UI to load the log files. It just mimics the unix command: tail -f <logfile> [a non-user-interactive view of running logs]

Tuesday, June 7, 2011

Relational persistance with Jaxb objects!

Found this very interesting - HyperJaxb3...

The typical scenario of usage is: Schema-driven XML-object-relational database persistence using Java.

With hyperjaxb3, once can just persist the data from XML document instances to Relational databases with very minimum and generic ORM coding in Java. In a nutshell, the XJB (XML-Java Binding) file contains the JAXB/ORM configuration and along with XSD file, hyperjaxb creates the respective Java files (during the build time) with necessary JPA annotations for ORM. So, for the programmers, now we already have a bunch of business domain objects that are JPA compliant ready with zero-coding. These can be readily used with some underlying JPA providers, to have an abstract persistence logic defined before hitting the actual RDBMS. More info available here at Hyperjaxb3 Confluence.


Monday, June 6, 2011

Maven build failure due to unresolved custom variables

The traditional maven build runs as: "mvn clean install". However, With a multiple module project structure, there is caveat to this approach.

Problem: The maven local respository contains the parent and child artifacts with their corresponding POM files. If the parent project used a custom variable say "release.number" which gets used in the child project, then "mvn install" would flush the POM file "as is" into the local repository, along with the built artifact for both parent and child. Now, down the line if any new project adds a dependency to the child project's artifact, then at the build time of the new project, the "release.number" referenced in the child dependency (that was supposed to be initialized in the parent POM file) remains unresolved.

Solution: This is because the POM file residing in the maven repository is only meant to dictate the group id, artifact id, version number etc, of the dependency and nothing more that that. So, the variables' value which is dynamic in nature will remain unresolved, as it is not participating in component build anymore. The solution (that I can think of for now) to this kind of problem is, to flush the artificat into the local repository in offline and not during the artificat build time. This can be done using the below mvn command:

mvn install:install-file -Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-Dsources=src.jar] \
[-Djavadoc=apidocs.jar] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=sources] \
[-DgeneratePom=true] \
[-DcreateChecksum=true]

Courtesy: http://maven.apache.org/plugins/maven-install-plugin/usage.html

This command doesn't take any mandatory pom.xml. Using -DpomFile parameter is optional, so please ignore it to addresss this kind of a scenario. Rather, use -DgeneratePom=true parameter which creates a POM file automatically, based on the other supplied parameters to the command.

Friday, June 3, 2011

Avoiding recurring POM downloads during maven build...

We might often end up in seeing the maven build trying to download poms for many of the dependencies, even if the dependent artifact is available in local repository.

This is a side effect of improper installation of maven dependency artifact.

We generally try to install a maven dependency manually as:

mvn install:install-file -Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-Dsources=src.jar] \
[-Djavadoc=apidocs.jar] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=sources] \
[-DgeneratePom=true] \
[-DcreateChecksum=true]

Courtesy: http://maven.apache.org/plugins/maven-install-plugin/usage.html

Since many of the parameters are optional, we tend to avoid -DgeneratePom option. When -DgeneratePom is not given, the manual dependency installation into the local repository will not have an associated pom generated with it. This is the root cause of your build always trying to download certain poms and there by increasing your build time. The solution is just to add -DgeneratePom=true while doing maven dependency installation.