0

I want to check the backends' healthy conditon, if a backend is dead, i will deactive this backend.

I use a ThreadPool to start a thread, ping my backend using the ip + port, per 10s.

By using Socket class, I don't close the socket at first.

   Socket socket = new Socket();
   InetSocketAddress address = new InetSocketAddress(domain, port);
   try {
     socket.connect(address, 3000);
     if (socket.isConnected()) {
       successCount++;
     }
   } catch (IOException e) {
     e.printStackTrace();
   }

Then I close the socket.

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress(domain, port);
try {
  socket.connect(address, 3000);
  if (socket.isConnected()) {
    successCount++;
  }
} catch (IOException e) {
  e.printStackTrace();
} finally {
  try {
    socket.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

I want to find the diffence between this conditon in usage of memory.

I use the jconsole to display the usage of memory.

But I find if i close the socket, the usage of memory is higher than i don't close it.

This is the picture about the memory. I want to know why about this. enter image description here

private void backendHealthCheck() {
    Map<String, Boolean> backendHealthMap = new ConcurrentHashMap<>();
    List<ProxyBackendConfiguration> allBackends = gatewayBackendManager.getAllBackends();
    for (ProxyBackendConfiguration backend : allBackends) {
      String backendName = backend.getName();
      Boolean backendStatus = gatewayBackendManager.getBackendStatus(backendName);
      log.debug("[{}] 对应的后端状态为 [{}]", backendName, backendStatus);
      backendHealthMap.put(backendName, backendStatus);
    }
    scheduledExecutorService.scheduleWithFixedDelay(
        () -> {
        log.info("Check the backend's healthy condition");
        List<ProxyBackendConfiguration> backends = gatewayBackendManager.getAllBackends();
        for (ProxyBackendConfiguration backend : backends) {
          int successCount = 0;
          String domain = backend.getProxyTo().substring(8, 36);
          int port = Integer.parseInt(backend.getProxyTo().substring(37, 41));
          String backendName = backend.getName();
          for (int i = 0; i < 7; i++) {
            Socket socket = new Socket();
            InetSocketAddress address = new InetSocketAddress(domain, port);
            try {
              socket.connect(address, 3000);
              if (socket.isConnected()) {
                successCount++;
              }
            } catch (IOException e) {
              e.printStackTrace();
            }
            if (successCount >= 2) {
              // if the backend is active then break, else active this backend
              // if this is not in map, get false active this backend
              // if this backend in this map,and status is false,then active this backend
              if (!backendHealthMap.getOrDefault(backendName, false)) {
                gatewayBackendManager.activateBackend(backendName);
                backendHealthMap.put(backendName, true);
              }
              break;
            }
            if (i == 6) {
              // if this backend in map and the status is false, continue
              // if this backend is not in map, get status is true,then deactive this backend
              // if this backedn in map, and status is true,then deactive this backend
              if (backendHealthMap.getOrDefault(backendName, true)) {
                gatewayBackendManager.deactivateBackend(backendName);
                backendHealthMap.put(backendName, false);
              }
            }
          }
        }
      },
        0,
        10,
        TimeUnit.SECONDS);
  }
Felix.
  • 19
  • 2
  • One possible reason could be, the execution of stream and channel close checks, closing hand shakes and waits associated with `socket.close()` – Liju Aug 21 '20 at 07:26
  • sorry, i don't understand your opinion.Can you describe more clearer, tx~ – Felix. Aug 21 '20 at 08:53
  • TCP connection requires handshakes while establishing and closing the connection, lets say a program takes `n` units of memory to `socket.connect()` with remote system (to exchange `SYN`, `ACK` signals, to create input readers etc.), it will also take `~n` units of memory when `socket.close()` (to send `FIN`, `ACK` signals, to close readers, channels, writers etc..), and all these operation has wait times associated with it, this might have consumed the extra memory in your case. – Liju Aug 21 '20 at 09:24

0 Answers0