Request - Simplified HTTP client
Super simple to use
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
var request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
Table of contents
- Streaming
- Promises & Async/Await
- Forms
- HTTP Authentication
- Custom HTTP Headers
- OAuth Signing
- Proxies
- Unix Domain Sockets
- TLS/SSL Protocol
- Support for HAR 1.2
- All Available Options
Request also offers convenience methods like
request.defaults
and request.post
, and there are
lots of usage examples and several
debugging techniques.
Streaming
You can stream any response to a file stream.
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types (in this case application/json
) and use the proper content-type
in the PUT request (if the headers don’t already provide one).
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
Request can also pipe
to itself. When doing so, content-type
and content-length
are preserved in the PUT headers.
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
Request emits a "response" event when a response is received. The response
argument will be an instance of http.IncomingMessage.
request
.get('http://google.com/img.png')
.on('response', function(response) {
console.log(response.statusCode) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.pipe(request.put('http://mysite.com/img.png'))
To easily handle errors when streaming requests, listen to the error
event before piping:
request
.get('http://mysite.com/doodle.png')
.on('error', function(err) {
console.log(err)
})
.pipe(fs.createWriteStream('doodle.png'))
Now let’s get fancy.
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
if (req.method === 'PUT') {
req.pipe(request.put('http://mysite.com/doodle.png'))
} else if (req.method === 'GET' || req.method === 'HEAD') {
request.get('http://mysite.com/doodle.png').pipe(resp)
}
}
})
You can also pipe()
from http.ServerRequest
instances, as well as to http.ServerResponse
instances. The HTTP method, headers, and entity-body data will be sent. Which means that, if you don't really care about security, you can do:
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png')
req.pipe(x)
x.pipe(resp)
}
})
And since pipe()
returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
Also, none of this new functionality conflicts with requests previous features, it just expands them.
var r = request.defaults({'proxy':'http://localproxy.com'})
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
})
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.
Promises & Async/Await
request
supports both streaming and callback interfaces natively. If you'd like request
to return a Promise instead, you can use an alternative interface wrapper for request
. These wrappers can be useful if you prefer to work with Promises, or if you'd like to use async
/await
in ES2017.
Several alternative interfaces are provided by the request team, including:
- request-promise
(uses Bluebird Promises)
- request-promise-native
(uses native Promises)
- request-promise-any
(uses any-promise Promises)
Forms
request
supports application/x-www-form-urlencoded
and multipart/form-data
form uploads. For multipart/related
refer to the multipart
API.
application/x-www-form-urlencoded (URL-Encoded Forms)
URL-encoded forms are simple.
request.post('http://service.com/upload', {form:{key:'value'}})
// or
request.post('http://service.com/upload').form({key:'value'})
// or
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })
multipart/form-data (Multipart Form Uploads)
For multipart/form-data
we use the form-data library by @felixge. For the most cases, you can pass your upload form data via the formData
option.
var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: Buffer.from([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/form-data/form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpeg'
}
}
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
For advanced cases, you can access the form-data object itself via r.form()
. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling form()
will clear the currently set form data for that request.)
// NOTE: Advanced use-case, for normal use see 'formData' usage above
var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
var form = r.form();
form.append('my_field', 'my_value');
form.append('my_buffer', Buffer.from([1, 2, 3]));
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
See the form-data README for more information & examples.
multipart/related
Some variations in different HTTP implementations require a newline/CRLF before, after, or both before and after the boundary of a multipart/related
request (using the multipart option). This has been observed in the .NET WebAPI version 4.0. You can turn on a boundary preambleCRLF or postamble by passing them as true
to your request options.
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' },
{ body: fs.createReadStream('image.png') }
],
// alternatively pass an object containing additional options
multipart: {
chunked: false,
data: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' }
]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
})
HTTP Authentication
request.get('http://some.server.com/').auth('username', 'password', false);
// or
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
// or
request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
// or
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
If passed as an option, auth
should be a hash containing values:
user
||username
pass
||password
sendImmediately
(optional)bearer
(optional)
The method form takes parameters
auth(username, password, sendImmediately, bearer)
.
sendImmediately
defaults to true
, which causes a basic or bearer
authentication header to be sent. If sendImmediately
is false
, then
request
will retry with a proper authentication header after receiving a
401
response from the server (which must contain a WWW-Authenticate
header
indicating the required authentication method).
Note that you can also specify basic authentication using the URL itself, as
detailed in RFC 1738. Simply pass the
user:password
before the host with an @
sign:
var username = 'username',
password = 'password',
url = 'http://' + username + ':' + password +<EMAIL_ADDRESS>request({url: url}, function (error, response, body) {
// Do more stuff with 'body' here
});
Digest authentication is supported, but it only works with sendImmediately
set to false
; otherwise request
will send basic authentication on the
initial request, which will probably cause the request to fail.
Bearer authentication is supported, and is activated when the bearer
value is
available. The value may be either a String
or a Function
returning a
String
. Using a function to supply the bearer token is particularly useful if
used in conjunction with defaults
to allow a single function to supply the
last known token at the time of sending a request, or to compute one on the fly.
Custom HTTP Headers
HTTP Headers, such as User-Agent
, can be set in the options
object.
In the example below, we call the github API to find out the number
of stars and forks for the request repository. This requires a
custom User-Agent
header as well as https.
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
OAuth Signing
OAuth version 1.0 is supported. The default signing algorithm is HMAC-SHA1:
// OAuth1.0 - 3-legged server side flow (Twitter example)
// step 1
var qs = require('querystring')
, oauth =
{ callback: 'http://mysite.com/callback/'
, consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
}
, url = 'https://api.twitter.com/oauth/request_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// Ideally, you would take the body in the response
// and construct a URL that a user clicks on (like a sign in button).
// The verifier is only available in the response after a user has
// verified with twitter that they are authorizing your app.
// step 2
var req_data = qs.parse(body)
var uri = 'https://api.twitter.com/oauth/authenticate'
+ '?' + qs.stringify({oauth_token: req_data.oauth_token})
// redirect the user to the authorize uri
// step 3
// after the user is redirected back to your server
var auth_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: auth_data.oauth_token
, token_secret: req_data.oauth_token_secret
, verifier: auth_data.oauth_verifier
}
, url = 'https://api.twitter.com/oauth/access_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// ready to make signed requests on behalf of the user
var perm_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: perm_data.oauth_token
, token_secret: perm_data.oauth_token_secret
}
, url = 'https://api.twitter.com/1.1/users/show.json'
, qs =
{ screen_name: perm_data.screen_name
, user_id: perm_data.user_id
}
;
request.get({url:url, oauth:oauth, qs:qs, json:true}, function (e, r, user) {
console.log(user)
})
})
})
For RSA-SHA1 signing, make
the following changes to the OAuth options object:
Pass signature_method : 'RSA-SHA1'
Instead of consumer_secret
, specify a private_key
string in
PEM format
For PLAINTEXT signing, make
the following changes to the OAuth options object:
* Pass signature_method : 'PLAINTEXT'
To send OAuth parameters via query params or in a post body as described in The
Consumer Request Parameters
section of the oauth1 spec:
Pass transport_method : 'query'
or transport_method : 'body'
in the OAuth
options object.
transport_method
defaults to 'header'
To use Request Body Hash you can either
Manually generate the body hash and pass it as a string body_hash: '...'
Automatically generate the body hash by passing body_hash: true
Proxies
If you specify a proxy
option, then the request (and any subsequent
redirects) will be sent via a connection to the proxy server.
If your endpoint is an https
url, and you are using a proxy, then
request will send a CONNECT
request to the proxy server first, and
then use the supplied connection to connect to the endpoint.
That is, first it will make a request like:
HTTP/1.1 CONNECT endpoint-server.com:80
Host: proxy-server.com
User-Agent: whatever user agent you specify
and then the proxy server make a TCP connection to endpoint-server
on port 80
, and return a response that looks like:
HTTP/1.1 200 OK
At this point, the connection is left open, and the client is
communicating directly with the endpoint-server.com
machine.
See the wikipedia page on HTTP Tunneling for more information.
By default, when proxying http
traffic, request will simply make a
standard proxied http
request. This is done by making the url
section of the initial line of the request a fully qualified url to
the endpoint.
For example, it will make a single request that looks like:
HTTP/1.1 GET http://endpoint-server.com/some-url
Host: proxy-server.com
Other-Headers: all go here
request body or whatever
Because a pure "http over http" tunnel offers no additional security
or other features, it is generally simpler to go with a
straightforward HTTP proxy in this case. However, if you would like
to force a tunneling proxy, you may set the tunnel
option to true
.
You can also make a standard proxied http
request by explicitly setting
tunnel : false
, but note that this will allow the proxy to see the traffic
to/from the destination server.
If you are using a tunneling proxy, you may set the
proxyHeaderWhiteList
to share certain headers with the proxy.
You can also set the proxyHeaderExclusiveList
to share certain
headers only with the proxy and not with destination host.
By default, this set is:
accept
accept-charset
accept-encoding
accept-language
accept-ranges
cache-control
content-encoding
content-language
content-length
content-location
content-md5
content-range
content-type
connection
date
expect
max-forwards
pragma
proxy-authorization
referer
te
transfer-encoding
user-agent
via
Note that, when using a tunneling proxy, the proxy-authorization
header and any headers from custom proxyHeaderExclusiveList
are
never sent to the endpoint server, but only to the proxy server.
Controlling proxy behaviour using environment variables
The following environment variables are respected by request
:
HTTP_PROXY
/http_proxy
HTTPS_PROXY
/https_proxy
NO_PROXY
/no_proxy
When HTTP_PROXY
/ http_proxy
are set, they will be used to proxy non-SSL requests that do not have an explicit proxy
configuration option present. Similarly, HTTPS_PROXY
/ https_proxy
will be respected for SSL requests that do not have an explicit proxy
configuration option. It is valid to define a proxy in one of the environment variables, but then override it for a specific request, using the proxy
configuration option. Furthermore, the proxy
configuration option can be explicitly set to false / null to opt out of proxying altogether for that request.
request
is also aware of the NO_PROXY
/no_proxy
environment variables. These variables provide a granular way to opt out of proxying, on a per-host basis. It should contain a comma separated list of hosts to opt out of proxying. It is also possible to opt of proxying when a particular destination port is used. Finally, the variable may be set to *
to opt out of the implicit proxy configuration of the other environment variables.
Here's some examples of valid no_proxy
values:
google.com
- don't proxy HTTP/HTTPS requests to Google.google.com:443
- don't proxy HTTPS requests to Google, but do proxy HTTP requests to Google.google.com:443, yahoo.com:80
- don't proxy HTTPS requests to Google, and don't proxy HTTP requests to Yahoo!*
- ignorehttps_proxy
/http_proxy
environment variables altogether.
UNIX Domain Sockets
request
supports making requests to UNIX Domain Sockets. To make one, use the following URL scheme:
/* Pattern */ 'http://unix:SOCKET:PATH'
/* Example */ request.get('http://unix:/absolute/path/to/unix.socket:/request/path')
Note: The SOCKET
path is assumed to be absolute to the root of the host file system.
TLS/SSL Protocol
TLS/SSL Protocol options, such as cert
, key
and passphrase
, can be
set directly in options
object, in the agentOptions
property of the options
object, or even in https.globalAgent.options
. Keep in mind that, although agentOptions
allows for a slightly wider range of configurations, the recommended way is via options
object directly, as using agentOptions
or https.globalAgent.options
would not be applied in the same way in proxied environments (as data travels through a TLS connection instead of an http/https agent).
var fs = require('fs')
, path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, caFile = path.resolve(__dirname, 'ssl/ca.cert.pem')
, request = require('request');
var options = {
url: 'https://api.some-server.com/',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
passphrase: 'password',
ca: fs.readFileSync(caFile)
};
request.get(options);
Using options.agentOptions
In the example below, we call an API that requires client side SSL certificate (in PEM format) with passphrase protected private key (in PEM format) and disable the SSLv3 protocol:
var fs = require('fs')
, path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, request = require('request');
var options = {
url: 'https://api.some-server.com/',
agentOptions: {
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
// Or use `pfx` property replacing `cert` and `key` when using private key, certificate and CA certs in PFX or PKCS12 format:
// pfx: fs.readFileSync(pfxFilePath),
passphrase: 'password',
securityOptions: 'SSL_OP_NO_SSLv3'
}
};
request.get(options);
It is able to force using SSLv3 only by specifying secureProtocol
:
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
secureProtocol: 'SSLv3_method'
}
});
It is possible to accept other certificates than those signed by generally allowed Certificate Authorities (CAs).
This can be useful, for example, when using self-signed certificates.
To require a different root certificate, you can specify the signing CA by adding the contents of the CA's certificate file to the agentOptions
.
The certificate the domain presents must be signed by the root certificate specified:
request.get({
url: 'https://api.some-server.com/',
agentOptions: {
ca: fs.readFileSync('ca.cert.pem')
}
});
Support for HAR 1.2
The options.har
property will override the values: url
, method
, qs
, headers
, form
, formData
, body
, json
, as well as construct multipart data and read files from disk when request.postData.params[].fileName
is present without a matching value
.
A validation step will check if the HAR Request format matches the latest spec (v1.2) and will skip parsing if not matching.
var request = require('request')
request({
// will be ignored
method: 'GET',
uri: 'http://www.google.com',
// HTTP Archive Request Object
har: {
url: 'http://www.mockbin.com/har',
method: 'POST',
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded'
}
],
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{
name: 'foo',
value: 'bar'
},
{
name: 'hello',
value: 'world'
}
]
}
}
})
// a POST request will be sent to http://www.mockbin.com
// with body an application/x-www-form-urlencoded body:
// foo=bar&hello=world
request(options, callback)
The first argument can be either a url
or an options
object. The only required option is uri
; all others are optional.
uri
||url
- fully qualified uri or a parsed url object fromurl.parse()
baseUrl
- fully qualified uri string used as the base url. Most useful withrequest.defaults
, for example when you want to do many requests to the same domain. IfbaseUrl
ishttps://example.com/api/
, then requesting/end/point?test=true
will fetchhttps://example.com/api/end/point?test=true
. WhenbaseUrl
is given,uri
must also be a string.method
- http method (default:"GET"
)headers
- http headers (default:{}
)
qs
- object containing querystring values to be appended to theuri
qsParseOptions
- object containing options to pass to the qs.parse method. Alternatively pass options to the querystring.parse method using this format{sep:';', eq:':', options:{}}
qsStringifyOptions
- object containing options to pass to the qs.stringify method. Alternatively pass options to the querystring.stringify method using this format{sep:';', eq:':', options:{}}
. For example, to change the way arrays are converted to query strings using theqs
module pass thearrayFormat
option with one ofindices|brackets|repeat
useQuerystring
- if true, usequerystring
to stringify and parse querystrings, otherwise useqs
(default:false
). Set this option totrue
if you need arrays to be serialized asfoo=bar&foo=baz
instead of the defaultfoo[0]=bar&foo[1]=baz
.
body
- entity body for PATCH, POST and PUT requests. Must be aBuffer
,String
orReadStream
. Ifjson
istrue
, thenbody
must be a JSON-serializable object.form
- when passed an object or a querystring, this setsbody
to a querystring representation of value, and addsContent-type: application/x-www-form-urlencoded
header. When passed no options, aFormData
instance is returned (and is piped to request). See "Forms" section above.formData
- data to pass for amultipart/form-data
request. See Forms section above.multipart
- array of objects which contain their own headers andbody
attributes. Sends amultipart/related
request. See Forms section above.- Alternatively you can pass in an object
{chunked: false, data: []}
wherechunked
is used to specify whether the request is sent in chunked transfer encoding In non-chunked requests, data items with body streams are not allowed. preambleCRLF
- append a newline/CRLF before the boundary of yourmultipart/form-data
request.postambleCRLF
- append a newline/CRLF at the end of the boundary of yourmultipart/form-data
request.json
- setsbody
to JSON representation of value and addsContent-type: application/json
header. Additionally, parses the response body as JSON.jsonReviver
- a reviver function that will be passed toJSON.parse()
when parsing a JSON response body.jsonReplacer
- a replacer function that will be passed toJSON.stringify()
when stringifying a JSON request body.
auth
- a hash containing valuesuser
||username
,pass
||password
, andsendImmediately
(optional). See documentation above.oauth
- options for OAuth HMAC-SHA1 signing. See documentation above.hawk
- options for Hawk signing. Thecredentials
key must contain the necessary signing info, see hawk docs for details.aws
-object
containing AWS signing information. Should have the propertieskey
,secret
, and optionallysession
(note that this only works for services that require session as part of the canonical string). Also requires the propertybucket
, unless you’re specifying yourbucket
as part of the path, or the request doesn’t use a bucket (i.e. GET Services). If you want to use AWS sign version 4 use the parametersign_version
with value4
otherwise the default is version 2. If you are using SigV4, you can also include aservice
property that specifies the service name. Note: you need tonpm install aws4
first.httpSignature
- options for the HTTP Signature Scheme using Joyent's library. ThekeyId
andkey
properties must be specified. See the docs for other options.
followRedirect
- follow HTTP 3xx responses as redirects (default:true
). This property can also be implemented as function which getsresponse
object as a single argument and should returntrue
if redirects should continue orfalse
otherwise.followAllRedirects
- follow non-GET HTTP 3xx responses as redirects (default:false
)followOriginalHttpMethod
- by default we redirect to HTTP method GET. you can enable this property to redirect to the original HTTP method (default:false
)maxRedirects
- the maximum number of redirects to follow (default:10
)removeRefererHeader
- removes the referer header when a redirect happens (default:false
). Note: if true, referer header set in the initial request is preserved during redirect chain.
encoding
- encoding to be used onsetEncoding
of response data. Ifnull
, thebody
is returned as aBuffer
. Anything else (including the default value ofundefined
) will be passed as the encoding parameter totoString()
(meaning this is effectivelyutf8
by default). (Note: if you expect binary data, you should setencoding: null
.)gzip
- iftrue
, add anAccept-Encoding
header to request compressed content encodings from the server (if not already present) and decode supported content encodings in the response. Note: Automatic decoding of the response content is performed on the body data returned throughrequest
(both through therequest
stream and passed to the callback function) but is not performed on theresponse
stream (available from theresponse
event) which is the unmodifiedhttp.IncomingMessage
object which may contain compressed data. See example below.jar
- iftrue
, remember cookies for future use (or define your custom cookie jar; see examples section)
agent
-http(s).Agent
instance to useagentClass
- alternatively specify your agent's class nameagentOptions
- and pass its options. Note: for HTTPS see tls API doc for TLS/SSL options and the documentation above.forever
- set totrue
to use the forever-agent Note: Defaults tohttp(s).Agent({keepAlive:true})
in node 0.12+pool
- an object describing which agents to use for the request. If this option is omitted the request will use the global agent (as long as your options allow for it). Otherwise, request will search the pool for your custom agent. If no custom agent is found, a new agent will be created and added to the pool. Note:pool
is used only when theagent
option is not specified.- A
maxSockets
property can also be provided on thepool
object to set the max number of sockets for all agents created (ex:pool: {maxSockets: Infinity}
). - Note that if you are sending multiple requests in a loop and creating
multiple new
pool
objects,maxSockets
will not work as intended. To work around this, either userequest.defaults
with your pool options or create the pool object with themaxSockets
property outside of the loop. timeout
- integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule thetimeout
option (the default in Linux can be anywhere from 20-120 seconds).
localAddress
- local interface to bind for network connections.proxy
- an HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for theurl
parameter (by embedding the auth info in theuri
)strictSSL
- iftrue
, requires SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that CA as an option.tunnel
- controls the behavior of HTTPCONNECT
tunneling as follows:undefined
(default) -true
if the destination ishttps
,false
otherwisetrue
- always tunnel to the destination by making aCONNECT
request to the proxyfalse
- request the destination as aGET
request.proxyHeaderWhiteList
- a whitelist of headers to send to a tunneling proxy.proxyHeaderExclusiveList
- a whitelist of headers to send exclusively to a tunneling proxy and not to destination.
time
- iftrue
, the request-response cycle (including all redirects) is timed at millisecond resolution. When set, the following properties are added to the response object:elapsedTime
Duration of the entire request/response in milliseconds (deprecated).responseStartTime
Timestamp when the response began (in Unix Epoch milliseconds) (deprecated).timingStart
Timestamp of the start of the request (in Unix Epoch milliseconds).timings
Contains event timestamps in millisecond resolution relative totimingStart
. If there were redirects, the properties reflect the timings of the final request in the redirect chain:socket
Relative timestamp when thehttp
module'ssocket
event fires. This happens when the socket is assigned to the request.lookup
Relative timestamp when thenet
module'slookup
event fires. This happens when the DNS has been resolved.connect
: Relative timestamp when thenet
module'sconnect
event fires. This happens when the server acknowledges the TCP connection.response
: Relative timestamp when thehttp
module'sresponse
event fires. This happens when the first bytes are received from the server.end
: Relative timestamp when the last bytes of the response are received.
-
timingPhases
Contains the durations of each request phase. If there were redirects, the properties reflect the timings of the final request in the redirect chain:wait
: Duration of socket initialization (timings.socket
)dns
: Duration of DNS lookup (timings.lookup
-timings.socket
)tcp
: Duration of TCP connection (timings.connect
-timings.socket
)firstByte
: Duration of HTTP server response (timings.response
-timings.connect
)download
: Duration of HTTP download (timings.end
-timings.response
)total
: Duration entire HTTP round-trip (timings.end
)
-
har
- a HAR 1.2 Request Object, will be processed from HAR format into options overwriting matching values (see the HAR 1.2 section for details) callback
- alternatively pass the request's callback in the options object
The callback argument gets 3 arguments:
- An
error
when applicable (usually fromhttp.ClientRequest
object) - An
http.IncomingMessage
object (Response object) - The third is the
response
body (String
orBuffer
, or JSON object if thejson
option is supplied)
Convenience methods
There are also shorthand methods for different HTTP METHODs and some other conveniences.
request.defaults(options)
This method returns a wrapper around the normal request API that defaults to whatever options you pass to it.
Note: request.defaults()
does not modify the global request API;
instead, it returns a wrapper that has your default settings applied to it.
Note: You can call .defaults()
on the wrapper that is returned from
request.defaults
to add/override defaults that were previously defaulted.
For example:
//requests using baseRequest() will set the 'x-token' header
var baseRequest = request.defaults({
headers: {'x-token': 'my-token'}
})
//requests using specialRequest() will include the 'x-token' header set in
//baseRequest and will also include the 'special' header
var specialRequest = baseRequest.defaults({
headers: {special: 'special value'}
})
request.METHOD()
These HTTP method convenience functions act just like request()
but with a default method already set for you:
- request.get(): Defaults to
method: "GET"
. - request.post(): Defaults to
method: "POST"
. - request.put(): Defaults to
method: "PUT"
. - request.patch(): Defaults to
method: "PATCH"
. - request.del() / request.delete(): Defaults to
method: "DELETE"
. - request.head(): Defaults to
method: "HEAD"
. - request.options(): Defaults to
method: "OPTIONS"
.
request.cookie()
Function that creates a new cookie.
request.cookie('key1=value1')
request.jar()
Function that creates a new cookie jar.
request.jar()
Debugging
There are at least three ways to debug the operation of request
:
-
Launch the node process like
NODE_DEBUG=request node script.js
(lib,request,otherlib
works too). -
Set
require('request').debug = true
at any time (this does the same thing as #1). -
Use the request-debug module to view request and response headers and bodies.
Timeouts
Most requests to external servers should have a timeout attached, in case the server is not responding in a timely manner. Without a timeout, your code may have a socket open/consume resources for minutes or more.
There are two main types of timeouts: connection timeouts and read timeouts. A connect timeout occurs if the timeout is hit while your client is attempting to establish a connection to a remote machine (corresponding to the connect() call on the socket). A read timeout occurs any time the server is too slow to send back a part of the response.
These two situations have widely different implications for what went wrong
with the request, so it's useful to be able to distinguish them. You can detect
timeout errors by checking err.code
for an 'ETIMEDOUT' value. Further, you
can detect whether the timeout was a connection timeout by checking if the
err.connect
property is set to true
.
request.get('http://<IP_ADDRESS>', {timeout: 1500}, function(err) {
console.log(err.code === 'ETIMEDOUT');
// Set to `true` if the timeout was a connection timeout, `false` or
// `undefined` otherwise.
console.log(err.connect === true);
process.exit(0);
});
Examples:
var request = require('request')
, rand = Math.floor(Math.random()*100000000).toString()
;
request(
{ method: 'PUT'
, uri: 'http://mikeal.iriscouch.com/testjs/' + rand
, multipart:
[ { 'content-type': 'application/json'
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
}
, { body: 'I am an attachment' }
]
}
, function (error, response, body) {
if(response.statusCode == 201){
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
} else {
console.log('error: '+ response.statusCode)
console.log(body)
}
}
)
For backwards-compatibility, response compression is not supported by default.
To accept gzip-compressed responses, set the gzip
option to true
. Note
that the body data passed through request
is automatically decompressed
while the response object is unmodified and will contain compressed data if
the server sent a compressed response.
var request = require('request')
request(
{ method: 'GET'
, uri: 'http://www.google.com'
, gzip: true
}
, function (error, response, body) {
// body is the decompressed response body
console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
console.log('the decoded data is: ' + body)
}
)
.on('data', function(data) {
// decompressed data as it is received
console.log('decoded chunk: ' + data)
})
.on('response', function(response) {
// unmodified http.IncomingMessage object
response.on('data', function(data) {
// compressed data as it is received
console.log('received ' + data.length + ' bytes of compressed data')
})
})
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar
to true
(either in defaults
or options
).
var request = request.defaults({jar: true})
request('http://www.google.com', function () {
request('http://images.google.com')
})
To use a custom cookie jar (instead of request
’s global cookie jar), set jar
to an instance of request.jar()
(either in defaults
or options
)
var j = request.jar()
var request = request.defaults({jar:j})
request('http://www.google.com', function () {
request('http://images.google.com')
})
OR
var j = request.jar();
var cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com')
})
To use a custom cookie store (such as a
FileCookieStore
which supports saving to and restoring from JSON files), pass it as a parameter
to request.jar()
:
var FileCookieStore = require('tough-cookie-filestore');
// NOTE - currently the 'cookies.json' file must already exist!
var j = request.jar(new FileCookieStore('cookies.json'));
request = request.defaults({ jar : j })
request('http://www.google.com', function() {
request('http://images.google.com')
})
The cookie store must be a
tough-cookie
store and it must support synchronous operations; see the
CookieStore
API docs
for details.
To inspect your cookie jar after a request:
var j = request.jar()
request({url: 'http://www.google.com', jar: j}, function () {
var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
var cookies = j.getCookies(url);
// [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
})


