@Override
  public void destroyConnection(final Connection connection) {
    if (connection == null) {
      return;
    }
    if (logger.isFinestEnabled()) {
      logger.finest("Destroying " + connection);
    }

    if (activeConnections.remove(connection)) {
      // this should not be needed; but some tests are using DroppingConnection which is not a
      // TcpIpConnection.
      if (connection instanceof TcpIpConnection) {
        ioThreadingModel.onConnectionRemoved((TcpIpConnection) connection);
      }
    }
    final Address endPoint = connection.getEndPoint();
    if (endPoint != null) {
      connectionsInProgress.remove(endPoint);
      connectionsMap.remove(endPoint, connection);
      fireConnectionRemovedEvent(connection, endPoint);
    }
    if (connection.isAlive()) {
      connection.close();
      closedCount.inc();
    }
  }
  @Override
  public void removeEndpoint(final ClientEndpoint ce, boolean closeImmediately) {
    checkNotNull(ce, "endpoint can't be null");

    ClientEndpointImpl endpoint = (ClientEndpointImpl) ce;

    endpoints.remove(endpoint.getConnection());
    logger.info("Destroying " + endpoint);
    try {
      endpoint.destroy();
    } catch (LoginException e) {
      logger.warning(e);
    }

    final Connection connection = endpoint.getConnection();
    if (closeImmediately) {
      try {
        connection.close();
      } catch (Throwable e) {
        logger.warning("While closing client connection: " + connection, e);
      }
    } else {
      nodeEngine
          .getExecutionService()
          .schedule(
              new Runnable() {
                public void run() {
                  if (connection.isAlive()) {
                    try {
                      connection.close();
                    } catch (Throwable e) {
                      logger.warning("While closing client connection: " + e.toString());
                    }
                  }
                }
              },
              DESTROY_ENDPOINT_DELAY_MS,
              TimeUnit.MILLISECONDS);
    }
    ClientEvent event =
        new ClientEvent(
            endpoint.getUuid(),
            ClientEventType.DISCONNECTED,
            endpoint.getSocketAddress(),
            endpoint.getClientType());
    clientEngine.sendClientEvent(event);
  }
예제 #3
0
 public void shutdown() {
   for (ClientEndpoint endpoint : endpoints.values()) {
     try {
       endpoint.destroy();
     } catch (LoginException e) {
       logger.finest(e.getMessage());
     }
     try {
       final Connection conn = endpoint.getConnection();
       if (conn.live()) {
         conn.close();
       }
     } catch (Exception e) {
       logger.finest(e);
     }
   }
   endpoints.clear();
 }
예제 #4
0
 @Override
 public void shutdown(boolean terminate) {
   for (ClientEndpoint ce : endpointManager.getEndpoints()) {
     ClientEndpointImpl endpoint = (ClientEndpointImpl) ce;
     try {
       endpoint.destroy();
     } catch (LoginException e) {
       logger.finest(e.getMessage());
     }
     try {
       final Connection conn = endpoint.getConnection();
       if (conn.isAlive()) {
         conn.close("Shutdown of ClientEngine", null);
       }
     } catch (Exception e) {
       logger.finest(e);
     }
   }
   endpointManager.clear();
   ownershipMappings.clear();
 }
예제 #5
0
  private void destroyEndpoint(ClientEndpoint endpoint, boolean closeImmediately) {
    if (endpoint != null) {
      logger.info("Destroying " + endpoint);
      try {
        endpoint.destroy();
      } catch (LoginException e) {
        logger.warning(e);
      }

      final Connection connection = endpoint.getConnection();
      if (closeImmediately) {
        try {
          connection.close();
        } catch (Throwable e) {
          logger.warning("While closing client connection: " + connection, e);
        }
      } else {
        nodeEngine
            .getExecutionService()
            .schedule(
                new Runnable() {
                  public void run() {
                    if (connection.live()) {
                      try {
                        connection.close();
                      } catch (Throwable e) {
                        logger.warning("While closing client connection: " + e.toString());
                      }
                    }
                  }
                },
                1111,
                TimeUnit.MILLISECONDS);
      }
      sendClientEvent(endpoint);
    }
  }
  void removeEndpoint(final ClientEndpoint endpoint, boolean closeImmediately) {
    endpoints.remove(endpoint.getConnection());
    LOGGER.info("Destroying " + endpoint);
    try {
      endpoint.destroy();
    } catch (LoginException e) {
      LOGGER.warning(e);
    }

    final Connection connection = endpoint.getConnection();
    if (closeImmediately) {
      try {
        connection.close();
      } catch (Throwable e) {
        LOGGER.warning("While closing client connection: " + connection, e);
      }
    } else {
      nodeEngine
          .getExecutionService()
          .schedule(
              new Runnable() {
                public void run() {
                  if (connection.live()) {
                    try {
                      connection.close();
                    } catch (Throwable e) {
                      LOGGER.warning("While closing client connection: " + e.toString());
                    }
                  }
                }
              },
              DESTROY_ENDPOINT_DELAY_MS,
              TimeUnit.MILLISECONDS);
    }
    clientEngine.sendClientEvent(endpoint);
  }
