AHeise commented on a change in pull request #114:
URL: https://github.com/apache/bahir-flink/pull/114#discussion_r597121114



##########
File path: 
flink-connector-influxdb2/src/main/java/org/apache/flink/streaming/connectors/influxdb/common/DataPoint.java
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.flink.streaming.connectors.influxdb.common;
+
+import com.influxdb.Arguments;
+import com.influxdb.client.domain.WritePrecision;
+import com.influxdb.client.write.Point;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import javax.annotation.Nullable;
+
+/**
+ * InfluxDB data point class.
+ *
+ * <h3>Elements of line protocol</h3>
+ *
+ * <pre>
+ *
+ * measurementName,tagKey=tagValue fieldKey="fieldValue" 1465839830100400200
+ * --------------- --------------- --------------------- -------------------
+ *      |               |                   |                     |
+ * Measurement       Tag set            Field set              Timestamp
+ *
+ * </pre>
+ *
+ * <p>{@link InfluxParser} parses line protocol into this data point 
representation.
+ */
+public final class DataPoint {
+
+    private final String measurement;
+    private final Map<String, String> tags = new HashMap();
+    private final Map<String, Object> fields = new HashMap();
+    private final Long timestamp;
+
+    DataPoint(final String measurementName, @Nullable final Long timestamp) {
+        Arguments.checkNotNull(measurementName, "measurement");

Review comment:
       Maybe rather use Flink's `Precondition` like in Source?

##########
File path: flink-connector-influxdb2/src/main/resources/log4j2.properties
##########
@@ -0,0 +1,31 @@
+#

Review comment:
       Please remove this file entirely. It can easily overwrite the user's 
configuration during shading and confuse the heck out of them.

##########
File path: 
flink-connector-influxdb2/src/main/java/org/apache/flink/streaming/connectors/influxdb/common/DataPoint.java
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.flink.streaming.connectors.influxdb.common;
+
+import com.influxdb.Arguments;
+import com.influxdb.client.domain.WritePrecision;
+import com.influxdb.client.write.Point;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import javax.annotation.Nullable;
+
+/**
+ * InfluxDB data point class.
+ *
+ * <h3>Elements of line protocol</h3>
+ *
+ * <pre>
+ *
+ * measurementName,tagKey=tagValue fieldKey="fieldValue" 1465839830100400200
+ * --------------- --------------- --------------------- -------------------
+ *      |               |                   |                     |
+ * Measurement       Tag set            Field set              Timestamp
+ *
+ * </pre>
+ *
+ * <p>{@link InfluxParser} parses line protocol into this data point 
representation.
+ */
+public final class DataPoint {
+
+    private final String measurement;
+    private final Map<String, String> tags = new HashMap();
+    private final Map<String, Object> fields = new HashMap();
+    private final Long timestamp;
+
+    DataPoint(final String measurementName, @Nullable final Long timestamp) {
+        Arguments.checkNotNull(measurementName, "measurement");
+        this.measurement = measurementName;
+        this.timestamp = timestamp;
+    }
+
+    /**
+     * Converts the DataPoint object to {@link Point} object.
+     *
+     * @return {@link Point}.
+     */
+    public Point toPoint() {
+        final Point point = new Point(this.measurement);
+        point.time(this.timestamp, WritePrecision.NS);

Review comment:
       Afaik, timestamp is in MS. Probably worth documenting if it's confusing 
you/me.

##########
File path: 
flink-connector-influxdb2/src/main/java/org/apache/flink/streaming/connectors/influxdb/source/http/WriteAPIHandler.java
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.flink.streaming.connectors.influxdb.source.http;
+
+import com.sun.net.httpserver.HttpExchange;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.nio.charset.StandardCharsets;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.flink.annotation.Internal;
+import 
org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue;
+import org.apache.flink.streaming.connectors.influxdb.common.DataPoint;
+import org.apache.flink.streaming.connectors.influxdb.common.InfluxParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class handles the incoming requests through the path /api/v2/write. 
The handle function
+ * reads each line in the body and uses the {@link InfluxParser} to pars them 
to {@link DataPoint}
+ * objects.
+ */
+@Internal
+public final class WriteAPIHandler extends Handler {
+    private static final Logger LOG = 
LoggerFactory.getLogger(WriteAPIHandler.class);
+
+    private final int maximumLinesPerRequest;
+    private final FutureCompletingBlockingQueue ingestionQueue;
+    private final int threadIndex;
+    private final long enqueueWaitTime;
+
+    public WriteAPIHandler(
+            final int maximumLinesPerRequest,
+            final FutureCompletingBlockingQueue ingestionQueue,
+            final int threadIndex,
+            final long enqueueWaitTime) {
+        this.maximumLinesPerRequest = maximumLinesPerRequest;
+        this.ingestionQueue = ingestionQueue;
+        this.threadIndex = threadIndex;
+        this.enqueueWaitTime = enqueueWaitTime;
+    }
+
+    @Override
+    public void handle(final HttpExchange t) throws IOException {
+        final BufferedReader in =
+                new BufferedReader(
+                        new InputStreamReader(t.getRequestBody(), 
StandardCharsets.UTF_8));
+
+        try {
+            String line;
+            final List<DataPoint> points = new ArrayList<>();
+            int numberOfLinesParsed = 0;
+            while ((line = in.readLine()) != null) {
+                final DataPoint dataPoint = 
InfluxParser.parseToDataPoint(line);
+                points.add(dataPoint);
+                numberOfLinesParsed++;
+                if (numberOfLinesParsed > this.maximumLinesPerRequest) {
+                    throw new RequestTooLargeException(
+                            String.format(
+                                    "Payload too large. Maximum number of 
lines per request is %d.",
+                                    this.maximumLinesPerRequest));
+                }
+            }
+
+            final boolean result =
+                    CompletableFuture.supplyAsync(
+                                    () -> {
+                                        try {
+                                            return this.ingestionQueue.put(
+                                                    this.threadIndex, points);
+                                        } catch (final InterruptedException e) 
{
+                                            return false;
+                                        }
+                                    })
+                            .get(this.enqueueWaitTime, TimeUnit.SECONDS);
+
+            if (!result) {
+                throw new TimeoutException("Failed to enqueue");
+            }
+
+            t.sendResponseHeaders(HttpURLConnection.HTTP_NO_CONTENT, -1);
+            this.ingestionQueue.notifyAvailable();
+        } catch (final ParseException e) {
+            Handler.sendResponse(t, HttpURLConnection.HTTP_BAD_REQUEST, 
e.getMessage());
+        } catch (final RequestTooLargeException e) {
+            Handler.sendResponse(t, HttpURLConnection.HTTP_ENTITY_TOO_LARGE, 
e.getMessage());
+        } catch (final TimeoutException e) {
+            final int HTTP_TOO_MANY_REQUESTS = 429;

Review comment:
       Extract as proper constant.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to