embulk-filter-copy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/README.md +5 -1
  4. data/build.gradle +6 -5
  5. data/example/config.yml +1 -0
  6. data/example/data.tsv +5 -5
  7. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  8. data/gradle/wrapper/gradle-wrapper.properties +1 -2
  9. data/gradlew +13 -10
  10. data/src/main/java/org/embulk/filter/copy/CopyFilterPlugin.java +26 -37
  11. data/src/main/java/org/embulk/filter/copy/forward/{ForwardBaseTask.java → ForwardParentTask.java} +1 -1
  12. data/src/main/java/org/embulk/filter/copy/forward/InForwardEventReader.java +67 -62
  13. data/src/main/java/org/embulk/filter/copy/forward/InForwardService.java +102 -35
  14. data/src/main/java/org/embulk/filter/copy/forward/OutForwardEventBuilder.java +66 -54
  15. data/src/main/java/org/embulk/filter/copy/forward/OutForwardService.java +2 -2
  16. data/src/main/java/org/embulk/filter/copy/plugin/InternalForwardInputPlugin.java +11 -13
  17. data/src/main/java/org/embulk/filter/copy/runner/AsyncEmbulkRunnerService.java +32 -0
  18. data/src/main/java/org/embulk/filter/copy/runner/EmbulkRunner.java +124 -0
  19. data/src/main/java/org/embulk/filter/copy/spi/DataBuilder.java +41 -0
  20. data/src/main/java/org/embulk/filter/copy/spi/DataReader.java +41 -0
  21. data/src/main/java/org/embulk/filter/copy/spi/ElapsedTime.java +81 -0
  22. data/src/main/java/org/embulk/filter/copy/spi/PageBuilder.java +16 -0
  23. data/src/main/java/org/embulk/filter/copy/spi/PageReader.java +13 -0
  24. data/src/main/java/org/embulk/filter/copy/{util → spi}/StandardColumnVisitor.java +6 -6
  25. data/src/test/java/org/embulk/filter/copy/TestCopyFilterPlugin.java +0 -11
  26. metadata +24 -16
  27. data/src/main/java/org/embulk/filter/copy/executor/EmbulkExecutor.java +0 -105
  28. data/src/main/java/org/embulk/filter/copy/executor/LocalThreadExecutor.java +0 -96
  29. data/src/main/java/org/embulk/filter/copy/forward/InForwardVisitor.java +0 -63
  30. data/src/main/java/org/embulk/filter/copy/forward/OutForwardVisitor.java +0 -63
  31. data/src/main/java/org/embulk/filter/copy/util/ElapsedTime.java +0 -165
@@ -1,24 +1,25 @@
1
1
  package org.embulk.filter.copy.forward;
2
2
 
3
3
  import com.google.common.base.Optional;
4
+ import com.google.common.util.concurrent.AbstractExecutionThreadService;
4
5
  import influent.EventStream;
5
6
  import influent.Tag;
6
7
  import influent.forward.ForwardCallback;
7
8
  import influent.forward.ForwardServer;
8
9
  import org.embulk.config.Config;
9
10
  import org.embulk.config.ConfigDefault;
10
- import org.embulk.filter.copy.util.ElapsedTime;
11
+ import org.embulk.spi.DataException;
11
12
  import org.embulk.spi.Exec;
12
13
  import org.slf4j.Logger;
14
+ import org.embulk.filter.copy.spi.ElapsedTime;
13
15
 
14
16
  import java.util.concurrent.CompletableFuture;
15
- import java.util.concurrent.Executor;
16
- import java.util.concurrent.ExecutorService;
17
- import java.util.concurrent.Executors;
17
+ import java.util.concurrent.ExecutionException;
18
18
  import java.util.concurrent.atomic.AtomicBoolean;
19
19
  import java.util.function.Consumer;
20
20
 
21
21
  public class InForwardService
22
+ extends AbstractExecutionThreadService
22
23
  {
23
24
  private final static Logger logger = Exec.getLogger(InForwardService.class);
24
25
 
@@ -55,7 +56,7 @@ public class InForwardService
55
56
  }
56
57
 
57
58
  public interface Task
