healchow commented on a change in pull request #1929:
URL: https://github.com/apache/incubator-inlong/pull/1929#discussion_r765435107



##########
File path: 
inlong-audit/audit-sdk/src/main/java/org/apache/inlong/audit/util/Config.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.inlong.audit.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
+
+public class Config {
+    private String localIP = "";
+    private String dockerId = "";
+
+    public void init() {
+        initIP();
+        initDockerId();
+    }
+
+    public String getLocalIP() {
+        return localIP;
+    }
+
+    public String getDockerId() {
+        return dockerId;
+    }
+
+    private void initIP() {
+        try {
+            for (Enumeration<NetworkInterface> en = 
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
+                NetworkInterface intf = en.nextElement();
+                String name = intf.getName();
+                if (!name.contains("docker") && !name.contains("lo")) {
+                    for (Enumeration<InetAddress> enumIpAddr = 
intf.getInetAddresses();
+                         enumIpAddr.hasMoreElements(); ) {
+                        InetAddress inetAddress = enumIpAddr.nextElement();
+                        if (!inetAddress.isLoopbackAddress()) {
+                            String ipaddress = inetAddress.getHostAddress();
+                            if (!ipaddress.contains("::") && 
!ipaddress.contains("0:0:")
+                                    && !ipaddress.contains("fe80")) {
+                                localIP = ipaddress;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (SocketException ex) {
+            localIP = "127.0.0.1";
+            return;
+        }
+    }
+
+    private void initDockerId() {
+        BufferedReader in = null;
+        try {
+            File file = new File("/proc/self/cgroup");
+            if (file.exists() == false) {
+                return;
+            }
+            in = new BufferedReader(new FileReader("/proc/self/cgroup"));
+            String dockerID = in.readLine();
+            if (dockerID.equals("") == false) {
+                int n = dockerID.indexOf("/");
+                String dockerID2 = dockerID.substring(n + 1, 
(dockerID.length() - n - 1));
+                n = dockerID2.indexOf("/");
+                dockerId = dockerID2.substring(n + 1, 12);
+            }
+        } catch (IOException e) {
+            System.out.println(e.getMessage());
+            return;
+        } catch (NullPointerException e2) {
+            System.out.println(e2.getMessage());
+            return;
+        } catch (Exception e3) {
+            System.out.println(e3.getMessage());
+            return;
+        } finally {

Review comment:
       It's recommended to use try-with-resource instead of try-finally.

##########
File path: 
inlong-audit/audit-sdk/src/main/java/org/apache/inlong/audit/util/Config.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.inlong.audit.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
+
+public class Config {
+    private String localIP = "";
+    private String dockerId = "";
+
+    public void init() {
+        initIP();
+        initDockerId();
+    }
+
+    public String getLocalIP() {
+        return localIP;
+    }
+
+    public String getDockerId() {
+        return dockerId;
+    }
+
+    private void initIP() {
+        try {
+            for (Enumeration<NetworkInterface> en = 
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
+                NetworkInterface intf = en.nextElement();
+                String name = intf.getName();
+                if (!name.contains("docker") && !name.contains("lo")) {
+                    for (Enumeration<InetAddress> enumIpAddr = 
intf.getInetAddresses();
+                         enumIpAddr.hasMoreElements(); ) {
+                        InetAddress inetAddress = enumIpAddr.nextElement();
+                        if (!inetAddress.isLoopbackAddress()) {
+                            String ipaddress = inetAddress.getHostAddress();
+                            if (!ipaddress.contains("::") && 
!ipaddress.contains("0:0:")
+                                    && !ipaddress.contains("fe80")) {
+                                localIP = ipaddress;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (SocketException ex) {
+            localIP = "127.0.0.1";
+            return;
+        }
+    }
+
+    private void initDockerId() {
+        BufferedReader in = null;
+        try {
+            File file = new File("/proc/self/cgroup");
+            if (file.exists() == false) {
+                return;
+            }
+            in = new BufferedReader(new FileReader("/proc/self/cgroup"));
+            String dockerID = in.readLine();
+            if (dockerID.equals("") == false) {
+                int n = dockerID.indexOf("/");
+                String dockerID2 = dockerID.substring(n + 1, 
(dockerID.length() - n - 1));
+                n = dockerID2.indexOf("/");
+                dockerId = dockerID2.substring(n + 1, 12);
+            }
+        } catch (IOException e) {
+            System.out.println(e.getMessage());

Review comment:
       do not print exceptions to the console.

##########
File path: 
inlong-audit/audit-sdk/src/test/java/org/apache/inlong/audit/send/SenderChannelTest.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.inlong.audit.send;
+
+import org.apache.inlong.audit.util.Encoder;
+import org.apache.inlong.audit.util.IpPort;
+import org.jboss.netty.bootstrap.ClientBootstrap;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelFuture;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
+import org.junit.Test;
+
+import java.util.concurrent.Executors;
+
+public class SenderChannelTest {
+    private ClientBootstrap client = new ClientBootstrap();
+    private IpPort ipPortObj = new IpPort("127.0.0.1", 80);
+    private ChannelFuture future;
+    SenderChannel senderChannel;
+
+    /**
+     * test  SenderChannel
+     */
+    public SenderChannelTest() {
+        try {
+            client.setFactory(new NioClientSocketChannelFactory(
+                    Executors.newCachedThreadPool(),
+                    Executors.newCachedThreadPool(),
+                    10));
+
+            client.setPipelineFactory(() -> {
+                ChannelPipeline pipeline = Channels.pipeline();
+                pipeline.addLast("encoder", new Encoder());
+                return pipeline;
+            });
+            client.setOption("tcpNoDelay", true);
+            client.setOption("child.tcpNoDelay", true);
+            client.setOption("keepAlive", true);
+            client.setOption("child.keepAlive", true);
+            client.setOption("reuseAddr", true);
+
+            future = client.connect(ipPortObj.addr).await();
+            senderChannel = new SenderChannel(future.getChannel(), ipPortObj, 
10);
+        } catch (InterruptedException e) {
+            System.out.println(e.getMessage());
+        }
+    }
+
+    @Test
+    public void tryAcquire() {
+        boolean ret = senderChannel.tryAcquire();
+        System.out.println(ret);
+    }
+
+    @Test
+    public void release() {
+        senderChannel.release();
+    }
+
+    @Test
+    public void testToString() {
+        IpPort ipPort = senderChannel.getIpPort();
+        System.out.println(ipPort);
+    }
+
+    @Test
+    public void getIpPort() {
+        String toString = senderChannel.toString();
+        System.out.println(toString);
+    }
+
+    @Test
+    public void getChannel() {
+        Channel channel = senderChannel.getChannel();
+        System.out.println(channel.getConfig().getConnectTimeoutMillis());
+        System.out.println(channel.getRemoteAddress());

Review comment:
       use assert instead of print to the console.

##########
File path: 
inlong-audit/audit-sdk/src/test/java/org/apache/inlong/audit/send/SenderGroupTest.java
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.inlong.audit.send;
+
+import org.apache.inlong.audit.protocol.AuditApi;
+import org.apache.inlong.audit.util.AuditData;
+import org.apache.inlong.audit.util.Config;
+import org.apache.inlong.audit.util.Decoder;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.Test;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public class SenderGroupTest {
+    Config testConfig = new Config();
+    SenderManager testManager = new SenderManager(testConfig);
+    SenderHandler clientHandler = new 
org.apache.inlong.audit.send.SenderHandler(testManager);
+    SenderGroup sender = new org.apache.inlong.audit.send.SenderGroup(10, new 
Decoder(), clientHandler);
+
+    @Test
+    public void send() {
+        AuditApi.AuditMessageHeader header = 
AuditApi.AuditMessageHeader.newBuilder().setIp("127.0.0.1").build();
+        AuditApi.AuditMessageBody body = 
AuditApi.AuditMessageBody.newBuilder().setAuditId("1").build();
+        AuditApi.AuditRequest content = 
AuditApi.AuditRequest.newBuilder().setMsgHeader(header)
+                                        .addMsgBody(body).build();
+        AuditData testData = new AuditData(System.currentTimeMillis(), 
content, 1L);
+        ChannelBuffer dataBuf = 
ChannelBuffers.wrappedBuffer(testData.getDataByte());
+        sender.send(dataBuf);
+    }
+
+    @Test
+    public void release() {
+        sender.release("127.0.9.1:80");
+    }
+
+    @Test
+    public void updateConfig() {
+        Set<String> ipLists = new LinkedHashSet<>();
+        ipLists.add("127.0.9.1:80");
+        ipLists.add("127.0.9.1:81");
+        ipLists.add("127.0.9.1:82");
+        sender.updateConfig(ipLists);
+    }
+
+    @Test
+    public void isHasSendError() {
+        sender.setHasSendError(false);
+        boolean isError = sender.isHasSendError();
+        System.out.println(isError);
+
+        sender.setHasSendError(true);
+        isError = sender.isHasSendError();
+        System.out.println(isError);

Review comment:
       ditto

##########
File path: 
inlong-audit/audit-sdk/src/test/java/org/apache/inlong/audit/send/SenderChannelTest.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.inlong.audit.send;
+
+import org.apache.inlong.audit.util.Encoder;
+import org.apache.inlong.audit.util.IpPort;
+import org.jboss.netty.bootstrap.ClientBootstrap;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelFuture;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
+import org.junit.Test;
+
+import java.util.concurrent.Executors;
+
+public class SenderChannelTest {
+    private ClientBootstrap client = new ClientBootstrap();
+    private IpPort ipPortObj = new IpPort("127.0.0.1", 80);
+    private ChannelFuture future;
+    SenderChannel senderChannel;
+
+    /**
+     * test  SenderChannel
+     */
+    public SenderChannelTest() {
+        try {
+            client.setFactory(new NioClientSocketChannelFactory(
+                    Executors.newCachedThreadPool(),
+                    Executors.newCachedThreadPool(),
+                    10));
+
+            client.setPipelineFactory(() -> {
+                ChannelPipeline pipeline = Channels.pipeline();
+                pipeline.addLast("encoder", new Encoder());
+                return pipeline;
+            });
+            client.setOption("tcpNoDelay", true);
+            client.setOption("child.tcpNoDelay", true);
+            client.setOption("keepAlive", true);
+            client.setOption("child.keepAlive", true);
+            client.setOption("reuseAddr", true);
+
+            future = client.connect(ipPortObj.addr).await();
+            senderChannel = new SenderChannel(future.getChannel(), ipPortObj, 
10);
+        } catch (InterruptedException e) {
+            System.out.println(e.getMessage());

Review comment:
       ditto




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to