Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/carbondata/pull/1152#discussion_r126906057
  
    --- Diff: 
core/src/main/java/org/apache/carbondata/core/dictionary/server/NonSecureDictionaryServer.java
 ---
    @@ -0,0 +1,247 @@
    +/*
    + * 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.carbondata.core.dictionary.server;
    +
    +import java.net.Inet4Address;
    +import java.net.InetAddress;
    +import java.net.InetSocketAddress;
    +import java.net.NetworkInterface;
    +import java.net.SocketException;
    +import java.net.UnknownHostException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.Enumeration;
    +import java.util.List;
    +
    +import org.apache.carbondata.common.logging.LogService;
    +import org.apache.carbondata.common.logging.LogServiceFactory;
    +import org.apache.carbondata.core.constants.CarbonCommonConstants;
    +import 
org.apache.carbondata.core.dictionary.generator.key.DictionaryMessage;
    +import 
org.apache.carbondata.core.dictionary.generator.key.DictionaryMessageType;
    +import org.apache.carbondata.core.util.CarbonProperties;
    +
    +
    +import io.netty.bootstrap.ServerBootstrap;
    +import io.netty.channel.ChannelInitializer;
    +import io.netty.channel.ChannelOption;
    +import io.netty.channel.ChannelPipeline;
    +import io.netty.channel.EventLoopGroup;
    +import io.netty.channel.nio.NioEventLoopGroup;
    +import io.netty.channel.socket.SocketChannel;
    +import io.netty.channel.socket.nio.NioServerSocketChannel;
    +import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
    +import org.apache.commons.lang3.SystemUtils;
    +import org.apache.spark.SparkConf;
    +
    +/**
    + * Dictionary Server to generate dictionary keys.
    + */
    +public class NonSecureDictionaryServer implements DictionaryServer {
    +
    +  private static final LogService LOGGER =
    +      
LogServiceFactory.getLogService(NonSecureDictionaryServer.class.getName());
    +
    +  private NonSecureDictionaryServerHandler 
nonSecureDictionaryServerHandler;
    +
    +  private EventLoopGroup boss;
    +  private EventLoopGroup worker;
    +  private int port;
    +  private String host;
    +  private static Object lock = new Object();
    +  private static NonSecureDictionaryServer INSTANCE = null;
    +
    +  private NonSecureDictionaryServer(int port) {
    +    startServer(null, null, port);
    +  }
    +
    +  public static DictionaryServer getInstance(int port) {
    +    if (INSTANCE == null) {
    +      synchronized (lock) {
    +        if (INSTANCE == null) {
    +          INSTANCE = new NonSecureDictionaryServer(port);
    +        }
    +      }
    +    }
    +    return INSTANCE;
    +  }
    +
    +  /**
    +   * start dictionary server
    +   *
    +   * @param port
    +   */
    +  @Override public void startServer(SparkConf conf, String host, int port) 
{
    +    nonSecureDictionaryServerHandler = new 
NonSecureDictionaryServerHandler();
    +    String workerThreads = CarbonProperties.getInstance()
    +        .getProperty(CarbonCommonConstants.DICTIONARY_WORKER_THREADS,
    +            CarbonCommonConstants.DICTIONARY_WORKER_THREADS_DEFAULT);
    +    boss = new NioEventLoopGroup(1);
    +    worker = new NioEventLoopGroup(Integer.parseInt(workerThreads));
    +    // Configure the server.
    +    bindToPort(null, host, port);
    +  }
    +
    +  /**
    +   * Binds dictionary server to an available port.
    +   *
    +   * @param port
    +   */
    +  @Override public void bindToPort(SparkConf orgConf, String host, int 
port) {
    +    long start = System.currentTimeMillis();
    +    // Configure the server.
    +    int i = 0;
    +    while (i < 10) {
    +      int newPort = port + i;
    +      try {
    +        ServerBootstrap bootstrap = new ServerBootstrap();
    +        bootstrap.group(boss, worker);
    +        bootstrap.channel(NioServerSocketChannel.class);
    +        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
    +          @Override public void initChannel(SocketChannel ch) throws 
Exception {
    +            ChannelPipeline pipeline = ch.pipeline();
    +            pipeline
    +                .addLast("LengthDecoder", new 
LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
    +            pipeline.addLast("NonSecureDictionaryServerHandler", 
nonSecureDictionaryServerHandler);
    +          }
    +        });
    +        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    +        String hostToBind = findLocalIpAddress();
    +        InetSocketAddress address = hostToBind == null ?
    +            new InetSocketAddress(newPort) :
    +            new InetSocketAddress(hostToBind, newPort);
    +        bootstrap.bind(address).sync();
    +        LOGGER.audit("Dictionary Server started, Time spent " + 
(System.currentTimeMillis() - start)
    +            + " Listening on port " + newPort);
    +        this.port = newPort;
    +        this.host = hostToBind;
    +        break;
    +      } catch (Exception e) {
    +        LOGGER.error(e, "Dictionary Server Failed to bind to port:");
    +        if (i == 9) {
    +          throw new RuntimeException("Dictionary Server Could not bind to 
any port");
    +        }
    +      }
    +      i++;
    +    }
    +  }
    +
    +  /**
    +   * @return Port on which the NonSecureDictionaryServer has started.
    +   */
    +  @Override public int getPort() {
    +    return port;
    +  }
    +
    +  @Override public String getHost() {
    +    return host;
    +  }
    +
    +  @Override public String getSecretKey() {
    +    return null;
    +  }
    +
    +  /**
    +   * shutdown dictionary server
    +   *
    +   * @throws Exception
    +   */
    +  @Override public void shutdown() throws Exception {
    +    LOGGER.info("Shutting down dictionary server");
    +    worker.shutdownGracefully();
    +    boss.shutdownGracefully();
    +  }
    +
    +  /**
    +   * Write dictionary to the store.
    +   *
    +   * @throws Exception
    +   */
    +  @Override
    +  public void writeDictionary() throws Exception {
    +    DictionaryMessage key = new DictionaryMessage();
    +    key.setType(DictionaryMessageType.WRITE_DICTIONARY);
    +    nonSecureDictionaryServerHandler.processMessage(key);
    +  }
    +
    +  /**
    +   * Write Dictionary for one table.
    +   *
    +   * @throws Exception
    +   */
    +
    +  @Override
    +  public void writeTableDictionary(String uniqueTableName) throws 
Exception {
    +    DictionaryMessage key = new DictionaryMessage();
    +    key.setTableUniqueName(uniqueTableName);
    +    key.setType(DictionaryMessageType.WRITE_TABLE_DICTIONARY);
    +    nonSecureDictionaryServerHandler.processMessage(key);
    +  }
    +
    +  @Override public String findLocalIpAddress() {
    --- End diff --
    
    All cannot be moved as some of the class belongs to Spark2 and some in Core.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to