OpenSuse 11.2 on Sony Vaio NS25G: Internal Microphone HOW-TO

On my fresh OpenSuse 11.2 setup, the VAIO inbuilt speakers worked well, but the internal mic did not seem to capture. KRecord and Skype did not pick the microphone inputs. Much tinkering and several days later…

My initial suspect of pulse-audio being broken was wrong. The following fix worked:

Dig into the Sound card hardware:

lspci | grep Audio
00:1b.0 Audio device: Intel Corporation 82801I (ICH9 Family) HD Audio Controller

This Intel-compatible audio controller would use the snd-hda-intel driver. Let’s find out:

yast_sound

From the Yast Control Center pick Hardware > Sound. In the screenshot below, the last line indicates the snd-hda-intel driver. Hit Edit and set the model option to toshiba-s06. Hit Next/Ok all the way thru.

YAST Sound Configuration

You can verify your settings out here:

sudo vi -R /etc/modprobe.d/50-sound.conf
options snd-hda-intel model=toshiba-s06

Use the alsamixer to make sure your speaker and capture volumes are cranked up.

alsamixer

alsa_mixer

The references below helped. The Ubuntu forums link below has references to other model names that might work for other laptop models.

[1] https://help.ubuntu.com/community/HdaIntelSoundHowto
[2] http://ubuntuforums.org/showthread.php?t=314383

Another Linux quickie – Determine your Linux distribution

I find myself staring at a remote shell, not knowing what Linux distribution the box is running. There does not seem to be any uname like universal command for this. Here are a few files to try:

OpenSuse:
cat /etc/SuSE-release
cat /etc/issue

Debian and it’s variants:
cat /etc/debian_version

RedHat:
cat/etc/redhat-release

Slackware:
cat /etc/slackware-version

And this one too:
dmesg | head -1

Penguin Notes: LCD Brightness, Firefox Annoyances

A few notes to self, trying to make my new OpenSuse desktop more hospitable.

Annoyance 1:
The laptop brightness controls did not adjust the brightness on KDE 4 running on OpenSuse 11.2. (They worked on Ubuntu Jaunty earlier). Neither could I find brightness sliders anywhere in KDE panels. This helped:

1
$ xbacklight -set 70

The value range is from 0 thru 100. Oddly, the laptop’s brightness shortcuts are BIOS controlled, so I wonder why they are dependent on the OS.

Annoyance 2:
A Ctrl+Bksp (Control + Backspace) on the Firefox address bar erased the entire address. I’m accustomed to using Ctrl + Bksp to erase segments of the URLs (”/” separated). The fix was in the Firefox “about:config”

  • Firefox Address bar: “about:config”
  • Filter by “layout”. Edit this item “layout.word_select.stop_at_punctuation”
  • Change that to a true

Referencing Spring Managed Beans from Non-Spring Java Code

I encountered the need to reference Spring managed beans from legacy Java code (i.e. Java code which is not managed by Spring).

An ideal approach is to avoid mixed-mode code and Spring’ify everything. But that’s not always an option in reality. Legacy projects could have a incremental migration to Spring.

Another option is to instantiate the bean object in your non-managed code. Since you would craft such an object by hand, you have to carefully ensure that all the object dependencies are satisfied. If you miss initializing some properties, you are likely to end up with NullPointerExceptions at runtime.

A preferred approach is to let Spring create and initialize the object for you and to get a handle to that object in your legacy code. We create a ApplicationContextAware bean as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringApplicationContextAware implements ApplicationContextAware {
 
  private static ApplicationContext applicationContext;
 
  public void setApplicationContext(ApplicationContext context)
                                                        throws BeansException {
      applicationContext = context;
  }
 
  public static Object getBean(String beanName) {
      return applicationContext.getBean(beanName);
  }
 
}

We then declare a singleton instance of this bean using something like this in the Spring config:

1
2
<bean id="applicationContextAware" 
class="com.somedomain.somesubdomain.SpringApplicationContextAware"/>

At the time of Spring initialization, Spring will instantiate the bean defined above. And since the class implements the ApplicationContextAware interface, Spring will invoke the setApplicationContext() method on it.

