text:
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/test';
var DB = null;
var UserTableName = "users";

/*** user operation ****/

function addUser(name, pwd) {

}

/*** test operation ****/

var insertData = function(db, callback) {
    //连接到表
    var collection = db.collection(UserTableName);
    //插入数据
    var data = [{"name":"zhen","password" : "zhen"},{"name":"wilson002","password":"zhen"}];
    collection.insert(data, function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }
        callback(result);
    });
}

var selectData = function (db, callback) {
    //连接到表
    var collection = db.collection(UserTableName);
    //查询数据
    var whereStr = {"name": 'zhen'};
    collection.find(whereStr).toArray(function (err, result) {
        if (err) {
            console.log('Error:' + err);
            return;
        }
        callback(result);
    });
};

var updateData = function(db, callback) {
    //连接到表
    var collection = db.collection(UserTableName);
    //更新数据
    var whereStr = {"name":'zhen'};
    var updateStr = {$set: { "password" : "123" }};
    collection.update(whereStr,updateStr, function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }
        callback(result);
    });
}

var delData = function(db, callback) {
    //连接到表
    var collection = db.collection(UserTableName);
    //删除数据
    var whereStr = {"name":'zhen'};
    collection.remove(whereStr, function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }
        callback(result);
    });
}

/*
var invokeProcData = function(db, callback) {
    //存储过程调用
    db.eval('get_tb2_count()', function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }
        callback(result);
    });
}
*/

function connectDB() {

    MongoClient.connect(DB_CONN_STR, function(err, db) {
        DB = db;
        console.log("连接成功!");
        deleteInfo();
    });
}

function deleteInfo() { //全部删除

    delData(DB, function(result){

        console.log(result);
    })
}

function update() { //更新第一条

    updateData(DB, function(result){

        console.log(result);
    })
}

function find() { //查找数据

    selectData(DB, function(result){

        console.log(result);
    })
}

function insert() { //添加数据

    insertData(DB, function(result){

        console.log(result);
    })
}

exports.connectionDB = connectDB;
exports.insert = insert;


text:
import Ember from "ember";
import { test, module } from "qunit";
import {stubRequest, stubEndpointForHttpRequest} from "../helpers/stub";

module("Unit: Stub Tests", {
    afterEach: function() {
        Ember.$.fauxjax.clear();
    }
});

test("stubRequest requires a request.url", function(assert) {
    try {
        stubRequest({});
    } catch(e) {
        assert.equal(e.message, "Please provide a url for the request");
    }
});

test("stubRequest will set request.cache to false by default", function(assert) {
    stubRequest({url: "/wat", method: "GET"});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal(false, stub.request.cache);
});

test("stubRequest will respect request.cache if set", function(assert) {
    stubRequest({url: "/wat", method: "GET", cache: true});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal(true, stub.request.cache);
});

test("stubRequest will set request.method to 'GET' by default", function(assert) {
    stubRequest({url: "/wat"});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("GET", stub.request.method);
});

test("stubRequest will respect request.method if set", function(assert) {
    stubRequest({url: "/wat", method: "POST"});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("POST", stub.request.method);
});

test("stubRequest will respect request.contentType if set", function(assert) {
    stubRequest({url: "/wat", method: "POST", contentType: "text/plain"}, {status: 201});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("text/plain", stub.request.contentType);
});

test("stubRequest will default request.contentType to 'application/x-www-form-urlencoded' if not set and no data", function(assert) {
    stubRequest({url: "/wat", method: "POST"}, {status: 201});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("application/x-www-form-urlencoded", stub.request.contentType);
});

test("stubRequest will default request.contentType to 'application/json' if not set and there is request.data", function(assert) {
    stubRequest({url: "/wat", method: "POST", data: {}}, {status: 201});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("application/json", stub.request.contentType);
});

test("stubRequest will set response.status to 200 by default", function(assert) {
    stubRequest({url: "/wat"});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal(200, stub.response.status);
});

test("stubRequest will respect response.status if set", function(assert) {
    stubRequest({url: "/wat", method: "POST"}, {status: 201});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal(201, stub.response.status);
});

test("stubEndpointForHttpRequest will set contentType to 'application/json' if post_data", function(assert) {
    stubEndpointForHttpRequest("/wat", {}, "POST", 201, {foo: "bar"});
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("application/json", stub.request.contentType);
});

test("stubEndpointForHttpRequest will default contentType to 'application/x-www-form-urlencoded' if no post_data", function(assert) {
    stubEndpointForHttpRequest("/wat", {}, "POST", 201);
    var stub = Ember.$.fauxjax.unfired()[0];
    assert.equal("application/x-www-form-urlencoded", stub.request.contentType);
});

redpajama_set_name: RedPajamaGithub


text:
<!--
  #%L
  %%
  Copyright (C) 2011 - 2017 BMW Car IT GmbH
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<groupId>io.joynr.cpp</groupId>
	<artifactId>libjoynr</artifactId>
	<modelVersion>4.0.0</modelVersion>
	<packaging>pom</packaging>
	<name>${project.groupId}:${project.artifactId}</name>

	<parent>
		<groupId>io.joynr</groupId>
		<artifactId>cpp</artifactId>
		<version>1.21.6</version>
		<relativePath>../pom.xml</relativePath>
	</parent>

	<build>
		<plugins>
			<plugin>
				<groupId>io.joynr.tools.generator</groupId>
				<artifactId>joynr-generator-maven-plugin</artifactId>
				<configuration>
					<model>${basedir}/../../basemodel/src/main/franca</model>
					<generationLanguage>cpp</generationLanguage>
					<outputPath>${basedir}/basemodel/generated</outputPath>
				</configuration>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<id>generate</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>io.joynr.tools.generator</groupId>
						<artifactId>cpp-generator</artifactId>
						<version>${project.version}</version>
					</dependency>
					<dependency>
						<groupId>io.joynr</groupId>
						<artifactId>basemodel</artifactId>
						<version>${project.version}</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<artifactId>maven-clean-plugin</artifactId>
				<configuration>
					<filesets>
						<fileset>
							<directory>${basedir}/basemodel/generated</directory>
							<excludes>
								<exclude>CMakeLists.txt</exclude>
							</excludes>
						</fileset>
					</filesets>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>


text:
<?php namespace Arcanedev\LaravelTracker\Exceptions;

use Illuminate\Support\Arr;

/**
 * Class     ExceptionFactory
 *
 * @package  Arcanedev\LaravelTracker\Exceptions
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class ExceptionFactory
{
    /* -----------------------------------------------------------------
     |  Properties
     | -----------------------------------------------------------------
     */

    /**
     * Supported exceptions.
     *
     * @var array
     */
    protected static $supported = [
        E_ERROR             => Errors\Error::class,
        E_WARNING           => Errors\Warning::class,
        E_PARSE             => Errors\Parse::class,
        E_NOTICE            => Errors\Notice::class,
        E_CORE_ERROR        => Errors\CoreError::class,
        E_CORE_WARNING      => Errors\CoreWarning::class,
        E_COMPILE_ERROR     => Errors\CompileError::class,
        E_COMPILE_WARNING   => Errors\CompileWarning::class,
        E_USER_ERROR        => Errors\UserError::class,
        E_USER_WARNING      => Errors\UserWarning::class,
        E_USER_NOTICE       => Errors\UserNotice::class,
        E_STRICT            => Errors\Strict::class,
        E_RECOVERABLE_ERROR => Errors\RecoverableError::class,
        E_DEPRECATED        => Errors\Deprecated::class,
        E_USER_DEPRECATED   => Errors\UserDeprecated::class,
    ];

    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    /**
     * Make exception.
     *
     * @param  int     $errorCode
     * @param  string  $errorMessage
     *
     * @return mixed
     */
    public static function make($errorCode, $errorMessage)
    {
        $exception = Arr::get(self::$supported, $errorCode, Errors\Error::class);

        return new $exception($errorMessage, $errorCode);
    }
}


text:
const httpMocks = require('node-mocks-http');
const errorRouter = require('./error.api');
const Logger = require('../logger');

describe('error api', () => {
  describe('log', () => {
    it('should log all given params', () => {
      Logger.error = jest.fn();

      const mockRequest = httpMocks.createRequest({
        url: '/log',
        method: 'POST',
        body: {
          message: 'testMessage',
          url: 'testUrl',
          lineNo: 1,
          colNo: 2,
        },
      });

      errorRouter.handle(mockRequest, httpMocks.createResponse());

      expect(Logger.error.mock.calls).toHaveLength(1);
      const logParams = Logger.error.mock.calls[0][0];
      expect(logParams).toContain('testMessage');
      expect(logParams).toContain('testUrl');
      expect(logParams).toContain('Row: 1');
      expect(logParams).toContain('Col: 2');
    });

    it('should log stack if given', () => {
      Logger.error = jest.fn();

      const mockRequest = httpMocks.createRequest({
        url: '/log',
        method: 'POST',
        body: {
          error: {
            stack: 'errorStack',
          },
        },
      });

      errorRouter.handle(mockRequest, httpMocks.createResponse());

      expect(Logger.error.mock.calls).toHaveLength(2);
      expect(Logger.error.mock.calls[1][0]).toContain('errorStack');
    });
  });
});


text:
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Internal;
using System.Reflection.Metadata.Ecma335;
using Xunit;

namespace System.Reflection.Metadata.Tests
{
    public class TagToTokenTests
    {
        private sealed class XxxTag
        {
            public Func<int> GetNumberOfBits;
            public Func<uint[]> GetTagToTokenTypeArray;
            public Func<TableMask> GetTablesReferenced;
            public Func<uint, EntityHandle> ConvertToHandle;
            public Func<EntityHandle, uint?> ConvertToTag;
            public Func<uint> GetTagMask;
            public Func<string, uint?> GetTagValue;
            public string Name;
        }