58
- extends ForwardBaseTask
59
+ extends ForwardParentTask
59
60
  {
60
61
  @Config("in_forward")
61
62
  @ConfigDefault("{}")
@@ -94,30 +95,19 @@ public class InForwardService
94
95
  return new Builder();
95
96
  }
96
97
 
97
- // TODO: configure?
98
- private static final String THREAD_NAME = "in_forward";
99
- private static final int NUM_THREADS = 1;
100
-
101
98
  private final Task task;
102
99
  private final ForwardServer server;
103
- private final ExecutorService callbackThread;
104
100
  private final AtomicBoolean shouldShutdown = new AtomicBoolean(false);
105
101
 
106
102
  private InForwardService(Task task, Consumer<EventStream> eventConsumer)
107
103
  {
108
104
  this.task = task;
109
- this.callbackThread = Executors.newFixedThreadPool(
110
- NUM_THREADS,
111
- r -> new Thread(r, THREAD_NAME));
112
- this.server = buildServer(task.getInForwardTask(), eventConsumer, callbackThread);
105
+ this.server = buildServer(task.getInForwardTask(), eventConsumer);
113
106
  }
114
107
 
115
- private ForwardServer buildServer(InForwardTask t, Consumer<EventStream> eventConsumer, Executor callbackThread)
108
+ private ForwardServer buildServer(InForwardTask t, Consumer<EventStream> eventConsumer)
116
109
  {
117
- ForwardServer.Builder builder = new ForwardServer.Builder(
118
- ForwardCallback.ofSyncConsumer(
119
- wrapEventConsumer(eventConsumer),
120
- callbackThread));
110
+ ForwardServer.Builder builder = new ForwardServer.Builder(wrapEventConsumer(eventConsumer));
121
111
 
122
112
  builder.localAddress(t.getPort());
123
113
 
@@ -139,25 +129,27 @@ public class InForwardService
139
129
  if (t.getTcpNoDelayEnabled().isPresent()) {
140
130
  builder.tcpNoDelayEnabled(t.getTcpNoDelayEnabled().get());
141
131
  }
132
+ // TODO: builder.workerPoolSize(1);
142
133
 
143
134
  return builder.build();
144
135
  }
145
136
 
146
- private Consumer<EventStream> wrapEventConsumer(Consumer<EventStream> eventConsumer)
137
+ private ForwardCallback wrapEventConsumer(Consumer<EventStream> eventConsumer)
147
138
  {
148
- return eventStream ->
139
+ return ForwardCallback.of(es ->
149
140
  {
150
- if (isShutdownTag(eventStream.getTag())) {
151
- logger.info("Receive shutdown tag: {}", eventStream.getTag());
141
+ if (isShutdownTag(es.getTag())) {
142
+ logger.info("Receive shutdown tag: {}", es.getTag());
152
143
  shouldShutdown.set(true);
153
144
  }
154
- else if (isMessageTag(eventStream.getTag())) {
155
- eventConsumer.accept(eventStream);
145
+ else if (isMessageTag(es.getTag())) {
146
+ eventConsumer.accept(es);
156
147
  }
157
148
  else {
158
- throw new RuntimeException(String.format("Unknown Tag received: %s", eventStream.getTag().getName()));
149
+ throw new DataException(String.format("Unknown Tag received: %s", es.getTag().getName()));
159
150
  }
160
- };
151
+ return CompletableFuture.completedFuture(null);
152
+ });
161
153
  }
162
154
 
163
155
  private boolean isShutdownTag(Tag tag)
@@ -170,18 +162,93 @@ public class InForwardService
170
162
  return tag.getName().contentEquals(task.getMessageTag());
171
163
  }
172
164
 
173
- public void runUntilShouldShutdown()
165
+ @Override
166
+ protected void startUp()
167
+ throws Exception
174
168
  {
175
169
  server.start();
176
- ElapsedTime.debugUntil(shouldShutdown::get, logger, "in_forward server", 1000L);
177
- shutdown();
178
170
  }
179
171
 