We can then access any named Spring bean as follows:

1
MyBean bean = (MyBean) SpringApplicationContext.getBean("whackyBeanName");

If you prefer the new Annotation based configuration, then we can skip the declaration above and use something like this:

1
<context:component-scan base-package="com.somedomain.somesubdomain"/>

And use the @Component annotation on the bean as follows:

1
2
3
4
5
6
7
8
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
@Component
public class SpringApplicationContextAware implements ApplicationContextAware {
	...
}

The holy grail of search!

Recently came across this article in the feb edition of wired magazine – how big G! keeps it’s search algorithms ticking.

Every year, tens of tweaks are rolled out to the core search algorithms. Invisible to casual observers like us (And you thought doodles were the only thing their search team has been busy with).

I was inspired to read how a team that is already way ahead of the competition, keeps its passion alive to engineer a great product. It was also inspiring to see top engineer and Google Fellow Amit Singhal being quoted by the Wired mag.

My post today was not triggered by the article on wired. Instead, an interesting goofup I saw during during my daily dose of Google news:


Google_Stalin_Google_Goofup

Mistaken identity by Google’s image matching. The great architect of communism impersonated as the deputy chief minister of Tamil Nadu. :)

google_story

Almost about to finish reading The Google Story by David Vise. I would love to write an excerpt some other time. Next in the stack is Thomas Friedman (The World is Flat) and Vir Sanghvi (Men of Steel).

Setting up an Apache2 Virtual Host for a Subdomain

I’ve done this several times before, so noting it for my own quick reference. This might be of help to others finding their way around Apache deployments.

I’ve tried this on a Ubuntu 9.04 (Jaunty Jackalope) with Apache2. Here is what we are trying to setup:

  • Subdomain for your web project: http://mysubdomain.mydomain.com (say, http://staging.mykillerapp.com)
  • You wish to give restricted access to a people in your team, to deploy the project. You don’t wish to give them sudo or admin rights to your Linux box.

Creating a Sub-domain

Your hosting provider is likely to give you a web-based domain management tool (CPanel or something else). Add a DNS “A record” as follows:

subdomain      PUBLICIPADDRESS      A

For example:

staging      x.x.x.x      A

Create a non-root user account to manage your application

$ adduser staging

I’m assuming that staging home is the usual /home/staging. Lets create a www under it, which will serve as the root directory for our virtual host /home/staging/www/

Enable the Apache Virtual Host Module

sudo a2enmod vhost_alias

Create a virtual host configuration for Apache2

Apache2 tracks the virtual hosts configurations in this location: /etc/apache2/sites-available/. There will be a default config file; Lets make a copy of that file at the same directory location.

1
2
3
$ cd /etc/apache2/sites-available/
$ cp default staging
$ vi staging

Make your virtual host file look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<VirtualHost *:80>
 
        ServerName staging.mykillerapp.com
        ServerAdmin appadmin@mykillerapp.com
        DocumentRoot /home/staging/www/
 
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
 
        <Directory /home/staging/www/>
                Options FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
 
        ErrorLog /var/log/apache2/error.log
 
        # Possible values include: debug, info, notice, 
        # warn, error, crit, alert, emerg.
        LogLevel warn
 
        CustomLog /var/log/apache2/access.log combined
 
</VirtualHost>

Enable your virtual host

$ sudo a2ensite staging

This sets up a soft-link in /etc/apache2/sites-enabled/ which points to your virtual host config file.

Finally, restart apache

$ sudo service apache2 restart

Hit your virtual host at http://staging.mykillerapp.com

Generating Unique Ids in PHP – A scheme for UUIDs generation in a distributed application

I was recently involved with a project which had 4 nodes running PHP code and a sharded MySQL database in the backend. The PHP nodes create data objects in a distributed manner and then persist them to the database shards.

Now, each object needs a unique identifier (key). MySQL auto-increment Ids could not be used here since two database shards could pick the same Ids resulting in a collision. We needed an efficient scheme for simultaneously generating unique identifiers on multiple hosts. A few additional constraints apply:

