You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2020/06/24 22:06:47 UTC

[GitHub] [geode] jdeppe-pivotal commented on a change in pull request #5289: refactor GeodeRedisServer

jdeppe-pivotal commented on a change in pull request #5289:
URL: https://github.com/apache/geode/pull/5289#discussion_r445196891



##########
File path: geode-redis/src/main/java/org/apache/geode/redis/internal/PassiveExpirationManager.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.geode.redis.internal;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.apache.geode.logging.internal.executors.LoggingExecutors.newSingleThreadScheduledExecutor;
+
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.cache.CacheClosedException;
+import org.apache.geode.cache.EntryDestroyedException;
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.partition.PartitionRegionHelper;
+import org.apache.geode.logging.internal.log4j.api.LogService;
+import org.apache.geode.redis.internal.data.ByteArrayWrapper;
+import org.apache.geode.redis.internal.data.RedisData;
+import org.apache.geode.redis.internal.executor.key.RedisKeyCommands;
+import org.apache.geode.redis.internal.executor.key.RedisKeyCommandsFunctionExecutor;
+
+public class PassiveExpirationManager {
+  private static final Logger logger = LogService.getLogger();
+
+  private final Region<ByteArrayWrapper, RedisData> dataRegion;
+  private final ScheduledExecutorService expirationExecutor;
+
+
+  public PassiveExpirationManager(Region<ByteArrayWrapper, RedisData> dataRegion) {
+    this.dataRegion = dataRegion;
+    expirationExecutor = newSingleThreadScheduledExecutor("GemFireRedis-PassiveExpiration-");
+  }
+
+  public void start() {
+    int INTERVAL = 1;
+    expirationExecutor.scheduleAtFixedRate(() -> doDataExpiration(dataRegion), INTERVAL, INTERVAL,

Review comment:
       I think this would be potentially better with `scheduleWithFixedDelay` so that there's a guaranteed pause between runs. I'm concerned that a large data set would result in `doDataExpiration` taking longer than 1 second and putting pressure on the system by constantly running back-to-back.

##########
File path: geode-redis/src/main/java/org/apache/geode/redis/internal/netty/NettyRedisServer.java
##########
@@ -0,0 +1,317 @@
+/*
+ * 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.geode.redis.internal.netty;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.util.concurrent.ThreadFactory;
+import java.util.function.Supplier;
+
+import javax.net.ssl.KeyManagerFactory;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.ServerChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslContextBuilder;
+import io.netty.handler.timeout.WriteTimeoutHandler;
+import io.netty.util.concurrent.Future;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.distributed.internal.DistributionConfig;
+import org.apache.geode.internal.admin.SSLConfig;
+import org.apache.geode.internal.inet.LocalHostUtil;
+import org.apache.geode.internal.net.SSLConfigurationFactory;
+import org.apache.geode.internal.security.SecurableCommunicationChannel;
+import org.apache.geode.logging.internal.executors.LoggingThreadFactory;
+import org.apache.geode.logging.internal.log4j.api.LogService;
+import org.apache.geode.management.ManagementException;
+import org.apache.geode.redis.internal.RegionProvider;
+import org.apache.geode.redis.internal.pubsub.PubSub;
+
+public class NettyRedisServer {
+
+  /**
+   * System property name that can be used to set the number of threads to be used by the
+   * GeodeRedisServer
+   */
+  private static final String NUM_THREADS_SYS_PROP_NAME = "gemfireredis.numthreads";
+
+  private static final int RANDOM_PORT_INDICATOR = 0;
+
+  private static final Logger logger = LogService.getLogger();
+  /**
+   * Connection timeout in milliseconds
+   */
+  private static final int connectTimeoutMillis = 1000;
+
+  /**
+   * The number of threads that will work on handling requests
+   */
+  private final int numWorkerThreads;
+
+  /**
+   * The number of threads that will work socket selectors
+   */
+  private final int numSelectorThreads;
+
+  /**
+   * whether to use old single thread per connection model for worker group
+   */
+  private final boolean singleThreadPerConnection;
+
+  private final Supplier<DistributionConfig> configSupplier;
+  private final RegionProvider regionProvider;
+  private final PubSub pubsub;
+  private final Supplier<Boolean> allowUnsupportedSupplier;
+  private final Runnable shutdownInvoker;
+
+
+  private Channel serverChannel;
+  private EventLoopGroup bossGroup;
+  private EventLoopGroup workerGroup;
+  private EventLoopGroup subscriberGroup;
+
+  private final InetAddress bindAddress;
+  private int serverPort;
+
+  public NettyRedisServer(Supplier<DistributionConfig> configSupplier,
+      RegionProvider regionProvider, PubSub pubsub, Supplier<Boolean> allowUnsupportedSupplier,
+      Runnable shutdownInvoker, int port, String requestedAddress) {
+    this.configSupplier = configSupplier;
+    this.regionProvider = regionProvider;
+    this.pubsub = pubsub;
+    this.allowUnsupportedSupplier = allowUnsupportedSupplier;
+    this.shutdownInvoker = shutdownInvoker;
+    if (port < RANDOM_PORT_INDICATOR) {
+      throw new IllegalArgumentException("Redis port cannot be less than 0");
+    }
+    serverPort = port;
+    this.bindAddress = getBindAddress(requestedAddress);
+    numWorkerThreads = setNumWorkerThreads();
+    singleThreadPerConnection = numWorkerThreads == 0;
+    numSelectorThreads = 1;
+  }
+
+
+  public void start() {
+    Class<? extends ServerChannel> socketClass = initializeEventLoopGroups();
+    ServerBootstrap serverBootstrap = new ServerBootstrap();
+
+    serverBootstrap.group(bossGroup, workerGroup).channel(socketClass)
+        .childHandler(createChannelInitializer())
+        .option(ChannelOption.SO_REUSEADDR, true)
+        .option(ChannelOption.SO_RCVBUF, getBufferSize())
+        .childOption(ChannelOption.SO_KEEPALIVE, true)
+        .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis)
+        .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
+
+    serverChannel = createBoundChannel(serverBootstrap);
+  }
+
+  public void stop() {
+    ChannelFuture closeFuture = null;
+    if (serverChannel != null) {
+      closeFuture = serverChannel.closeFuture();
+    }
+    workerGroup.shutdownGracefully();
+    Future<?> bossFuture = bossGroup.shutdownGracefully();
+    if (serverChannel != null) {
+      serverChannel.close();
+    }
+    bossFuture.syncUninterruptibly();
+    if (closeFuture != null) {
+      closeFuture.syncUninterruptibly();
+    }
+  }
+
+  public int getPort() {
+    return serverPort;
+  }
+
+  private ChannelInitializer<SocketChannel> createChannelInitializer() {
+    String redisPassword = configSupplier.get().getRedisPassword();
+    final byte[] redisPasswordBytes = Coder.stringToBytes(redisPassword);
+
+    return new ChannelInitializer<SocketChannel>() {
+      @Override
+      public void initChannel(SocketChannel socketChannel) {
+        if (logger.isDebugEnabled()) {
+          logger.debug(
+              "GeodeRedisServer-Connection established with " + socketChannel.remoteAddress());
+        }
+        ChannelPipeline pipeline = socketChannel.pipeline();
+        addSSLIfEnabled(socketChannel, pipeline);
+        pipeline.addLast(ByteToCommandDecoder.class.getSimpleName(), new ByteToCommandDecoder());
+        pipeline.addLast(new WriteTimeoutHandler(10));
+        pipeline.addLast(ExecutionHandlerContext.class.getSimpleName(),
+            new ExecutionHandlerContext(socketChannel, regionProvider, pubsub, subscriberGroup,
+                allowUnsupportedSupplier, shutdownInvoker, redisPasswordBytes));
+      }
+    };
+  }
+
+  private void addSSLIfEnabled(SocketChannel ch, ChannelPipeline p) {
+
+    SSLConfig sslConfigForComponent =
+        SSLConfigurationFactory.getSSLConfigForComponent(configSupplier.get(),
+            SecurableCommunicationChannel.SERVER);
+
+    if (!sslConfigForComponent.isEnabled()) {
+      return;
+    }
+
+    SslContext sslContext;
+    try {
+      KeyStore ks = KeyStore.getInstance("JKS");
+      ks.load(new FileInputStream(sslConfigForComponent.getKeystore()),
+          sslConfigForComponent.getKeystorePassword().toCharArray()/**/);
+
+      // Set up key manager factory to use our key store
+      KeyManagerFactory kmf =
+          KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+      kmf.init(ks, sslConfigForComponent.getKeystorePassword().toCharArray());
+
+      SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(kmf);
+      sslContext = sslContextBuilder.build();
+
+    } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | IOException
+        | CertificateException e) {
+      throw new RuntimeException(e);
+    }
+    p.addLast(sslContext.newHandler(ch.alloc()));
+  }
+
+  private Class<? extends ServerChannel> initializeEventLoopGroups() {
+    ThreadFactory selectorThreadFactory =
+        new LoggingThreadFactory("GeodeRedisServer-SelectorThread-", false);
+
+    ThreadFactory workerThreadFactory =
+        new LoggingThreadFactory("GeodeRedisServer-WorkerThread-", true);
+
+    if (singleThreadPerConnection) {

Review comment:
       What do you think about removing this altogether? We do no testing with this mode and I wouldn't expect anyone to actually use it.




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