private List<MembershipEvent> detectMembershipEvents(Map<String, MemberImpl> prevMembers) {
   final List<MembershipEvent> events = new LinkedList<MembershipEvent>();
   final Set<Member> eventMembers =
       Collections.unmodifiableSet(new LinkedHashSet<Member>(members));
   for (MemberImpl member : members) {
     final MemberImpl former = prevMembers.remove(member.getUuid());
     if (former == null) {
       events.add(
           new MembershipEvent(
               client.getCluster(), member, MembershipEvent.MEMBER_ADDED, eventMembers));
     }
   }
   for (MemberImpl member : prevMembers.values()) {
     events.add(
         new MembershipEvent(
             client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, eventMembers));
     if (clusterService.getMember(member.getAddress()) == null) {
       final Connection connection = connectionManager.getConnection(member.getAddress());
       if (connection != null) {
         connectionManager.destroyConnection(connection);
       }
     }
   }
   return events;
 }
  @Test
  public void testHeartbeatResumedEvent() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();

    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());

    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(
        new ConnectionHeartbeatListener() {
          @Override
          public void heartbeatResumed(Connection connection) {
            assertEquals(
                instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
            countDownLatch.countDown();
          }

          @Override
          public void heartbeatStopped(Connection connection) {}
        });

    blockMessagesFromInstance(instance2, client);
    sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
    unblockMessagesFromInstance(instance2, client);

    assertOpenEventually(countDownLatch);
  }
  private ClientInvocation(
      HazelcastClientInstanceImpl client,
      EventHandler handler,
      ClientMessage clientMessage,
      int partitionId,
      Address address,
      Connection connection) {
    this.lifecycleService = client.getLifecycleService();
    this.invocationService = client.getInvocationService();
    this.executionService = client.getClientExecutionService();
    this.listenerService = (ClientListenerServiceImpl) client.getListenerService();
    this.handler = handler;
    this.clientMessage = clientMessage;
    this.partitionId = partitionId;
    this.address = address;
    this.connection = connection;
    final ClientProperties clientProperties = client.getClientProperties();

    int waitTime = clientProperties.getInvocationTimeoutSeconds().getInteger();
    long retryTimeoutInSeconds =
        waitTime > 0
            ? waitTime
            : Integer.parseInt(ClientProperties.PROP_INVOCATION_TIMEOUT_SECONDS_DEFAULT);

    clientInvocationFuture = new ClientInvocationFuture(this, client, clientMessage, handler);
    this.retryCountLimit = retryTimeoutInSeconds / RETRY_WAIT_TIME_IN_SECONDS;

    int interval = clientProperties.getHeartbeatInterval().getInteger();
    this.heartBeatInterval =
        interval > 0 ? interval : Integer.parseInt(PROP_HEARTBEAT_INTERVAL_DEFAULT);
  }
  void listenMembershipEvents(Address ownerConnectionAddress) {
    initialListFetchedLatch = new CountDownLatch(1);
    try {
      ClientMessage clientMessage = RegisterMembershipListenerParameters.encode();

      Connection connection = connectionManager.getConnection(ownerConnectionAddress);
      if (connection == null) {
        System.out.println("FATAL connection null " + ownerConnectionAddress);
        throw new IllegalStateException(
            "Can not load initial members list because owner connection is null. "
                + "Address "
                + ownerConnectionAddress);
      }
      ClientInvocation invocation = new ClientInvocation(client, this, clientMessage, connection);
      invocation.invoke().get();
      waitInitialMemberListFetched();

    } catch (Exception e) {
      if (client.getLifecycleService().isRunning()) {
        if (LOGGER.isFinestEnabled()) {
          LOGGER.warning(
              "Error while registering to cluster events! -> " + ownerConnectionAddress, e);
        } else {
          LOGGER.warning(
              "Error while registering to cluster events! -> "
                  + ownerConnectionAddress
                  + ", Error: "
                  + e.toString());
        }
      }
    }
  }
  public String addDistributedObjectListener(final DistributedObjectListener listener) {
    final DistributedObjectListenerRequest request = new DistributedObjectListenerRequest();
    final EventHandler<PortableDistributedObjectEvent> eventHandler =
        new EventHandler<PortableDistributedObjectEvent>() {
          public void handle(PortableDistributedObjectEvent e) {
            final ObjectNamespace ns = new DefaultObjectNamespace(e.getServiceName(), e.getName());
            ClientProxyFuture future = proxies.get(ns);
            ClientProxy proxy = future == null ? null : future.get();
            if (proxy == null) {
              proxy = getOrCreateProxy(e.getServiceName(), e.getName());
            }

            DistributedObjectEvent event =
                new DistributedObjectEvent(e.getEventType(), e.getServiceName(), proxy);
            if (DistributedObjectEvent.EventType.CREATED.equals(e.getEventType())) {
              listener.distributedObjectCreated(event);
            } else if (DistributedObjectEvent.EventType.DESTROYED.equals(e.getEventType())) {
              listener.distributedObjectDestroyed(event);
            }
          }

          @Override
          public void beforeListenerRegister() {}

          @Override
          public void onListenerRegister() {}
        };
    return client.getListenerService().startListening(request, null, eventHandler);
  }