        private IEnumerable<XxxTag> GetTags()
        {
            Type[] types = new[] {
                typeof(CustomAttributeTypeTag),
                typeof(HasConstantTag),
                typeof(HasCustomAttributeTag),
                typeof(HasDeclSecurityTag),
                typeof(HasFieldMarshalTag),
                typeof(HasSemanticsTag),
                typeof(ImplementationTag),
                typeof(MemberForwardedTag),
                typeof(MemberRefParentTag),
                typeof(MethodDefOrRefTag),
                typeof(ResolutionScopeTag),
                typeof(TypeDefOrRefTag),
                typeof(TypeOrMethodDefTag)
            };

            return from type in types
                   let typeInfo = type.GetTypeInfo()
                   select new XxxTag
                   {
                       GetTagToTokenTypeArray = () =>
                       {
                           var array = typeInfo.GetDeclaredField("TagToTokenTypeArray");
                           var vector = typeInfo.GetDeclaredField("TagToTokenTypeByteVector");

                           Assert.True((array == null) ^ (vector == null), typeInfo.Name + " does not have exactly one of TagToTokenTypeArray or TagToTokenTypeByteVector");

                           if (array != null)
                           {
                               return (uint[])array.GetValue(null);
                           }

                           Assert.Contains(vector.FieldType, new[] { typeof(uint), typeof(ulong) });
                           if (vector.FieldType == typeof(uint))
                           {
                               return BitConverter.GetBytes((uint)vector.GetValue(null)).Select(t => (uint)t << TokenTypeIds.RowIdBitCount).ToArray();
                           }
                           else
                           {
                               return BitConverter.GetBytes((ulong)vector.GetValue(null)).Select(t => (uint)t << TokenTypeIds.RowIdBitCount).ToArray();
                           }
                       },

                       GetNumberOfBits = () => (int)typeInfo.GetDeclaredField("NumberOfBits").GetValue(null),
                       ConvertToHandle = (Func<uint, EntityHandle>)typeInfo.GetDeclaredMethod("ConvertToHandle").CreateDelegate(typeof(Func<uint, EntityHandle>)),
                       ConvertToTag = handle => { var m = typeInfo.GetDeclaredMethod("ConvertToTag"); return m == null ? null : (uint?)m.Invoke(null, new object[] { handle }); },
                       GetTablesReferenced = () => (TableMask)typeInfo.GetDeclaredField("TablesReferenced").GetValue(null),
                       GetTagMask = () => (uint)typeInfo.GetDeclaredField("TagMask").GetValue(null),
                       GetTagValue = name => { var f = typeInfo.GetDeclaredField(name); return f == null ? null : (uint?)f.GetValue(null); },
                       Name = typeInfo.Name
                   };
        }

        [Fact]
        public void ValidateTagToTokenConversion()
        {
            foreach (var tag in GetTags())
            {
                Assert.True((1 << tag.GetNumberOfBits()) <= tag.GetTagToTokenTypeArray().Length,
                    tag.Name + " has mismatch between NumberOfBits and TagToTokenTypeArray.Length");

                Assert.True(tag.GetTagMask() == (1 << tag.GetNumberOfBits()) - 1,
                    tag.Name + " has mismatch between NumberOfBits and TagMask");

                TableMask tablesNotUsed = tag.GetTablesReferenced();

                Assert.True(tablesNotUsed != 0,
                    tag.Name + " does not have anything in TablesReferenced.");

                int badTagCount = 0;
                Random random = new Random(42);

                for (uint i = 0, n = (uint)(1 << tag.GetNumberOfBits()); i < n; i++)
                {
                    uint rowId = (uint)random.Next(((int)TokenTypeIds.RIDMask + 1));
                    uint codedIndex = i | (rowId << tag.GetNumberOfBits());

                    EntityHandle handle;

                    try
                    {
                        handle = tag.ConvertToHandle(codedIndex);
                    }
                    catch (BadImageFormatException)
                    {
                        badTagCount++;
                        continue;
                    }

                    Assert.True(handle.RowId == rowId,
                        tag.Name + " did not return correct row id.");

                    uint badRowId = (uint)random.Next((int)TokenTypeIds.RIDMask + 1, int.MaxValue);
                    Assert.Throws<BadImageFormatException>(() => tag.ConvertToHandle(i | ~tag.GetTagMask()));
                    Assert.Throws<BadImageFormatException>(() => tag.ConvertToHandle(i | ((TokenTypeIds.RIDMask + 1) << tag.GetNumberOfBits())));
                    Assert.Throws<BadImageFormatException>(() => tag.ConvertToHandle(i | (badRowId << tag.GetNumberOfBits())));

                    Assert.True((uint)(handle.Kind) << 24 == tag.GetTagToTokenTypeArray()[i],
                        tag.Name + " did not return handle type matching its TagToTokenTypeArray or TagToTokenTypeByteVector");

                    TableMask handleTableMask = (TableMask)(1UL << (int)handle.Kind);

                    Assert.True(tag.GetTagValue(handleTableMask.ToString()) == i,
                        tag.Name + " does not have a constant for '" + handleTableMask.ToString() + "' with matching value: " + i);

                    Assert.True((tag.GetTablesReferenced() & handleTableMask) == handleTableMask,
                        tag.Name + " does not declare that it references '" + handleTableMask.ToString() + "' table.");

                    TableMask tablesNotUsedPreviously = tablesNotUsed;
                    tablesNotUsed &= ~handleTableMask;

                    Assert.True(handleTableMask == 0 || tablesNotUsedPreviously != tablesNotUsed,
                        tag.Name + " did not use any table for tag value: " + i);

                    uint? roundTripped = tag.ConvertToTag(handle);
                    Assert.True(roundTripped == null || roundTripped == codedIndex,
                        tag.Name + " did not round trip coded index -> handle -> coded index");
                }

                Assert.True(badTagCount == (1 << tag.GetNumberOfBits()) - BitArithmetic.CountBits((ulong)tag.GetTablesReferenced()),
                    tag.Name + " did not find the correct number of bad tags.");

                Assert.True(tablesNotUsed == 0,
                    tag.Name + " did not use all of TablesReferenced when passed all possible tag bits up to NumberOfBits.");
            }
        }
    }
}


text:
/*
{ splash: null,
     id: '123123123',
     icon: '123123123',
     name: 'name' }
*/

/**
 * Represents a Guild that the client only has limited information for - e.g. from invites.
 */
class PartialGuild {
  constructor(client, data) {
    /**
     * The client that instantiated this PartialGuild
     * @type {Client}
     */
    this.client = client;
    Object.defineProperty(this, 'client', { enumerable: false, configurable: false });

    this.setup(data);
  }

  setup(data) {
    /**
     * The ID of this guild
     * @type {string}
     */
    this.id = data.id;

    /**
     * The name of this guild
     * @type {string}
     */
    this.name = data.name;

    /**
     * The hash of this guild's icon, or null if there is none.
     * @type {?string}
     */
    this.icon = data.icon;

    /**
     * The hash of the guild splash image, or null if no splash (VIP only)
     * @type {?string}
     */
    this.splash = data.splash;
  }
}

module.exports = PartialGuild;


text:
var connect = require('gulp-connect'),
    path = require('path'),
    runSequence = require('run-sequence');

module.exports = function(gulp, config) {

  gulp.task('dev:server', function() {
    connect.server({
      root: config.documentation.example.dist,
      fallback: path.join(config.documentation.example.dist, 'index.html'),
      port: 8000,
      livereload: true
    });
  });

  gulp.task('dev', function() {
    runSequence('dev:server',
                'watch');
  });

};


text:
chrome.storage.local.get(null, (store) => {
  let text = (store.count) ? store.count : 0
  chrome.browserAction.setBadgeText({ text: text.toString() });
})

chrome.storage.onChanged.addListener((changes, namespace) => {
  chrome.browserAction.setBadgeText({ text: changes.count.newValue.toString() });
})


text:
<!DOCTYPE html>
<html>
<script src="../../resources/js-test.js"></script>
<script>
// Ensure that events are handled correctly when an image element is adopted.

jsTestIsAsync = true;

var doc = document.documentElement.appendChild(document.createElement("iframe")).contentDocument;
var adopting = false;
var readyStateChangeEventHandlerTriggeredWhenComplete = false;

doc.open();
doc.onreadystatechange = function() {
    if (doc.readyState == "complete") {
        readyStateChangeEventHandlerTriggeredWhenComplete = true;
        testPassed("readystatechange event handler was executed when complete");
        if (adopting)
            testFailed("script triggered during adoption");
        finishJSTest();
    }
}

var img = doc.appendChild(doc.createElement("img"));
var blobUrl = URL.createObjectURL(new Blob);

var x = new XMLHttpRequest;
x.onload = function() {
    img.src = blobUrl;
}
x.onloadend = function() {
    doc.close();
    URL.revokeObjectURL(blobUrl);
    adopting = true;
    document.adoptNode(img);
    adopting = false;
    if (doc.readyState == "complete" && !readyStateChangeEventHandlerTriggeredWhenComplete) {
        testFailed("readystatechange event was dispatched but the handler was suppressed");
    }
}
x.open("get", "data:text/html,");
x.send();
</script>
</html>


text:
module GitStats
  class Generator
    delegate :add_command_observer, to: :@repo
    delegate :render_all, to: :@view

    def initialize(options)
      validate_repo_path(options[:path])

      @repo = GitData::Repo.new(options)
      view_data = StatsView::ViewData.new(@repo)
      @view = StatsView::View.new(view_data, options[:out_path])

      yield self if block_given?
    end

    private

    def validate_repo_path(repo_path)
      raise ArgumentError, "#{repo_path} is not a git repository" unless valid_repo_path?(repo_path)
    end

    def valid_repo_path?(repo_path)
      Dir.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/HEAD")
    end

  end

end


text:
This is My train, demo, tech experience CODE.
--- --- ---
develop, test, grunt, requireJS,,,


text:
package com.feetao.web.wx.vo;

public class MessageImageSendVO extends MessageSendVO {

	private Image image;

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	@Override
	public String toXML() {
		StringBuilder sb	= new StringBuilder("<xml>");
		sb.append("<ToUserName><![CDATA[").append(this.toUserName).append("]]></ToUserName>");
		sb.append("<FromUserName><![CDATA[").append(this.fromUserName).append("]]></FromUserName>");
		sb.append("<CreateTime>").append(this.createTime).append("</CreateTime>");
		sb.append("<MsgType><![CDATA[").append(this.msgType).append("]]></MsgType>");
		if(image != null)  image.toXML(sb);
		sb.append("</xml>");
		return sb.toString();
	}
}