예제 #7
0
  void handleJoinRequest(JoinRequest joinRequest) {
    final long now = Clock.currentTimeMillis();
    String msg =
        "Handling join from "
            + joinRequest.address
            + ", inProgress: "
            + joinInProgress
            + (timeToStartJoin > 0 ? ", timeToStart: " + (timeToStartJoin - now) : "");
    logger.log(Level.FINEST, msg);
    boolean validJoinRequest;
    try {
      validJoinRequest = node.validateJoinRequest(joinRequest);
    } catch (Exception e) {
      validJoinRequest = false;
    }
    final Connection conn = joinRequest.getConnection();
    if (validJoinRequest) {
      final MemberImpl member = getMember(joinRequest.address);
      if (member != null) {
        if (joinRequest.getUuid().equals(member.getUuid())) {
          String message = "Ignoring join request, member already exists.. => " + joinRequest;
          logger.log(Level.FINEST, message);

          // send members update back to node trying to join again...
          final long clusterTime = node.getClusterImpl().getClusterTime();
          sendProcessableTo(new MembersUpdateCall(lsMembers, clusterTime), conn);
          sendProcessableTo(new SyncProcess(), conn);
          return;
        }
        // If this node is master then remove old member and process join request.
        // If requesting address is equal to master node's address, that means master node
        // somehow disconnected and wants to join back.
        // So drop old member and process join request if this node becomes master.
        if (isMaster() || member.getAddress().equals(getMasterAddress())) {
          logger.log(
              Level.WARNING,
              "New join request has been received from an existing endpoint! => "
                  + member
                  + " Removing old member and processing join request...");
          // If existing connection of endpoint is different from current connection
          // destroy it, otherwise keep it.
          //                    final Connection existingConnection =
          // node.connectionManager.getConnection(joinRequest.address);
          //                    final boolean destroyExistingConnection = existingConnection !=
          // conn;
          doRemoveAddress(member.getAddress(), false);
        }
      }
      if (!node.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled()) {
        if (node.isActive() && node.joined() && node.getMasterAddress() != null && !isMaster()) {
          sendProcessableTo(new Master(node.getMasterAddress()), conn);
        }
      }
      if (isMaster() && node.joined() && node.isActive()) {
        final MemberInfo newMemberInfo =
            new MemberInfo(joinRequest.address, joinRequest.nodeType, joinRequest.getUuid());
        if (node.securityContext != null && !setJoins.contains(newMemberInfo)) {
          final ILogger securityLogger = node.loggingService.getLogger("com.hazelcast.security");
          final Credentials cr = joinRequest.getCredentials();
          if (cr == null) {
            securityLogger.log(
                Level.SEVERE,
                "Expecting security credentials "
                    + "but credentials could not be found in JoinRequest!");
            sendAuthFail(conn);
            return;
          } else {
            try {
              LoginContext lc = node.securityContext.createMemberLoginContext(cr);
              lc.login();
            } catch (LoginException e) {
              securityLogger.log(
                  Level.SEVERE,
                  "Authentication has failed for "
                      + cr.getPrincipal()
                      + '@'
                      + cr.getEndpoint()
                      + " => ("
                      + e.getMessage()
                      + ")");
              securityLogger.log(Level.FINEST, e.getMessage(), e);
              sendAuthFail(conn);
              return;
            }
          }
        }
        if (joinRequest.to != null && !joinRequest.to.equals(thisAddress)) {
          sendProcessableTo(new Master(node.getMasterAddress()), conn);
          return;
        }
        if (!joinInProgress) {
          if (firstJoinRequest != 0
              && now - firstJoinRequest >= MAX_WAIT_SECONDS_BEFORE_JOIN * 1000) {
            startJoin();
          } else {
            if (setJoins.add(newMemberInfo)) {
              sendProcessableTo(new Master(node.getMasterAddress()), conn);
              if (firstJoinRequest == 0) {
                firstJoinRequest = now;
              }
              if (now - firstJoinRequest < MAX_WAIT_SECONDS_BEFORE_JOIN * 1000) {
                timeToStartJoin = now + WAIT_MILLIS_BEFORE_JOIN;
              }
            }
            if (now > timeToStartJoin) {
              startJoin();
            }
          }
        }
      }
    } else {
      conn.close();
    }
  }