Exemple #6
0
  public Address findNextAddressToSendCreateRequest() {
    int clusterSize = client.getClientClusterService().getSize();
    Member liteMember = null;

    final LoadBalancer loadBalancer = client.getLoadBalancer();
    for (int i = 0; i < clusterSize; i++) {
      Member member = loadBalancer.next();
      if (member != null && !member.isLiteMember()) {
        return member.getAddress();
      } else if (liteMember == null) {
        liteMember = member;
      }
    }

    return liteMember != null ? liteMember.getAddress() : null;
  }
 public ClientConnection(
     HazelcastClientInstanceImpl client,
     IOSelector in,
     IOSelector out,
     int connectionId,
     SocketChannelWrapper socketChannelWrapper)
     throws IOException {
   final Socket socket = socketChannelWrapper.socket();
   this.connectionManager = client.getConnectionManager();
   this.serializationService = client.getSerializationService();
   this.lifecycleService = client.getLifecycleService();
   this.socketChannelWrapper = socketChannelWrapper;
   this.connectionId = connectionId;
   this.readHandler = new ClientReadHandler(this, in, socket.getReceiveBufferSize());
   this.writeHandler = new ClientWriteHandler(this, out, socket.getSendBufferSize());
 }
Exemple #8
0
 private long getRetryCountLimit() {
   HazelcastProperties hazelcastProperties = client.getProperties();
   int waitTime = hazelcastProperties.getSeconds(INVOCATION_TIMEOUT_SECONDS);
   long retryTimeoutInSeconds =
       waitTime > 0 ? waitTime : Integer.parseInt(INVOCATION_TIMEOUT_SECONDS.getDefaultValue());
   return retryTimeoutInSeconds / ClientInvocation.RETRY_WAIT_TIME_IN_SECONDS;
 }
Exemple #9
0
  public String addDistributedObjectListener(final DistributedObjectListener listener) {
    ClientMessage request = AddDistributedObjectListenerParameters.encode();
    final EventHandler<ClientMessage> eventHandler =
        new EventHandler<ClientMessage>() {
          public void handle(ClientMessage clientMessage) {
            DistributedObjectEventParameters e =
                DistributedObjectEventParameters.decode(clientMessage);
            final ObjectNamespace ns = new DefaultObjectNamespace(e.serviceName, e.name);
            ClientProxyFuture future = proxies.get(ns);
            ClientProxy proxy = future == null ? null : future.get();
            if (proxy == null) {
              proxy = getOrCreateProxy(e.serviceName, e.name);
            }

            DistributedObjectEvent event =
                new DistributedObjectEvent(e.eventType, e.serviceName, proxy);
            if (DistributedObjectEvent.EventType.CREATED.equals(e.eventType)) {
              listener.distributedObjectCreated(event);
            } else if (DistributedObjectEvent.EventType.DESTROYED.equals(e.eventType)) {
              listener.distributedObjectDestroyed(event);
            }
          }

          @Override
          public void beforeListenerRegister() {}

          @Override
          public void onListenerRegister() {}
        };
    return client.getListenerService().startListening(request, null, eventHandler);
  }