180
- private void shutdown()
172
+ @Override
173
+ protected void shutDown()
174
+ throws Exception
181
175
  {
182
- ElapsedTime.debug(logger, "shutdown in_forward callback", callbackThread::shutdown);
183
- CompletableFuture<Void> shutdown = server.shutdown();
184
- ElapsedTime.debugUntil(() -> shutdown.isCancelled() || shutdown.isCompletedExceptionally() || shutdown.isDone(),
185
- logger, "shutdown in_forward server", 1000L);
176
+ ElapsedTime.measureWithPolling(new ElapsedTime.Pollable<Void>() {
177
+ private CompletableFuture<Void> future;
178
+
179
+ @Override
180
+ public boolean poll()
181
+ {
182
+ return future.isCancelled() || future.isCompletedExceptionally() || future.isDone();
183
+ }
184
+
185
+ @Override
186
+ public Void getResult()
187
+ {
188
+ try {
189
+ return future.get();
190
+ }
191
+ catch (InterruptedException | ExecutionException e) {
192
+ logger.warn("InForwardService: Server Shutdown is failed.", e);
193
+ return null;
194
+ }
195
+ }
196
+
197
+ @Override
198
+ public void onStart()
199
+ {
200
+ logger.info("InForwardService: Server Shutdown Start.");
201
+ this.future = server.shutdown();
202
+ }
203
+
204
+ @Override
205
+ public void onWaiting(long elapsedMillis)
206
+ {
207
+ logger.info("InForwardService: Server Shutdown Running. (Elapsed: {} ms)", elapsedMillis);
208
+ }
209
+
210
+ @Override
211
+ public void onFinished(long elapsedMillis)
212
+ {
213
+ logger.info("InForwardService: Server Shutdown Finish. (Elapsed: {} ms)", elapsedMillis);
214
+ }
215
+ });
216
+ }
217
+
218
+ @Override
219
+ protected void run()
220
+ throws Exception
221
+ {
222
+ ElapsedTime.measureWithPolling(new ElapsedTime.Pollable<Void>() {
223
+ @Override
224
+ public boolean poll()
225
+ {
226
+ return shouldShutdown.get();
227
+ }
228
+
229
+ @Override
230
+ public Void getResult()
231
+ {
232
+ return null;
233
+ }
234
+
235
+ @Override
236
+ public void onStart()
237
+ {
238
+ logger.info("InForwardService: Server Start.");
239
+ }
240
+
241
+ @Override
242
+ public void onWaiting(long elapsedMillis)
243
+ {
244
+ logger.info("InForwardService: Server Running. (Elapsed: {} ms)", elapsedMillis);
245
+ }
246
+
247
+ @Override
248
+ public void onFinished(long elapsedMillis)
249
+ {
250
+ logger.info("InForwardService: Server Shutdown. (Elapsed: {} ms)", elapsedMillis);
251
+ }
252
+ });
186
253
  }
187
254
  }
@@ -1,135 +1,147 @@
1
1
  package org.embulk.filter.copy.forward;
2
2
 
3
+ import com.google.common.collect.Lists;
3
4
  import com.google.common.collect.Maps;
4
5
  import org.embulk.spi.Column;
5
6
  import org.embulk.spi.Schema;
6
7
  import org.embulk.spi.time.Timestamp;
7
- import org.embulk.spi.time.TimestampFormatter;
8
8
  import org.msgpack.value.Value;
9
+ import org.embulk.filter.copy.spi.DataBuilder;
9
10
 
11
+ import java.util.List;
10
12
  import java.util.Map;
11
13
 
12
14
  public class OutForwardEventBuilder
15
+ implements DataBuilder
13
16
  {
17
+ private final static String VALUES_KEY = "values";
18
+
14
19
  private final Schema schema;
15
- private final TimestampFormatter timestampFormatter;
20
+ private final OutForwardService outForward;
16
21
 
17
- private Map<String, Object> message;
22
+ private List<Object> values;
18
23
 
19
24
  public OutForwardEventBuilder(
20
25
  Schema schema,
21
- TimestampFormatter timestampFormatter)
26
+ OutForwardService outForward)
22
27
  {
23
-
24
28
  this.schema = schema;
25
- this.timestampFormatter = timestampFormatter;
29
+ this.outForward = outForward;
26
30
 
27
31
  setNewMessage();
28
32
  }
29
33
 
30
34
  private void setNewMessage()
31
35
  {
32
- this.message = Maps.newHashMap();
33
- }
34
-
35
- public void emitMessage(OutForwardService outForward)
36
- {
37
- outForward.emit(message);
38
- setNewMessage();
36
+ this.values = Lists.newArrayListWithCapacity(schema.getColumnCount());
39
37
  }
