Saturday, July 26, 2014

Amazon cloud services - EC2

EC2  - elastic Compute - on demand, disposable.

Is a shared environment - physical machines shared by multiple customers.

these are the  tech layers

  1. customer instances
  2. vpn using Zen Hypervisor
  3. virtual interfaces
  4. Security groups
  5. firewall
  6. physical interfaces

AMI - amazon machine instance (virtual machine)

can be running or stopped
ECS is more public
VPC - Virtual private cloud - more control of network layers - better for high security companies -eg bank

Globally there are Regions - you pick one

then in each region there are AZ - Availability zones

SSS - S3 Simple Storage Service

S3 buckets (buckets of storage - logical term)
snapshots/backups are made to S3 - in a region

Instance - AMI

unit of control/ unit of scale / unit of resilience
you can bundle, spread for ha, add and remove, control cost
scale out multiples of this
large variety of instances.
easy to size up from small..

AMI can be Amazon maintained, community maintained, your own images for your company
e.g. Linux, Enterprise Linux, windows

this in an on demand service

Launching an Instance


set region, instance size, AMI. key pair, security Groups

Key pairs

Public key is auto inserted into new running instance
you get a private-
only you can access your instance due to this.
eg use RDP to get onto the instance.

Credentials - access key and secret key for API

used with all API usage provided by EC2
X.509
used to authenticate against some old API's

Security groups

set name, desc, protocol, port range , ip address, or another security group

How do I launch an ECS AMI instance ?

Windows- Can use powershell commands / script to launch an instance
Linux - from scripting language - commands available
also can use python libraries to run instances.

IAM roles and EC2 tools

After creating a an instance - assign a IAM role.
Create a role with plus instance with an automatic installation of all tools
Also ensures all secret keys automatically set up.
Can revoke roles.

Costing

price per hour
Reserved instance for x years - discounts
spot instances - bid for excess capacity - cheaper, but not reliable

ELB - elastic load balancer

EPH - epherial non persistent storage

CloudWatch -

monitoring tool,
with alarms -
thresholds - can triggers to scale up, scale down - eg cpu power
uses policies to enable.
Additional actions - eg fire to http, email, queue - so we can programmatically
take advantage of alerts
Write metrics
Can see billing details etc.

Route 53 - control of dns - eg rolling deployments


Beanstalk - autoscaling for popular tools eg ruby and python


OpsWorks - automate configuration


Cloud formation - make templates for everything


Need to stop thinking of resources as fixed.







Thursday, June 19, 2014

java sets

Sets

  • HashSet – a set implementation based on a HashMap with dummy values (same Object is used for every value). Has the same properties as a HashMap. Due to such implementation, consumes more memory than actually required for this data structure.
  • EnumSet – a set of enum values. Each enum in Java is mapped into an int: one distinct int for each enum value. This allows to use a BitSet-like structure for this collection, where each bit is mapped to a distinct enum value. There are 2 actual implementations – RegularEnumSet backed up by a single long (and capable to store up to 64 enum values, which covers 99.9% use cases) and JumboEnumSet backed by a long[].
  • BitSet – a bit set. You should always keep in mind that you may use a BitSet for representing a dense set of integers (like ids starting from a number known in advance). This class uses a long[] for bit storage.
  • LinkedHashSet – like HashSet, this class is implemented on top of a LinkedHashMap. This is the only set which keeps its elements in the insertion order.
  • TreeSet – like HashSet, this class is based on a TreeMap instance. This is the only sorted set in the single threaded part of the standard JDK. 

design patterns

singleton
object pool - re-use -save on obkect creation
factory

commands
chain of resp
iterator


punlicsh subscribe


gang of four patts - authors fo a book - 23 design patterns

Design Patterns: Elements of Reusable Object-Oriented Software
From WIKI:
Design Patterns: Elements of Reusable Object-Oriented Software is a software engineering book describing recurring solutions to common problems in software design. The book's authors are Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides with a foreword by Grady Booch. The authors are often referred to as the Gang of Four, GoF,or Go4


Creational eg factory
Structural adaptor
berhaviour - iterator, command, chain, observer

Swotting threads in Java

Threads are sometimes called lightweight processes

subclass thread

implement runnable - pass it to a thread object

Thread class has static methods - tools for threading

Sleep  - to millis and nanos -suspends execution

Thread. interupted

Join - if ont thread calls another threads, join - it waits for theat thread to complete before continueing.

Synchronization - overhead

synch on method locks class

suynch on code locks a local object - faster - more complex