Exemple #10
0
 public String addDistributedObjectListener(final DistributedObjectListener listener) {
   final EventHandler<ClientMessage> eventHandler =
       new DistributedObjectEventHandler(listener, this);
   return client
       .getListenerService()
       .registerListener(distributedObjectListenerCodec, eventHandler);
 }
 private void registerInvocation(ClientInvocation clientInvocation) {
   short protocolVersion = client.getProtocolVersion();
   final int correlationId = newCorrelationId();
   clientInvocation.getClientMessage().setCorrelationId(correlationId).setVersion(protocolVersion);
   callIdMap.put(correlationId, clientInvocation);
   if (clientInvocation instanceof ClientListenerInvocation) {
     eventHandlerMap.put(correlationId, (ClientListenerInvocation) clientInvocation);
   }
 }
 private void memberAdded(MemberImpl member) {
   members.add(member);
   applyMemberListChanges();
   MembershipEvent event =
       new MembershipEvent(
           client.getCluster(),
           member,
           ClientInitialMembershipEvent.MEMBER_ADDED,
           Collections.unmodifiableSet(new LinkedHashSet<Member>(members)));
   clusterService.fireMembershipEvent(event);
 }
Exemple #13
0
 public ProxyManager(HazelcastClientInstanceImpl client) {
   this.client = client;
   final List<ListenerConfig> listenerConfigs = client.getClientConfig().getListenerConfigs();
   if (listenerConfigs != null && !listenerConfigs.isEmpty()) {
     for (ListenerConfig listenerConfig : listenerConfigs) {
       if (listenerConfig.getImplementation() instanceof DistributedObjectListener) {
         addDistributedObjectListener(
             (DistributedObjectListener) listenerConfig.getImplementation());
       }
     }
   }
 }