text:
!---------------------------------------------------!
! Copyright (c) 2017 Shunsuke A. Sato               !
! Released under the MIT license                    !
! https://opensource.org/licenses/mit-license.php   !
!---------------------------------------------------!
subroutine thermal_bath_distribution
  use global_variables
  use random_number_module
  implicit none
  real(8) :: alpha,x1,x2
  integer :: j

  do j = 1,Num_HO
    alpha = 2d0*tanh(beta_kB*Omega_HO(j)/2d0)/Omega_HO(j)
    call gaussian_random_number(x1,x2)
    X_HO(j) = x1/sqrt(alpha*M_HO*Omega_HO(j)**2)
    V_HO(j) = x2/sqrt(M_HO*alpha)
  end do

end subroutine thermal_bath_distribution


text:
bespoke.from('article', [
  bespoke.plugins.classes(),
  bespoke.plugins.keys(),
  bespoke.plugins.touch(),
  bespoke.plugins.run()
]);


text:
apetools.builders.subbuilders.basedevicebuilder
===============================================

.. automodule:: apetools.builders.subbuilders.basedevicebuilder

   .. rubric:: Classes

   .. autosummary::

      ABCMeta
      BaseClass
      BaseDeviceBuilder


text:
"use strict";

/**
 * The extension data is used as a initialization data for an
 * ExtendedSolarSystem.
 *
 * @constructor
 * @param {everoute.universe.SolarSystem} baseSystem the base solar system.
 * @memberof everoute.universe
 */
function SolarSystemExtensionData(baseSystem) {
  /**
   * @type {everoute.universe.SolarSystem}
   */
  this.base = baseSystem;

  /**
   * @type {Array.<everoute.universe.JumpBuilder>}
   */
  this.jumpBuilders = [];

  /**
   * @type {Array.<everoute.travel.TravelCost>}
   */
  this.costs = [];
}

module.exports = SolarSystemExtensionData;