need to make it findegrained

  • Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
  • Reads and writes are atomic for all variables declared volatile (including long and double variables).
Atomic actions cannot be interleaved, so they can be used without fear of thread interference.

deadlock, starvation, livelock

loops needs waits

More sophisticated locking idioms are supported by the java.util.concurrent.locks package.

private final Lock lock = new ReentrantLock();
The biggest advantage of Lock objects over implicit locks is their ability to 
back out of an attempt to acquire a lock. The tryLock method backs out if the 
lock is not available immediately or before a timeout expires (if specified). 

----

there's a close connection between the task being done by a new thread, as defined by its Runnable object, and the thread itself, as defined by a Thread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors

The Executor Interface = 

e.execute(r); - r is a runnable

The ScheduledExecutorService Interface

Trhead pool - specifed number fo threads active, pulling ona queue of tasks/events.
Java 7 has fork/join - work stealing - divides work, waits for result, carries on
recursive task and recursive action
Java 8 uses frosk join on mutiple processors in a "parallelSort" in Arrays Class - leverages concurrency and processors
fork join is use in the lamda framework.
Home Page > Essential Classes > Concurrency

Concurrent Collections

The java.util.concurrent package includes a number of additions to the Java Collections Framework. These are most easily categorized by the collection interfaces provided:
  • BlockingQueue defines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue.
  • ConcurrentMap is a subinterface of java.util.Map that defines useful atomic operations. These operations remove or replace a key-value pair only if the key is present, or add a key-value pair only if the key is absent. Making these operations atomic helps avoid synchronization. The standard general-purpose implementation of ConcurrentMap is ConcurrentHashMap, which is a concurrent analog of HashMap.
  • ConcurrentNavigableMap is a subinterface of ConcurrentMap that supports approximate matches. The standard general-purpose implementation of ConcurrentNavigableMap is ConcurrentSkipListMap, which is a concurrent analog of TreeMap.
All of these collections help avoid Memory Consistency Errors by defining a happens-before relationship between an operation that adds an object to the collection with subsequent operations that access or remove that object.

Preparing for a FIX protocol interview

Gotta prepare for an interview and need to brush up on the FIX protocol

FIX protocol - Common messages and their types.

FIX protocol - Common fields and their types

35 = order type


FIX protocol - Session level messages

on the header section

34 msgseqnum
52 sending time

56TargetCompID
49SenderCompID

SOA - service orientated architecture

Table 1. Web services and SOA
Enabled by Web servicesTechnology neutralEndpoint platform independence.
StandardizedStandards-based protocols.
ConsumableEnabling automated discovery and usage.
Enabled by SOAReusableUse of Service, not reuse by copying of code/implementation.
AbstractedService is abstracted from the implementation.
PublishedPrecise, published specification functionality of service interface, not implementation.
FormalFormal contract between endpoints places obligations on provider and consumer.
RelevantFunctionality presented at a granularity recognized by the user as a meaningful service.
If the principles summarized in Table 1 are complied with, we get some interesting benefits:

Summary

The goal for a SOA is a world wide mesh of collaborating services, which are published and available for invocation on the Service Bus. Adopting SOA is essential to deliver the business agility and IT flexibility promised by Web Services. These benefits are delivered not by just viewing service architecture from a technology perspective and the adoption of Web Service protocols, but require the creation of a Service Oriented Environment that is based on the following key principals we have articulated in this article;
  • Service is the important concept. Web Services are the set of protocols by which Services can be published, discovered and used in a technology neutral, standard form.
  • SOA is not just an architecture of services seen from a technology perspective, but the policies, practices, and frameworks by which we ensure the right services are provided and consumed.
  • With SOA it is critical to implement processes that ensure that there are at least two different and separate processes—for provider and consumer.
  • Rather than leaving developers to discover individual services and put them into context, the Business Service Bus is instead their starting point that guides them to a coherent set that has been assembled for their domain

Tuesday, June 17, 2014

enerics in a nutshell


meh no time - essetially can get compile time erros and not runtime.

things to study

generics
fix protocol
collections
sets
maps
jdbc

message streaming

gang of four - development patterns

testing

inheritance

@Override helps the compiler check your actually overriding

overriding method cna reurn a subtype

can change the code, can call the super code/

Static.

define an identical signature, and you hide the parent static method


Interfaces - cribnotes for the interview exam

Reference type - you can pass interface around

cool to use in jsf and annotation - jboss will wire your implementation to an interface usign a annotation. Weld baby

Only constants, method sigs, default methods, static ,methods, nested types

interface can be extended by another interface

example from orACLE