Exemple #14
0
  private Connection getTargetOrOwnerConnection(final Address target) throws IOException {
    if (target == null) {
      throw new IOException("Not able to setup owner connection!");
    }

    final ClientConnectionManager connectionManager = client.getConnectionManager();
    Connection connection = connectionManager.getConnection(target);
    if (connection == null) {
      final Address ownerConnectionAddress =
          client.getClientClusterService().getOwnerConnectionAddress();
      if (ownerConnectionAddress == null) {
        throw new IOException("Not able to setup owner connection!");
      }

      connection = connectionManager.getConnection(ownerConnectionAddress);
      if (connection == null) {
        throw new IOException("Client is not connected to member " + target);
      }
    }

    return connection;
  }
  @Test
  public void testHeartbeatStoppedEvent() throws InterruptedException {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());

    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(
        new ConnectionHeartbeatListener() {
          @Override
          public void heartbeatResumed(Connection connection) {}

          @Override
          public void heartbeatStopped(Connection connection) {
            countDownLatch.countDown();
          }
        });

    blockMessagesFromInstance(instance, client);
    assertOpenEventually(countDownLatch);
  }
  protected ClientInvocation(
      HazelcastClientInstanceImpl client,
      ClientMessage clientMessage,
      int partitionId,
      Address address,
      Connection connection) {
    this.lifecycleService = client.getLifecycleService();
    this.invocationService = client.getInvocationService();
    this.executionService = client.getClientExecutionService();
    this.clientMessage = clientMessage;
    this.partitionId = partitionId;
    this.address = address;
    this.connection = connection;

    HazelcastProperties hazelcastProperties = client.getProperties();
    long waitTime = hazelcastProperties.getMillis(INVOCATION_TIMEOUT_SECONDS);
    long waitTimeResolved =
        waitTime > 0 ? waitTime : Integer.parseInt(INVOCATION_TIMEOUT_SECONDS.getDefaultValue());
    retryTimeoutPointInMillis = System.currentTimeMillis() + waitTimeResolved;

    logger = ((ClientInvocationServiceSupport) invocationService).invocationLogger;
    clientInvocationFuture = new ClientInvocationFuture(this, client, clientMessage, logger);
  }
 private void memberRemoved(MemberImpl member) {
   members.remove(member);
   final Connection connection = connectionManager.getConnection(member.getAddress());
   if (connection != null) {
     connectionManager.destroyConnection(connection);
   }
   applyMemberListChanges();
   MembershipEvent event =
       new MembershipEvent(
           client.getCluster(),
           member,
           ClientInitialMembershipEvent.MEMBER_REMOVED,
           Collections.unmodifiableSet(new LinkedHashSet<Member>(members)));
   clusterService.fireMembershipEvent(event);
 }
 @Override
 public void handle(String uuid, String key, int opType, String value) {
   Map<Address, Member> memberMap = clusterService.getMembersRef();
   if (memberMap == null) {
     return;
   }
   for (Member target : memberMap.values()) {
     if (target.getUuid().equals(uuid)) {
       final MemberAttributeOperationType operationType =
           MemberAttributeOperationType.getValue(opType);
       ((AbstractMember) target).updateAttribute(operationType, key, value);
       MemberAttributeEvent memberAttributeEvent =
           new MemberAttributeEvent(client.getCluster(), target, operationType, key, value);
       clusterService.fireMemberAttributeEvent(memberAttributeEvent);
       break;
     }
   }
 }
 public ClientInvocationServiceSupport(HazelcastClientInstanceImpl client) {
   this.client = client;
   this.connectionManager = client.getConnectionManager();
   this.executionService = client.getClientExecutionService();
   connectionManager.addConnectionListener(this);
   connectionManager.addConnectionHeartbeatListener(this);
   this.partitionService = client.getClientPartitionService();
   responseThread =
       new ResponseThread(
           client.getThreadGroup(),
           client.getName() + ".response-",
           client.getClientConfig().getClassLoader());
   responseThread.start();
 }
  void listenMembershipEvents(Address ownerConnectionAddress) {
    initialListFetchedLatch = new CountDownLatch(1);
    try {
      ClientMessage clientMessage = ClientMembershipListenerCodec.encodeRequest();

      Connection connection = connectionManager.getConnection(ownerConnectionAddress);
      if (connection == null) {
        throw new IllegalStateException(
            "Can not load initial members list because owner connection is null. "
                + "Address "
                + ownerConnectionAddress);
      }
      ClientInvocation invocation =
          new ClientListenerInvocation(
              client,
              this,
              clientMessage,
              connection,
              new ClientMessageDecoder() {
                @Override
                public <T> T decodeClientMessage(ClientMessage clientMessage) {
                  return (T) ClientMembershipListenerCodec.decodeResponse(clientMessage).response;
                }
              });
      invocation.invoke().get();
      waitInitialMemberListFetched();

    } catch (Exception e) {
      if (client.getLifecycleService().isRunning()) {
        if (LOGGER.isFinestEnabled()) {
          LOGGER.warning(
              "Error while registering to cluster events! -> " + ownerConnectionAddress, e);
        } else {
          LOGGER.warning(
              "Error while registering to cluster events! -> "
                  + ownerConnectionAddress
                  + ", Error: "
                  + e.toString());
        }
      }
    }
  }
