taegeonum commented on a change in pull request #231: [NEMO-406] Invoke 
LambdaExecutor without creating containers
URL: https://github.com/apache/incubator-nemo/pull/231#discussion_r315082057
 
 

 ##########
 File path: 
runtime/master/src/main/java/org/apache/nemo/runtime/master/LambdaMaster.java
 ##########
 @@ -0,0 +1,184 @@
+/*
+ * 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.nemo.runtime.master;
+
+import com.amazonaws.regions.Regions;
+import com.amazonaws.services.lambda.AWSLambda;
+import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
+import com.amazonaws.services.lambda.model.InvokeRequest;
+import com.amazonaws.services.lambda.model.InvokeResult;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.group.ChannelGroup;
+import io.netty.channel.group.DefaultChannelGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import io.netty.util.concurrent.GlobalEventExecutor;
+import org.apache.nemo.runtime.master.resource.LambdaInboundHandler;
+import org.apache.nemo.runtime.master.resource.NettyChannelInitializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.*;
+
+/**
+ * Invoked by runtime master, with utilities for the LambdaExecutor to 
communicate
+ * Set up netty server for LambdaExecutors to connect to.
+ * Invoke LambdaExecutor with public addr and port.
+ */
+public class LambdaMaster {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(LambdaMaster.class.getName());
+  private static final int SERVER_BOSS_NUM_THREADS = 3;
+  private static final int SERVER_WORKER_NUM_THREADS = 10;
+  private static final String CLASS_NAME = LambdaMaster.class.getName();
+
+  private EventLoopGroup serverBossGroup;
+  private EventLoopGroup serverWorkerGroup;
+  private Channel acceptor;
+  // A container object keeping track of all existing channels
+  private final ChannelGroup serverChannelGroup = new 
DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
+
+  private int port = 9999;
+  private String localAddress;
+  private String publicAddress;
+
+  private final AWSLambda client;
+  private final String lambdaFunctionName;
+
+  public LambdaMaster() {
+    this.setNetty();
+
+    Regions region = Regions.fromName("ap-northeast-1");
+    this.client = AWSLambdaClientBuilder.standard().withRegion(region).build();
+    this.lambdaFunctionName = "lambda-dev-executor";
+  }
+
+  /**
+   * Invoke LambdaExecutor, which connects to the netty channel.
+   */
+  public void invokeExecutor() {
+     InvokeRequest request = new InvokeRequest()
+       .withFunctionName(this.lambdaFunctionName)
+       
.withInvocationType("Event").withLogType("Tail").withClientContext("Lambda 
Executor")
+       .withPayload(String.format("{\"address\":\"%s\", \"port\": %d}",
+         this.publicAddress, this.port));
+
+     InvokeResult response = client.invoke(request);
+  }
+
+  private void setNetty() {
+    LOG.info("##### Set up netty server #####");
+    this.serverBossGroup = new NioEventLoopGroup(SERVER_BOSS_NUM_THREADS,
+      new DefaultThreadFactory(CLASS_NAME + "SourceServerBoss"));
+    this.serverWorkerGroup = new NioEventLoopGroup(SERVER_WORKER_NUM_THREADS,
+      new DefaultThreadFactory(CLASS_NAME + "SourceServerWorker"));
+
+    final ServerBootstrap serverBootstrap = new ServerBootstrap();
+    ChannelInboundHandlerAdapter channelInboundHandlerAdapter = new 
LambdaInboundHandler(this.serverChannelGroup);
+
+    serverBootstrap.group(this.serverBossGroup, this.serverWorkerGroup)
+      .channel(NioServerSocketChannel.class)
+      .childHandler(new NettyChannelInitializer(channelInboundHandlerAdapter))
+      .option(ChannelOption.SO_BACKLOG, 128)
+      .option(ChannelOption.SO_REUSEADDR, true)
+      .childOption(ChannelOption.SO_KEEPALIVE, true);
+    System.out.println("##### Server Bootstrap set #####");
+
+    this.publicAddress = this.getPublicIP();
+    try {
+      this.localAddress = this.getLocalHostLANAddress().getHostAddress();
+    } catch (UnknownHostException e) {
+      e.printStackTrace();
+      throw new RuntimeException(e);
+    }
+
+    try {
+      this.acceptor = serverBootstrap.bind(
+        new InetSocketAddress(localAddress, this.port)).sync().channel();
+      LOG.info("Server address: {}, Assigned server port = {}", localAddress, 
port);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+
+    LOG.info("Public address: {}, localAddress: {}, port: {}", publicAddress, 
localAddress, port);
+    LOG.info("Acceptor open: {}, active: {}", acceptor.isOpen(), 
acceptor.isActive());
+  }
+
+  private String getPublicIP() {
+    final URL whatismyip;
+    try {
+      whatismyip = new URL("http://checkip.amazonaws.com";);
+    } catch (MalformedURLException e) {
+      e.printStackTrace();
+      throw new RuntimeException(e);
+    }
+
+    final BufferedReader in;
+    try {
+      in = new BufferedReader(new InputStreamReader(
+        whatismyip.openStream()));
+    } catch (IOException e) {
+      e.printStackTrace();
+      throw new RuntimeException(e);
+    }
+
+    String ip = null; //you get the IP as a String
+    try {
+      ip = in.readLine();
+    } catch (IOException e) {
+      e.printStackTrace();
+      throw new RuntimeException(e);
+    }
+    return ip;
+  }
+
+  private InetAddress getLocalHostLANAddress() throws UnknownHostException {
 
 Review comment:
   please reference a site (if you  copy this code from the site) 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to