You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2019/03/20 01:49:12 UTC

[GitHub] [pulsar] sijie commented on a change in pull request #3823: [Issue 3822] [pulsar-io] Add a Pulsar IO connector for Alluxio sink

sijie commented on a change in pull request #3823: [Issue 3822] [pulsar-io] Add a Pulsar IO connector for Alluxio sink
URL: https://github.com/apache/pulsar/pull/3823#discussion_r267159319
 
 

 ##########
 File path: pulsar-io/alluxio/src/main/java/org/apache/pulsar/io/alluxio/sink/AlluxioAbstractSink.java
 ##########
 @@ -0,0 +1,236 @@
+/**
+ * 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.pulsar.io.alluxio.sink;
+
+import alluxio.AlluxioURI;
+import alluxio.Configuration;
+import alluxio.PropertyKey;
+import alluxio.client.WriteType;
+import alluxio.client.file.FileOutStream;
+import alluxio.client.file.FileSystem;
+import alluxio.client.file.options.CreateFileOptions;
+import alluxio.exception.AlluxioException;
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.functions.api.Record;
+import org.apache.pulsar.io.core.KeyValue;
+import org.apache.pulsar.io.core.Sink;
+import org.apache.pulsar.io.core.SinkContext;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+/**
+ * A Simple abstract class for Alluxio sink
+ * Users need to implement extractKeyValue function to use this sink
+ */
+@Slf4j
+public abstract class AlluxioAbstractSink<K, V> implements Sink<V> {
+
+    private FileSystem fileSystem;
+    private FileOutStream fileOutStream;
+    private CreateFileOptions createFileOptions;
+    private long recordsNum;
+    private String tmpFilePath;
+    private String fileDirPath;
+    private String tmpFileDirPath;
+    private long lastRotationTime;
+    private long rotationRecordsNum;
+    private long rotationInterval;
+    private AlluxioSinkConfig alluxioSinkConfig;
+    private AlluxioState alluxioState;
+
+    private List<Record<V>> recordsToAck;
+
+    @Override
+    public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
+        alluxioSinkConfig = AlluxioSinkConfig.load(config);
+        alluxioSinkConfig.validate();
+
+        // initialize FileSystem
+        String alluxioMasterHost = alluxioSinkConfig.getAlluxioMasterHost();
+        int alluxioMasterPort = alluxioSinkConfig.getAlluxioMasterPort();
+        Configuration.set(PropertyKey.MASTER_HOSTNAME, alluxioMasterHost);
+        Configuration.set(PropertyKey.MASTER_RPC_PORT, alluxioMasterPort);
+        if (alluxioSinkConfig.getSecurityLoginUser() != null) {
+            Configuration.set(PropertyKey.SECURITY_LOGIN_USERNAME, alluxioSinkConfig.getSecurityLoginUser());
+        }
+        fileSystem = FileSystem.Factory.get();
+
+        // initialize alluxio dirs
+        String alluxioDir = alluxioSinkConfig.getAlluxioDir();
+        fileDirPath = alluxioDir.startsWith("/") ? alluxioDir : "/" + alluxioDir;
+        tmpFileDirPath = fileDirPath + "/tmp";
+
+        AlluxioURI alluxioDirPath = new AlluxioURI(fileDirPath);
+        if (!fileSystem.exists(alluxioDirPath)) {
+            fileSystem.createDirectory(alluxioDirPath);
+        }
+
+        AlluxioURI tmpAlluxioDirPath = new AlluxioURI(tmpFileDirPath);
+        if (!fileSystem.exists(tmpAlluxioDirPath)) {
+            fileSystem.createDirectory(tmpAlluxioDirPath);
+        }
+
+        createFileOptions = CreateFileOptions.defaults();
+
+        recordsNum = 0;
+        recordsToAck = Lists.newArrayList();
+        tmpFilePath = "";
+        alluxioState = AlluxioState.WRITE_STARTED;
+
+        lastRotationTime = System.currentTimeMillis();
+        rotationRecordsNum = alluxioSinkConfig.getRotationRecords();
+        rotationInterval =  alluxioSinkConfig.getRotationInterval();
+    }
+
+    @Override
+    public void write(Record<V> record) {
+        long now = System.currentTimeMillis();
+
+        switch (alluxioState.ordinal()) {
+            case 0:
+                try {
+                    writeToAlluxio(record);
+                    if (!shouldRotate(now)) {
+                        break;
+                    }
+                    alluxioState = AlluxioState.FILE_ROTATED;
+                } catch (AlluxioException | IOException e) {
+                    log.error("Unable to write record to alluxio.", e);
+                    record.fail();
+                }
+            case 1:
+                try {
+                    closeAndCommitTmpFile();
+                    alluxioState = AlluxioState.FILE_COMMITTED;
+                    ackRecords();
+                } catch (AlluxioException | IOException e) {
+                    log.error("Unable to flush records to alluxio.", e);
+                    failRecords();
+                }
+            case 2:
+                alluxioState = AlluxioState.WRITE_STARTED;
+                break;
+            default:
+                log.error("{} is not a valid state when writing record to alluxio temp dir {}.",
+                    alluxioState, tmpFileDirPath);
 
 Review comment:
   nit: a good practice is to add `break` for default branch as well

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services