Low Collision Probability:

  • The UUIDs should offer strong guarantees of uniqueness. Even if two nodes create UUIDs at the same instant of time, there would be a very, very low probability of a collision.

Scalable:

  • There would be no central node / authority to generate them. A central node would be a single point of failure. It would also be a bottleneck as our cluster grows.
  • The UUIDs should be generated in a distributed manner. Any web-layer node can generate the UUID locally and this should be efficient to generate.

Length of the UUID:

  • We were not trying to achieve a theoretical uniqueness here. Yet we were looking at 20-50 million unique objects in our database.
  • An 18-20 digit UUID seemed reasonable.

Not Cryptographically Secure:

  • We were not particularly looking for something which is hard to predict or something cryptographically secure. So if the Ids were monotonically increasing or somewhat easier to predict, it was acceptable.

Non-strict Timestamp Synchronization:

  • Nodes which generate UUIDs might not be strictly synchronized in time – however they would be within 5-10 seconds of each other.

The PHP Solution

PHP supports the uniqid() method (since PHP 4), which generates a unique id based on the current microsecond timestamp of the local system.

1
2
3
4
for($i=0; $i<10; $i++){
	echo uniqid();
	echo "<br/>";
}

It returns a 13 character hex string – monotonically increasing timestamp values:
4aa5c7f2c2d33
4aa5c7f2c2d4a
4aa5c7f2c2d4e
4aa5c7f2c2d51…

In a scenario where multiple nodes simultaneously generate the unique Id, there is a small probability of a collision. We add a 4 digit random prefix, to reduce the collision probability further.

1
2
3
4
5
6
7
// Generate a random prefix.
$rand = md5(rand());
$randomPrefix = substr($rand, 0, 4);
 
// Generate a UniqId having the random prefix.
// (4 digit prefix + 13 digit Uniq) is good enough for us.
$uniq = uniqid($randomPrefix);

Modulo and Shard Numbers

In our scheme, the shard number of an object was determined based on the modulus of the unique Id. Thus:

Shard Number = UniqId % TOTAL_SHARDS

For very large numbers the PHP mod (%) operator results in an overflow. So we consider only the 5 LSB digits for computing the modulo value.

1
2
3
4
5
6
7
8
9
10
11
12
  function getModulo($uniqId) {
 
    // Take 5 LSB digits only.
    $uuidLsb = substr($uniqId, -5);
 
    // Convert Hex string to Int. Mod only works on Ints.
    $intUuidLsb = (int) hexdec($uuidLsb);
 
    // Compute Mod
    $mod = $intUuidLsb % TOTAL_SHARDS;
    return $mod;
  }

UUID Generation – Final Cut

We extend the UUID scheme a bit further in order to help us quickly determine the shard number from a given UUID string. We prefix the shard number (mod value) to every UUID. The final version of our scheme looks like this:

  • Generate random number.
  • Generate UniqId with the random number prefix.
  • Generate UUID = mod(UniqId) + UniqId

Here is the final version of our code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  public function generateUUID() {
 
    // Generate a random prefix. 
    $rand = md5(rand());
    $randomPrefix = substr($rand, 0, 4);
 
    // Generate a UniqId having the random prefix. 
    // (4 digit prefix + 13 digit Uniq) is good enough for us.
    $uniq = uniqid($randomPrefix);
 
    // Compute modulo.
    $modVal = $this->getModulo($uniq);
 
    // UUID = (1 digit mod val + 4 digit prefix + 13 digit Uniq).
    $uuid = $modVal.$uniq;
    return $uuid;
  }
 
  private function getModulo($uuid) {
 
    // Take 5 LSB digits only for the modulo computation.
    $uuidLsb = substr($uuid, -5);
 
    // Convert Hex string to Int. Mod only works on Ints.
    $intUuidLsb = (int) hexdec($uuidLsb);
 
    // Compute Mod
    $mod = $intUuidLsb % TOTAL_SHARDS;
    return $mod;
  }

Please Note that this scheme is not compliant with the UUID RFC 4122, but should suffice for web applications in general.