40
38
 
39
+ @Override
41
40
  public Schema getSchema()
42
41
  {
43
42
  return schema;
44
43
  }
45
44
 
46
- private Column getColumn(int columnIndex)
47
- {
48
- return getSchema().getColumn(columnIndex);
49
- }
50
-
51
- private void setValue(String columnName, Object value)
52
- {
53
- message.put(columnName, value);
54
- }
55
-
56
- private void setValue(Column column, Object value)
45
+ @Override
46
+ public void addRecord()
57
47
  {
58
- setValue(column.getName(), value);
48
+ Map<String, Object> message = Maps.newHashMap();
49
+ message.put(VALUES_KEY, values);
50
+ outForward.emit(message);
51
+ setNewMessage();
59
52
  }
60
53
 
61
- public void setValue(int columnIndex, Object value)
54
+ private void setValue(int columnIndex, Object v)
62
55
  {
63
- setValue(getColumn(columnIndex), value);
56
+ values.add(columnIndex, v);
64
57
  }
65
58
 
59
+ @Override
66
60
  public void setNull(Column column)
67
61
  {
68
- setValue(column, null);
62
+ setNull(column.getIndex());
69
63
  }
70
64
 
71
- private void setNull(int columnIndex)
65
+ @Override
66
+ public void setNull(int columnIndex)
72
67
  {
73
- setNull(getColumn(columnIndex));
68
+ setValue(columnIndex, null);
74
69
  }
75
70
 
76
- public void setBoolean(Column column, boolean value)
71
+ @Override
72
+ public void setBoolean(Column column, boolean v)
77
73
  {
78
- setValue(column, value);
74
+ setBoolean(column.getIndex(), v);
79
75
  }
80
76
 
81
- public void setBoolean(int columnIndex, boolean value)
77
+ @Override
78
+ public void setBoolean(int columnIndex, boolean v)
82
79
  {
83
- setBoolean(getColumn(columnIndex), value);
80
+ setValue(columnIndex, v);
84
81
  }
85
82
 
86
- public void setLong(Column column, long value)
83
+ @Override
84
+ public void setString(Column column, String v)
87
85
  {
88
- setValue(column, value);
86
+ setString(column.getIndex(), v);
89
87
  }
90
88
 
91
- public void setLong(int columnIndex, long value)
89
+ @Override
90
+ public void setString(int columnIndex, String v)
92
91
  {
93
- setLong(getColumn(columnIndex), value);
92
+ setValue(columnIndex, v);
94
93
  }
95
94
 
96
- public void setDouble(Column column, double value)
95
+ @Override
96
+ public void setLong(Column column, long v)
97
97
  {
98
- setValue(column, value);
98
+ setLong(column.getIndex(), v);
99
99
  }
100
100
 
101
- public void setDouble(int columnIndex, double value)
101
+ @Override
102
+ public void setLong(int columnIndex, long v)
102
103
  {
103
- setDouble(getColumn(columnIndex), value);
104
+ setValue(columnIndex, v);
104
105
  }
105
106
 
106
- public void setString(Column column, String value)
107
+ @Override
108
+ public void setDouble(Column column, double v)
107
109
  {
108
- setValue(column, value);
110
+ setDouble(column.getIndex(), v);
109
111
  }
110
112
 
111
- public void setString(int columnIndex, String value)
113
+ @Override
114
+ public void setDouble(int columnIndex, double v)
112
115
  {
113
- setString(getColumn(columnIndex), value);
116
+ setValue(columnIndex, v);
114
117
  }
115
118
 
116
- public void setTimestamp(Column column, Timestamp value)
119
+ @Override
120
+ public void setTimestamp(Column column, Timestamp v)
117
121
  {
118
- setValue(column, timestampFormatter.format(value));
122
+
123
+ setTimestamp(column.getIndex(), v);
119
124
  }
120
125
 
121
- public void setTimestamp(int columnIndex, Timestamp value)
126
+ @Override
127
+ public void setTimestamp(int columnIndex, Timestamp v)
122
128
  {
123
- setTimestamp(getColumn(columnIndex), value);
129
+ // TODO: confirm correct value is stored
130
+ long epochSecond = v.getEpochSecond();
131
+ long nanoAdjustment = v.getNano();
132
+ setValue(columnIndex, new long[]{epochSecond, nanoAdjustment});
124
133
  }
