embulk-input-http 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0fd9d1c50eb1690a68282058341d8dfe47c2706
4
- data.tar.gz: e64cca9c2d869cbd0a107331aad8ca91e4187441
3
+ metadata.gz: 0be0755b221d7db3033f0c0193aecddb989ab46f
4
+ data.tar.gz: 1d027f7fca3f58087e93bc922a271f0126864842
5
5
  SHA512:
6
- metadata.gz: 72182010d6bb889cc86203e466d1cdcf35a64d47e4e38167414b7d204320558e7659aa3c09c0448e679c10a41a6e9884b278e63b545a4271d3b6f10571222760
7
- data.tar.gz: cfe7a025c51abc126e4837578815b2139f7c938a0ee41e32f3a5d919589fdb1f03d18c61fa768b299bcd6d2258154dc764ff768c700de6c59c966d14e77bd782
6
+ metadata.gz: 1f520102f0a5df0f642e9789e3fe035b6007e2bef3bfc3a3d57d816c7989e80dac7e6340455b4cfd802150033ac5b9c7737e3914da5aff7cfedd4dd96b631938
7
+ data.tar.gz: ace4460f528b39465978d4753492ab9ad259c33cd3c7373df1d5583907592d3a067cf6052ed9b5d87156a0fc4d10b92e4eaf7240a4d3ceb27bdda451f736110b
data/build.gradle CHANGED
@@ -12,12 +12,12 @@ configurations {
12
12
  provided
13
13
  }
14
14
 
15
- version = "0.0.6"
15
+ version = "0.0.7"
16
16
 
17
17
  dependencies {
18
- compile "org.embulk:embulk-core:0.5.2"
18
+ compile "org.embulk:embulk-core:0.6.8"
19
19
  compile "org.apache.httpcomponents:httpclient:4.4"
20
- provided "org.embulk:embulk-core:0.5.2"
20
+ provided "org.embulk:embulk-core:0.6.8"
21
21
  testCompile "junit:junit:4.+"
22
22
  }
23
23
 
@@ -4,14 +4,11 @@ import com.google.common.base.Optional;
4
4
  import com.google.common.base.Throwables;
5
5
  import com.google.common.collect.Lists;
6
6
  import org.apache.http.Header;
7
- import org.apache.http.HttpException;
8
- import org.apache.http.HttpResponse;
9
7
  import org.apache.http.NameValuePair;
10
8
  import org.apache.http.auth.AuthScope;
11
9
  import org.apache.http.auth.UsernamePasswordCredentials;
12
10
  import org.apache.http.client.CredentialsProvider;
13
11
  import org.apache.http.client.HttpClient;
14
- import org.apache.http.client.HttpRequestRetryHandler;
15
12
  import org.apache.http.client.config.RequestConfig;
16
13
  import org.apache.http.client.entity.UrlEncodedFormEntity;
17
14
  import org.apache.http.client.methods.HttpGet;
@@ -22,13 +19,13 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
22
19
  import org.apache.http.impl.client.HttpClientBuilder;
23
20
  import org.apache.http.message.BasicHeader;
24
21
  import org.apache.http.message.BasicNameValuePair;
25
- import org.apache.http.util.EntityUtils;
26
22
  import org.embulk.config.*;
27
23
  import org.embulk.spi.BufferAllocator;
28
24
  import org.embulk.spi.Exec;
29
25
  import org.embulk.spi.FileInputPlugin;
30
26
  import org.embulk.spi.TransactionalFileInput;
31
27
  import org.embulk.spi.util.InputStreamFileInput;
28
+ import org.embulk.spi.util.RetryExecutor;
32
29
  import org.slf4j.Logger;
33
30
 
34
31
  import java.io.IOException;
@@ -162,16 +159,10 @@ public class HttpInputPlugin implements FileInputPlugin {
162
159
  }
163
160
 
164
161
  HttpClientBuilder builder = HttpClientBuilder.create()
162
+ .disableAutomaticRetries()
165
163
  .setDefaultRequestConfig(makeRequestConfig(task))
166
164
  .setDefaultHeaders(makeHeaders(task));
167
165
 