{% extends 'basics/base.html' %} {% load filter%} {% load staticfiles %} {% block content %}
If you delete This Need, the data will be permanently lost.
Would you like to rethink your decision?
If you delete This Comment, the data will be permanently lost.
Would you like to rethink your decision?


You an a orange rectangle. To control him you use the arrow keys(the buttons you see to control on mobile).
There are 2 types of enemys green and yellow. At the end of the game there is a boss that takes 10 hits to kill.
To kill enemys you need to jump on the enemys heads. To beat a screen you need to kill all enemys on it.
At the end of a level you will see a green wall at the end at the end of the screen. That is how you play the game.


{% load static %} {% include "nav.html" %}
We’ve emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly.
If you don’t receive an email, please make sure you’ve entered the address you registered with, and check your Spam folder or Promotions folder.


1 x $ 38.00
1 x $ 33.00
We are Whole business, our strategists will help you set an objective and choose your tools, developing a plan that is custom-built for your business.


https://cybellum.com/doubleagentzero-day-code-injection-and-persistence-technique/
https://github.com/Cybellum/DoubleAgent
https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ms220948(v=vs.90)


dhon.manalo
"Any fool can write code that a computer can understand"
Home
Disclaimer
Archives
Feed
Baked with
JBake v2.6.7
Theme by
Mark Otto
Tag: java
November 2018
03 -
Start Learning WebSocket
September 2017
29 -
Profiling Remote Java Application
June 2017
17 -
Spring Boot with Angular Setup
May 2017
03 -
Installing Nexus OSS on Centos 7
February 2017
01 -
Hello Ofbiz
June 2016
18 -
Spring Boot SOAP Client
11 -
Exceptional Exceptions
May 2016
21 -
Installing Java 1.8 on Centos
01 -
Blogging With JBake
April 2016
30 -
Custom JSP Tags
January 2016
26 -
TeamCity With Postgres
December 2015
21 -
Making Java Groovy
October 2015
13 -
Jenkins on CentOS
May 2015
20 -
Observer Pattern
20 -
Factory Pattern
19 -
JVM Garbage Collector Switches
April 2015
18 -
applicationContext.xml and dispatcher-servlet.xml
05 -
Expression-based Access with FreeMarker
March 2015
04 -
Spring Form Binding with FreeMarker
September 2014
13 -
Adding Local JAR to Maven
03 -
Spring Persistence with Hibernate
August 2014
27 -
Spring Standalone Project