text:
package s101;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import org.apache.commons.io.IOUtils;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class S101Configurer {
	private static final Pattern VERSION = Pattern.compile("<local-project .* version=\"(.*?)\"");

	private static final int BUFFER = 1024;
	private static final long TOOBIG = 0x10000000; // ~268M
	private static final int TOOMANY = 200;

	private final MustacheFactory mustache = new DefaultMustacheFactory();
	private final Mustache hspTemplate;
	private final Mustache repositoryTemplate;

	private final Path licenseDirectory;

	private final Project project;
	private final Logger logger;

	public S101Configurer(Project project) {
		this.project = project;
		this.logger = project.getLogger();
		Resource template = new ClassPathResource("s101/project.java.hsp");
		try (InputStream is = template.getInputStream()) {
			this.hspTemplate = this.mustache.compile(new InputStreamReader(is), "project");
		} catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
		template = new ClassPathResource("s101/repository.xml");
		try (InputStream is = template.getInputStream()) {
			this.repositoryTemplate = this.mustache.compile(new InputStreamReader(is), "repository");
		} catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
		this.licenseDirectory = new File(System.getProperty("user.home") + "/.Structure101/java").toPath();
	}

	public void license(String licenseId) {
		Path licenseFile = this.licenseDirectory.resolve(".structure101license.properties");
		if (needsLicense(licenseFile, licenseId)) {
			writeLicense(licenseFile, licenseId);
		}
	}

	private boolean needsLicense(Path licenseFile, String licenseId) {
		if (!licenseFile.toFile().exists()) {
			return true;
		}
		try {
			String license = new String(Files.readAllBytes(licenseFile));
			return !license.contains(licenseId);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}

	private void writeLicense(Path licenseFile, String licenseId) {
		if (!this.licenseDirectory.toFile().mkdirs()) {
			this.licenseDirectory.forEach((path) -> path.toFile().delete());
		}
		try (PrintWriter pw = new PrintWriter(licenseFile.toFile())) {
			pw.println("licensecode=" + licenseId);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}

	public void install(File installationDirectory, File configurationDirectory) {
		deleteDirectory(installationDirectory);
		installBuildTool(installationDirectory, configurationDirectory);
	}

	public void configure(File installationDirectory, File configurationDirectory) {
		deleteDirectory(configurationDirectory);
		String version = computeVersionFromInstallation(installationDirectory);
		configureProject(version, configurationDirectory);
	}

	private String computeVersionFromInstallation(File installationDirectory) {
		File buildJar = new File(installationDirectory, "structure101-java-build.jar");
		try (JarInputStream input = new JarInputStream(new FileInputStream(buildJar))) {
			JarEntry entry;
			while ((entry = input.getNextJarEntry()) != null) {
				if (entry.getName().contains("structure101-build.properties")) {
					Properties properties = new Properties();
					properties.load(input);
					return properties.getProperty("s101-build");
				}
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		throw new IllegalStateException("Unable to determine Structure101 version");
	}

	private boolean deleteDirectory(File directoryToBeDeleted) {
		File[] allContents = directoryToBeDeleted.listFiles();
		if (allContents != null) {
			for (File file : allContents) {
				deleteDirectory(file);
			}
		}
		return directoryToBeDeleted.delete();
	}

	private String installBuildTool(File installationDirectory, File configurationDirectory) {
		String source = "https://structure101.com/binaries/v6";
		try (final WebClient webClient = new WebClient()) {
			HtmlPage page = webClient.getPage(source);
			Matcher matcher = null;
			for (HtmlAnchor anchor : page.getAnchors()) {
				Matcher candidate = Pattern.compile("(structure101-build-java-all-)(.*).zip").matcher(anchor.getHrefAttribute());
				if (candidate.find()) {
					matcher = candidate;
				}
			}
			if (matcher == null) {
				return null;
			}
			copyZipToFilesystem(source, installationDirectory, matcher.group(1) + matcher.group(2));
			return matcher.group(2);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	private void copyZipToFilesystem(String source, File destination, String name) {
		try (ZipInputStream in = new ZipInputStream(new URL(source + "/" + name + ".zip").openStream())) {
			ZipEntry entry;
			String build = destination.getName();
			int entries = 0;
			long size = 0;
			while ((entry = in.getNextEntry()) != null) {
				if (entry.getName().equals(name + "/")) {
					destination.mkdirs();
				} else if (entry.getName().startsWith(name)) {
					if (entries++ > TOOMANY) {
						throw new IllegalArgumentException("Zip file has more entries than expected");
					}
					if (size + BUFFER > TOOBIG) {
						throw new IllegalArgumentException("Zip file is larger than expected");
					}
					String filename = entry.getName().replace(name, build);
					if (filename.contains("maven")) {
						continue;
					}
					if (filename.contains("jxbrowser")) {
						continue;
					}
					if (filename.contains("jetty")) {
						continue;
					}
					if (filename.contains("jfreechart")) {
						continue;
					}
					if (filename.contains("piccolo2d")) {
						continue;
					}
					if (filename.contains("plexus")) {
						continue;
					}
					if (filename.contains("websocket")) {
						continue;
					}
					validateFilename(filename, build);
					this.logger.info("Downloading " + filename);
					try (OutputStream out = new FileOutputStream(new File(destination.getParentFile(), filename))) {
						byte[] data = new byte[BUFFER];
						int read;
						while ((read = in.read(data, 0, BUFFER)) != -1 && TOOBIG - size >= read) {
							out.write(data, 0, read);
							size += read;
						}
					}
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private String validateFilename(String filename, String intendedDir)
			throws java.io.IOException {
		File f = new File(filename);
		String canonicalPath = f.getCanonicalPath();

		File iD = new File(intendedDir);
		String canonicalID = iD.getCanonicalPath();

		if (canonicalPath.startsWith(canonicalID)) {
			return canonicalPath;
		} else {
			throw new IllegalArgumentException("File is outside extraction target directory.");
		}
	}

	private void configureProject(String version, File configurationDirectory) {
		configurationDirectory.mkdirs();
		Map<String, Object> model = hspTemplateValues(version, configurationDirectory);
		copyToProject(this.hspTemplate, model, new File(configurationDirectory, "project.java.hsp"));
		copyToProject("s101/config.xml", new File(configurationDirectory, "config.xml"));
		File repository = new File(configurationDirectory, "repository");
		File snapshots = new File(repository, "snapshots");
		if (!snapshots.exists() && !snapshots.mkdirs()) {
			throw new IllegalStateException("Unable to create snapshots directory");
		}
		copyToProject(this.repositoryTemplate, model, new File(repository, "repository.xml"));
	}

	private void copyToProject(String location, File destination) {
		Resource resource = new ClassPathResource(location);
		try (InputStream is = resource.getInputStream();
			 OutputStream os = new FileOutputStream(destination)) {
			IOUtils.copy(is, os);
		} catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
	}

	private void copyToProject(Mustache view, Map<String, Object> model, File destination) {
		try (OutputStream os = new FileOutputStream(destination)) {
			view.execute(new OutputStreamWriter(os), model).flush();
		} catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
	}

	private Map<String, Object> hspTemplateValues(String version, File configurationDirectory) {
		Map<String, Object> values = new LinkedHashMap<>();
		values.put("version", version);
		values.put("patchVersion", version.split("\\.")[2]);
		values.put("relativeTo", "const(THIS_FILE)/" + configurationDirectory.toPath().relativize(this.project.getProjectDir().toPath()));

		List<Map<String, Object>> entries = new ArrayList<>();
		Set<Project> projects = this.project.getAllprojects();
		for (Project p : projects) {
			SourceSetContainer sourceSets = (SourceSetContainer) p.getExtensions().findByName("sourceSets");
			if (sourceSets == null) {
				continue;
			}
			for (SourceSet source : sourceSets) {
				Set<File> classDirs = source.getOutput().getClassesDirs().getFiles();
				for (File directory : classDirs) {
					Map<String, Object> entry = new HashMap<>();
					entry.put("path", this.project.getProjectDir().toPath().relativize(directory.toPath()));
					entry.put("module", p.getName());
					entries.add(entry);
				}
			}
		}
		values.put("entries", entries);
		return values;
	}
}


text:
package serene.validation.jaxp;

import java.util.Stack;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.DTDHandler;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.LocatorImpl;

import javax.xml.validation.ValidatorHandler;

import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;

import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EntityDeclaration;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.DTD;
import javax.xml.stream.events.NotationDeclaration;

import javax.xml.namespace.QName;

import serene.Constants;

import serene.util.IntStack;

class StAXHandler extends Handler{
    int depth = 0;

    StAXHandler(){
        super();
    }

    void handle(String systemId, ValidatorHandler validatorHandler, XMLEventReader xmlEventReader) throws SAXException{
        this.validatorHandler = validatorHandler;
        if(locator == null){
            locator = new LocatorImpl();
            locator.setSystemId(systemId);
            locator.setPublicId(null);
            locator.setLineNumber(-1);
            locator.setColumnNumber(-1);
        }

        validatorHandler.setDocumentLocator(locator);

        //create attributes
        if(attributes == null)attributes = new AttributesImpl();

        validatorHandler.startDocument();

        XMLEvent currentEvent;
        int eventType;
        depth = 0;
        loop: while (xmlEventReader.hasNext()) {
            try{
                currentEvent = xmlEventReader.nextEvent();
            }catch(XMLStreamException e){
                throw new SAXException(e);
            }
            eventType = currentEvent.getEventType();
            switch (eventType) {
                case XMLStreamConstants.START_ELEMENT:
                    ++depth;
                    handleLocation(currentEvent.getLocation());
                    handleStartElement(currentEvent.asStartElement());
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    handleLocation(currentEvent.getLocation());
                    handleEndElement(currentEvent.asEndElement());
                    if(--depth <= 0)break loop;
                    break;
                case XMLStreamConstants.CHARACTERS:
                case XMLStreamConstants.SPACE:
                case XMLStreamConstants.CDATA:
                    handleLocation(currentEvent.getLocation());
                    handleCharacters(currentEvent.asCharacters().getData());
                    break;
                case XMLStreamConstants.START_DOCUMENT:
                    ++depth;
                    break;
                case XMLStreamConstants.DTD:
                    handleDTDContext((DTD) currentEvent, (DTDHandler)validatorHandler.getProperty(Constants.DTD_HANDLER_PROPERTY));
                    break;
            }
        }
        validatorHandler.endDocument();
    }

    void handle(String systemId, ValidatorHandler validatorHandler, XMLStreamReader xmlStreamReader) throws SAXException{
        this.validatorHandler = validatorHandler;
        if(locator == null){
            locator = new LocatorImpl();
            locator.setSystemId(systemId);
            locator.setPublicId(null);
            locator.setLineNumber(-1);
            locator.setColumnNumber(-1);
        }

        validatorHandler.setDocumentLocator(locator);

        //create attributes
        if(attributes == null)attributes = new AttributesImpl();

        validatorHandler.startDocument();

        int eventType;
        depth = 0;
        try{
            loop: while (xmlStreamReader.hasNext()) {
                //eventType = xmlStreamReader.getEventType();
                eventType = xmlStreamReader.next();
                switch (eventType) {
                    case XMLStreamConstants.START_ELEMENT:
                        ++depth;
                        handleLocation(xmlStreamReader.getLocation());
                        handleStartElement(xmlStreamReader);
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        handleLocation(xmlStreamReader.getLocation());
                        handleEndElement(xmlStreamReader);
                        if(--depth <= 0)break loop;
                        break;
                    case XMLStreamConstants.CHARACTERS:
                    case XMLStreamConstants.SPACE:
                    case XMLStreamConstants.CDATA:
                        handleLocation(xmlStreamReader.getLocation());
                        // review this, it could be more efficient with char[]
                        handleCharacters(xmlStreamReader.getText());
                        break;
                    case XMLStreamConstants.START_DOCUMENT:
                        ++depth;
                        break;
                    case XMLStreamConstants.DTD:
                        handleDTDContext(xmlStreamReader, (DTDHandler)validatorHandler.getProperty(Constants.DTD_HANDLER_PROPERTY));
                        break;
                }
            }
        }catch(XMLStreamException e){
            throw new SAXException(e);
        }
        validatorHandler.endDocument();
    }

    void handleLocation(Location location){
        locator.setSystemId(location.getSystemId());
        locator.setPublicId(location.getPublicId());
        locator.setLineNumber(location.getLineNumber());
        locator.setColumnNumber(location.getColumnNumber());
    }

    void handleStartElement(StartElement startElement) throws SAXException{
        Iterator nsIterator = startElement.getNamespaces();
        while(nsIterator.hasNext()){
            Namespace ns = (Namespace)nsIterator.next();
            validatorHandler.startPrefixMapping(ns.getPrefix(), ns.getNamespaceURI());
        }

        Iterator attrsIterator = startElement.getAttributes();
        attributes.clear();
        while(attrsIterator.hasNext()){
            Attribute attribute = (Attribute)attrsIterator.next();
            QName attrName = attribute.getName();
            String namespaceURI = attrName.getNamespaceURI();
            if(namespaceURI == null) namespaceURI = "";
            String localPart = attrName.getLocalPart();
            String prefix = attrName.getPrefix();
            String qName = "";
            if(prefix != null && !prefix.equals("")){
                qName = prefix+ ":" + localPart;
            }else{
                qName = localPart;
            }
            String value = attribute.getValue();
            String type = attribute.getDTDType();
            attributes.addAttribute(namespaceURI, localPart, qName, type, value);
        }
        QName elemName = startElement.getName();
        String namespaceURI = elemName.getNamespaceURI();
        if(namespaceURI == null) namespaceURI = "";
        String localPart = elemName.getLocalPart();
        String prefix = elemName.getPrefix();
        String qName = "";
        if(prefix != null && !prefix.equals("")){
            qName = prefix+ ":" + localPart;
        }else{
            qName = localPart;
        }
        validatorHandler.startElement(namespaceURI, localPart, qName, attributes);
    }

    void handleStartElement(XMLStreamReader xmlStreamReader) throws SAXException{
        int nsCount = xmlStreamReader.getNamespaceCount();
        for(int i = 0; i < nsCount; i++){
            validatorHandler.startPrefixMapping(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i));
        }

        int attrCount = xmlStreamReader.getAttributeCount();
        attributes.clear();
        for(int i = 0; i < attrCount; i++){
            String namespaceURI = xmlStreamReader.getAttributeNamespace(i);
            if(namespaceURI == null)namespaceURI="";
            String prefix = xmlStreamReader.getAttributePrefix(i);
            String localPart = xmlStreamReader.getAttributeLocalName(i);
            String qName = "";
            if(prefix != null && !prefix.equals("")){
                qName = prefix+ ":" + localPart;
            }else{
                qName = localPart;
            }
            String type = xmlStreamReader.getAttributeType(i);
            String value = xmlStreamReader.getAttributeValue(i);
            attributes.addAttribute(namespaceURI, localPart, qName, type, value);
        }
        String namespaceURI = xmlStreamReader.getNamespaceURI();
        if(namespaceURI == null)namespaceURI="";
        String localPart = xmlStreamReader.getLocalName();
        String prefix = xmlStreamReader.getPrefix();
        String qName = "";
        if(prefix != null && !prefix.equals("")){
            qName = prefix+ ":" + localPart;
        }else{
            qName = localPart;
        }
        validatorHandler.startElement(namespaceURI, localPart, qName, attributes);
    }

    void handleEndElement(EndElement endElement) throws SAXException{
        QName elemName = endElement.getName();
        String namespaceURI = elemName.getNamespaceURI();
        if(namespaceURI == null) namespaceURI = "";
        String localPart = elemName.getLocalPart();
        String prefix = elemName.getPrefix();
        String qName = "";
        if(prefix != null && !prefix.equals("")){
            qName = prefix+ ":" + localPart;
        }else{
            qName = localPart;
        }
        validatorHandler.endElement(namespaceURI, localPart, qName);

        Iterator nsIterator = endElement.getNamespaces();
        while(nsIterator.hasNext()){
            Namespace ns = (Namespace)nsIterator.next();
            validatorHandler.endPrefixMapping(ns.getPrefix());
        }
    }

    void handleEndElement(XMLStreamReader xmlStreamReader) throws SAXException{
        String namespaceURI = xmlStreamReader.getNamespaceURI();
        if(namespaceURI == null)namespaceURI="";
        String localPart = xmlStreamReader.getLocalName();
        String prefix = xmlStreamReader.getPrefix();
        String qName = "";
        if(prefix != null && !prefix.equals("")){
            qName = prefix+ ":" + localPart;
        }else{
            qName = localPart;
        }
        validatorHandler.endElement(namespaceURI, localPart, qName);

        int nsCount = xmlStreamReader.getNamespaceCount();
        for(int i = 0; i < nsCount; i++){
            validatorHandler.endPrefixMapping(xmlStreamReader.getNamespacePrefix(i));
        }
    }

    void handleCharacters(String string) throws SAXException{
        if (string != null) {
            int length = string.length();
            int remainder = length & CHUNK_MASK;
            if (remainder > 0) {
                string.getChars(0, remainder, chars, 0);
                validatorHandler.characters(chars, 0, remainder);
            }
            int i = remainder;
            while (i < length) {
                string.getChars(i, i += CHUNK_SIZE, chars, 0);
                validatorHandler.characters(chars, 0, CHUNK_SIZE);
            }
        }
    }

    void handleDTDContext(DTD dtd, DTDHandler dtdHandler) throws SAXException{
        List entities = dtd.getEntities();
        for(int i = 0; i < entities.size(); i++){
            EntityDeclaration entity = (EntityDeclaration)entities.get(i);
            if(entity.getNotationName() != null){//unparsed entity
                dtdHandler.unparsedEntityDecl(entity.getName(), entity.getPublicId(), entity.getSystemId(), entity.getNotationName());
            }
        }

        List notations = dtd.getNotations();
        for(int i = 0; i < notations.size(); i++){
            NotationDeclaration notation = (NotationDeclaration)notations.get(i);
            dtdHandler.notationDecl(notation.getName(), notation.getPublicId(), notation.getSystemId());
        }
    }

    void handleDTDContext(XMLStreamReader xmlStreamReader, DTDHandler dtdHandler) throws SAXException{
        List entities = (List)xmlStreamReader.getProperty("javax.xml.stream.entities");
        for(int i = 0; i < entities.size(); i++){
            EntityDeclaration entity = (EntityDeclaration)entities.get(i);
            if(entity.getNotationName() != null){//unparsed entity
                dtdHandler.unparsedEntityDecl(entity.getName(), entity.getPublicId(), entity.getSystemId(), entity.getNotationName());
            }
        }

        List notations = (List)xmlStreamReader.getProperty("javax.xml.stream.notations");
        for(int i = 0; i < notations.size(); i++){
            NotationDeclaration notation = (NotationDeclaration)notations.get(i);
            dtdHandler.notationDecl(notation.getName(), notation.getPublicId(), notation.getSystemId());
        }
    }
}


text:
package ru.job4j.start;

import org.junit.Test;

import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

/**
 * StubInputTest class.
 *
 * @author pandrei (istubb@me.com).
 * @version 1.
 * @since 04.09.2017.
 */
public class StubInputTest {

    /**
     * Метод whenUserAddItemThenTrackerHasNewItemWithSameName().
     */
    @Test
    public void whenUserAddItemThenTrackerHasNewItemWithSameName() {
        Tracker tracker = new Tracker();
        Input input = new StubInput(new String[]{"0", "1", "name", "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findAll().get(0).getName(), is("name"));
    }

    /**
     * Метод whenUpdateThenTrackerHasUpdatedValue().
     */
    @Test
    public void whenEditThenTrackerHasUpdatedValue() {
        Tracker tracker = new Tracker();
        Item item = tracker.add(new Item("1", "name", 143L));
        Input input = new StubInput(new String[]{"2", item.getId(), "name1", "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findById(item.getId()).getName(), is("name1"));
    }

    /**
     * Метод whenEditThenTrackerHasUpdatedValue().
     */
    @Test
    public void whenDeleteItemThenTrackerHasUpdatedValue() {
        Tracker tracker = new Tracker();
        Item item = tracker.add(new Item("1", "name", 143L));
        Item item2 = tracker.add(new Item("2", "name2", 144L));
        Input input = new StubInput(new String[]{"3", item.getId(), "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findById(item2.getId()).getName(), is("name2"));
    }

    /**
     * Метод showAllItems().
     */
    @Test
    public void whenAddTwoItemsThenShowAllItems() {
        Tracker tracker = new Tracker();
        Item item = tracker.add(new Item("1", "name", 143L));
        Item item2 = tracker.add(new Item("2", "name2", 144L));
        Input input = new StubInput(new String[]{"1", "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findAll(), is(Arrays.asList(item, item2)));
    }

    /**
     * Метод whenEditThenTrackerHasUpdatedValue().
     */
    @Test
    public void whenAddTwoItemsThenFindById() {
        Tracker tracker = new Tracker();
        tracker.add(new Item("1", "name", 143L));
        Item item2 = tracker.add(new Item("2", "name2", 144L));
        Input input = new StubInput(new String[]{"4", "2", "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findById("2").getName(), is(item2.getName()));
    }

    /**
     * Метод whenAddTenItemsThenFindByName().
     */
    @Test
    public void whenAddTenItemsThenFindByName() {
        Tracker tracker = new Tracker();
        for (int i = 0; i < 10; i++) {
            tracker.add(new Item(String.valueOf(i), "name " + i, 100L + i));
        }
        Item item = tracker.add(new Item("10", "name 9", 110L));
        Input input = new StubInput(new String[]{"5", "name 9", "6"});
        StartUI.setStartUI(new StartUI(input, tracker));
        StartUI.getStartUI().init();
        assertThat(tracker.findByName("name 9").get(0).getName(),
                is(new Item("9", "name 9", 110L).getName()));
    }
}


text:
class CreateVideos < ActiveRecord::Migration
  def change
    create_table :videos do |t|
      t.text :uri
      t.text :title
      t.integer :likeCount
      t.references :category
      t.timestamps
    end
  end
end


text:
/* Base */
.navbar-brand {
    font-family: 'Scope One', serif;
}


text: - [TS中文文档](https://zhongsp.gitbooks.io/typescript-handbook/doc/handbook/)


text:
layout: post102
title:  Overview
categories: XAP102ADM
parent: the-sla-overview.html
weight: 100
---

{% summary   %} {% endsummary %}

The GigaSpaces runtime environment (A.K.A the Service Grid) provides SLA-driven capabilities via the [GSM](/product_overview/service-grid.html#gsm) and the [GSC](/product_overview/service-grid.html#gsc) runtime components. The GSC is responsible for running one or more Processing Units; while the GSM is responsible for analyzing the deployment and provisioning the processing unit instances to the available GSCs.

{% note title=Enforcing SLA definitions %}
The SLA definition are only enforced when deploying the processing unit on to the GigaSpaces service grid, since this environment actively manages and controls the deployment using the GSM(s). When running within your IDE or in standalone mode these definitions are ignored.
{% endnote %}

The SLA definitions can be provided as part of the processing unit package or during the processing unit's deployment process. They define the number of processing unit instances that should be running and deploy-time requirements such as the amount of free memory or CPU or the clustering topology for processing units which contain a space. The GSM reads the SLA definition, and deploys the processing unit onto the available GSCs according to it.

# Defining Your Processing Unit's SLA

The SLA contains the deployment criteria in terms of clustering topology (if it contains a space) and deployment-time requirements.

It can be defined in multiple ways:

- Include an `sla.xml` file which contains the definitions within the processing unit's jar file. This file can be located at the root of the processing unit jar or under the `META-INF/spring` directory, alongside the processing unit's XML file. This is the most recommended way to follow.

-  Embed the SLA definitions within the processing unit's `pu.xml` file.

-  Provide a separate XML files with the SLA definitions to the GSM at deployment via one of the [deployment tools]({%currentjavaurl%}/deploying-onto-the-service-grid.html).

- Use the deployment tools themselves to provide/override the processing unit's SLA (see below). For example, the GUI deployment dialogue enables you to type in various SLA definitions, such as the number of instances, number of backups and space topology.

The SLA definition, whether it comes in a separate file or embedded inside the `pu.xml` file, is composed of a single `<os-sla:sla>` XML element. A sample SLA definition is shown below (note that you can use plain Spring definitions or GigaSpaces specific namespace bindings):

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:os-sla="http://www.openspaces.org/schema/sla"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-{%version spring%}.xsd
       http://www.openspaces.org/schema/sla http://www.openspaces.org/schema/{%currentversion%}/sla/openspaces-sla.xsd">

    <os-sla:sla cluster-schema="partitioned-sync2backup"
            number-of-instances="2" number-of-backups="1"
            max-instances-per-vm="1">
    ...
    </os-sla:sla>
</beans>
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-{%version spring%}.xsd">

	<bean id="SLA" class="org.openspaces.pu.sla.SLA">
		<property name="clusterSchema" value="partitioned-sync2backup" />
		<property name="numberOfInstances" value="2" />
		<property name="numberOfBackups" value="1" />
		<property name="maxInstancesPerVM" value="1" />
	</bean>
	...
</beans>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

The SLA definition above creates 4 instances of a Processing Unit using the `partitioned-sync2backup` [space topology](/product_overview/space-topologies.html). It defines 2 partitions (`number-of-instances="2"`), each with one backup (`number-of-backups="1"`). In addition, it requires that a primary and a backup instance of the same partition will not be provisioned to the same GSC (`max-instances-per-vm="1"`).

{% tip %}
It is up to the deployer to configure the SLA correctly. Trying to deploy a Processing Unit with a cluster schema that requires backups without specifying `numberOfBackups` causes the deployment to fail.
{%endtip%}

{% info %}
In previous releases, the SLA definition also included dynamic runtime policies, e.g. creating additional processing unit instances based on CPU load, relocating a certain instance when the memory becomes saturated, etc. These capabilities are still supported, but are considered deprecated.  Version 7.0 introduces the [Administration and Monitoring API]({%currentjavaurl%}/administration-and-monitoring-api.html) which supports the above and and much more.
{% endinfo %}

# Defining the Space Cluster Topology

When a processing unit contains a space, the SLA specifies the clustering topology for that space. The XML element that defines this is the `cluster-schema`, and is an attribute of the `os-sla:sla` XML element, as shown above.

The reason for defining the space's clustering topology within the SLA definition rather than the space definition with the `pu.xml` file is twofold:

- The clustering topology directly affects the number of instances of the processing unit, and is therefore considered part of its SLA. For example, a partitioned space can have 2 primaries and 2 backups, and a replicated space can have 5 instances. This means that the containing processing unit will have the same number of instances.

- More importantly, separating the clustering topology from the actual space definition enables to truly implement the "write once, scale anywhere" principal. It means you can run the same processing unit within your IDE for unit tests using the default, single space instance clustering topology, and then deploy it to the runtime environment and run the same processing unit with the real clustering topology (e.g. partitioned with 4 partitions).

There are numerous clustering topologies you can choose from:

- **`default`**: Single space instance, no replication or partitioning

- **`sync-replicated`**: Multiple space instances. When written to one of the space instances, objects are **synchronously** replicated to all space instances. The maximum capacity of this topology is the one of the smallest JVM in the cluster.

- **`async-replicated`** Multiple space instances. When written to one of the space instances, objects are **asynchronously** replicated to all space instances. The maximum capacity of this topology is the one of the smallest JVM in the cluster.

- **`partitioned-sync2backup`** Multiple space instances. Objects are distributed across all of the space instances, such that each instance contains a separate subset of the data and forms a separate partition. The partitioning (distribution) of objects is based on their routing property. Optionally, when using this topology, you can designate one or more backup instances to each of the partitions, such that when an object is written to  a certain partition, it is synchronously replicated to the backup copy(ies) of that partition. The maximum capacity of this topology is the overall capacity of all of the JVMs in the cluster, divided by the number of backups+1.

From the client application's perspective (the one that connects to the space from another process), the clustering topology is transparent in most cases.

{% refer %}
Please refer to [this page](/product_overview/space-topologies.html) for more details about space clustering topologies and guidelines regarding when to use each of the topologies.
The number-of-backups parameter should be used with the partitioned-sync2backup cluster schema. It is not supported with the sync-replicated or async-replicated cluster schema.
{% endrefer %}

# SLA Based Distribution and Provisioning

When deployed to the service grid, the processing unit instances are distributed based on the SLA definitions. These definitions form a set of constraints and requirements that should be met when a processing unit instance is provisioned on to a specific container (GSC). The SLA definitions considered during the initial deployment, relocation, and re-provisioning of an instance after failure.

# Default SLA Definitions

If no SLA definition is provided either within the Processing Unit XML configuration or during deploy time, a default SLA is used. The following is the default SLA definition:

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<os-sla:sla number-of-instances="1" />
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="numberOfInstances" value="1" />
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

# Max Instances per VM/Machine

The SLA definition allows you to define the maximum number of instances for a certain processing unit, either per JVM (GSC) or physical machine (regardless of the number of JVMs/GSCs that are running on it).

The max-instances parameter has different semantics when applied to processing units that contain a space with primary-backup semantics (i.e. that uses the  partitioned-sync2backup cluster schema and defines at least one backup) and when applied to a processing unit which contains no embedded space, or which contains a space with no primary-backup semantics.

{% note %}
When applied to a processing unit which contains no embedded space, or which contains a space with no primary-backup semantics, the max-instances parameter defines the **total** number of instances that can be deployed on a single JVM or on a single machine.
{%endnote%}

When applied to a processing unit which contains a space with primary-backup semantics, the max-instances parameter defines the total number of instances which belong to the same primary-backup group (or partition) that can be provisioned to a single JVM or a single machine.

The most common usage of the max-instances feature is when using a processing unit which contains a space with primary-backup semantics. By setting its value to `1`, you ensure that a primary and its backup(s) cannot be provisioned to the same JVM (GSC) / physical machine.

{% vbar %}
The `max-instances-per-vm` means for a PU with an embedded space the max amount of instances per partition. A partition may have primary or backup instance. The `max-instances-per-vm=1` means you won't have primary and a backup of the same partition provisioned into the same GSC. You may have multiple partitions with primary or backup instances provisioned into the same GSC. You can't limit the amount of instances from different partitions a GSC may host.

If you have enough GSCs (as the amount of partitions X 2) you will end up having a single instance per GSC.
If you won't have enough GSCs we will distribute the primary or backup instances of the different partitions across all the existing GSCs, and will do our best to distribute primary instances in even manner across all the GSCs on all the machines.
If you will increase the amount of GSCs after the initial deploy you will have to "re-balance" the system - meaning; distribute all the primaries across all the GSCs.

You may perform this activity via API or automatically by using the ESM. Rebalancing the instances will increase the capacity of the data-grid (more GSCs hosting the data-grid).
The rebalance concept is based on the assumption that you had initially more partitions that GSCs. We call the ratio between partitions to GSCs the scaling factor; meaning how much your data-grid can expand itself without any shutdown and without increasing the amount of partitions.

Example: you start with 4 GSCs and 20 partitions with backups (40 instances) which means each GSC will host initially 10 instances and may end up with 40 GSCs (after one or more re-balance operations) where each will host a single instance. With this example we have increased the capacity of the data grid to be 10 times larger without downtime while the amount of partitions remain the same.
See the [capacity planning](/sbp/capacity-planning.html) for more details.
{% endvbar %}

Here is an example of setting the `max-instances-per-vm` parameter:

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<os-sla:sla max-instances-per-vm="1" />
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="maxInstancesOfVM" value="1" />
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

Here is an example of setting the max instances per machine parameter:

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<os-sla:sla max-instances-per-machine="1" />
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="maxInstancesOfMachine" value="1" />
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

{%comment%}
# Max Instances per Zone

The SLA definition allows to define maximum instances of a processing unit per zone. The semantics of the setting is different when using a processing unit that has an embedded space with primary / backup, and all other cases (non backup processing unit with embedded space, and plain processing units).

When there are no backups, the setting basically controls the maximum instances per zone out of all the processing unit instances. The setting is usually used with the zone requirement setting to constrain where the processing unit instances will be created. Here is an example of a deployment where only 4 instances are allowed in zone1, and 3 instances are allowed in zone2, and the processing unit should only be instantiated on these two zones:

{% highlight xml %}
<os-sla:sla max-instances-per-zone="zone1/4,zone2/3">
    <os-sla:requirements>
        <os-sla:zone name="zone1" />
        <os-sla:zone name="zone2" />
    </os-sla:requirements>
</os-sla:sla>
{% endhighlight %}

If, for example, we want to only have 4 instances running on zone1, 3 instances running on zone2, and the rest on zone3, then we can simply allow processing units to be created on zone3 as well in the requirements:

{% highlight xml %}
<os-sla:sla max-instances-per-zone="zone1/4,zone2/3">
    <os-sla:requirements>
        <os-sla:zone name="zone1" />
        <os-sla:zone name="zone2" />
        <os-sla:zone name="zone3" />
    </os-sla:requirements>
</os-sla:sla>
{% endhighlight %}

When a processing unit does start an embedded space with a backup topology, the semantics of the SLA applies on a per partition level. This means that the setting allows to control if the primary and backup will run on the same zone or not. For example, if we want to deploy a primary with backup topology with 10 partitions each with one backup, and have the primary and backup not run on the same zone we can do the following:

{% highlight xml %}
<os-sla:sla number-of-instances="10" number-of=backups="1" max-instances-per-zone="zone1/1,zone2/1">
    <os-sla:requirements>
        <os-sla:zone name="zone1" />
        <os-sla:zone name="zone2" />
    </os-sla:requirements>
</os-sla:sla>
{% endhighlight %}

In the above case, the primary and the backup will not run on the same zone. If the primary of partition 1 was started on zone1, then the backup of partition 1 will be started on zone2. This comes very handy when defining rack aware deployments.

{% tip %}
You may use the [Primary-Backup Zone Controller](/sbp/primary-backup-zone-controller.html) to deploy primary and backup instances on specific different zones.
{% endtip %}

{%endcomment%}

# Total Max Instances Per VM

This parameter controls the total amount of PU instances that can be instantiated within a GSC. This is very different than the `max-instances-per-vm` that controls how many instances a partition may have within a GSC. It does not control the total amount of instances from different PUs or other partitions that can be provisioned into a GSC. To control the Total Max Instances Per VM you should use the system property `com.gigaspaces.grid.gsc.serviceLimit` and set its value before running the GSC:

{% highlight java %}
set GSC_JAVA_OPTIONS=-Dcom.gigaspaces.grid.gsc.serviceLimit=2
{% endhighlight %}

The default value of the `com.gigaspaces.grid.gsc.serviceLimit` is **500**.

{%comment%}
{% anchor deployment-reqs %}

# Deployment Requirements - Hosts, Zones and Machine Utilization

The provisioning process of processing unit instances makes no guarantee regarding which GSCs each instance is provisioned on. It makes a best effort to evenly distribute the instances across available GSCs, taking into account the number of services on each host.

The SLA enables you to define requirements which control the provisioning process of processing unit instances on the available GSCs. The requirements are based on machine level statistics and grouping of the GSC processes to zones (see below).

The following example shows the variety of supported requirements:

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<os-sla:sla>
    <os-sla:requirements>
        <os-sla:host ip="192.168.0.1" />
        <os-sla:host ip="192.168.0.2" />
        <os-sla:zone name="zone1" />
        <os-sla:zone name="zone2" />
        <os-sla:cpu high=".9" />
        <os-sla:memory high=".8" />
    </os-sla:requirements>
</os-sla:sla>
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="requirements">
        <list>
            <bean class="org.openspaces.pu.sla.requirement.HostRequirement">
                <property name="id" value="192.168.0.1" />
            <bean class="org.openspaces.pu.sla.requirement.HostRequirement">
                <property name="id" value="192.168.0.2" />
            </bean>
            <bean class="org.openspaces.pu.sla.requirement.ZoneRequirement">
                <property name="zone" value="zone1" />
            </bean>
            <bean class="org.openspaces.pu.sla.requirement.ZoneRequirement">
                <property name="zone" value="zone2" />
            </bean>
            <bean class="org.openspaces.pu.sla.requirement.CpuRequirement">
                <property name="high" value=".9" />
            </bean>
            <bean class="org.openspaces.pu.sla.requirement.MemoryRequirement">
                <property name="high" value=".8" />
            </bean>
        </list>
    </property>
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

The above requirements contain the following provisioning constraints which are enforced by the GSM:

- Only provision the processing unit instances to hosts 192.168.0.1 and 192.168.0.2
- Only provision the processing unit instances to GSCs which belong to zone "zone1" or zone "zone2"
- Do not provision processing unit instances to GSCs whose hosting machine utilizes more than 90% of its CPU
- Do not provision processing unit instances to GSCs whose hosting JVM process utilizes more than 80% of its RAM

When using the host or zone requirements, note that more than one requirement can be defined (for example, to define a set of machines this Processing Unit can be deployed to; define the machine's CPU utilization limit; and define the GSC's memory usage limit ).

## Using Zones

![zones.jpg](/attachment_files/zones.jpg)

Defining zones allows you to configure logical tags for each GSC, according to which the processing unit instances will be provisioned to. You can use any alphanumeric combination for a zone name. The GSC can be started with a set of zones that are tagged to it, which match (or not) a given zone requirement in the processing unit's SLA (the zone name matching is case sensitive). The zones of a GSC are specified via the `com.gs.zones` system property. Typically this will be done by setting the GSC_JAVA_OPTIONS environment variable before running the GSC, as shown below (note that you can also edit the `setenv.sh/bat` script manually, but it is less recommended since it touches a core system script):

{% inittab gsc_zones|top %}
{% tabcontent Unix %}

{% highlight java %}
export GSC_JAVA_OPTIONS=-Dcom.gs.zones=zone1,zone3 ${GSC_JAVA_OPTIONS}
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Windows %}

{% highlight java %}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones=zone1,zone3 %GSC_JAVA_OPTIONS%
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Code %}

{% highlight java %}
Admin admin = new AdminFactory().create();
admin.getGridServiceAgents().waitForAtLeastOne();
GridServiceAgents agents = admin.getGridServiceAgents();
for (GridServiceAgent gridServiceAgent : agents) {
	if (gridServiceAgent.getMachine().getHostName().equalsIgnoreCase("machineA"))
	{
		GridServiceAgent agent = admin.getGridServiceAgents().waitForAtLeastOne();
		GridServiceContainerOptions gscOptions = new GridServiceContainerOptions();
		gscOptions.vmInputArgument("-Dcom.gs.zones=zone1,zone3");
		gscOptions.vmInputArgument("-Xmx256m");
		agent.startGridService(gscOptions);
	}
}
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

The above defines zones "zone1" and "zone3" to be tagged to the GSC which will be started. Note that multiple zones can be specified using a comma separated list.

The following table explains when a container with a specified zone can satisfy a Processing Unit with a required zone:

{: .table .table-bordered}
|Processing Unit|Container|Zone match?|
|:--------------|:--------|:----------|
|PU\[\]|GSC\[\]|YES|
|PU\[\]|GSC\[zone1\]|YES|
|PU\[zone1\]|GSC\[zone1\]|YES|
|PU\[zone1,zone2\]|GSC\[zone1\]|YES|
|PU\[zone1\]|GSC\[zone1,zone2\]|YES|
|PU\[zone1\]|GSC\[\]|NO|
|PU\[zone1\]|GSC\[zone2\]|NO|

## Instance Level Requirements

You can also define SLA deployment requirements on per processing unit instance basis. Here is an example:

{% inittab os_simple_space|top %}
{% tabcontent Namespace %}

{% highlight xml %}
<os-sla:sla>
    <os-sla:requirements>
        <os-sla:cpu high=".9" />
        <os-sla:memory high=".8" />
    </os-sla:requirements>
    <os-sla:instance-SLAs>
        <os-sla:instance-SLA instance-id="1">
             <os-sla:requirements>
                <os-sla:host ip="100.0.0.1" />
             </os-sla:requirements>
        </os-sla:instance-SLA>
        <os-sla:instance-SLA instance-id="1" backup-id="1">
             <os-sla:requirements>
                <os-sla:host ip="100.0.0.2" />
             </os-sla:requirements>
        </os-sla:instance-SLA>
    </os-sla:instance-SLAs>
</os-sla:sla>
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="requirements">
        <list>
            <bean class="org.openspaces.pu.sla.requirement.CpuRequirement">
                <property name="high" value=".9" />
            </bean>
            <bean class="org.openspaces.pu.sla.requirement.MemoryRequirement">
                <property name="high" value=".8" />
            </bean>
        </list>
    </property>
    <property name="instanceSLAs">
        <list>
            <bean class="org.openspaces.pu.sla.InstanceSLA">
                <property name="instanceId" value="1" />
                <property name="requirements">
                    <list>
                        <bean class="org.openspaces.pu.sla.requirement.HostRequirement">
                            <property name="id" value="100.0.0.1" />
                        </bean>
                    </list>
                </property>
            </bean>
            <bean class="org.openspaces.pu.sla.InstanceSLA">
                <property name="instanceId" value="1" />
                <property name="backupId" value="1" />
                <property name="requirements">
                    <list>
                        <bean class="org.openspaces.pu.sla.requirement.HostRequirement">
                            <property name="id" value="100.0.0.2" />
                        </bean>
                    </list>
                </property>
            </bean>
        </list>
    </property>
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

The above example verifies that the **first instance** is deployed to a specific machine (specified by its IP address), and its **second instance** for the same partition is deployed to a different machine. All instances share the "general" requirements of CPU and memory. The first instance **might be** the primary and the second might be the backup, but there is no guarantee these will remain primary/backup as these are runtime properties and might change during the life-cycle of the clustered space. The activation property of the space (primary or backup) is determined once the instance is deployed and is not controlled by the GSM but as part of the primary election process.

To control the location of the primary and backup instances during the life-cycle of the clustered space you should use the [Primary-Backup Zone Controller best practice](/sbp/primary-backup-zone-controller.html).

### Example: Zone Based Partitioning Provisioning
To accommodate partitions with different size we can use the zones configuration to provision each partition into a different zone. Each zone will be associated with GSCs having a different heap size (Xmx). The assumption here is a GSCs hosting a single partition instance (primary or a backup instance). With the following example we deploy a cluster with 3 partitions where each partition deployed into a different zone: Small (1GB GSC), Medium (2GB GSC ) and Large (3GB GSC).

To start the small zone:
{%highlight console%}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones=zoneSmall -Xmx1g
gs-agent.bat
{%endhighlight%}

To start the medium zone:
{%highlight console%}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones=zoneMedium -Xmx2g
gs-agent.bat
{%endhighlight%}

To start the large zone:
{%highlight console%}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones=zoneLarge -Xmx3g
gs-agent.bat
{%endhighlight%}

Deploy a 3 partitions with a backup space cluster using the following sla.xml:

{%highlight xml%}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:os-sla="http://www.openspaces.org/schema/sla"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-{%version spring%}.xsd
http://www.openspaces.org/schema/sla http://www.openspaces.org/schema/sla/openspaces-sla.xsd">

<os-sla:sla>
	<os-sla:instance-SLAs>
		<os-sla:instance-SLA instance-id="1">
			<os-sla:requirements>
				<os-sla:zone name="zoneSmall" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
		<os-sla:instance-SLA instance-id="1" backup-id="1">
			<os-sla:requirements>
				<os-sla:zone name="zoneSmall" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
		<os-sla:instance-SLA instance-id="2">
			<os-sla:requirements>
				<os-sla:zone name="zoneMedium" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
		<os-sla:instance-SLA instance-id="2" backup-id="1">
			<os-sla:requirements>
				<os-sla:zone name="zoneMedium" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
		<os-sla:instance-SLA instance-id="3">
			<os-sla:requirements>
				<os-sla:zone name="zoneLarge" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
		<os-sla:instance-SLA instance-id="3" backup-id="1">
			<os-sla:requirements>
				<os-sla:zone name="zoneLarge" />
			</os-sla:requirements>
		</os-sla:instance-SLA>
	</os-sla:instance-SLAs>
</os-sla:sla>
</beans>
{%endhighlight%}

{% note %}
When using instance level SLA, max-instances settings do not apply (or any cluster level setting).
{% endnote %}

{%endcomment%}

{%comment%}
# Deterministic Deployment

When a deploying a data grid, primary and backup instances will be provisioned in an arbitrary manner across the available machines running GSA/GSCs. You don't have a control where these will be physically located as the primary election mechanism determines the primary and backup instances location at the deploy time (first instance per partition elected as the primary).

In some cases you would like to determine the primary and backup instances location in an explicit manner. A simple approach would be to use zones, having one specific zone to host the primary instances and another zone to host the backup instances. These zones do not determine specific physical machines to host the primary/backup instances, but logical group of GSCs associated with a specific zone once started. Usually, the zone might reflect machines located in specific different racks or different data centers that are nearby having very fast and reliable connectivity in between.

## When Deterministic Deployment should be used?
The Primary-Backup Zone Controller approach intended to be used with read mostly scenarios (80% read) where the latency between the sites is extremely low (below one-two milliseconds) with high bandwidth capacity.

Having primary and backup instances on different remote sites that are far away from each other is not a recommended approach with read/write applications. In this case primary and backup instances should be located within the same LAN with high speed connectivity and high capacity bandwidth to allow the primary replicate data as fast as it can to the backup to minimize the replication overhead on the application behavior.

When there is a requirement to leverage remote sites for disaster recovery or remote clients access - most systems will have their data grid primary and backup instances (using synchronous replication mode) within the same LAN (Master data grid) with another grid and its data-grid (not sharing the same lookup service and GSM with the master data grid) running as a slave data grid where the [WAN Replication Gateway](/sbp/wan-replication-gateway.html) used to replicate data between the Master and Slave Data-Grid asynchronously. With multi-master architecture the WAN Replication Gateway may run a conflict resolver to handle concurrent updates of the same object in both sites.

## Controlling Primary/Backup Location
The Primary-Backup Zone Controller used with Deterministic Deployment should be deployed with the data grid PU. It allows you to specify a specific zone for primary instances and a different zone for backup instances. Once the Primary-Backup Zone Controller deployed/started it relocates all the primary instances into GSCs associated with the primary zone and later relocates all the backup instances into GSCs associated with the backup zone. The Primary-Backup Zone Controller periodically checks the status of the deployed data grid and relocates relevant instances as needed.

{%indent%}
![primarybackupzonecontroller.jpg](/attachment_files/primarybackupzonecontroller.jpg)
{%endindent%}

## Primary-Backup Zone Controller Example

This example deploys a partitioned data grid with two partitions and a backup where `Zone A` hosting primary instances:

*sla.xml*

{% highlight xml%}
<os-sla:sla cluster-schema="partitioned-sync2backup"
            number-of-instances="2" number-of-backups="1" max-instances-per-zone="A/1,B/1"
            primary-zone="A">

    <os-sla:requirements >
        <os-sla:zone name="A"/>
        <os-sla:zone name="B"/>
    </os-sla:requirements>

</os-sla:sla>
{%endhighlight%}

*pu.xml*

Add the following snippet to your PU.
{%highlight xml%}

<bean id="primaryZoneController" class="org.openspaces.pu.sla.PrimaryZoneController" >
<property name="primaryZone" value="A" />
</bean>

{%endhighlight%}

#### Setup the zones

Define two zones (zone1,zone2) need to be defined:

*Start Zone A*

{% inittab zoneA|top %}
{% tabcontent Windows %}

{% highlight xml %}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones="A"
gs-agent gsa.gsc 2 gsa.lus 0 gsa.gsm 0
{%endhighlight%}
{%endtabcontent%}

{% tabcontent Linux %}
{% highlight xml %}

export GSC_JAVA_OPTIONS=-Dcom.gs.zones="A"
./gs-agent.sh gsa.gsc 2 gsa.lus 0 gsa.gsm 0
{%endhighlight%}

{%endtabcontent%}
{%endinittab%}

*Start Zone B*

{% inittab zoneB|top %}
{% tabcontent Windows %}

{% highlight xml %}
set GSC_JAVA_OPTIONS=-Dcom.gs.zones="B"
gs-agent gsa.gsc 2 gsa.lus 0 gsa.gsm 0
{%endhighlight%}
{%endtabcontent%}

{% tabcontent Linux %}
{% highlight xml %}
export GSC_JAVA_OPTIONS=-Dcom.gs.zones="B"
./gs-agent.sh gsa.gsc 2 gsa.lus 0 gsa.gsm 0
{%endhighlight%}

{%endtabcontent%}
{%endinittab%}

### Deploy the PU

Deploy your PU from CLI or UI.
[See CLI example]({%currentadmurl%}/deploy-command-line-interface.html#deploy-pu).

When deployed all primary instances will be allocated in zone `A` and backups in zone `B`. If primary fails the order will be restored by using restart.

### Multiple primary zones

Several primary zones can be specified:

{%highlight xml%}
primary-zone="A,C,D"
{%endhighlight%}

Primary instances will be provisioned in the configured order - A,C,D.

### Limitations

* Deterministic deployment supports only clusters with singe backup (X,1).
* Deterministic deployment requires to set _max-instances-per-zone_ to 1.

In the above example: max-instances-per-zone="A/1,B/1.

{%endcomment%}

{% anchor livenessDetection %}

# Monitoring the Liveness of Processing Unit Instances

The [GSM](/product_overview/service-grid.html#gsm) monitors the liveness of all the processing unit instances it provisioned to the GSCs. The GSM pings each instance in the cluster to see whether it's available.

You can also control how often a processing unit instance will be monitored by the GSM, and in case of failure, how many times the GSM will retry to ping the instance and for how long it will wait between retry attempts.

This is done using the <os-sla:member-alive-indicator> element. It contains the following attributes:

{: .table .table-bordered}
| Attribute | Description | Default |
|:----------|:------------|:--------|
| invocation-delay | How often (in milliseconds) an instance will be monitored and verified to be alive by the GSM | 5000 (5 seconds) |
| retry-count | Once an instance has been determined to not be alive, how many times to check it before giving up on it | 3 |
| retry-timeout | Once an instance has been determined to not be alive, what is the timeout interval between retries (in milliseconds) | 500 (0.5 seconds) |

When a processing unit instance is determined as unavailable, the GSM which manages its deployment tries to re-provision the instance on another GSC (according to the SLA definitions)

{% inittab os_simple_space|top %}
{% tabcontent NameSpace %}

{% highlight xml %}
<os-sla:sla>
    <os-sla:member-alive-indicator invocation-delay="5000" retry-count="3" retry-timeout="500" />
</os-sla:sla>
{% endhighlight %}

{% endtabcontent %}
{% tabcontent Plain %}

{% highlight xml %}
<bean id="SLA" class="org.openspaces.pu.sla.SLA">
    <property name="member-alive-indicator">
        <bean class="org.openspaces.pu.sla.MemberAliveIndicator">
            <property name="invocationDelay" value="5000" />
            <property name="retryCount" value="3" />
            <property name="retryTimeout" value="500" />
        </bean>
    </property>
</bean>
{% endhighlight %}

{% endtabcontent %}
{% endinittab %}

#### Troubleshooting the Liveness Detection Mechanism

For troubleshooting purposes, you can lower the logging threshold of the relevant log category by modifying the log configuration file located under `<XAP root>/config/gs_logging.properties` on the GSM. The default definition is as follows:

`org.openspaces.pu.container.servicegrid.PUFaultDetectionHandler.level` = INFO
You can change it to one of the below thresholds for more information:

{: .table .table-bordered}
| Level | Description |
|:------|:------------|
| CONFIG | Logs the configurations applied |
| FINE | Logs once a member is determined as not alive |
| FINER | Logs once a member is indicated as not alive (on each retry) |
| FINEST | every fault detection attempt |

{% note %} For service-failure troubleshooting, Level.FINE should suffice.{%endnote%}

{%comment%}
# Schema

The SLA schema and complete configuration options are described below:

{% indent %}
![sla_schema.jpg](/attachment_files/sla_schema.jpg)
{% endindent %}
{%endcomment%}


text:
<?php

namespace Raptek\Regon\Validator;

use InvalidArgumentException;
use Raptek\Regon\Exception\InvalidArgumentLengthException;

class NipValidator implements ValidatorInterface
{
    public function validate($value)
    {
        if (null === $value || '' === $value || !is_scalar($value)) {
            throw new InvalidArgumentException();
        }

        $stringValue = (string) $value;
        $nip = preg_replace('/[ -]/im', '', $stringValue);
        $length = strlen($nip);

        if ($length !== 10) {
            throw new InvalidArgumentLengthException();
        }

        $mod = 11;
        $sum = 0;
        $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
        $digits = [];
        preg_match_all("/\d/", $nip, $digits);
        $digitsArray = $digits[0];

        foreach ($digitsArray as $digit) {
            $weight = current($weights);
            $sum += $digit * $weight;
            next($weights);
        }

        if ((($sum % $mod == 10) ? 0 : $sum % $mod) != $digitsArray[$length - 1]) {
            throw new InvalidArgumentException('Podany numer NIP jest niepoprawny');
        }

        return true;
    }
}


text:
from __future__ import division
import cv2
from matplotlib import pyplot as plt
import numpy as n
import numpy.random as rnd

class RansacFeature(object):
    '''
    Class for feature detection inside images and videos with
    the :abbr:`RANSAC (Random Sample Consensus)` algorithm.

    Attributes:
        feature(:py:class:`pyransac.features.Feature`): the feature class.
        max_it(int): Max Number of iterations for the RANSAC loop.
        inliers_percent(float): percentage of inliers over total points.\
                         Used for stop criteria in the RANSAC loop.
        threshold(int): Value of threshold used after the image normalization \
                   (can be an integer from 1 to 254).
        dst(float): the distance of the inliers pixels from the feature (i.e.\
             a pixel is considered an inlier if its distance is < dst).
    '''

    def __init__(self,feature,max_it=100,inliers_percent=0.6, threshold = 100, dst = 10):
        self.feature = feature
        self.max_it = max_it
        self.inliers_percent = inliers_percent
        self.threshold = threshold
        self.dst = dst

    def detect_feature(self,pixels):
        ''' This method look for the feature inside a set of points.

        Args:
            points(numpy.ndarray): a (n,n)-shaped numpy array of points.

        Returns:
            (list): list containing:

                feature(:py:attr:`pyransac.ransac.RansacFeature.feature`): The detected feature object.

                percent(float): The percentage of "fitness" (i.e inliers/total_points) of the feature detected \
                    in the image.

        '''

        # -- Starting Loop -- #

        # Pre-allocating guess points
        pts = n.zeros((self.feature.min_points,2))

        # Starting iterations
        it = 0

        # Current percent of inliers over the total points
        percent = 0

        while not(percent>self.inliers_percent or it>self.max_it):

            # Guess three pixels from the non-zero ones
            pts = pixels[rnd.randint(n.size(pixels[:,0]),size=self.feature.min_points)]

            # Generating Circle from the three given points
            try:
                guess_feature = self.feature(pts)
            except RuntimeError: # If the three points are collinear the circle cannot be computed
                continue
            # Compute distance of all non-zero points from the circumference
            distances = guess_feature.points_distance(pixels)

            # Count how many points are inliers (i.e. near the circle)
            inliers = n.size(n.nonzero(distances <= self.dst)[0])

            #Compute the percentage
            percent_new = inliers/n.size(pixels)

            it = it+1

            if percent_new > percent:
                # Update if better approximation
                percent = percent_new
                feature = guess_feature

        #=======================================================================
        # if it >self.max_it:
        #     warnings.warn('''Max Iterations number reached. The current percentage of fitness is {0}'''\
        #                   .format(percent),RuntimeWarning)
        #=======================================================================s
        return (feature,percent)

    def image_search(self,image):
        ''' This method look for the feature inside a grayscale image.

        Args:
            image(numpy.ndarray): the image where to detect the circle.

        Returns:
            (list): list containing:

                feature (:py:class:`pyransac.ransac.RansacFeature.feature`): The detected feature object

                percent (float): the percentage of 'fitness' (i.e.inliers/total_points)
                of the detected feature

        Raises:
            ValueError: If the thresholded image is completely empty (all pixels intensities
                == 0, a ValueError is raised).
        '''

        #=======================================================================
        # TODO: Check if the image is gray
        #=======================================================================

        # Normalization
        image = cv2.normalize(image,image, alpha=0,norm_type=cv2.NORM_MINMAX, beta = 255)

        # Thresholding
        _ret,image = cv2.threshold(image,self.threshold,255,cv2.THRESH_BINARY)

        # Only the non-zero pixels
        pixels = n.where(image>0)

        #Thresholded image can be empty
        if not(pixels):
            raise ValueError('Thresholded image is completely empty.\
                            The threshold argument is too high or the image\
                            is totally black')

        # Orienting correctly the points in a (n,2) shape
        # needed because of arguments of Circle.points_distance()
        pixels = n.transpose(n.vstack([pixels[0],pixels[1]]))

        return self.detect_feature(pixels)

    def video_processing(self,videofile,save_frames=False):
        ''' This method look for the feature inside each frame of
        a video.

        Args:
            videofile (str): path string of the video file.

        Returns:
            fs (numpy.ndarray): the array of features detected

        Raises:
            ValueError: If the thresholded image is completely empty (all pixels intensities
                        == 0, a ValueError is raised).
        '''
        video = cv2.VideoCapture(videofile)
        nframes = int(video.get(cv2.CAP_PROP_FRAME_COUNT))

        #Pre-allocating dataset for feature array
        fs = n.empty(nframes,dtype=self.feature)

        if save_frames:
            theta = n.linspace(-n.pi,n.pi,100)

        for i in xrange(nframes):
            succ, frame = video.read()
            frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

            if succ: # If successfully got the video frame

                try:
                    feature,_percent = self.image_search(frame)
                    fs[i] = feature

                    if save_frames:
                        plt.imshow(frame, cmap='gray')
                        plt.plot(feature.yc + feature.radius*n.cos(theta), feature.xc + feature.radius*n.sin(theta),'r-',linewidth=2)
                        plt.axis('off')
                        plt.savefig('Frame_'+str(i))
                        plt.close()

                except ValueError:
                    fs[i] = None

            else:
                raise RuntimeError("Error in retrieving video frames.")

        return fs