Defining an interface is similar to creating a new class:
public interface OperateCar {

   // constant declarations, if any

   // method signatures
   
   // An enum with values RIGHT, LEFT
   int turn(Direction direction,
            double radius,
            double startSpeed,
            double endSpeed);
   int changeLanes(Direction direction,
                   double startSpeed,
                   double endSpeed);
   int signalTurn(Direction direction,
                  boolean signalOn);
   int getRadarFront(double distanceToCar,
                     double speedOfCar);
   int getRadarRear(double distanceToCar,
                    double speedOfCar);
         ......
   // more method signatures
}
Note that the method signatures have no braces and are terminated with a semicolon.


COMMON USAGE
callbacks
events same thing
clone
serialize
Runnable
etc

abstract class in java

declared abstract - mixture of implemented and abstract methods


abstract void dddd(a a,s s,s s,a s);


abstract method means class must be declared abstract

abstact keyword not needed in an interface

abstract is for code sharing

interface is for behaviour describibing to unrelated classes

an example is abstractMap - Collections framework

note abstract calls can implement an interface but you dont have to do the implementation - just the subclass

Java- oo concepts to beat the interview

1) Encapsulation - data hiding, private fields, getters and setters
2) Encapsulation - benifit?  stops other people from breaking code - users of the class. Allows class writes to change the private algos on the data/class and not break other code. (unless we mess the logic/data)


A way to restrict access to an object components

A way to aggregate copmplaex stuff and provide a simple interface to a user of a class/object



Monday, June 16, 2014

Java - GC - garbage collection

Man this is a question always asked at a interview.

everyone reels out the standard - it cant be initiated, the garbage collector gets objects that are not referenced.

Heap - memory allocated to the JVM

-Xmx1024m max size
-Xms512m min size available

the VM tracks live objects. the rest is ok for GC

There are four kinds of GC roots in Java:
  1. Local variables are kept alive by the stack of a thread. This is not a real object virtual reference and thus is not visible. For all intents and purposes, local variables are GC roots.
  2. Active Java threads are always considered live objects and are therefore GC roots. This is especially important for thread local variables.
  3. Static variables are referenced by their classes. This fact makes them de facto GC roots. Classes themselves can be garbage-collected, which would remove all referenced static variables. This is of special importance when we use application servers, OSGi containers or class loaders in general. We will discuss the related problems in the Problem Patterns section.
  4. JNI References are Java objects that the native code has created as part of a JNI call. Objects thus created are treated specially because the JVM does not know if it is being referenced by the native code or not. Such objects represent a very special form of GC root, which we will examine in more detail in the Problem Patterns section below.

jvm runs a mark and  sweep algo

  1. The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
  2. All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.

java algo is impacted by live objects. not dead/free ones - the more live objects the more impact the gc has on the system

GC also does compaction  (packs stuff in heap mem) affects perf

  • Compacting is executed not for every GC cycle, but only once a certain level of fragmentation is reached (e.g., if more than 50% of the free memory is not continuous).
  • One can configure a target fragmentation. Instead of compacting everything, the garbage collector compacts only until a designated percentage of the free memory is available as a continuous block


reduce impact- use concurrent mark and sweep

reduce number of objects if poss


Generation oldies and youngies

new objects are traversed a lot, old objects are moved to the geriatrix divsion and skipped


Copy Garbage collection

two spaces - copy live from one to the other - free the not live area

Oracle hotspot

three areas

Eden, survivor/survivor, tenured (survived multi gc cycles)
things like classes and app code go straight to tenured. not necessary for gc cycle




Sunday, June 15, 2014

The java object - great interview question

Tell me about the Java Object

Well its what everything in java inherits from.

lt's like the super of all objects




  • Method Summary

    Methods 
    Modifier and TypeMethod and Description
    protected Objectclone()
    Creates and returns a copy of this object.
    booleanequals(Object obj)
    Indicates whether some other object is "equal to" this one.
    protected voidfinalize()
    Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
    Class<?>getClass()
    Returns the runtime class of this Object.
    inthashCode()
    Returns a hash code value for the object.
    voidnotify()
    Wakes up a single thread that is waiting on this object's monitor.
    voidnotifyAll()
    Wakes up all threads that are waiting on this object's monitor.
    StringtoString()
    Returns a string representation of the object.
    voidwait()
    Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
    voidwait(long timeout)
    Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
    voidwait(long timeout, int nanos)
    Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

Java initialization -- Static vs code block

Static code block gets called once

code block gets called on each instantiation

simples

non static block actually get copied into the compiler