125
134
 
126
- public void setJson(Column column, Value value)
135
+ @Override
136
+ public void setJson(Column column, Value v)
127
137
  {
128
- setValue(column, value.toJson());
138
+ setJson(column.getIndex(), v);
129
139
  }
130
140
 
131
- public void setJson(int columnIndex, Value value)
141
+ @Override
142
+ public void setJson(int columnIndex, Value v)
132
143
  {
133
- setJson(getColumn(columnIndex), value);
144
+ // TODO: confirm correct value is stored
145
+ setValue(columnIndex, v.toJson());
134
146
  }
135
147
  }
@@ -64,7 +64,7 @@ public class OutForwardService
64
64
  }
65
65
 
66
66
  public interface Task
67
- extends ForwardBaseTask
67
+ extends ForwardParentTask
68
68
  {
69
69
  @Config("out_forward")
70
70
  @ConfigDefault("{}")
@@ -73,7 +73,7 @@ public class OutForwardService
73
73
 
74
74
  public static void sendShutdownMessage(Task task)
75
75
  {
76
- logger.info("out_forward: send shutdown message.");
76
+ logger.info("OutForwardService: Send a Shutdown Message.");
77
77
  OutForwardService outForward = new OutForwardService(task);
78
78
  outForward.emit(task.getShutdownTag(), Maps.newHashMap());
79
79
  outForward.finish();
@@ -9,11 +9,12 @@ import org.embulk.config.TaskReport;
9
9
  import org.embulk.config.TaskSource;
10
10
  import org.embulk.filter.copy.forward.InForwardEventReader;
11
11
  import org.embulk.filter.copy.forward.InForwardService;
12
- import org.embulk.filter.copy.forward.InForwardVisitor;
12
+ import org.embulk.filter.copy.spi.PageBuilder;
13
+ import org.embulk.filter.copy.spi.StandardColumnVisitor;
13
14
  import org.embulk.spi.BufferAllocator;
15
+ import org.embulk.spi.ColumnVisitor;
14
16
  import org.embulk.spi.Exec;
15
17
  import org.embulk.spi.InputPlugin;
16
- import org.embulk.spi.PageBuilder;
17
18
  import org.embulk.spi.PageOutput;
18
19
  import org.embulk.spi.Schema;
19
20
  import org.embulk.spi.SchemaConfig;
@@ -74,28 +75,25 @@ public class InternalForwardInputPlugin
74
75
  PluginTask task = taskSource.loadTask(PluginTask.class);
75
76
 
76
77
  try (PageBuilder pageBuilder = new PageBuilder(task.getBufferAllocator(), schema, output)) {
77
- TimestampParser timestampParser = new TimestampParser(
78
- task.getJRuby(),
79
- task.getDefaultTimestampFormat(),
80
- task.getDefaultTimeZone());
81
- InForwardEventReader eventReader = new InForwardEventReader(schema, timestampParser);
82
- InForwardVisitor inForwardVisitor = new InForwardVisitor(eventReader, pageBuilder);
83
-
84
- InForwardService.builder()
78
+ InForwardEventReader eventReader = new InForwardEventReader(schema);
79
+ ColumnVisitor inForwardVisitor = new StandardColumnVisitor(eventReader, pageBuilder);
80
+
81
+ InForwardService inForwardService = InForwardService.builder()
85
82
  .task(task)
86
83
  .forEachEventCallback(
87
84
  event ->
88
85
  {
89
86
  // TODO: here is not thread-safe
90
87
  eventReader.setEvent(event);
91
- while (eventReader.nextMessage()) {
88
+ while (eventReader.nextRecord()) {
92
89
  schema.visitColumns(inForwardVisitor);
93
90
  pageBuilder.addRecord();
94
91
  }
95
92
  }
96
93
  )
97
- .build()
98
- .runUntilShouldShutdown();
94
+ .build();
95
+
96
+ inForwardService.startAsync().awaitTerminated();
99
97
 
100
98
  pageBuilder.finish();
101
99
  }