Tuesday, July 21, 2015

C# Notation Reminder

I often forget the nomenclature of a few C# coding constructs. Maybe writing them down will help.

In C# 2.0, generics were introduced. For a generic there is a <T> which stands for Generic Type Parameter. The generic type is a blueprint for what you actually want to implement. An example is the code below where I want to implement an IoC container to write session details to Redis.


            /// <summary>
            /// funq IoC container for in memory cache client like Redis
            /// used for Auth
            /// </summary>
            /// <param name="container"></param>
            public override void Configure(Container container)
            {
                Plugins.Add(new AuthFeature(
                    () => new AuthUserSession(),
                    new IAuthProvider[] {new BasicAuthProvider(), }));

                container.Register<ICacheClient>(new MemoryCacheClient());
                var userRepository = new InMemoryAuthRepository();
                container.Register<IUserAuthRepository>(userRepository);
            }
        }

The other thing I forget is what () => means in for lambdas. To create a lambda expression, you optionally specify input parameters on the left side of the lambda operator =>, and you put the expression or statement block on the other side. I always forget that () is just an empty parameter list. I am not even sure why I used an empty list in the delegate above. Why!?

For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.

REFS:
https://msdn.microsoft.com/en-us/library/0zk36dx2.aspx
https://msdn.microsoft.com/en-us/library/bb397687.aspx

Thursday, July 9, 2015

NodeJS - Concurrency Model

NodeJS is magical. It is fast and easy and monolingual from UI to DAL. It is the purest unicorn of technology. It supports concurrency but is single threaded.So your code executes in the single thread, However, all I/O is evented and asynchronous, so the following won't block the server. The callback and the promise abstractions provide easy access to the event loop. Any I/O call saves the callback and returns control to the node runtime environment.One key idea is "CPU-intensive work should be split off to another process with which you can interact as with events, or by using an abstraction like WebWorkers." Competing ideas to ponder are the use of the cluster module presented in Portland's use group Chris McDonald's example or using PM2 as outlined here clustering made easy managing eventing

Basic Concepts
  • Continuation passing style functional programing paradigm where each function provides an extra argument to pass a return value to it. That means that when invoking a CPS function, the calling function is required to supply a procedure to be invoked with the subroutine's "return" value. Expressing code in this form makes a number of things explicit which are implicit in direct style. Procedure returns become apparent as calls to a continuation; intermediate values, which are all given names; order of argument evaluation, which is made explicit; and the final action of the called procedure, which simply call a procedure with the same continuation, unmodified, that was passed to the caller.
  • Streams Readable and writable streams an alternative way of interacting with (file|network|process) I/O.
  • Buffers Buffers provide a binary-friendly, higher-performance alternative to strings by exposing raw memory allocation outside the V8 heap.
  • Events Many Node.js core libraries emit events. You can use EventEmitters to implement this pattern in your own applications.
  • Timers setTimeout for one-time delayed execution of code, setInterval for periodically repeating execution of code. See http://book.mixu.net/node/ch9.html

Cautionary Tales and Coding Practices
http://callbackhell.com/
http://becausejavascript.com/node-js-process-nexttick-vs-setimmediate/
http://howtonode.org/understanding-process-next-tick

PayPal has a lot of data and a lot of concurrent users. This is a problem I want to have, so when I see them move from Java to Node, I pay attention. Paypal developed Java and Node pages side by side to benchmark performance. To quote, the node.js app was:
  • Built almost twice as fast with fewer people
  • Written in 33% fewer lines of code
  • Constructed with 40% fewer files

Both CRUD apps were simple (few routes, few API calls). Node outperformed Java, even given the fact that the Java app had a two month head start. You can see the performance benchmarks here
https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

I care about Node because I want an easier path to building a site to secure access to business intelligence assets. Most data services teams I work with have no web developers at all, and rely on Sharepoint and Power BI to deliver what-if analysis. Based on my review of Express with templating and the elegance of D3 on Angular for charting, I think Node becomes less "too hard" to implement for a reporting application team.

I wanted to understand the concurrency model as compared to using the actor pattern implemented in Hadoop via Akka.



Some ideas I liked
"

It is useful to understand how node and V8 interact. Node handles waiting for I/O or timers from the operating system. When node wakes up from I/O or a timer, it generally has some JavaScript callbacks to invoke. When node runs these callbacks, control is passed into V8 until V8 returns back to node.
So, if you do var ii = 1; ii++;, you will never find that ii is anything other than 2. All JavaScript runs until completion, and then control is passed back to node. If you do doSomething(); doSomething(); that will always run doSomething twice, and it will not return to node's event loop until the second invocation of doSomething returns. This means you can completely lock up node from a simple error like this:
for (var i=0 ; i >= 0 ; i++) {}
It doesn't mater how many I/O callbacks you have registered, timers set to go off, or sockets waiting to be read. Until V8 returns from that infinite loop, node does no more work.
This is part of what makes programming in node so nice. You never have to worry about locking. There are no race conditions or critical sections. There is only one thread where your JavaScript code runs."

