Use parameterized logging for increased performance with disabled logging level(s)

See http://www.slf4j.org/faq.html#logging_performance

Advertisements

Setup EhCache using Spring’s Property Configurator

EhCache supports the ability to externalize cache configuration by reading specific properties from system properties

Similarly Spring’s support for property placeholder provides the flexibility to read application specific properties from externalized property files to configure various spring managed beans.

Mixing the two is pretty easy, and requires just a simple setup to push required ehcache configuration properties from spring property files into system properties using a technique explained here

See sample artifacts on GitHub, which include

 

See code below

 

Javascript class definition

There are various techniques for creating classes in Javascript but I find the below the most basic (explicit prototype use helps me, others though equally valid, seem like syntactic sugar. Anyways my 2 cents)

//class definition 
var BankAccount = (function () {
  function BankAccount(initially) {
    this.balance = initially;
  }
  BankAccount.prototype.deposit = function (credit) {
    this.balance += credit;
    return this.balance;
  };
  return BankAccount;
})();

//instantiate object using the above class definition
var myAccount=new BankAccount(100);
console.log(myAccount.balance);
myAccount.deposit(10);
console.log(myAccount.balance);

Bash scripts I use often

Run the same command remotely using SSH on multiple machines

Here is how it works:-

$> on='vinay@serverA vinay@serverB' run='free -g' ./now
running free -g on vinay@serverA
             total       used       free     shared    buffers     cached
Mem:           125         93         32          0          0         13
-/+ buffers/cache:         79         46
Swap:            0          0          0

running free -g on vinay@serverB
             total       used       free     shared    buffers     cached
Mem:           125         84         41          0          0          0
-/+ buffers/cache:         84         41
Swap:            0          0          0

Here is the script now that does that. Simple huh!!

#!/bin/bash
for server in $on
do 
 echo "running $run on $server"
 ssh $server $run
done

looping over an range from file

ssh doesn’t pick the environment set by .bash_profile & therefore one has to explicitly set it for the ssh command executed.

#!/bin/bash
for server in `cat activeServers.txt` 
do
  ssh vinay@$server '. ~/.bash_profile;nohup ./server/bigmemory-max-4.0.5/server/bin/start-tc-server.sh &'
done

10+1 steps to setup BigMemory Max platform with connected clients

Updated on 14th August 2014 to reflect  latest GA release of BigMemory , version 4.1.3

Quick and dirty 10+1 steps to :-

  1. Setup Terracotta Server Array (TSA) of single stripe, active & mirror server pair
  2. Setup of Terracotta Management Console to monitor TSA and connected clients
  3. BigMemory Max client that pushes KeyValue(KV) pair to TSA
  4. BigMemory Max client that fetches KV pair from TSA

Here is what the end-state of the setup looks like:-

ScreenCastSteps

See screen cast on  http://www.youtube.com/watch?v=PuBrgwhwf8o & encoded within the script below:-


#1 download BigMemory Max distribution
# from http://www.terracotta.org/downloads &
# copy the terracotta trial license key
# (terracotta-license.key) received by email
# over to a suitable folder
#

$> ls

bigmemory-max-4.1.3.tar.gz terracotta-license.key

#2 unzip the BigMemory-Max distribution
#
$> tar -xf bigmemory-max-4.1.3.tar.gz

#3. lets setup terracotta server array (TSA),
# the sub-component responsible for distributing in-memory
# data for connected client.
# To setup a simple/minimalist TSA with one stripe,
# a pair of active and mirror server, define a
# configuration similar to the one below.
# The configuration file defines the TSA topology & is used to
# start up all the server instances participating in the TSA
#
$>curl -L https://raw.github.com/vinaynair/tc-labs/master/BigMemoryMaxEhCacheClient/TSA/tc-config-ha.xml -o bigmemory-max-4.1.3/config-samples/tc-config-ha.xml

#4 also copy over the terracotta license key to the install folder
#
$>cp terracotta-license.key bigmemory-max-4.1.3/

#5 first lets start server1 defined within the configuration,
# & since its the first one to come up within this stripe it will
# assume ACTIVE role
#
$>nohup ./bigmemory-max-4.1.3/server/bin/start-tc-server.sh -f bigmemory-max-4.1.3/config-samples/tc-config-ha.xml -n server1 >server1.log 2>&1 &

#6 start server2, which will assume PASSIVE a.k.a MIRROR role
#
$>nohup ./bigmemory-max-4.1.3/server/bin/start-tc-server.sh -f bigmemory-max-4.1.3/config-samples/tc-config-ha.xml -n server2 >server2.log 2>&1 &

#7 start Terracotta Management Console (TMC), component
# responsible for monitoring TSA and connected clients.
# For simplicity lets disable authentication for now
#
$>./bigmemory-max-4.1.3/tools/management-console/bin/start-tmc.sh

# Logon to TMC on the browser @ http://localhost:9889/tmc . As requested, with no authentication set, lets restart TMC & view TSA statistics
#
$>./bigmemory-max-4.1.3/tools/management-console/bin/start-tmc.sh &

# Lets setup simple clients to BigMemoryMax
#8 download simple BigMemoryMaxEhCacheClient (requires git & maven)
#
$>git clone https://github.com/vinaynair/BigMemoryMaxEhCacheClient.git

#9 copy the same terracotta license key to the client folder
#
$>cp terracotta-license.key BigMemoryMaxEhCacheClient/

#10 run the sample client that puts into the BigMemory Max distributed in-memory data store
#
$>cd BigMemoryMaxEhCacheClient/
$>mvn clean compile exec:exec -Daction=put

#11 run the sample client that gets from the BigMemory Max distributed in-memory data store
#
$>mvn clean compile exec:exec -Daction=get

 

 

(Eh)Cache as a Scala Map

Project source is on github @ https://github.com/vinaynair/EhCacheAsScalaMap

Exposing relevant EhCache features as a scala mutable Map provides interesting possibilities for use & integration.
Note by merely switching the ehcache implementation with BigMemory , we can exploit the same set of
features with local offheap in-memory store as well as clustered caches.

Apart from the obvious type-safety with a Map, other obvious features include:-

Add

val m = new CacheAsMap[Int, String]
//add key+value pair using the following syntax
m(1) = "a"
//or
m += (2 -> "b")
//or since its mutable
m+(3->"c")
//or to add 2 cache maps together
val n = new CacheAsMap[Int,String]()
n(4)="d"
n++=m

Remove

m-=2
//or, remove more than one key at a time
n -=(1,2)

Get

println(m(1))

Filter

val m = new CacheAsMap[String, String]()
//add elements
m("apple") = "fruit"
m("potato") = "vegetable"
m("grapes") = "fruit"
//now filter
//For a key value pair, filter results on a value, for example, find all fruits
 val filteredResults=m.filter { case(key,value) => "fruit".equals(value) }

Transform the values using a closure

val m = new CacheAsMap[Int,Int]()
    m(1)=1
    m(2)=2
    m(3)=3
    // for a given key,value pair, increment all the values by 1
    m transform ( (k,v) => v+1 )

Building & running samples

Use SBT and run the unit tests

TODO

  • Figure how to have a BigMemory profile within SBT along with the opensource ehcache to test and try BigMemory features