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 2021/04/20 15:15:55 UTC

[GitHub] [geode] sabbey37 commented on a change in pull request #6346: GEODE-9172: Add Junit rule to start docker-based redis cluster

sabbey37 commented on a change in pull request #6346:
URL: https://github.com/apache/geode/pull/6346#discussion_r616779833



##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxy.java
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.proxy;
+
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+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.redis.RedisArrayAggregator;
+import io.netty.handler.codec.redis.RedisBulkStringAggregator;
+import io.netty.handler.codec.redis.RedisDecoder;
+import io.netty.handler.codec.redis.RedisEncoder;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+/**
+ * This proxy handles mangling Redis responses in whatever way is necessary. In the case of
+ * creating a docker-based redis cluster, we need to translate internal addresses into external
+ * addresses - see {@link RedisProxyInboundHandler#channelRead(ChannelHandlerContext, Object)}.
+ */
+public final class RedisProxy {
+
+  private static final Logger logger = LogService.getLogger();
+
+  private static final int LOCAL_PORT = Integer.parseInt(System.getProperty("localPort", "5379"));
+  private static final String REMOTE_HOST = System.getProperty("remoteHost", "localhost");
+  private static final int REMOTE_PORT = Integer.parseInt(System.getProperty("remotePort", "6379"));
+
+  private final EventLoopGroup bossGroup;
+  private final EventLoopGroup workerGroup;
+  private final int exposedPort;
+  private final Map<HostPort, HostPort> mappings = new HashMap<>();
+
+  public RedisProxy(int targetPort) {
+    bossGroup = new NioEventLoopGroup(1);
+    workerGroup = new NioEventLoopGroup(1);
+
+    ChannelFuture future = new ServerBootstrap().group(bossGroup, workerGroup)
+        .channel(NioServerSocketChannel.class)
+        .childHandler(new ChannelInitializer<SocketChannel>() {
+          @Override
+          protected void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline p = ch.pipeline();
+            // p.addLast(new LoggingHandler(LogLevel.INFO));

Review comment:
       Do we still need this?

##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxy.java
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.proxy;
+
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+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.redis.RedisArrayAggregator;
+import io.netty.handler.codec.redis.RedisBulkStringAggregator;
+import io.netty.handler.codec.redis.RedisDecoder;
+import io.netty.handler.codec.redis.RedisEncoder;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+/**
+ * This proxy handles mangling Redis responses in whatever way is necessary. In the case of
+ * creating a docker-based redis cluster, we need to translate internal addresses into external
+ * addresses - see {@link RedisProxyInboundHandler#channelRead(ChannelHandlerContext, Object)}.
+ */
+public final class RedisProxy {
+
+  private static final Logger logger = LogService.getLogger();
+
+  private static final int LOCAL_PORT = Integer.parseInt(System.getProperty("localPort", "5379"));
+  private static final String REMOTE_HOST = System.getProperty("remoteHost", "localhost");
+  private static final int REMOTE_PORT = Integer.parseInt(System.getProperty("remotePort", "6379"));

Review comment:
       It doesn't seem like these are being used.

##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxyInboundHandler.java
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.proxy;
+
+import java.util.Map;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.redis.ArrayRedisMessage;
+import io.netty.handler.codec.redis.FullBulkStringRedisMessage;
+import io.netty.handler.codec.redis.RedisArrayAggregator;
+import io.netty.handler.codec.redis.RedisBulkStringAggregator;
+import io.netty.handler.codec.redis.RedisDecoder;
+import io.netty.handler.codec.redis.RedisEncoder;
+import io.netty.util.CharsetUtil;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+public class RedisProxyInboundHandler extends ChannelInboundHandlerAdapter {
+
+  private static final Logger logger = LogService.getLogger();
+  private final String remoteHost;
+  private final int remotePort;
+  private final Map<HostPort, HostPort> mappings;
+  private Channel outboundChannel;
+  private RedisProxyOutboundHandler outboundHandler;
+  private final ClusterSlotsResponseProcessor slotsResponseProcessor;
+  private final ClusterNodesResponseProcessor nodesResponseProcessor;
+  private MovedResponseHandler movedResponseHandler;
+
+  public RedisProxyInboundHandler(String remoteHost, int remotePort,
+      Map<HostPort, HostPort> mappings) {
+    this.remoteHost = remoteHost;
+    this.remotePort = remotePort;
+    this.mappings = mappings;
+    this.slotsResponseProcessor = new ClusterSlotsResponseProcessor(mappings);
+    this.nodesResponseProcessor = new ClusterNodesResponseProcessor(mappings);
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext ctx) {
+    Channel inboundChannel = ctx.channel();
+    outboundHandler = new RedisProxyOutboundHandler(inboundChannel);
+    movedResponseHandler = new MovedResponseHandler(inboundChannel, mappings);
+
+    // Start the connection attempt.
+    Bootstrap b = new Bootstrap();
+    b.group(inboundChannel.eventLoop())
+        .channel(ctx.channel().getClass())
+        .handler(new ChannelInitializer<SocketChannel>() {
+          @Override
+          protected void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline p = ch.pipeline();
+            // p.addLast(new LoggingHandler(LogLevel.INFO));
+            p.addLast(new RedisEncoder());
+            p.addLast(new RedisDecoder());
+            p.addLast(new RedisBulkStringAggregator());
+            p.addLast(new RedisArrayAggregator());
+            p.addLast(movedResponseHandler);
+            p.addLast(outboundHandler);
+          }
+        });
+    // .option(ChannelOption.AUTO_READ, false);
+    ChannelFuture f = b.connect(remoteHost, remotePort);
+    outboundChannel = f.channel();
+    f.addListener((ChannelFutureListener) future -> {
+      if (future.isSuccess()) {
+        // connection complete start to read first data
+        inboundChannel.read();
+      } else {
+        logger.error("Failed to connect", future.cause());
+        inboundChannel.close();
+      }
+    });
+  }
+
+  /**
+   * Any redis commands which return an IP or port which needs to be translated into an external
+   * IP/port need to be added to this method and an appropriate {@link RedisResponseProcessor}
+   * needs to be implemented.
+   * <p/>
+   * Note that each inbound command has an explicit outbound processor associated. Commands that
+   * do not need any processing are simply handled by a {@link NoopRedisResponseProcessor}.
+   */
+  @Override
+  public void channelRead(final ChannelHandlerContext ctx, Object msg) {
+    if (outboundChannel.isActive()) {
+      // Commands always consist of an array of bulk strings
+      ArrayRedisMessage rMessage = (ArrayRedisMessage) msg;
+      String command = getArg(rMessage, 0);
+
+      // logger.info("--- {}", command);

Review comment:
       Do we still need this?

##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxyInboundHandler.java
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.proxy;
+
+import java.util.Map;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.redis.ArrayRedisMessage;
+import io.netty.handler.codec.redis.FullBulkStringRedisMessage;
+import io.netty.handler.codec.redis.RedisArrayAggregator;
+import io.netty.handler.codec.redis.RedisBulkStringAggregator;
+import io.netty.handler.codec.redis.RedisDecoder;
+import io.netty.handler.codec.redis.RedisEncoder;
+import io.netty.util.CharsetUtil;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+public class RedisProxyInboundHandler extends ChannelInboundHandlerAdapter {
+
+  private static final Logger logger = LogService.getLogger();
+  private final String remoteHost;
+  private final int remotePort;
+  private final Map<HostPort, HostPort> mappings;
+  private Channel outboundChannel;
+  private RedisProxyOutboundHandler outboundHandler;
+  private final ClusterSlotsResponseProcessor slotsResponseProcessor;
+  private final ClusterNodesResponseProcessor nodesResponseProcessor;
+  private MovedResponseHandler movedResponseHandler;
+
+  public RedisProxyInboundHandler(String remoteHost, int remotePort,
+      Map<HostPort, HostPort> mappings) {
+    this.remoteHost = remoteHost;
+    this.remotePort = remotePort;
+    this.mappings = mappings;
+    this.slotsResponseProcessor = new ClusterSlotsResponseProcessor(mappings);
+    this.nodesResponseProcessor = new ClusterNodesResponseProcessor(mappings);
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext ctx) {
+    Channel inboundChannel = ctx.channel();
+    outboundHandler = new RedisProxyOutboundHandler(inboundChannel);
+    movedResponseHandler = new MovedResponseHandler(inboundChannel, mappings);
+
+    // Start the connection attempt.
+    Bootstrap b = new Bootstrap();
+    b.group(inboundChannel.eventLoop())
+        .channel(ctx.channel().getClass())
+        .handler(new ChannelInitializer<SocketChannel>() {
+          @Override
+          protected void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline p = ch.pipeline();
+            // p.addLast(new LoggingHandler(LogLevel.INFO));

Review comment:
       Do we still need this?

##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxyInboundHandler.java
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.proxy;
+
+import java.util.Map;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.redis.ArrayRedisMessage;
+import io.netty.handler.codec.redis.FullBulkStringRedisMessage;
+import io.netty.handler.codec.redis.RedisArrayAggregator;
+import io.netty.handler.codec.redis.RedisBulkStringAggregator;
+import io.netty.handler.codec.redis.RedisDecoder;
+import io.netty.handler.codec.redis.RedisEncoder;
+import io.netty.util.CharsetUtil;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+public class RedisProxyInboundHandler extends ChannelInboundHandlerAdapter {
+
+  private static final Logger logger = LogService.getLogger();
+  private final String remoteHost;
+  private final int remotePort;
+  private final Map<HostPort, HostPort> mappings;
+  private Channel outboundChannel;
+  private RedisProxyOutboundHandler outboundHandler;
+  private final ClusterSlotsResponseProcessor slotsResponseProcessor;
+  private final ClusterNodesResponseProcessor nodesResponseProcessor;
+  private MovedResponseHandler movedResponseHandler;
+
+  public RedisProxyInboundHandler(String remoteHost, int remotePort,
+      Map<HostPort, HostPort> mappings) {
+    this.remoteHost = remoteHost;
+    this.remotePort = remotePort;
+    this.mappings = mappings;
+    this.slotsResponseProcessor = new ClusterSlotsResponseProcessor(mappings);
+    this.nodesResponseProcessor = new ClusterNodesResponseProcessor(mappings);
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext ctx) {
+    Channel inboundChannel = ctx.channel();
+    outboundHandler = new RedisProxyOutboundHandler(inboundChannel);
+    movedResponseHandler = new MovedResponseHandler(inboundChannel, mappings);
+
+    // Start the connection attempt.
+    Bootstrap b = new Bootstrap();
+    b.group(inboundChannel.eventLoop())
+        .channel(ctx.channel().getClass())
+        .handler(new ChannelInitializer<SocketChannel>() {
+          @Override
+          protected void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline p = ch.pipeline();
+            // p.addLast(new LoggingHandler(LogLevel.INFO));
+            p.addLast(new RedisEncoder());
+            p.addLast(new RedisDecoder());
+            p.addLast(new RedisBulkStringAggregator());
+            p.addLast(new RedisArrayAggregator());
+            p.addLast(movedResponseHandler);
+            p.addLast(outboundHandler);
+          }
+        });
+    // .option(ChannelOption.AUTO_READ, false);

Review comment:
       Do we still need this?

##########
File path: geode-apis-compatible-with-redis/src/commonTest/java/org/apache/geode/redis/internal/proxy/RedisProxyOutboundHandler.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.proxy;
+
+import java.util.Queue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+public class RedisProxyOutboundHandler extends ChannelInboundHandlerAdapter {
+
+  private static final Logger logger = LogService.getLogger();
+  private final Queue<RedisResponseProcessor> processors = new LinkedBlockingQueue<>();
+  private final Channel inboundChannel;
+
+  public RedisProxyOutboundHandler(Channel inboundChannel) {
+    this.inboundChannel = inboundChannel;
+  }
+
+  @Override
+  public void channelActive(ChannelHandlerContext ctx) {
+    ctx.read();
+  }
+
+  @Override
+  public void channelRead(final ChannelHandlerContext ctx, Object msg) {
+    RedisResponseProcessor processor = processors.poll();
+    if (processor == null) {
+      logger.warn("No processor queued - will use default noop processor");
+      processor = NoopRedisResponseProcessor.INSTANCE;
+    }
+
+    inboundChannel.writeAndFlush(processor.process(msg, ctx.channel()))
+        .addListener((ChannelFutureListener) future -> {
+          if (future.isSuccess()) {
+            inboundChannel.read();
+            // ctx.channel().read();

Review comment:
       Do we still need this?




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