REFS
https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
http://stackoverflow.com/questions/5153492/models-of-concurrency-in-nodejs
https://creationix.com/jsconf.pdf
https://github.com/xk/node-threads-a-gogo

Wednesday, July 1, 2015

A Basic Outline For NodeJS Rapid Development

I have intentionally veered away from web development work. This is due to some preconceived notions about the market and talent. The basic nay saying sounded like this: there is too much abstraction to understand web code, the learning curve is too steep in modern web development, there are already too many talented Java/.Net web developers out there to compete with, etc. My is really forcing me to rethink what lines I should draw for myself. A good way to break down some of that lack of knowledge seems to be learning the Node JS ecosystem (ie MEAN stack). It has been tons of fun so far.

Here is a set of notes I am using to learn the different templating engines available for scaffolding a new Node project. This is based on a windows work environment. I can already see how ramping up a templated site to allow for D3 on Angular visualizations is a great entry point for any BI project. Since it is all java script some of the IoC and MVC pattern abstractions that make C# code hard to read become far less indirect (at least to me so far). Also, everything has a cute name.

notes:


//install node  I used
Write-host "install node globally" -foreground "Darkcyan"
choco install node
npm --update
Write-host "install express globally" -foreground "DarkCyan"
npm install -g express
Write-host "install node-inspector debugger globally" -foreground "DarkCyan"
npm install -g node-inspector
Write-host "install yeoman app templating globally" -foreground "DarkCyan"npm install -g yo
Write-host "install daemon to monitor changes to your node site globally" -foreground "Darkcyan"
npm install -g nodemon
Write-host "install grunt gulp and bowerbuild support globally" -foreground "Darkcyan"
npm install -g bower grunt-cli gulp
Write-host "install express site templating generator. Sort of sucks but oh well. You can extend it with Hogan for logicless templates or ejs/jade for logic supporting templates" -foreground "darkcyan"
npm install -g express-generator
Write-host "install hottowel app templating generator." -foreground "darkcyan"
npm install -g generator-hottowel

/*
-h, --help          output usage information
-V, --version       output the version number
-e, --ejs           add ejs engine support (defaults to jade)
    --hbs           add handlebars engine support
-H, --hogan         add hogan.js engine support
-c, --css <engine>  add stylesheet <engine> support (less|stylus|compass) (defaults to plain css)
    --git           add .gitignore
-f, --force         force on non-empty directory
*/


//change to root source dir

express AppName  -t hogan -c less ; cd AppName; npm install;pwd; ls;
if (Test-Path node_modules){write-host "node-modules found"} else {write-host "node-modules not found. did you do npm install as root?"}
more .\package.json;.\bin\www

//change the second line in bin\www appName:server to just app (matching the app.js in root dir)
//add a Gruntfile.js
New-Item Gruntfile.js -ItemType file

//start Node CLI repl
node --debug

//start an inspector
node-inspector
//open a chrome tab to the posted URL

//Run site in Debug
SET DEBUG=App:* ; npm start

//open chrome brownser to url and port in bin/www settings
http://127.0.0.1:3000/

//run app
node ./bin/www
//or maybe
npm start

//or let's try creating a new app using hottowel
mkdir yoApp
cd yoApp
yo hottowel helloWorld
//another way to start an app
gulp serve-dev --sync
///////////////////////////////////
//To run site in Debug
SET DEBUG=App:* ; npm start

//open chrome brownser to url and port in bin/www settings
http://127.0.0.1:3000/

//run app
node ./bin/www

//or maybe
npm start

//or let's try creating a new app using hottowel
mkdir yoApp
cd yoApp
yo hottowel helloWorld
//another way to start an app
gulp serve-dev --sync

# uninstall commands
$NodeInstallPath  = cmd /c where node
$NpmInstallPath  = cmd /c where npm;if($NpmInstallPath.Count -gt 1){$NpmInstallPath = $NpmInstallPath[0]}


$AppData = Get-Childitem env:APPDATA | %{ $_.Value }
$userconfig       = npm config get -g -Filter userconfig
$globalconfig     = npm config get -g -Filter globalconfig
$globalignorefile = npm config get -g -Filter globalignorefile
$cache            = npm config get -g -Filter cache
$prefix           = npm config get -g -Filter prefix

if (test-path $globalconfig){
    write-host -foreground "cyan" "cleaning out $globalconfig"
    rmdir -force -recurse $globalconfig}
if (test-path $userconfig){
    write-host -foreground "cyan" "cleaning out $userconfig"
    rmdir -force -recurse $userconfig}
if (test-path $globalignorefile){
    write-host -foreground "cyan" "cleaning out $globalignorefile"
    rmdir -force -recurse $globalignorefile}
if (test-path $cache){
    write-host -foreground "cyan" "cleaning out $cache"
    rmdir -force -recurse $cache}
if (test-path $prefix){
    write-host -foreground "cyan" "cleaning out $prefix"
    rmdir -force -recurse $prefix}
if (test-path $NodeInstallPath){
    write-host -foreground "cyan" "cleaning out $NodeInstallPath"
    choco uninstall nodejs}