With almost 4 years of software development experience, I would like to apply for a Fellow position to continue the work at CERN.
I was recruited to CERN as PJAS in 2012 to the GS-AIS-EB (Electronic Business) section where I have had the chance to work on a few exciting projects:
During my time at CERN I have had the opportunity to work with different teams (HR, CERN Hotel representatives, external consultants) as well as with different people in our section while implementing diverse projects together. Therefore, my focus is not only on my personal technical level improvement, but also on better understanding of business processes and needs of the whole team, and enhancing of communication skills.
I spend a lot of time on independent study. During the last year, I launched the technical blog of mine, designed several widgets for open-source Linux window manager Awesome, deepened my knowledge of Java, Ruby, Python, RegEx.
I take part in the development of business critical eFiles document management system based on open source solution - Alfresco – the market leader in records management. One of my projects is e-Personnel – CMS system. I work in a small team of three; development is performed using agile software development with Scrum. As an Alfresco developer I work on extending and customizing Alfresco, which is needed since out of the box solutions of Alfresco do not provide enough functionality for CERN needs. Through this project, I have learned business processes of other teams, as we constantly work with CERN HR team, developing the way to link their needs with technical abilities of Alfresco software.
Selected Accomplishments:
Customization of server-side and client-side parts of Alfresco in correspondence to the organisation's internal processes and requirements.
Integrating Alfresco with AppDynamics - application performance management system. This project consisted of setting up the AppDynamics and development of the monitoring extensions in order to provide metrics.
I worked in GS-AIS-EB (Electronic Business) section for EDH (Electronic Document Handling) system, which covers many life aspects at CERN (such as absences, HR & Training, purchasing, logistics and safety). In this role, I was developing EDH documents and improving functionality of existing ones. I worked in a team of 8-10 people; agile software development with Scrum was used for a development performance. One of the biggest and the most interesting project during this period was the development of CERN Hotel booking application. This activity included the work with Micros/Oracle consultant and Hotel’s representatives.
Selected Accomplishments:
Migrating existing business processes from BPMN to Activiti.
Modification of the Phone Request document in order to get the possibility to choose LYNC phone.
Extending functionality of CERN Hotel web booking application (hostel.cern.ch). Main feature developed is the possibility to create a Waiting-List reservations in cases when no rooms were available for the given period, which was not supported by Fidelio. I was working closely with customers from Hotel as well as consultant from Micros/Oracle.
3rd Line support
While finishing the study at University I got a job at Probusinessbank in Moscow, one of TOP-50 banks in Russia. During my time there, I was responsible for several activities:
Back-End
Front-End
Others
Technical Blog - http://pavelmakhov.com
Awesome WM (https://github.com/streetturtle/AwesomeWM) - collection of widgets for the Awesome Window Manager for Linux. Written in Lua and Python.
AppDynamics Extensions (https://github.com/streetturtle/AppDynamics-Extensions) - several extensions for AppDynamics. They were developed because out-of-the-box extensions are written in Java and it was hard to maintain and customize them.
jekyll-clean-dark (https://github.com/streetturtle/jekyll-clean-dark) - theme for Jekyll - static site generator. I created this theme for myself and then published it (http://drjekyllthemes.github.io/)as a separate project.
CERN provides a big variety of sport activities, since I started to work here I became a member of CERN Boxing club, Ski Club, recently I joined a HIIT classes. I am also a big fan of cycling, rollerskating and skateboarding. Apart of sport activities I'm learning to play guitar, working on my blog and on some separate open-source projects.


Toggle navigation
front
Home
crud
signin
admin
About
Contact
Dropdown
dashboard
crud
Something else here
Nav header
Separated link
One more separated link
Default
Static top
Fixed top
(current)


{{ define "main" }} {{ partial "jumbotron" (dict "imageUrl" "/img/7.jpg" "title" "This is where the heading goes" "subtitle" "This is where the subtitle goes") }} {{ .Content }}
{{ if and .Site.Params.CommentoURL (and (not .Site.BuildDrafts) (not .Site.IsServer)) -}}


001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.ha; 020 021import java.io.IOException; 022import java.util.Arrays; 023import java.util.List; 024import java.util.concurrent.CountDownLatch; 025import java.util.concurrent.TimeUnit; 026import java.util.concurrent.locks.Lock; 027import java.util.concurrent.locks.ReentrantLock; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.apache.hadoop.HadoopIllegalArgumentException; 032import org.apache.hadoop.classification.InterfaceAudience; 033import org.apache.hadoop.classification.InterfaceStability; 034import org.apache.hadoop.util.ZKUtil.ZKAuthInfo; 035import org.apache.hadoop.util.StringUtils; 036import org.apache.zookeeper.data.ACL; 037import org.apache.zookeeper.KeeperException; 038import org.apache.zookeeper.Watcher; 039import org.apache.zookeeper.WatchedEvent; 040import org.apache.zookeeper.Watcher.Event; 041import org.apache.zookeeper.ZKUtil; 042import org.apache.zookeeper.ZooKeeper; 043import org.apache.zookeeper.CreateMode; 044import org.apache.zookeeper.AsyncCallback.*; 045import org.apache.zookeeper.data.Stat; 046import org.apache.zookeeper.KeeperException.Code; 047 048import com.google.common.annotations.VisibleForTesting; 049import com.google.common.base.Preconditions; 050 051/** 052 * 053 * This class implements a simple library to perform leader election on top of 054 * Apache Zookeeper. Using Zookeeper as a coordination service, leader election 055 * can be performed by atomically creating an ephemeral lock file (znode) on 056 * Zookeeper. The service instance that successfully creates the znode becomes 057 * active and the rest become standbys.
 058 * This election mechanism is only efficient for small number of election 059 * candidates (order of 10's) because contention on single znode by a large 060 * number of candidates can result in Zookeeper overload.
 061 * The elector does not guarantee fencing (protection of shared resources) among 062 * service instances. After it has notified an instance about becoming a leader, 063 * then that instance must ensure that it meets the service consistency 064 * requirements. If it cannot do so, then it is recommended to quit the 065 * election. The application implements the {@link ActiveStandbyElectorCallback} 066 * to interact with the elector 067 */<EMAIL_ADDRESS><EMAIL_ADDRESS>070public class ActiveStandbyElector implements StatCallback, StringCallback { 071 072 /** 073 * Callback interface to interact with the ActiveStandbyElector object.
 074 * The application will be notified with a callback only on state changes 075 * (i.e. there will never be successive calls to becomeActive without an 076 * intermediate call to enterNeutralMode).
 077 * The callbacks will be running on Zookeeper client library threads. The 078 * application should return from these callbacks quickly so as not to impede 079 * Zookeeper client library performance and notifications. The app will 080 * typically remember the state change and return from the callback. It will 081 * then proceed with implementing actions around that state change. It is 082 * possible to be called back again while these actions are in flight and the 083 * app should handle this scenario. 084 */ 085 public interface ActiveStandbyElectorCallback { 086 /** 087 * This method is called when the app becomes the active leader. 088 * If the service fails to become active, it should throw 089 * ServiceFailedException. This will cause the elector to 090 * sleep for a short period, then re-join the election. 091 * 092 * Callback implementations are expected to manage their own 093 * timeouts (e.g. when making an RPC to a remote node). 094 */ 095 void becomeActive() throws ServiceFailedException; 096 097 /** 098 * This method is called when the app becomes a standby 099 */ 100 void becomeStandby(); 101 102 /** 103 * If the elector gets disconnected from Zookeeper and does not know about 104 * the lock state, then it will notify the service via the enterNeutralMode 105 * interface. The service may choose to ignore this or stop doing state 106 * changing operations. Upon reconnection, the elector verifies the leader 107 * status and calls back on the becomeActive and becomeStandby app 108 * interfaces.
 109 * Zookeeper disconnects can happen due to network issues or loss of 110 * Zookeeper quorum. Thus enterNeutralMode can be used to guard against 111 * split-brain issues. In such situations it might be prudent to call 112 * becomeStandby too. However, such state change operations might be 113 * expensive and enterNeutralMode can help guard against doing that for 114 * transient issues. 115 */ 116 void enterNeutralMode(); 117 118 /** 119 * If there is any fatal error (e.g. wrong ACL's, unexpected Zookeeper 120 * errors or Zookeeper persistent unavailability) then notifyFatalError is 121 * called to notify the app about it. 122 */ 123 void notifyFatalError(String errorMessage); 124 125 /** 126 * If an old active has failed, rather than exited gracefully, then 127 * the new active may need to take some fencing actions against it 128 * before proceeding with failover. 129 * 130 * @param oldActiveData the application data provided by the prior active 131 */ 132 void fenceOldActive(byte[] oldActiveData); 133 } 134 135 /** 136 * Name of the lock znode used by the library. Protected for access in test 137 * classes 138 */ 139 @VisibleForTesting 140 protected static final String LOCK_FILENAME = "ActiveStandbyElectorLock"; 141 @VisibleForTesting 142 protected static final String BREADCRUMB_FILENAME = "ActiveBreadCrumb"; 143 144 public static final Log LOG = LogFactory.getLog(ActiveStandbyElector.class); 145 146 private static final int SLEEP_AFTER_FAILURE_TO_BECOME_ACTIVE = 1000; 147 148 private static enum ConnectionState { 149 DISCONNECTED, CONNECTED, TERMINATED 150 }; 151 152 static enum State { 153 INIT, ACTIVE, STANDBY, NEUTRAL 154 }; 155 156 private State state = State.INIT; 157 private int createRetryCount = 0; 158 private int statRetryCount = 0; 159 private ZooKeeper zkClient; 160 private WatcherWithClientRef watcher; 161 private ConnectionState zkConnectionState = ConnectionState.TERMINATED; 162 163 private final ActiveStandbyElectorCallback appClient; 164 private final String zkHostPort; 165 private final int zkSessionTimeout; 166 private final List<ACL> zkAcl; 167 private final List<ZKAuthInfo> zkAuthInfo; 168 private byte[] appData; 169 private final String zkLockFilePath; 170 private final String zkBreadCrumbPath; 171 private final String znodeWorkingDir; 172 private final int maxRetryNum; 173 174 private Lock sessionReestablishLockForTests = new ReentrantLock(); 175 private boolean wantToBeInElection; 176 private boolean monitorLockNodePending = false; 177 private ZooKeeper monitorLockNodeClient; 178 179 /** 180 * Create a new ActiveStandbyElector object
 181 * The elector is created by providing to it the Zookeeper configuration, the 182 * parent znode under which to create the znode and a reference to the 183 * callback interface.
 184 * The parent znode name must be the same for all service instances and 185 * different across services.
 186 * After the leader has been lost, a new leader will be elected after the 187 * session timeout expires. Hence, the app must set this parameter based on 188 * its needs for failure response time. The session timeout must be greater 189 * than the Zookeeper disconnect timeout and is recommended to be 3X that 190 * value to enable Zookeeper to retry transient disconnections. Setting a very 191 * short session timeout may result in frequent transitions between active and 192 * standby states during issues like network outages/GS pauses. 193 * 194 * @param zookeeperHostPorts 195 * ZooKeeper hostPort for all ZooKeeper servers 196 * @param zookeeperSessionTimeout 197 * ZooKeeper session timeout 198 * @param parentZnodeName 199 * znode under which to create the lock 200 * @param acl 201 * ZooKeeper ACL's 202 * @param authInfo a list of authentication credentials to add to the 203 * ZK connection 204 * @param app 205 * reference to callback interface object 206 * @throws IOException 207 * @throws HadoopIllegalArgumentException 208 */ 209 public ActiveStandbyElector(String zookeeperHostPorts, 210 int zookeeperSessionTimeout, String parentZnodeName, List<ACL> acl, 211 List<ZKAuthInfo> authInfo, ActiveStandbyElectorCallback app, 212 int maxRetryNum) throws IOException, HadoopIllegalArgumentException, 213 KeeperException { 214 this(zookeeperHostPorts, zookeeperSessionTimeout, parentZnodeName, acl, 215 authInfo, app, maxRetryNum, true); 216 } 217 218 /** 219 * Create a new ActiveStandbyElector object
 220 * The elector is created by providing to it the Zookeeper configuration, the 221 * parent znode under which to create the znode and a reference to the 222 * callback interface.
 223 * The parent znode name must be the same for all service instances and 224 * different across services.
 225 * After the leader has been lost, a new leader will be elected after the 226 * session timeout expires. Hence, the app must set this parameter based on 227 * its needs for failure response time. The session timeout must be greater 228 * than the Zookeeper disconnect timeout and is recommended to be 3X that 229 * value to enable Zookeeper to retry transient disconnections. Setting a very 230 * short session timeout may result in frequent transitions between active and 231 * standby states during issues like network outages/GS pauses. 232 * 233 * @param zookeeperHostPorts 234 * ZooKeeper hostPort for all ZooKeeper servers 235 * @param zookeeperSessionTimeout 236 * ZooKeeper session timeout 237 * @param parentZnodeName 238 * znode under which to create the lock 239 * @param acl 240 * ZooKeeper ACL's 241 * @param authInfo a list of authentication credentials to add to the 242 * ZK connection 243 * @param app 244 * reference to callback interface object 245 * @param failFast 246 * whether need to add the retry when establishing ZK connection. 247 * @throws IOException 248 * @throws HadoopIllegalArgumentException 249 */ 250 public ActiveStandbyElector(String zookeeperHostPorts, 251 int zookeeperSessionTimeout, String parentZnodeName, List<ACL> acl, 252 List<ZKAuthInfo> authInfo, ActiveStandbyElectorCallback app, 253 int maxRetryNum, boolean failFast) throws IOException, 254 HadoopIllegalArgumentException, KeeperException { 255 if (app == null || acl == null || parentZnodeName == null 256 || zookeeperHostPorts == null || zookeeperSessionTimeout <= 0) { 257 throw new HadoopIllegalArgumentException("Invalid argument"); 258 } 259 zkHostPort = zookeeperHostPorts; 260 zkSessionTimeout = zookeeperSessionTimeout; 261 zkAcl = acl; 262 zkAuthInfo = authInfo; 263 appClient = app; 264 znodeWorkingDir = parentZnodeName; 265 zkLockFilePath = znodeWorkingDir + "/" + LOCK_FILENAME; 266 zkBreadCrumbPath = znodeWorkingDir + "/" + BREADCRUMB_FILENAME; 267 this.maxRetryNum = maxRetryNum; 268 269 // establish the ZK Connection for future API calls 270 if (failFast) { 271 createConnection(); 272 } else { 273 reEstablishSession(); 274 } 275 } 276 277 /** 278 * To participate in election, the app will call joinElection. The result will 279 * be notified by a callback on either the becomeActive or becomeStandby app 280 * interfaces.
 281 * After this the elector will automatically monitor the leader status and 282 * perform re-election if necessary
 283 * The app could potentially start off in standby mode and ignore the 284 * becomeStandby call. 285 * 286 * @param data 287 * to be set by the app. non-null data must be set. 288 * @throws HadoopIllegalArgumentException 289 * if valid data is not supplied 290 */ 291 public synchronized void joinElection(byte[] data) 292 throws HadoopIllegalArgumentException { 293 294 if (data == null) { 295 throw new HadoopIllegalArgumentException("data cannot be null"); 296 } 297 298 if (wantToBeInElection) { 299 LOG.info("Already in election. Not re-connecting."); 300 return; 301 } 302 303 appData = new byte[data.length]; 304 System.arraycopy(data, 0, appData, 0, data.length); 305 306 if (LOG.isDebugEnabled()) { 307 LOG.debug("Attempting active election for " + this); 308 } 309 joinElectionInternal(); 310 } 311 312 /** 313 * @return true if the configured parent znode exists 314 */ 315 public synchronized boolean parentZNodeExists() 316 throws IOException, InterruptedException { 317 Preconditions.checkState(zkClient != null); 318 try { 319 return zkClient.exists(znodeWorkingDir, false) != null; 320 } catch (KeeperException e) { 321 throw new IOException("Couldn't determine existence of znode '" + 322 znodeWorkingDir + "'", e); 323 } 324 } 325 326 /** 327 * Utility function to ensure that the configured base znode exists. 328 * This recursively creates the znode as well as all of its parents. 329 */ 330 public synchronized void ensureParentZNode() 331 throws IOException, InterruptedException { 332 Preconditions.checkState(!wantToBeInElection, 333 "ensureParentZNode() may not be called while in the election"); 334 335 String pathParts[] = znodeWorkingDir.split("/"); 336 Preconditions.checkArgument(pathParts.length >= 1 && 337 pathParts[0].isEmpty(), 338 "Invalid path: %s", znodeWorkingDir); 339 340 StringBuilder sb = new StringBuilder(); 341 for (int i = 1; i < pathParts.length; i++) { 342 sb.append("/").append(pathParts[i]); 343 String prefixPath = sb.toString(); 344 LOG.debug("Ensuring existence of " + prefixPath); 345 try { 346 createWithRetries(prefixPath, new byte[]{}, zkAcl, CreateMode.PERSISTENT); 347 } catch (KeeperException e) { 348 if (isNodeExists(e.code())) { 349 // This is OK - just ensuring existence. 350 continue; 351 } else { 352 throw new IOException("Couldn't create " + prefixPath, e); 353 } 354 } 355 } 356 357 LOG.info("Successfully created " + znodeWorkingDir + " in ZK."); 358 } 359 360 /** 361 * Clear all of the state held within the parent ZNode. 362 * This recursively deletes everything within the znode as well as the 363 * parent znode itself. It should only be used when it's certain that 364 * no electors are currently participating in the election. 365 */ 366 public synchronized void clearParentZNode() 367 throws IOException, InterruptedException { 368 Preconditions.checkState(!wantToBeInElection, 369 "clearParentZNode() may not be called while in the election"); 370 371 try { 372 LOG.info("Recursively deleting " + znodeWorkingDir + " from ZK..."); 373 374 zkDoWithRetries(new ZKAction<Void>() { 375 @Override 376 public Void run() throws KeeperException, InterruptedException { 377 ZKUtil.deleteRecursive(zkClient, znodeWorkingDir); 378 return null; 379 } 380 }); 381 } catch (KeeperException e) { 382 throw new IOException("Couldn't clear parent znode " + znodeWorkingDir, 383 e); 384 } 385 LOG.info("Successfully deleted " + znodeWorkingDir + " from ZK."); 386 } 387 388 389 /** 390 * Any service instance can drop out of the election by calling quitElection. 391 *
 392 * This will lose any leader status, if held, and stop monitoring of the lock 393 * node.
 394 * If the instance wants to participate in election again, then it needs to 395 * call joinElection().
 396 * This allows service instances to take themselves out of rotation for known 397 * impending unavailable states (e.g. long GC pause or software upgrade). 398 * 399 * @param needFence true if the underlying daemon may need to be fenced 400 * if a failover occurs due to dropping out of the election. 401 */ 402 public synchronized void quitElection(boolean needFence) { 403 LOG.info("Yielding from election"); 404 if (!needFence && state == State.ACTIVE) { 405 // If active is gracefully going back to standby mode, remove 406 // our permanent znode so no one fences us. 407 tryDeleteOwnBreadCrumbNode(); 408 } 409 reset(); 410 wantToBeInElection = false; 411 } 412 413 /** 414 * Exception thrown when there is no active leader 415 */ 416 public static class ActiveNotFoundException extends Exception { 417 private static final long serialVersionUID =<PHONE_NUMBER>342846462L; 418 } 419 420 /** 421 * get data set by the active leader 422 * 423 * @return data set by the active instance 424 * @throws ActiveNotFoundException 425 * when there is no active leader 426 * @throws KeeperException 427 * other zookeeper operation errors 428 * @throws InterruptedException 429 * @throws IOException 430 * when ZooKeeper connection could not be established 431 */ 432 public synchronized byte[] getActiveData() throws ActiveNotFoundException, 433 KeeperException, InterruptedException, IOException { 434 try { 435 if (zkClient == null) { 436 createConnection(); 437 } 438 Stat stat = new Stat(); 439 return getDataWithRetries(zkLockFilePath, false, stat); 440 } catch(KeeperException e) { 441 Code code = e.code(); 442 if (isNodeDoesNotExist(code)) { 443 // handle the commonly expected cases that make sense for us 444 throw new ActiveNotFoundException(); 445 } else { 446 throw e; 447 } 448 } 449 } 450 451 /** 452 * interface implementation of Zookeeper callback for create 453 */ 454 @Override 455 public synchronized void processResult(int rc, String path, Object ctx, 456 String name) { 457 if (isStaleClient(ctx)) return; 458 if (LOG.isDebugEnabled()) { 459 LOG.debug("CreateNode result: " + rc + " for path: " + path 460 + " connectionState: " + zkConnectionState + 461 " for " + this); 462 } 463 464 Code code = Code.get(rc); 465 if (isSuccess(code)) { 466 // we successfully created the znode. we are the leader. start monitoring 467 if (becomeActive()) { 468 monitorActiveStatus(); 469 } else { 470 reJoinElectionAfterFailureToBecomeActive(); 471 } 472 return; 473 } 474 475 if (isNodeExists(code)) { 476 if (createRetryCount == 0) { 477 // znode exists and we did not retry the operation. so a different 478 // instance has created it. become standby and monitor lock. 479 becomeStandby(); 480 } 481 // if we had retried then the znode could have been created by our first 482 // attempt to the server (that we lost) and this node exists response is 483 // for the second attempt. verify this case via ephemeral node owner. this 484 // will happen on the callback for monitoring the lock. 485 monitorActiveStatus(); 486 return; 487 } 488 489 String errorMessage = "Received create error from Zookeeper. code:" 490 + code.toString() + " for path " + path; 491 LOG.debug(errorMessage); 492 493 if (shouldRetry(code)) { 494 if (createRetryCount < maxRetryNum) { 495 LOG.debug("Retrying createNode createRetryCount: " + createRetryCount); 496 ++createRetryCount; 497 createLockNodeAsync(); 498 return; 499 } 500 errorMessage = errorMessage 501 + ". Not retrying further znode create connection errors."; 502 } else if (isSessionExpired(code)) { 503 // This isn't fatal - the client Watcher will re-join the election 504 LOG.warn("Lock acquisition failed because session was lost"); 505 return; 506 } 507 508 fatalError(errorMessage); 509 } 510 511 /** 512 * interface implementation of Zookeeper callback for monitor (exists) 513 */ 514 @Override 515 public synchronized void processResult(int rc, String path, Object ctx, 516 Stat stat) { 517 if (isStaleClient(ctx)) return; 518 monitorLockNodePending = false; 519 520 assert wantToBeInElection : 521 "Got a StatNode result after quitting election"; 522 523 if (LOG.isDebugEnabled()) { 524 LOG.debug("StatNode result: " + rc + " for path: " + path 525 + " connectionState: " + zkConnectionState + " for " + this); 526 } 527 528 Code code = Code.get(rc); 529 if (isSuccess(code)) { 530 // the following owner check completes verification in case the lock znode 531 // creation was retried 532 if (stat.getEphemeralOwner() == zkClient.getSessionId()) { 533 // we own the lock znode. so we are the leader 534 if (!becomeActive()) { 535 reJoinElectionAfterFailureToBecomeActive(); 536 } 537 } else { 538 // we dont own the lock znode. so we are a standby. 539 becomeStandby(); 540 } 541 // the watch set by us will notify about changes 542 return; 543 } 544 545 if (isNodeDoesNotExist(code)) { 546 // the lock znode disappeared before we started monitoring it 547 enterNeutralMode(); 548 joinElectionInternal(); 549 return; 550 } 551 552 String errorMessage = "Received stat error from Zookeeper. code:" 553 + code.toString(); 554 LOG.debug(errorMessage); 555 556 if (shouldRetry(code)) { 557 if (statRetryCount < maxRetryNum) { 558 ++statRetryCount; 559 monitorLockNodeAsync(); 560 return; 561 } 562 errorMessage = errorMessage 563 + ". Not retrying further znode monitoring connection errors."; 564 } else if (isSessionExpired(code)) { 565 // This isn't fatal - the client Watcher will re-join the election 566 LOG.warn("Lock monitoring failed because session was lost"); 567 return; 568 } 569 570 fatalError(errorMessage); 571 } 572 573 /** 574 * We failed to become active. Re-join the election, but 575 * sleep for a few seconds after terminating our existing 576 * session, so that other nodes have a chance to become active. 577 * The failure to become active is already logged inside 578 * becomeActive(). 579 */ 580 private void reJoinElectionAfterFailureToBecomeActive() { 581 reJoinElection(SLEEP_AFTER_FAILURE_TO_BECOME_ACTIVE); 582 } 583 584 /** 585 * interface implementation of Zookeeper watch events (connection and node), 586 * proxied by {@link WatcherWithClientRef}. 587 */ 588 synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) { 589 Event.EventType eventType = event.getType(); 590 if (isStaleClient(zk)) return; 591 if (LOG.isDebugEnabled()) { 592 LOG.debug("Watcher event type: " + eventType + " with state:" 593 + event.getState() + " for path:" + event.getPath() 594 + " connectionState: " + zkConnectionState 595 + " for " + this); 596 } 597 598 if (eventType == Event.EventType.None) { 599 // the connection state has changed 600 switch (event.getState()) { 601 case SyncConnected: 602 LOG.info("Session connected."); 603 // if the listener was asked to move to safe state then it needs to 604 // be undone 605 ConnectionState prevConnectionState = zkConnectionState; 606 zkConnectionState = ConnectionState.CONNECTED; 607 if (prevConnectionState == ConnectionState.DISCONNECTED && 608 wantToBeInElection) { 609 monitorActiveStatus(); 610 } 611 break; 612 case Disconnected: 613 LOG.info("Session disconnected. Entering neutral mode..."); 614 615 // ask the app to move to safe state because zookeeper connection 616 // is not active and we dont know our state 617 zkConnectionState = ConnectionState.DISCONNECTED; 618 enterNeutralMode(); 619 break; 620 case Expired: 621 // the connection got terminated because of session timeout 622 // call listener to reconnect 623 LOG.info("Session expired. Entering neutral mode and rejoining..."); 624 enterNeutralMode(); 625 reJoinElection(0); 626 break; 627 case SaslAuthenticated: 628 LOG.info("Successfully authenticated to ZooKeeper using SASL."); 629 break; 630 default: 631 fatalError("Unexpected Zookeeper watch event state: " 632 + event.getState()); 633 break; 634 } 635 636 return; 637 } 638 639 // a watch on lock path in zookeeper has fired. so something has changed on 640 // the lock. ideally we should check that the path is the same as the lock 641 // path but trusting zookeeper for now 642 String path = event.getPath(); 643 if (path != null) { 644 switch (eventType) { 645 case NodeDeleted: 646 if (state == State.ACTIVE) { 647 enterNeutralMode(); 648 } 649 joinElectionInternal(); 650 break; 651 case NodeDataChanged: 652 monitorActiveStatus(); 653 break; 654 default: 655 if (LOG.isDebugEnabled()) { 656 LOG.debug("Unexpected node event: " + eventType + " for path: " + path); 657 } 658 monitorActiveStatus(); 659 } 660 661 return; 662 } 663 664 // some unexpected error has occurred 665 fatalError("Unexpected watch error from Zookeeper"); 666 } 667 668 /** 669 * Get a new zookeeper client instance. protected so that test class can 670 * inherit and mock out the zookeeper instance 671 * 672 * @return new zookeeper client instance 673 * @throws IOException 674 * @throws KeeperException zookeeper connectionloss exception 675 */ 676 protected synchronized ZooKeeper connectToZooKeeper() throws IOException, 677 KeeperException { 678 679 // Unfortunately, the ZooKeeper constructor connects to ZooKeeper and 680 // may trigger the Connected event immediately. So, if we register the 681 // watcher after constructing ZooKeeper, we may miss that event. Instead, 682 // we construct the watcher first, and have it block any events it receives 683 // before we can set its ZooKeeper reference. 684 watcher = new WatcherWithClientRef(); 685 ZooKeeper zk = createZooKeeper(); 686 watcher.setZooKeeperRef(zk); 687 688 // Wait for the asynchronous success/failure. This may throw an exception 689 // if we don't connect within the session timeout. 690 watcher.waitForZKConnectionEvent(zkSessionTimeout); 691 692 for (ZKAuthInfo auth : zkAuthInfo) { 693 zk.addAuthInfo(auth.getScheme(), auth.getAuth()); 694 } 695 return zk; 696 } 697 698 /** 699 * Get a new zookeeper client instance. protected so that test class can 700 * inherit and pass in a mock object for zookeeper 701 * 702 * @return new zookeeper client instance 703 * @throws IOException 704 */ 705 protected ZooKeeper createZooKeeper() throws IOException { 706 return new ZooKeeper(zkHostPort, zkSessionTimeout, watcher); 707 } 708 709 private void fatalError(String errorMessage) { 710 LOG.fatal(errorMessage); 711 reset(); 712 appClient.notifyFatalError(errorMessage); 713 } 714 715 private void monitorActiveStatus() { 716 assert wantToBeInElection; 717 if (LOG.isDebugEnabled()) { 718 LOG.debug("Monitoring active leader for " + this); 719 } 720 statRetryCount = 0; 721 monitorLockNodeAsync(); 722 } 723 724 private void joinElectionInternal() { 725 Preconditions.checkState(appData != null, 726 "trying to join election without any app data"); 727 if (zkClient == null) { 728 if (!reEstablishSession()) { 729 fatalError("Failed to reEstablish connection with ZooKeeper"); 730 return; 731 } 732 } 733 734 createRetryCount = 0; 735 wantToBeInElection = true; 736 createLockNodeAsync(); 737 } 738 739 private void reJoinElection(int sleepTime) { 740 LOG.info("Trying to re-establish ZK session"); 741 742 // Some of the test cases rely on expiring the ZK sessions and 743 // ensuring that the other node takes over. But, there's a race 744 // where the original lease holder could reconnect faster than the other 745 // thread manages to take the lock itself. This lock allows the 746 // tests to block the reconnection. It's a shame that this leaked 747 // into non-test code, but the lock is only acquired here so will never 748 // be contended. 749 sessionReestablishLockForTests.lock(); 750 try { 751 terminateConnection(); 752 sleepFor(sleepTime); 753 // Should not join election even before the SERVICE is reported 754 // as HEALTHY from ZKFC monitoring. 755 if (appData != null) { 756 joinElectionInternal(); 757 } else { 758 LOG.info("Not joining election since service has not yet been " + 759 "reported as healthy."); 760 } 761 } finally { 762 sessionReestablishLockForTests.unlock(); 763 } 764 } 765 766 /** 767 * Sleep for the given number of milliseconds. 768 * This is non-static, and separated out, so that unit tests 769 * can override the behavior not to sleep. 770 */ 771 @VisibleForTesting 772 protected void sleepFor(int sleepMs) { 773 if (sleepMs > 0) { 774 try { 775 Thread.sleep(sleepMs); 776 } catch (InterruptedException e) { 777 Thread.currentThread().interrupt(); 778 } 779 } 780 } 781 782 @VisibleForTesting 783 void preventSessionReestablishmentForTests() { 784 sessionReestablishLockForTests.lock(); 785 } 786 787 @VisibleForTesting 788 void allowSessionReestablishmentForTests() { 789 sessionReestablishLockForTests.unlock(); 790 } 791 792 @VisibleForTesting 793 synchronized long getZKSessionIdForTests() { 794 if (zkClient != null) { 795 return zkClient.getSessionId(); 796 } else { 797 return -1; 798 } 799 } 800 801 @VisibleForTesting 802 synchronized State getStateForTests() { 803 return state; 804 } 805 806 @VisibleForTesting 807 synchronized boolean isMonitorLockNodePending() { 808 return monitorLockNodePending; 809 } 810 811 private boolean reEstablishSession() { 812 int connectionRetryCount = 0; 813 boolean success = false; 814 while(!success && connectionRetryCount < maxRetryNum) { 815 if (LOG.isDebugEnabled()) { 816 LOG.debug("Establishing zookeeper connection for " + this); 817 } 818 try { 819 createConnection(); 820 success = true; 821 } catch(IOException e) { 822 LOG.warn(e); 823 sleepFor(5000); 824 } catch(KeeperException e) { 825 LOG.warn(e); 826 sleepFor(5000); 827 } 828 ++connectionRetryCount; 829 } 830 return success; 831 } 832 833 private void createConnection() throws IOException, KeeperException { 834 if (zkClient != null) { 835 try { 836 zkClient.close(); 837 } catch (InterruptedException e) { 838 throw new IOException("Interrupted while closing ZK", 839 e); 840 } 841 zkClient = null; 842 watcher = null; 843 } 844 zkClient = connectToZooKeeper(); 845 if (LOG.isDebugEnabled()) { 846 LOG.debug("Created new connection for " + this); 847 } 848 } 849 850 @InterfaceAudience.Private 851 public synchronized void terminateConnection() { 852 if (zkClient == null) { 853 return; 854 } 855 if (LOG.isDebugEnabled()) { 856 LOG.debug("Terminating ZK connection for " + this); 857 } 858 ZooKeeper tempZk = zkClient; 859 zkClient = null; 860 watcher = null; 861 try { 862 tempZk.close(); 863 } catch(InterruptedException e) { 864 LOG.warn(e); 865 } 866 zkConnectionState = ConnectionState.TERMINATED; 867 wantToBeInElection = false; 868 } 869 870 private void reset() { 871 state = State.INIT; 872 terminateConnection(); 873 } 874 875 private boolean becomeActive() { 876 assert wantToBeInElection; 877 if (state == State.ACTIVE) { 878 // already active 879 return true; 880 } 881 try { 882 Stat oldBreadcrumbStat = fenceOldActive(); 883 writeBreadCrumbNode(oldBreadcrumbStat); 884 885 if (LOG.isDebugEnabled()) { 886 LOG.debug("Becoming active for " + this); 887 } 888 appClient.becomeActive(); 889 state = State.ACTIVE; 890 return true; 891 } catch (Exception e) { 892 LOG.warn("Exception handling the winning of election", e); 893 // Caller will handle quitting and rejoining the election. 894 return false; 895 } 896 } 897 898 /** 899 * Write the "ActiveBreadCrumb" node, indicating that this node may need 900 * to be fenced on failover. 901 * @param oldBreadcrumbStat 902 */ 903 private void writeBreadCrumbNode(Stat oldBreadcrumbStat) 904 throws KeeperException, InterruptedException { 905 Preconditions.checkState(appData != null, "no appdata"); 906 907 LOG.info("Writing znode " + zkBreadCrumbPath + 908 " to indicate that the local node is the most recent active..."); 909 if (oldBreadcrumbStat == null) { 910 // No previous active, just create the node 911 createWithRetries(zkBreadCrumbPath, appData, zkAcl, 912 CreateMode.PERSISTENT); 913 } else { 914 // There was a previous active, update the node 915 setDataWithRetries(zkBreadCrumbPath, appData, oldBreadcrumbStat.getVersion()); 916 } 917 } 918 919 /** 920 * Try to delete the "ActiveBreadCrumb" node when gracefully giving up 921 * active status. 922 * If this fails, it will simply warn, since the graceful release behavior 923 * is only an optimization. 924 */ 925 private void tryDeleteOwnBreadCrumbNode() { 926 assert state == State.ACTIVE; 927 LOG.info("Deleting bread-crumb of active node..."); 928 929 // Sanity check the data. This shouldn't be strictly necessary, 930 // but better to play it safe. 931 Stat stat = new Stat(); 932 byte[] data = null; 933 try { 934 data = zkClient.getData(zkBreadCrumbPath, false, stat); 935 936 if (!Arrays.equals(data, appData)) { 937 throw new IllegalStateException( 938 "We thought we were active, but in fact " + 939 "the active znode had the wrong data: " + 940 StringUtils.byteToHexString(data) + " (stat=" + stat + ")"); 941 } 942 943 deleteWithRetries(zkBreadCrumbPath, stat.getVersion()); 944 } catch (Exception e) { 945 LOG.warn("Unable to delete our own bread-crumb of being active at " + 946 zkBreadCrumbPath + ": " + e.getLocalizedMessage() + ". " + 947 "Expecting to be fenced by the next active."); 948 } 949 } 950 951 /** 952 * If there is a breadcrumb node indicating that another node may need 953 * fencing, try to fence that node. 954 * @return the Stat of the breadcrumb node that was read, or null 955 * if no breadcrumb node existed 956 */ 957 private Stat fenceOldActive() throws InterruptedException, KeeperException { 958 final Stat stat = new Stat(); 959 byte[] data; 960 LOG.info("Checking for any old active which needs to be fenced..."); 961 try { 962 data = zkDoWithRetries(new ZKAction<byte[]>() { 963 @Override 964 public byte[] run() throws KeeperException, InterruptedException { 965 return zkClient.getData(zkBreadCrumbPath, false, stat); 966 } 967 }); 968 } catch (KeeperException ke) { 969 if (isNodeDoesNotExist(ke.code())) { 970 LOG.info("No old node to fence"); 971 return null; 972 } 973 974 // If we failed to read for any other reason, then likely we lost 975 // our session, or we don't have permissions, etc. In any case, 976 // we probably shouldn't become active, and failing the whole 977 // thing is the best bet. 978 throw ke; 979 } 980 981 LOG.info("Old node exists: " + StringUtils.byteToHexString(data)); 982 if (Arrays.equals(data, appData)) { 983 LOG.info("But old node has our own data, so don't need to fence it."); 984 } else { 985 appClient.fenceOldActive(data); 986 } 987 return stat; 988 } 989 990 private void becomeStandby() { 991 if (state != State.STANDBY) { 992 if (LOG.isDebugEnabled()) { 993 LOG.debug("Becoming standby for " + this); 994 } 995 state = State.STANDBY; 996 appClient.becomeStandby(); 997 } 998 } 999 1000 private void enterNeutralMode() { 1001 if (state != State.NEUTRAL) { 1002 if (LOG.isDebugEnabled()) { 1003 LOG.debug("Entering neutral mode for " + this); 1004 } 1005 state = State.NEUTRAL; 1006 appClient.enterNeutralMode(); 1007 } 1008 } 1009 1010 private void createLockNodeAsync() { 1011 zkClient.create(zkLockFilePath, appData, zkAcl, CreateMode.EPHEMERAL, 1012 this, zkClient); 1013 } 1014 1015 private void monitorLockNodeAsync() { 1016 if (monitorLockNodePending && monitorLockNodeClient == zkClient) { 1017 LOG.info("Ignore duplicate monitor lock-node request."); 1018 return; 1019 } 1020 monitorLockNodePending = true; 1021 monitorLockNodeClient = zkClient; 1022 zkClient.exists(zkLockFilePath, 1023 watcher, this, 1024 zkClient); 1025 } 1026 1027 private String createWithRetries(final String path, final byte[] data, 1028 final List<ACL> acl, final CreateMode mode) 1029 throws InterruptedException, KeeperException { 1030 return zkDoWithRetries(new ZKAction<String>() { 1031 @Override 1032 public String run() throws KeeperException, InterruptedException { 1033 return zkClient.create(path, data, acl, mode); 1034 } 1035 }); 1036 } 1037 1038 private byte[] getDataWithRetries(final String path, final boolean watch, 1039 final Stat stat) throws InterruptedException, KeeperException { 1040 return zkDoWithRetries(new ZKAction<byte[]>() { 1041 @Override 1042 public byte[] run() throws KeeperException, InterruptedException { 1043 return zkClient.getData(path, watch, stat); 1044 } 1045 }); 1046 } 1047 1048 private Stat setDataWithRetries(final String path, final byte[] data, 1049 final int version) throws InterruptedException, KeeperException { 1050 return zkDoWithRetries(new ZKAction<Stat>() { 1051 @Override 1052 public Stat run() throws KeeperException, InterruptedException { 1053 return zkClient.setData(path, data, version); 1054 } 1055 }); 1056 } 1057 1058 private void deleteWithRetries(final String path, final int version) 1059 throws KeeperException, InterruptedException { 1060 zkDoWithRetries(new ZKAction<Void>() { 1061 @Override 1062 public Void run() throws KeeperException, InterruptedException { 1063 zkClient.delete(path, version); 1064 return null; 1065 } 1066 }); 1067 } 1068 1069 private <T> T zkDoWithRetries(ZKAction<T> action) throws KeeperException, 1070 InterruptedException { 1071 int retry = 0; 1072 while (true) { 1073 try { 1074 return action.run(); 1075 } catch (KeeperException ke) { 1076 if (shouldRetry(ke.code()) && ++retry < maxRetryNum) { 1077 continue; 1078 } 1079 throw ke; 1080 } 1081 } 1082 } 1083 1084 private interface ZKAction<T> { 1085 T run() throws KeeperException, InterruptedException; 1086 } 1087 1088 /** 1089 * The callbacks and watchers pass a reference to the ZK client 1090 * which made the original call. We don't want to take action 1091 * based on any callbacks from prior clients after we quit 1092 * the election. 1093 * @param ctx the ZK client passed into the watcher 1094 * @return true if it matches the current client 1095 */ 1096 private synchronized boolean isStaleClient(Object ctx) { 1097 Preconditions.checkNotNull(ctx); 1098 if (zkClient != (ZooKeeper)ctx) { 1099 LOG.warn("Ignoring stale result from old client with sessionId " + 1100 String.format("0x%08x", ((ZooKeeper)ctx).getSessionId())); 1101 return true; 1102 } 1103 return false; 1104 } 1105 1106 /** 1107 * Watcher implementation which keeps a reference around to the 1108 * original ZK connection, and passes it back along with any 1109 * events. 1110 */ 1111 private final class WatcherWithClientRef implements Watcher { 1112 private ZooKeeper zk; 1113 1114 /** 1115 * Latch fired whenever any event arrives. This is used in order 1116 * to wait for the Connected event when the client is first created. 1117 */ 1118 private CountDownLatch hasReceivedEvent = new CountDownLatch(1); 1119 1120 /** 1121 * Latch used to wait until the reference to ZooKeeper is set. 1122 */ 1123 private CountDownLatch hasSetZooKeeper = new CountDownLatch(1); 1124 1125 /** 1126 * Waits for the next event from ZooKeeper to arrive. 1127 * 1128 * @param connectionTimeoutMs zookeeper connection timeout in milliseconds 1129 * @throws KeeperException if the connection attempt times out. This will 1130 * be a ZooKeeper ConnectionLoss exception code. 1131 * @throws IOException if interrupted while connecting to ZooKeeper 1132 */ 1133 private void waitForZKConnectionEvent(int connectionTimeoutMs) 1134 throws KeeperException, IOException { 1135 try { 1136 if (!hasReceivedEvent.await(connectionTimeoutMs, TimeUnit.MILLISECONDS)) { 1137 LOG.error("Connection timed out: couldn't connect to ZooKeeper in " 1138 + connectionTimeoutMs + " milliseconds"); 1139 zk.close(); 1140 throw KeeperException.create(Code.CONNECTIONLOSS); 1141 } 1142 } catch (InterruptedException e) { 1143 Thread.currentThread().interrupt(); 1144 throw new IOException( 1145 "Interrupted when connecting to zookeeper server", e); 1146 } 1147 } 1148 1149 private void setZooKeeperRef(ZooKeeper zk) { 1150 Preconditions.checkState(this.zk == null, 1151 "zk already set -- must be set exactly once"); 1152 this.zk = zk; 1153 hasSetZooKeeper.countDown(); 1154 } 1155 1156 @Override 1157 public void process(WatchedEvent event) { 1158 hasReceivedEvent.countDown(); 1159 try { 1160 if (!hasSetZooKeeper.await(zkSessionTimeout, TimeUnit.MILLISECONDS)) { 1161 LOG.debug("Event received with stale zk"); 1162 } 1163 ActiveStandbyElector.this.processWatchEvent( 1164 zk, event); 1165 } catch (Throwable t) { 1166 fatalError( 1167 "Failed to process watcher event " + event + ": " + 1168 StringUtils.stringifyException(t)); 1169 } 1170 } 1171 } 1172 1173 private static boolean isSuccess(Code code) { 1174 return (code == Code.OK); 1175 } 1176 1177 private static boolean isNodeExists(Code code) { 1178 return (code == Code.NODEEXISTS); 1179 } 1180 1181 private static boolean isNodeDoesNotExist(Code code) { 1182 return (code == Code.NONODE); 1183 } 1184 1185 private static boolean isSessionExpired(Code code) { 1186 return (code == Code.SESSIONEXPIRED); 1187 } 1188 1189 private static boolean shouldRetry(Code code) { 1190 return code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT; 1191 } 1192 1193 @Override 1194 public String toString() { 1195 return "elector id=" + System.identityHashCode(this) + 1196 " appData=" + 1197 ((appData == null) ? "null" : StringUtils.byteToHexString(appData)) + 1198 " cb=" + appClient; 1199 } 1200 1201 public String getHAZookeeperConnectionState() { 1202 return this.zkConnectionState.name(); 1203 } 1204}


else
keywords in if
—else
statements and statement chains.
The else
keyword is redundant when all previous branches end with a
return
, throw
, break
, or continue
statement. In this case,
the statements from the else
branch can be placed after the if
statement, and the
else
keyword can be removed.
Example:
if (name == null) {
throw new IllegalArgumentException();
} else {
System.out.println(name);
}
After the quick-fix is applied:
if (name == null) {
throw new IllegalArgumentException();
}
System.out.println(name);
Disable the Report when there are no more statements after the 'if' statement option to ignore cases where the if
—else
statement is the last statement in a code block.


Nation: (de: German)
Nation: (en: English)
Nation: (es: Spanish)
Nation: (pt-br: Brazilian Portuguese)
Nation: (ja: Japanese)
Send


Sweet Free Stuff
///////////////////////////////////////////
Free Renew Life Digest Smart High-Potency Digestive Enzyme Sample
Posted: 12 Jul 2016 10:24 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Free Renew Life Digest Smart High-Potency Digestive Enzyme Sample
The post Free Renew Life Digest Smart High-Potency Digestive Enzyme Sample
appeared first on SweetFreeStuff.com.
///////////////////////////////////////////
Free Mighty Leaf Tea Green Tea Tropical Pyramid Pouch Sample
Posted: 12 Jul 2016 08:25 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Free Mighty Leaf Tea Green Tea Tropical Pyramid Pouch Sample
The post Free Mighty Leaf Tea Green Tea Tropical Pyramid Pouch Sample
appeared first on SweetFreeStuff.com.
///////////////////////////////////////////
Free 2-Week Sample of La Prairie Anti-Aging Rapid Response Booster
Posted: 12 Jul 2016 07:28 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Scroll down and enter your e-mail address to receive a coupon for a Free
2-Week Sample of La Prairie Anti-Aging Rapid Response Booster
The post Free 2-Week Sample of La Prairie Anti-Aging Rapid Response Booster
appeared first on SweetFreeStuff.com.
///////////////////////////////////////////
Free Subscription to Diabetes Self-Management
Posted: 12 Jul 2016 07:14 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Free Subscription to Diabetes Self-Management
The post Free Subscription to Diabetes Self-Management appeared first on
SweetFreeStuff.com.
///////////////////////////////////////////
Free Asako Premium Detox Foot Pads
Posted: 12 Jul 2016 06:32 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Free Asako Premium Detox Foot Pads
The post Free Asako Premium Detox Foot Pads appeared first on
SweetFreeStuff.com.
///////////////////////////////////////////
Free Entree at Chick-fil-A on 7/12
Posted: 12 Jul 2016 01:12 AM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
This year we’ve simplified the reward. In order to make the award more
consistent for all and provide the highest guest experience, anyone wearing
anything cowlike will receive a free entrée! Free Entree at Chick-fil-A on
7/12
The post Free Entree at Chick-fil-A on 7/12 appeared first on
SweetFreeStuff.com.
///////////////////////////////////////////
Free Breathe Right Nasal Strips
Posted: 11 Jul 2016 11:00 PM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Free Breathe Right Nasal Strips
The post Free Breathe Right Nasal Strips appeared first on
SweetFreeStuff.com.
CommentsI got my samples fast and I love these lavender strips by Michelle
JohnsonI got my samples fast thank you very much by AutumnThank you! by
Tammy EdmondsOh really I ordered mine months ago and they still have
not … by BBThey work great!! And were shipped within 4 days! by JuliaPlus
8 more…
///////////////////////////////////////////
Inbox Dollars - Free $5
Posted: 11 Jul 2016 11:00 PM PDT
http://feedproxy.google.com/&edburner&utm_medium=email
Inbox Dollars pays members to read emails, take surveys online, play games,
and go shopping! New enrollees also receive $5 just for joining! Inbox
Dollars Free $5 for signing up
The post Inbox Dollars Free $5 appeared first on SweetFreeStuff.com.


| Class and Description |
|---|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| Class and Description |
|---|
| ESignatureInput |
| IAttachment |
| ItemProfile
An item profile is a set of properties of an item type.
|
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| IWorkItemReferences
References from a work item to other items and artifacts.
|
| IWorkItemType
Represents a work item's type.
|
| MultiStaleDataException |
| Class and Description |
|---|
| CategoryId
The identifier of a category, unique within a project area.
|
| IAttachment |
| IAttachmentHandle
An attachment handle is used for referencing an attachmen by item id
or a particular state of an attachment by item and state ids.
|
| IAttribute
An attribute of a work item.
|
| IAttributeHandle
An attribute handle is used for referencing an attribute by item id
or a particular state of an attribute by item and state ids.
|
| ICategory
A category of work items.
|
| ICategoryHandle
A category handle is used for referencing a category by item id
or a particular state of a category by item and state ids.
|
| IDeliverable
A deliverable represents a release like a milestone or final release.
|
| IDeliverableHandle
A deliverable handle is used for referencing a deliverable by item id
or a particular state of a deliverable by item and state ids.
|
| Identifier
Identifier of an artifact of a particular type.
|
| IEnumeration
Represents an enumeration.
|
| ILiteral
Represents a literal of an enumeration.
|
| ItemProfile
An item profile is a set of properties of an item type.
|
| ITimeSheetEntry |
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| IWorkItemReferences
References from a work item to other items and artifacts.
|
| IWorkItemType
Represents a work item's type.
|
| Class and Description |
|---|
| AttributeOperation |
| IAttribute
An attribute of a work item.
|
| ItemProfile
An item profile is a set of properties of an item type.
|
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| Class and Description |
|---|
| ICategoryHandle
A category handle is used for referencing a category by item id
or a particular state of a category by item and state ids.
|
| Class and Description |
|---|
| AttributeType |
| Class and Description |
|---|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| Class and Description |
|---|
| AttributeDescriptor |
| AttributeOperation |
| AttributeType |
| CategoryId
The identifier of a category, unique within a project area.
|
| Duration
Represents a duration in milliseconds.
|
| DurationFormat
Formats a duration object into a consumable String.
|
| DurationFormat.Units |
| IApproval
An approval from a single contributor with a particular state.
|
| IApprovalDescriptor
An approval descriptor aggregates approvals from contributors.
|
| IApprovals
Operations for organizing approvals of a particular work item.
|
| IApprovalState
The state of an approval.
|
| IApprovalType
The type of an approval.
|
| IAttachment |
| IAttachmentHandle
An attachment handle is used for referencing an attachmen by item id
or a particular state of an attachment by item and state ids.
|
| IAttribute
An attribute of a work item.
|
| IAttributeHandle
An attribute handle is used for referencing an attribute by item id
or a particular state of an attribute by item and state ids.
|
| ICategory
A category of work items.
|
| ICategoryHandle
A category handle is used for referencing a category by item id
or a particular state of a category by item and state ids.
|
| IComment
A comment of a work item.
|
| IComments
Comments of a work item.
|
| IDeliverable
A deliverable represents a release like a milestone or final release.
|
| IDeliverableHandle
A deliverable handle is used for referencing a deliverable by item id
or a particular state of a deliverable by item and state ids.
|
| Identifier
Identifier of an artifact of a particular type.
|
| ILiteral
Represents a literal of an enumeration.
|
| IPriority
Represents a priority.
|
| IProjectScoped
Represents an item that is scoped to a project area
|
| IResolution |
| ISeverity
Represents a severity.
|
| IState |
| IStateTransition |
| IStateTransitions |
| ISubscriptions
Subscribers of a work item.
|
| ItemProfile
An item profile is a set of properties of an item type.
|
| ITimeSheetEntry |
| ITimeSheetEntryHandle |
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| IWorkItemHandle
A work item handle is used for referencing a work item by item id
or a particular state of a work item by item and state ids.
|
| ListChangeDetails |
| Class and Description |
|---|
| IProjectScoped
Represents an item that is scoped to a project area
|
| ItemProfile
An item profile is a set of properties of an item type.
|
| Class and Description |
|---|
| IAttribute
An attribute of a work item.
|
| Identifier
Identifier of an artifact of a particular type.
|
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| Class and Description |
|---|
| IAttachment |
| IWorkItem
A work item represents a piece of work in a certain state (like new, in progress, done).
|
| Class and Description |
|---|
| Identifier
Identifier of an artifact of a particular type.
|
| IResolution |
| IState |


Here is an eminent seismologist pointing at seismological data with a small stick, or possibly a pencil. Not only is this seismologist a key member of the splendidly-named Colorado Earthquake Hazard Mitigation Council, he is also Director of the U.S. Geological Survey's National Earthquake Information Centre, based in Denver. I draw him to your attention for a particular reason. It was this seismologist who, after the Boxing Day tsunami, criticised the lack of an early warning system of tide gauges that could have saved thousands of lives. And his name is Waverly Person.
Thanks to Ed Baxter


What are some good gluten free banana bread recipes?
What kind of pan should I use to sear steak?
What are some must have knives??
Does anyone have a vegetarian jambalaya recipe?
Tips for doubling a recipe?
Tammy B.
If you have a vanilla bean, you can use it as a substitute for extract in a cookie recipe. You can scoop out the seeds of the vanilla bean and add them to melted butter or any other liquid ingredient. Or, you can whisk the beans into the sugar.
Tom W.
I love using vanilla bean rather than extract.
Kim L.
Me too! Much stronger more natural taste!
Me


If you like African djembe percussion and would like a workout that is real short and powerful check out the video below:


Attn. This mail may come as a surprise to you for we have neither met nor seen each other. I got your contact through a discrete enquiry. I am Mr. chuks ohakanu of the First National Bank of South Africa,here in Johannesburg. I work with the Audit department as an Executive Accountant. I am married with three kids. I discovered an account that has been dormant for the last seven years, In the course of my work. I further investigated the account and found out that the owner,Mr Arthur Dennis,from German,died in an auto crash on 7th May,1998. On subsequent inquiries,I learnt that he died without a heir or a will. If unclaimed,these funds(Sixteen Million U.S Dollars,$16m) shall be recovered by the Government as unclaimed funds. I am offering to do this business with you based on the records you possess as honest And God fearing.. All you are required to do is present an account where this money shall be moved.An already existing account can serve or you set up a new one for this.to the deceased.I shall process all required legal documents to buttress this. The bank shall release the funds to you,then I shall come over to your country for the sharing of the money and subsequent investment potentials. If you are interested,do not hesitate to mail me, so that I can give you the next step.I am offering you 35% of this money.5% for charity and 5% to cover any expenses either parties may have incurred during the course of this transaction. I shall then keep 55% to myself for investment purposes. I am earnestly waiting for your response and believing that you shall not let me down during or after this transaction. Regards, Mr. chuks ohakanu