[NO ISSUE][STO] Use actual server address in RequestReference

- user model changes: no
- storage format changes: no
- interface changes: yes

Details:

- Add server local address to IServletRequest.
- Use actual server address in IRequestReference rather
  than fixed node name.

Change-Id: I8ff2f4e83ffe1a75753b89e68cae2a9264bdf20b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/7823
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/Receptionist.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/Receptionist.java
index ec4fb6f..84bee6a 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/Receptionist.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/Receptionist.java
@@ -18,6 +18,7 @@
  */
 package org.apache.asterix.translator;
 
+import java.net.InetSocketAddress;
 import java.util.UUID;
 
 import org.apache.asterix.common.api.IClientRequest;
@@ -33,16 +34,12 @@
 
 public class Receptionist implements IReceptionist {
 
-    private final String node;
-
-    public Receptionist(String node) {
-        this.node = node;
-    }
-
     @Override
     public IRequestReference welcome(IServletRequest request) {
         final String uuid = UUID.randomUUID().toString();
-        final RequestReference ref = RequestReference.of(uuid, node, System.currentTimeMillis());
+        final InetSocketAddress localAddress = request.getLocalAddress();
+        final RequestReference ref =
+                RequestReference.of(uuid, NetworkUtil.toHostPort(localAddress), System.currentTimeMillis());
         ref.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
         ref.setRemoteAddr(NetworkUtil.toHostPort(request.getRemoteAddress()));
         return ref;
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplication.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplication.java
index 17cf664..d06b8ab 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplication.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplication.java
@@ -161,8 +161,8 @@
         componentProvider = new StorageComponentProvider();
         ccExtensionManager = new CCExtensionManager(new ArrayList<>(getExtensions()));
         IGlobalRecoveryManager globalRecoveryManager = createGlobalRecoveryManager();
-        appCtx = createApplicationContext(null, globalRecoveryManager, lifecycleCoordinator,
-                () -> new Receptionist("CC"), ConfigValidator::new, ccExtensionManager, new AdapterFactoryService());
+        appCtx = createApplicationContext(null, globalRecoveryManager, lifecycleCoordinator, Receptionist::new,
+                ConfigValidator::new, ccExtensionManager, new AdapterFactoryService());
         final CCConfig ccConfig = controllerService.getCCConfig();
         if (System.getProperty("java.rmi.server.hostname") == null) {
             System.setProperty("java.rmi.server.hostname", ccConfig.getClusterPublicAddress());
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplication.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplication.java
index 1036fb2..e90976e 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplication.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplication.java
@@ -175,7 +175,7 @@
     }
 
     protected IReceptionistFactory getReceptionistFactory() {
-        return () -> new Receptionist(nodeId);
+        return Receptionist::new;
     }
 
     protected IConfigValidatorFactory getConfigValidatorFactory() {
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/api/IServletRequest.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/api/IServletRequest.java
index ce1734a..a8996e3 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/api/IServletRequest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/api/IServletRequest.java
@@ -96,4 +96,11 @@
      * Indicates which scheme the client used making this request
      */
     HttpScheme getScheme();
+
+    /**
+     * Gets the local address of this request
+     *
+     * @return the remote address
+     */
+    InetSocketAddress getLocalAddress();
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
index bd56eb3..f5749b1 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/BaseRequest.java
@@ -38,17 +38,20 @@
     protected final Map<String, List<String>> parameters;
     protected final InetSocketAddress remoteAddress;
     protected final HttpScheme scheme;
+    protected final InetSocketAddress localAddress;
 
     public static IServletRequest create(ChannelHandlerContext ctx, FullHttpRequest request, HttpScheme scheme) {
         QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
         Map<String, List<String>> param = decoder.parameters();
         InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
-        return new BaseRequest(request, remoteAddress, param, scheme);
+        InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
+        return new BaseRequest(request, localAddress, remoteAddress, param, scheme);
     }
 
-    protected BaseRequest(FullHttpRequest request, InetSocketAddress remoteAddress,
+    protected BaseRequest(FullHttpRequest request, InetSocketAddress localAddress, InetSocketAddress remoteAddress,
             Map<String, List<String>> parameters, HttpScheme scheme) {
         this.request = request;
+        this.localAddress = localAddress;
         this.remoteAddress = remoteAddress;
         this.parameters = parameters;
         this.scheme = scheme;
@@ -99,4 +102,9 @@
     public HttpScheme getScheme() {
         return scheme;
     }
+
+    @Override
+    public InetSocketAddress getLocalAddress() {
+        return localAddress;
+    }
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/FormUrlEncodedRequest.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/FormUrlEncodedRequest.java
index 0e57d8d..de6ed72 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/FormUrlEncodedRequest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/FormUrlEncodedRequest.java
@@ -44,11 +44,12 @@
         new QueryStringDecoder(request.uri()).parameters()
                 .forEach((name, value) -> parameters.computeIfAbsent(name, a -> new ArrayList<>()).addAll(value));
         InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
-        return new FormUrlEncodedRequest(request, remoteAddress, parameters, scheme);
+        InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
+        return new FormUrlEncodedRequest(request, localAddress, remoteAddress, parameters, scheme);
     }
 
-    private FormUrlEncodedRequest(FullHttpRequest request, InetSocketAddress remoteAddress,
-            Map<String, List<String>> parameters, HttpScheme scheme) {
-        super(request, remoteAddress, parameters, scheme);
+    private FormUrlEncodedRequest(FullHttpRequest request, InetSocketAddress localAddress,
+            InetSocketAddress remoteAddress, Map<String, List<String>> parameters, HttpScheme scheme) {
+        super(request, localAddress, remoteAddress, parameters, scheme);
     }
 }