Wednesday, 30 August 2017

What is IoC ?


If Object A depends on object B, IoC is to remove its dependency.

Example 1. Need to hardcode 'Circle' or 'Triange' for object creation.
Circle c = new Circle()
c.draw();
Triange t = new Triange();
t.draw();

 
Example 2. POLYMORPHISM, but still hardocded to use Circle or Triange
Aplication class knows which one to use.

Shape c = new Circle()
c.draw();
Shape t = new Triange();
t.draw();


Example 3. Create a method to pass a generic type (parent class/interface)
TRUE POLYMORPHISM, DYNAMIC, LOOSELY COUPLEDBut still somewhere in the class the object should to be created and passed to method1.

method1(Shape shape) {
    shape.draw();
}

 
Example 4. Create a separate class in which object is not created.
CLASS INDEPENDENT OF THE USAGE

class Drawing {
   private Shape shape;
   public setShape(Shape shape){
       this.shape = shape;
   }

   public drawShape(){
       this.shape.draw();
   }

}
 

// Dependency injected in some other class
Triange triange = new Triange();
drawing.setShape(triange);
drawing.drawShape();

How to create a Spring application and use BeanFactory ?


PreRequisites

  • Download Spring Jars and Apache Logging Jars
  • In eclipse, New > Java project
  • Properties > Java build path > Add Lib / Jars
    • Choose {downloaded package}/dist/*.jar
    • Choose Commons-logging-*.jar
Code
public class DrawingApp {
  p.s.v. main(String[] args) {
    // Triangle t = new Triangle();
    BeanFactory factory = new XmlBeanFactory(new   

    FileSystemResource("spring.xml"));
    Triangle t = (Triangle) factory.getBean("triange");
    t.draw();
  }
}
public class Triangle {
  public void draw() {
    s.o.p("Draw the Triangle");
  }
}

spring.xml - Use code from a sample ApplicationContext.xml
<beans>
   <bean id="triangle" class="com.shaan.exmaple.Triangle" />
</beans>



Saturday, 26 August 2017

How to configure load balancing with HAProxy ?


1. Install HAProxy on your Linux system
# yum install haproxy

OR

# wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.gz -O ~/haproxy.tar.gz
# tar xzvf ~/haproxy.tar.gz -C ~/


2. Edit the configuration file : /etc/haproxy/haproxy.cfg

SINGLE SERVER
# Simple configuration for an HTTP proxy listening on port 80
# on all interfaces and forwarding requests to a single backend "server"
# with a single server "web1" listening on 127.0.0.1:8000

global
        daemon
        maxconn 256
 

defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
 

frontend http-in
        bind *:80
        default_backend
apache

backend apache
        server web1 10.0.0.10:8080




LOAD BALACING
If Load balancing needed, provide LB algorithm and add more servers.
backend apache
     mode http
     balance roundrobin
     server web1 10.0.0.10:8080
     server web2 10.0.0.20:8080
     server web3 10.0.0.30:8080



3. After the configuration, restart HAProxy service
# service haproxy restart


HEALTH CHECK
Each backend host can have a URI defined which will be used to determine whether the host is alive.
 
backend web
    option httpchk HEAD /check.php HTTP/1.1\r\nHost:\ example.com


USING TCP CHECKS (Example for Redis servers)
backend redis
   option tcp-check
   tcp-check send PING\r\n
  
tcp-check expect string +PONG
  
tcp-check send info\ replication\r\n
  
tcp-check expect string role:master
  
tcp-check send QUIT\r\n
  
tcp-check expect string +OK

   server redis1 10.0.0.11:6379 check inter 1s
   server
redis2 10.0.0.12:6379 check inter 1s

This example first sends a ping and expect a PONG reply. Then test if the host is master.

Load balancing terminology used with HAProxy


  • HAProxy
    • High Availability Proxy
    • Open source TCP/HTTP LB and Proxy solution for Linux/Solaris/FreeBSD
    • used to improve the performance and reliability of system
  • BackendA set of servers that receives forwarded requests. It contains :
    • LB algorithm to use
    • A list of servers and ports
      Adding more servers to your backend will increase your potential load capacity by spreading the load over multiple servers

          backend blog-backend 
     balance roundrobin
     mode http
          server blog1 blog1.abc.com:80 check
          server blog1 blog1.abc.com:80 check

          balance specifies LB algorithm
         
mode specifies use of layer 7 protocol
          check specifies that health checks should be performed on those backend servers

  • Frontend
    Defines how requests should be forwarded to backends. It consists :
    • A set of IP addresses and a port (e.g. 10.1.1.7:80, *:443, etc.)
    • ACLs
    • use_backend rules
      which defines which backends to use depending on which ACL conditions are matched, and/or a default_backend rule that handles every other case

  • Types of LB
    • No LB : Single Web server. Slow experience when accessed by simultaneous users.
      User > WS (Web server) > DB
    • Layer4 LB : forward user traffic based on IP range and port. LB selects the web server based on algorithm. All web server should serve the identical contents and all web servers connect to same DB.
      User > LB (Backend) > WS1, WS2 > DB
    • Layer7 LB : forward requests to different backend servers based on the content of the user's request. Single LB can have multiple backend for multiple applications.
      User > LB (Frontend) > Backend 1 > WS1, WS2 | Backend 2 > WS3, WS4
  • Implementation of Layer7 LB
    • Example : When user request abc.com/blog, forward it to blog backend. For other requests, forward to web backend.
      In addition to backend config, there will be frontend config to map the backends.
      frontend http
        bind *:80
        mode http
        acl url_blog path_beg /blog
        use_backend blog-backend if url_blog

        default_backend web-backend
  • LB Algorithms
    • determines which server will be selected when load balancing
    • Servers can be assigned a weight parameter to manipulate how frequently the server is selected compared to other servers.
    • Most common algorithms are : 
      • roundrobin : Default Algo. Selects servers in turns.
      • leastconn : Selects the server with the least number of connections. Recommended for longer sessions.
      • source : selects which server to use based on a hash of the source IP. It ensure that a user will connect to the same server.
  • Sticky Sessions 
    • Some applications require that a user continues to connect to the same backend server. This persistence is achieved through sticky sessions.
  • Health Check 
    • It determines if a backend server is available to process requests by checking if the backend server is listening on the configured IP address and port.
      This avoids having to manually remove a server from the backend if it becomes unavailable.
    • If a server fails a health check, it is automatically disabled in the backend so traffic will not be forwarded to it until it becomes healthy again.
    • If all servers in a backend fail, the service will become unavailable until at least one of those backend servers becomes healthy again.

What are the features provided by HAProxy ?


Basic features provided by HAProxy
Proxying

  • Protect server against any client-side attack
  • Listen to multiple IP address/ports

SSL

  • Certificate-based client authentication
  • Multi-hosting with no limit on sites count
  • Support for wildcard certificates

Monitoring

  • focuses a lot on availability, Servers state is continuously monitored
  • Various check methods are available : TCP connect, HTTP request, SMTP hello, SSL hello, LDAP, SQL, Redis, send/expect scripts, all with/without SSL

High availability

  • Ensure the best global service continuity
  • Only valid servers are used
  • Backup servers are automatically used when active servers are down. Replace them so that sessions are not lost when possible.
  • Support for a graceful shutdown without affecting any connection
  • Return a global failed status for a farm when too many servers are down.

Load balancing

  • offers a complete set of load balancing features
  • 9 LB algorithms are supported including :
    • round-robin (for short connections, pick each server in turn)
    • leastconn (for long connections, pick the least recently used of the servers with the lowest connection count)
    • source (for SSL farms or terminal server farms, the server directly depends on the client's source address)
    • uri (for HTTP caches, the server directly depends on the HTTP URI)
    • hdr (the server directly depends on the contents of a specific HTTP header field)
    • first (for short-lived virtual machines, all connections are packed on the smallest possible subset of servers so that unused ones can be powered down)
  • All algorithms support per-server weights
  • Dynamic weights are supported for round-robin, leastconn and consistent hashing where server weights is modified on the fly from the CLI or even by an agent running on the server
  • Slow-start is supported in case of dynamic weight which allows a server to progressively take the traffic
Stickiness
Application load balancing would be useless without stickiness.
  • It ensures to maintain a visitor on the same server even across various events such as server addition/removal, down/up cycles
  • Stickiness info can be individually matched and learned from different places if desired.
    • For example a JSESSIONID may be matched both in a cookie and in the URL.
  • Stickiness information can come from anything that can be seen within a request or response, including source address, TCP payload offset and length, HTTTP query string elements, header field values, cookies, and so on...
  • Stick-tables are replicated between all nodes in a multi-master fashion
  • Possible to decide not to stick to certain servers, such as backup servers, so that when the nominal server comes back, it automatically takes the load back.
  • The server may decide to change or clean the stickiness cookie on logout, so that leaving visitors are automatically unbound from the server
  • Multiple server entries may share the same stickiness keys so that stickiness is not lost in multi-path environments when one path goes down
Maps
  • Maps are a powerful type of converter consisting in loading a two-columns file into memory at boot time, then looking up each input sample from the first column and either returning the corresponding pattern on the second column if the entry was found, or returning a default value.
ACLs and conditions
  • Most operations in HAProxy can be made conditional.
  • Conditions are built by combining multiple ACLs using logic operators (AND/OR/NOT).
Content switching
  • Content-based switching is the principle is that a connection or request arrives on a frontend, then the information carried with this request or connection are processed, and at this point it is possible to write ACLs-based conditions making use of these information to decide what backend will process the request.
Formated strings
  • Possible to manipulate character strings, such as logs, redirects, header additions, and so on.
  • This provides a powerful way to build header contents or to customize log lines.
HTTP rewriting and redirection
  • Requests / response headers can be adjusted to make the LB appear as the origin server and to fix hardcoded information.
  • This comes with changing the path in requests (which is strongly advised against), modifying Host header field, modifying the Location response header field for redirects, modifying the path and domain attribute for cookies, and so on.
  • Sometimes LB have to intercept some requests and respond with a redirect to a new target URL.
    (Rewriting makes the client and the server see different things while Redirects ask the client to visit the new URL so that it sees the same location as the server.)
Server protection
  • Protect servers against overloading and attacks.
  • HAProxy contains buffers to store requests and responses, and that by only sending a request to a server when it's complete and by reading the whole response very quickly from the local network, the server side connection is used for a very short time and this preserves server resources as much as possible.
  • When a protocol violation or attack is detected, there are various options to respond to the user, such as returning the common "HTTP 400 bad request", closing the connection with a TCP reset, faking an error after a long delay ("tarpit") to confuse the attacker.
 
Logging
  • Very detailed logs, with millisecond accuracy and the exact connection accept time that can be searched in firewalls logs.
  • Detailed logs containing everything needed for troubleshooting, such as source IP address and port, frontend, backend, server, timers (request receipt duration, queue duration, connection setup time, response headers time, data transfer time), global process state, connection counts, queue status, retries count, detailed stickiness actions and disconnect reasons
    Header captures with a safe output encoding.
 
Statistics
  • provides a web-based statistics reporting interface with authentication, security levels and scopes.
  • Possible to provide each hosted customer with his own page showing only his own instances.
 

Friday, 25 August 2017

At what levels Load balancers works ?


Load Balancers may act at different levels :

1. Link level - Link LB
  • Chooses what network link to send a packet to.
  • Acts at the packet level.
    Installed on the normal path of the traffic and divert it according to the configuration.
  • 1-to-1 relation between input and output packets
  • usually implemented in hardware (ASICs) allowing to reach line rate
  • Return traffic doesn't necessarily pass through the LB.
    The LB may also replace the packets' source address with its own in order to force the return traffic to pass through it
2. Network level - Network LB
  • Chooses what route a series of packets will follow.
  • Acts on session contents.
    Operations are always stateful. Return traffic must pass through the LB. 
  • Generally performed by proxies and they're often called Layer 7 LB or L7Clients and servers are not required to use the same protocol (for example IPv4 vs IPv6, clear vs SSL). No relation between input and output packets sizes nor counts.
  • Generally achieved by pure software, even if embedded into hardware appliances. 
  • Very well suited for server load balancing

3. Server level - Server LB
  • decides what server will process a connection or request.
  • Proxy-based LB are deployed as a server with their own IP address and ports. 
  • Some LB may have to adjust some servers' responses to make this possible (eg: the HTTP Location header field used in HTTP redirects).

Thursday, 24 August 2017

What is Load balancing ?


Load balancing


Aggregated multiple components in order to achieve a total processing capacity above individual capacity, in a scalable way.
It allows more operations being performed simultaneously by the time it takes a component to perform only one.
 

Using LB a single operation :
  • will still be performed on a single component at a time
  • will not get faster than without load balancing.

It always requires at least as many operations as available components to make use of all components and to fully benefit from the load balancing.
 

Real world example 
Number of lanes on a highway which allows as many cars to pass during the same time frame without increasing their individual speed.
Examples of load balancing

  • Process scheduling in multi-processor systems
  • Link LB (eg: EtherChannel, Bonding)
  • IP address LB (eg: ECMP, DNS roundrobin)
  • Server LB (via Load balancers)

Load balancer
The mechanism or component which performs the load balancing operation.

What is HAProxy and what is not ?


HAProxy
Free, very fast and reliable solution for :
  • HA (High Availability)
  • Load balancing - suited for very high traffic web sites
  • Proxying



It can load balance any TCP service, particularly suited for HTTP as it supports session persistence and layer 7 processing.

Usage
  • De-facto standard
  • Open source
  • Shipped with most mainstream Linux distributions
  • Often deployed by default in cloud platforms


HAProxy is :
  • TCP Proxy 
    • Accept a TCP connection from a listening socket, connect to a server.
    • Allows traffic in both directions
  • HTTP RP
    • Also called Gateway
    • As a server, receives HTTP requests and passes the requests to servers
  • Server load balancer
    It can load balance TCP connections and HTTP requests
    • TCP mode : Load balancing decisions are taken for the whole connection.
    • HTTP mode : Decisions are taken per request.

  •  Content-based switch 
    • Any element from the request can decide what server to pass the request or connection to. 
    • Makes possible to handle multiple protocols over a same port (eg: http, https, ssh)
  • SSL terminator / initiator / offloader
    • SSL/TLS may be used on the connection coming from the client / to the server / both side
  • TCP normalizer 
    • Protects abnormal traffic - invalid packets, flag combinations, window advertisements, sequence numbers, incomplete connections (SYN floods)
  • HTTP normalizer
    • When configured to process HTTP traffic, only valid complete requests are passed.
    • protects against a lot of protocol-based attacks
  • HTTP fixing tool
    • can modify / fix / add / remove / rewrite the URL or any request or response header
    • helps fixing interoperability issues in complex environments
  • Traffic regulator
    • can apply some thresholds, which protects the servers against overloading
    • adjust traffic priorities based on the contents
  • A protection against DDoS and service abuse
    • Detect when an abuse is happening, then take action (slow down the offenders, block them, send them to outdated contents, etc).
  • HTTP compression offloader
    • It can compress responses which were not compressed by the server, thus reducing the page load time for clients with poor connectivity or using high-latency, mobile networks.

HAProxy is not :
  • Explicit HTTP proxy
    proxy that browsers use to reach the internet. Like, Squid
  • Caching proxy
    returns as-is the contents its received from the server. Like, Varnish
  • Data scrubber
    It will not modify the body of requests nor responses.
  • Web server
    During startup, it isolates itself inside a chroot jail and drops its privileges, so that it will not perform any single file-system access once started.
    So, it cannot be turned into a web server. Like, Apache or Nginx
  • Packet-based LB
    It will not see IP packets nor UDP datagrams and will not perform NAT or even less DSR. Like, IPVS (Linux Virtual Server) 
     

Thursday, 3 August 2017

What is BDD and How to implement using Cucumber ?


Cucumber
  • A testing framework which supports BDD.
  • Define your application behavior in plain english language.
    • Grammer is defined by Gherkin langauge


Challenges with TDD 
Normally, developers develops features but write test code later. Due to need of release, he does quick but inefficient testing.  
Test-cases focus on the functionality of its integration.


BDD
  • Takes care of workflow in place of functionality.
  • Application is designed by describing its behavior for outside observer.
  • Behavior is written in readable and understandable language.
  • Behavior is defined in Given-When-Then steps.

Gherkin
Business-readable domain specific language
Understandable for BA, testers and customer perspective
Allows creating precise acceptance criteria

Example
Scenario: Eat 5 out of 12 
Given there are 12 cucumbers 
When I eat 5 cucumbers
Then I should have 7 cucumbers 




Feature file
consists following components :  
1. Feature: Description of the current test script which has to be executed.
2. Scenario: Description of the steps and expected outcome for a particular test case.
3. Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The data is provided by a tabular structure separated by |
4. Given: Context of the text to be executed. By using data tables "Given", step can also be parameterized.
5. When: Test action that has to performed
6. Then: Expected outcome of the test can be represented by "Then"




STEP DEFINITION
It maps the test case steps in the feature files (introduced by Given/When/Then) to code, which executes and checks the outcomes from the system under test.