Exemple #21
0
  private void readProxyDescriptors() {
    try {
      ClassLoader classLoader = client.getClientConfig().getClassLoader();
      Iterator<Class<ClientProxyDescriptorProvider>> iter =
          com.hazelcast.util.ServiceLoader.classIterator(PROVIDER_ID, classLoader);

      while (iter.hasNext()) {
        Class<ClientProxyDescriptorProvider> clazz = iter.next();
        Constructor<ClientProxyDescriptorProvider> constructor = clazz.getDeclaredConstructor();
        ClientProxyDescriptorProvider provider = constructor.newInstance();
        ClientProxyDescriptor[] services = provider.createClientProxyDescriptors();

        for (ClientProxyDescriptor serviceDescriptor : services) {
          register(serviceDescriptor.getServiceName(), serviceDescriptor.getClientProxyClass());
        }
      }
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
  }
 public ClientListenerServiceImpl(
     HazelcastClientInstanceImpl client, int eventThreadCount, int eventQueueCapacity) {
   this.client = client;
   this.executionService = (ClientExecutionServiceImpl) client.getClientExecutionService();
   this.invocationService = client.getInvocationService();
   this.serializationService = client.getSerializationService();
   client.getClientClusterService().addMembershipListener(this);
   this.eventExecutor =
       new StripedExecutor(
           logger,
           client.getName() + ".event",
           client.getThreadGroup(),
           eventThreadCount,
           eventQueueCapacity);
 }
 private void memberAttributeChanged(MemberAttributeChange memberAttributeChange) {
   Map<Address, MemberImpl> memberMap = clusterService.getMembersRef();
   if (memberMap == null) {
     return;
   }
   if (memberAttributeChange == null) {
     return;
   }
   for (MemberImpl target : memberMap.values()) {
     if (target.getUuid().equals(memberAttributeChange.getUuid())) {
       final MemberAttributeOperationType operationType = memberAttributeChange.getOperationType();
       final String key = memberAttributeChange.getKey();
       final Object value = memberAttributeChange.getValue();
       target.updateAttribute(operationType, key, value);
       MemberAttributeEvent memberAttributeEvent =
           new MemberAttributeEvent(client.getCluster(), target, operationType, key, value);
       clusterService.fireMemberAttributeEvent(memberAttributeEvent);
       break;
     }
   }
 }
 public ClientSmartListenerService(
     HazelcastClientInstanceImpl client, int eventThreadCount, int eventQueueCapacity) {
   super(client, eventThreadCount, eventQueueCapacity);
   client.getClientClusterService().addMembershipListener(this);
 }
 private void unblockMessagesFromInstance(HazelcastInstance instance, HazelcastInstance client) {
   HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
   ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
   Address address = instance.getCluster().getLocalMember().getAddress();
   ((TestClientRegistry.MockClientConnectionManager) connectionManager).unblock(address);
 }
 public ClientMembershipListener(HazelcastClientInstanceImpl client) {
   this.client = client;
   connectionManager = (ClientConnectionManagerImpl) client.getConnectionManager();
   partitionService = (ClientPartitionServiceImpl) client.getClientPartitionService();
   clusterService = (ClientClusterServiceImpl) client.getClientClusterService();
 }
Exemple #27
0
 public boolean removeDistributedObjectListener(String id) {
   return client.getListenerService().deregisterListener(id);
 }
Exemple #28
0
 public boolean removeDistributedObjectListener(String id) {
   ClientMessage request = RemoveDistributedObjectListenerParameters.encode(id);
   return client.getListenerService().stopListening(request, id);
 }
Exemple #29
0
 private <T> Class<? extends ClientProxy> getServiceProxy(Class<T> service) {
   ClientExtension clientExtension = client.getClientExtension();
   return clientExtension.getServiceProxy(service);
 }
Exemple #30
0
 public boolean removeDistributedObjectListener(String id) {
   final RemoveDistributedObjectListenerRequest request =
       new RemoveDistributedObjectListenerRequest(id);
   return client.getListenerService().stopListening(request, id);
 }