168
- if (task.getMaxRetries() > 0) {
169
- final int retry = task.getMaxRetries();
170
- final int interval = task.getRetryInterval();
171
- HttpRequestRetryHandler retryHandler = new RetryHandler(retry, interval);
172
- builder.setRetryHandler(retryHandler);
173
- }
174
-
175
166
  if (task.getBasicAuth().isPresent()) {
176
167
  builder.setDefaultCredentialsProvider(makeCredentialsProvider(task.getBasicAuth().get(),
177
168
  request));
@@ -179,28 +170,28 @@ public class HttpInputPlugin implements FileInputPlugin {
179
170
 
180
171
  HttpClient client = builder.build();
181
172
 
182
- if (task.getSleepBeforeRequest() > 0) {
183
- try {
184
- logger.info(String.format("Waiting %d msec ...", task.getSleepBeforeRequest()));
185
- Thread.sleep(task.getSleepBeforeRequest());
186
- } catch (InterruptedException e) {
187
- }
188
- }
189
-
190
173
  logger.info(String.format("%s \"%s\"", task.getMethod().toUpperCase(),
191
174
  request.getURI().toString()));
175
+
176
+ RetryableHandler retryable = new RetryableHandler(client, request);
192
177
  try {
193
- HttpResponse response = client.execute(request);
194
- statusIsOkOrThrow(response);
195
- //final String body = EntityUtils.toString(response.getEntity());
196
- //InputStream stream = new ByteArrayInputStream(body.getBytes());
197
- InputStream stream = response.getEntity().getContent();
178
+ if (task.getSleepBeforeRequest() > 0) {
179
+ logger.info(String.format("wait %d msec before request", task.getSleepBeforeRequest()));
180
+ Thread.sleep(task.getSleepBeforeRequest());
181
+ }
182
+ RetryExecutor.retryExecutor().
183
+ withRetryLimit(task.getMaxRetries()).
184
+ withInitialRetryWait(task.getRetryInterval()).
185
+ withMaxRetryWait(30 * 60 * 1000).
186
+ runInterruptible(retryable);
187
+ InputStream stream = retryable.getResponse().getEntity().getContent();
198
188
  PluginFileInput input = new PluginFileInput(task, stream);
199
189
  stream = null;
200
190
  return input;
201
- } catch (IOException | HttpException e) {
191
+ } catch (Exception e) {
202
192
  throw Throwables.propagate(e);
203
193
  }
194
+
204
195
  }
205
196
 
206
197
  private CredentialsProvider makeCredentialsProvider(BasicAuthConfig config, HttpRequestBase scopeRequest) {
@@ -264,18 +255,6 @@ public class HttpInputPlugin implements FileInputPlugin {
264
255
  .build();
265
256
  }
266
257
 
267
- private void statusIsOkOrThrow(HttpResponse response)
268
- throws HttpException, IOException {
269
- int code = response.getStatusLine().getStatusCode();
270
- switch (response.getStatusLine().getStatusCode()) {
271
- case 200:
272
- return;
273
- default:
274
- throw new HttpException(String.format("Request is not successful, code=%d, body=%s",
275
- code, EntityUtils.toString(response.getEntity())));
276
- }
277
- }
278
-
279
258
  public static class PluginFileInput extends InputStreamFileInput
280
259
  implements TransactionalFileInput {
281
260
 
@@ -0,0 +1,90 @@
1
+ package org.embulk.input;
2
+
3
+ import com.google.common.collect.ImmutableList;
4
+ import org.apache.http.HttpException;
5
+ import org.apache.http.HttpResponse;
6
+ import org.apache.http.client.HttpClient;
7
+ import org.apache.http.client.methods.HttpRequestBase;
8
+ import org.apache.http.util.EntityUtils;
9
+ import org.embulk.spi.Exec;
10
+ import org.embulk.spi.util.RetryExecutor;
11
+ import org.slf4j.Logger;
12
+
13
+ import javax.net.ssl.SSLException;
14
+ import java.io.IOException;
15
+ import java.io.InterruptedIOException;
16
+ import java.net.UnknownHostException;
17
+ import java.util.List;
18
+
19
+ public class RetryableHandler implements RetryExecutor.Retryable {
20
+
21
+ protected final Logger logger = Exec.getLogger(getClass());
22
+
23
+ private static List<Class<? extends IOException>> NOT_RETRIABLE_CLAASSES;
24
+
25
+ private final HttpClient client;
26
+ private final HttpRequestBase request;
27
+ private HttpResponse response;
28
+
29
+ static {
30
+ ImmutableList.Builder<Class<? extends IOException>> classes = ImmutableList.builder();
31
+ classes.add(UnknownHostException.class).
32
+ add(InterruptedIOException.class).
33
+ add(SSLException.class);
34
+ NOT_RETRIABLE_CLAASSES = classes.build();
35
+ }
36
+
37
+ public RetryableHandler(HttpClient client, HttpRequestBase request) {
38
+ this.client = client;
39
+ this.request = request;
40
+ }
41
+
42
+ public HttpResponse getResponse() {
43
+ return response;
44
+ }
45
+
46
+ @Override
47
+ public Object call() throws Exception {
48
+ if (response != null) throw new IllegalStateException("response is already set");
49
+ HttpResponse response = client.execute(request);
50
+ statusIsOkOrThrow(response);
51
+ this.response = response;
52
+ return null;
53
+ }
54
+
55
+ @Override
56
+ public boolean isRetryableException(Exception exception) {
57
+ if (NOT_RETRIABLE_CLAASSES.contains(exception.getClass())) {
58
+ logger.error(String.format("'%s' is not retriable", exception.getClass()));
59
+ return false;
60
+ }
61
+ return true;
62
+ }
63
+
64
+ @Override
65
+ public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait)
66
+ throws RetryExecutor.RetryGiveupException {
67
+ logger.warn("retrying {}/{} after {} seconds. Message: {}",
68
+ retryCount, retryLimit, retryWait / 1000,
69
+ exception.getMessage());
70
+ }
71
+
72
+ @Override
73
+ public void onGiveup(Exception firstException, Exception lastException)
74
+ throws RetryExecutor.RetryGiveupException {
75
+ logger.error("giveup {}", lastException.getMessage());
76
+ }
77
+
78
+ protected void statusIsOkOrThrow(HttpResponse response)
79
+ throws HttpException, IOException {
80
+ int code = response.getStatusLine().getStatusCode();
81
+ switch (response.getStatusLine().getStatusCode()) {
82
+ case 200:
83
+ return;
84
+ default:
85
+ throw new HttpException(String.format("Request is not successful, code=%d, body=%s",
86
+ code, EntityUtils.toString(response.getEntity())));
87
+ }
88
+ }
89
+
90
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-input-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takuma kanari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -59,15 +59,15 @@ files:
59
59
  - src/main/java/org/embulk/input/HttpInputPlugin.java
60
60
  - src/main/java/org/embulk/input/ParamsConfig.java
61
61
  - src/main/java/org/embulk/input/QueryConfig.java
62
- - src/main/java/org/embulk/input/RetryHandler.java
62
+ - src/main/java/org/embulk/input/RetryableHandler.java
63
63
  - src/test/java/org/embulk/input/TestHttpInputPlugin.java
64
64
  - src/test/java/org/embulk/input/TestParamsConfig.java
65
65
  - src/test/java/org/embulk/input/TestQueryConfig.java
66
- - classpath/httpclient-4.4.jar
66
+ - classpath/commons-codec-1.9.jar
67
67
  - classpath/commons-logging-1.2.jar
68
+ - classpath/embulk-input-http-0.0.7.jar
69
+ - classpath/httpclient-4.4.jar
68
70
  - classpath/httpcore-4.4.jar
69
- - classpath/commons-codec-1.9.jar
70
- - classpath/embulk-input-http-0.0.6.jar
71
71
  homepage: https://github.com/takumakanari/embulk-input-http
72
72
  licenses:
73
73
  - MIT
@@ -1,44 +0,0 @@
1
- package org.embulk.input;
2
-
3
- import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
4
- import org.apache.http.protocol.HttpContext;
5
- import org.embulk.spi.Exec;
6
- import org.slf4j.Logger;
7
-
8
- import javax.net.ssl.SSLException;
9
- import java.io.IOException;
10
- import java.net.ConnectException;
11
- import java.net.UnknownHostException;
12
- import java.util.Arrays;
13
-
14
- public class RetryHandler extends DefaultHttpRequestRetryHandler
15
- {
16
-
17
- private final Logger logger = Exec.getLogger(getClass());
18
-
19
- private int interval = 0;
20
-
21
- public RetryHandler(int retry, int interval)
22
- {
23
- super(retry, true, Arrays.asList(
24
- UnknownHostException.class,
25
- ConnectException.class,
26
- SSLException.class));
27
- this.interval = interval;
28
- }
29
-
30
- @Override
31
- public boolean retryRequest(final IOException exception,
32
- final int executionCount, final HttpContext context)
33
- {
34
- final boolean isRetriable = super.retryRequest(exception, executionCount, context);
35
- if (isRetriable) {
36
- try {
37
- logger.info(String.format("Sleep %d msec before retry", interval));
38
- Thread.sleep(interval);
39
- } catch (InterruptedException e) {}
40
- }
41
- return isRetriable;
42
- }
43
-
44
- }