@Override
  public ICompletableFuture<Long> addAllAsync(
      Collection<? extends E> collection, OverflowPolicy overflowPolicy) {
    checkNotNull(collection, "collection can't be null");
    checkNotNull(overflowPolicy, "overflowPolicy can't be null");
    checkFalse(collection.isEmpty(), "collection can't be empty");
    checkTrue(
        collection.size() <= MAX_BATCH_SIZE, "collection can't be larger than " + MAX_BATCH_SIZE);

    final List<Data> valueList = new ArrayList<Data>(collection.size());
    for (E e : collection) {
      throwExceptionIfNull(e);
      valueList.add(toData(e));
    }

    ClientMessage request =
        RingbufferAddAllAsyncCodec.encodeRequest(name, valueList, overflowPolicy.getId());
    request.setPartitionId(partitionId);

    try {
      ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request).invoke();
      return new ClientDelegatingFuture<Long>(
          invocationFuture, getContext().getSerializationService(), ADD_ALL_ASYNC_RESPONSE_DECODER);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
  }
Ejemplo n.º 2
0
  @Test
  public void shouldEncodeAndDecodeClientMessageCorrectly_withPayLoadData_fromOffset()
      throws UnsupportedEncodingException {
    SafeBuffer byteBuffer = new SafeBuffer(new byte[150]);
    int offset = 100;

    ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, offset);

    cmEncode
        .setMessageType(7)
        .setVersion((short) 3)
        .addFlag(ClientMessage.BEGIN_AND_END_FLAGS)
        .setCorrelationId(66)
        .setPartitionId(77);

    byte[] bytes = VAR_DATA_STR_1.getBytes();
    final int calculatedFrameSize =
        ClientMessage.HEADER_SIZE
            + Bits.INT_SIZE_IN_BYTES
            + ParameterUtil.calculateByteArrayDataSize(bytes);
    cmEncode.set(1);
    cmEncode.set(bytes);
    cmEncode.updateFrameLength();

    ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, offset);

    assertEquals(1, cmDecode.getInt());
    assertArrayEquals(bytes, cmDecode.getByteArray());

    assertEquals(calculatedFrameSize, cmDecode.getFrameLength());
  }
    private void handleClientMessage(ClientMessage clientMessage)
        throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
            InvocationTargetException, InstantiationException {
      int correlationId = clientMessage.getCorrelationId();

      final ClientInvocation future = deRegisterCallId(correlationId);
      if (future == null) {
        logger.warning("No call for callId: " + correlationId + ", response: " + clientMessage);
        return;
      }

      if (ErrorCodec.TYPE == clientMessage.getMessageType()) {
        ErrorCodec exParameters = ErrorCodec.decode(clientMessage);
        Throwable exception =
            clientExceptionFactory.createException(
                exParameters.errorCode,
                exParameters.className,
                exParameters.message,
                exParameters.stackTrace,
                exParameters.causeErrorCode,
                exParameters.causeClassName);
        future.notifyException(exception);
      } else {
        future.notify(clientMessage);
      }
    }
 protected void sendClientMessage(ClientMessage resultClientMessage) {
   resultClientMessage.setCorrelationId(clientMessage.getCorrelationId());
   resultClientMessage.addFlag(ClientMessage.BEGIN_AND_END_FLAGS);
   resultClientMessage.setVersion(ClientMessage.VERSION);
   final Connection connection = endpoint.getConnection();
   // TODO framing not implemented yet, should be split into frames before writing to connection
   connection.write(resultClientMessage);
 }
 @Test
 public void shouldNotAccumulateInCompleteFrameSize() {
   ClientMessage accumulator = ClientMessage.create();
   final byte[] array = new byte[] {1, 2, 3};
   final ByteBuffer inBuffer = ByteBuffer.wrap(array);
   assertFalse(accumulator.readFrom(inBuffer));
   assertFalse(accumulator.isComplete());
 }
Ejemplo n.º 6
0
  @Test
  public void shouldEncodeWithNewVersionAndDecodeWithOldVersionCorrectly_withPayLoadData()
      throws UnsupportedEncodingException {
    SafeBuffer byteBuffer = new SafeBuffer(new byte[1024]);

    FutureClientMessage cmEncode = new FutureClientMessage();
    cmEncode.wrapForEncode(byteBuffer, 0);

    cmEncode
        .theNewField(999)
        .setMessageType(7)
        .setVersion((short) 3)
        .addFlag(ClientMessage.BEGIN_AND_END_FLAGS)
        .setCorrelationId(66)
        .setPartitionId(77);

    final int calculatedFrameSize =
        FutureClientMessage.THE_NEW_HEADER_SIZE
            + ParameterUtil.calculateByteArrayDataSize(BYTE_DATA);
    cmEncode.set(BYTE_DATA);
    cmEncode.updateFrameLength();

    ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0);

    final byte[] cmDecodeVarData1 = cmDecode.getByteArray();

    assertEquals(7, cmDecode.getMessageType());
    assertEquals(3, cmDecode.getVersion());
    assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode.getFlags());
    assertEquals(66, cmDecode.getCorrelationId());
    assertEquals(77, cmDecode.getPartitionId());
    assertEquals(calculatedFrameSize, cmDecode.getFrameLength());
    assertArrayEquals(cmDecodeVarData1, BYTE_DATA);
  }
Ejemplo n.º 7
0
 public static void encode(Member member, ClientMessage clientMessage) {
   AddressCodec.encode(member.getAddress(), clientMessage);
   clientMessage.set(member.getUuid());
   Map<String, Object> attributes = new HashMap<String, Object>(member.getAttributes());
   clientMessage.set(attributes.size());
   for (Map.Entry<String, Object> entry : attributes.entrySet()) {
     clientMessage.set(entry.getKey());
     Object value = entry.getValue();
     clientMessage.set(value.toString());
   }
 }
Ejemplo n.º 8
0
  public static Member decode(ClientMessage clientMessage) {
    final Address address = AddressCodec.decode(clientMessage);
    String uuid = clientMessage.getStringUtf8();
    int attributeSize = clientMessage.getInt();
    Map<String, Object> attributes = new HashMap<String, Object>();
    for (int i = 0; i < attributeSize; i++) {
      String key = clientMessage.getStringUtf8();
      String value = clientMessage.getStringUtf8();
      attributes.put(key, value);
    }

    return new com.hazelcast.client.impl.MemberImpl(address, uuid, attributes);
  }
Ejemplo n.º 9
0
  @Test
  public void shouldEncodeAndDecodeClientMessageCorrectly_withPayLoadData()
      throws UnsupportedEncodingException {
    SafeBuffer byteBuffer = new SafeBuffer(new byte[1024]);

    ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0);

    cmEncode
        .setMessageType(7)
        .setVersion((short) 3)
        .addFlag(ClientMessage.BEGIN_AND_END_FLAGS)
        .setCorrelationId(66)
        .setPartitionId(77);

    final byte[] data1 = VAR_DATA_STR_1.getBytes(DEFAULT_ENCODING);
    final int calculatedFrameSize =
        ClientMessage.HEADER_SIZE + ParameterUtil.calculateByteArrayDataSize(data1);
    cmEncode.set(data1);
    cmEncode.updateFrameLength();

    ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0);

    byte[] cmDecodeVarData1 = cmDecode.getByteArray();

    assertEquals(calculatedFrameSize, cmDecode.getFrameLength());
    assertArrayEquals(cmDecodeVarData1, data1);
  }
  protected void send(ClientInvocation invocation, ClientConnection connection) throws IOException {
    if (isShutdown) {
      throw new HazelcastClientNotActiveException("Client is shut down");
    }
    registerInvocation(invocation);

    ClientMessage clientMessage = invocation.getClientMessage();
    if (!isAllowedToSendRequest(connection, invocation)
        || !writeToConnection(connection, clientMessage)) {
      final int callId = clientMessage.getCorrelationId();
      deRegisterCallId(callId);
      deRegisterEventHandler(callId);
      throw new IOException("Packet not send to " + connection.getRemoteEndpoint());
    }
    invocation.setSendConnection(connection);
  }
Ejemplo n.º 11
0
  @Test
  public void shouldEncodeClientMessageCorrectly() {
    ByteBuffer byteBuffer = ByteBuffer.allocate(512);
    SafeBuffer safeBuffer = new SafeBuffer(byteBuffer.array());

    ClientMessage cmEncode = TestClientMessage.createForEncode(safeBuffer, 0);

    cmEncode
        .setMessageType(0x1122)
        .setVersion((short) 0xEF)
        .addFlag(ClientMessage.BEGIN_AND_END_FLAGS)
        .setCorrelationId(0x12345678)
        .setPartitionId(0x11223344);

    // little endian
    // FRAME LENGTH
    assertThat(byteBuffer.get(0), is((byte) ClientMessage.HEADER_SIZE));
    assertThat(byteBuffer.get(1), is((byte) 0));
    assertThat(byteBuffer.get(2), is((byte) 0));
    assertThat(byteBuffer.get(3), is((byte) 0));

    // VERSION
    assertThat(byteBuffer.get(4), is((byte) 0xEF));

    // FLAGS
    assertThat(byteBuffer.get(5), is((byte) 0xC0));

    // TYPE
    assertThat(byteBuffer.get(6), is((byte) 0x22));
    assertThat(byteBuffer.get(7), is((byte) 0x11));

    // setCorrelationId
    assertThat(byteBuffer.get(8), is((byte) 0x78));
    assertThat(byteBuffer.get(9), is((byte) 0x56));
    assertThat(byteBuffer.get(10), is((byte) 0x34));
    assertThat(byteBuffer.get(11), is((byte) 0x12));

    // setPartitionId
    assertThat(byteBuffer.get(12), is((byte) 0x44));
    assertThat(byteBuffer.get(13), is((byte) 0x33));
    assertThat(byteBuffer.get(14), is((byte) 0x22));
    assertThat(byteBuffer.get(15), is((byte) 0x11));

    // data offset
    assertThat(byteBuffer.get(16), is((byte) ClientMessage.HEADER_SIZE));
    assertThat(byteBuffer.get(17), is((byte) 0x00));
  }
Ejemplo n.º 12
0
 @Override
 public void handle(ClientMessage clientMessage) {
   if (clientMessage.getMessageType() == ClientMessageType.MEMBER_LIST_RESULT.id()) {
     final MemberListResultParameters memberListResultParameters =
         MemberListResultParameters.decode(clientMessage);
     initialMembers(memberListResultParameters.memberList);
     initialListFetchedLatch.countDown();
   } else if (clientMessage.getMessageType() == ClientMessageType.MEMBER_RESULT.id()) {
     handleMember(clientMessage);
   } else if (clientMessage.getMessageType() == ClientMessageType.MEMBER_ATTRIBUTE_RESULT.id()) {
     final MemberAttributeChangeResultParameters parameters =
         MemberAttributeChangeResultParameters.decode(clientMessage);
     memberAttributeChanged(parameters.memberAttributeChange);
   } else {
     LOGGER.warning("Unknown message type :" + clientMessage.getMessageType());
   }
 }
  private void invoke(ClientRegistrationKey registrationKey, Address address) throws Exception {
    ListenerMessageCodec codec = registrationKey.getCodec();
    ClientMessage request = codec.encodeAddRequest(true);
    EventHandler handler = registrationKey.getHandler();
    handler.beforeListenerRegister();

    ClientInvocation invocation = new ClientInvocation(client, request, address);
    invocation.setEventHandler(handler);
    String serverRegistrationId = codec.decodeAddResponse(invocation.invoke().get());

    handler.onListenerRegister();
    int correlationId = request.getCorrelationId();
    ClientEventRegistration registration =
        new ClientEventRegistration(serverRegistrationId, correlationId, address, codec);

    Map<Address, ClientEventRegistration> registrationMap = registrations.get(registrationKey);
    registrationMap.put(address, registration);
  }
Ejemplo n.º 14
0
 public void handleClientMessage(ClientMessage clientMessage, Connection connection) {
   int partitionId = clientMessage.getPartitionId();
   final MessageTask messageTask = messageTaskFactory.create(clientMessage, connection);
   if (partitionId < 0) {
     executor.execute(messageTask);
   } else {
     InternalOperationService operationService = nodeEngine.getOperationService();
     operationService.execute(messageTask);
   }
 }
  @Override
  public ICompletableFuture<Long> addAsync(E item, OverflowPolicy overflowPolicy) {
    checkNotNull(item, "item can't be null");
    checkNotNull(overflowPolicy, "overflowPolicy can't be null");

    Data element = toData(item);
    ClientMessage request = RingbufferAddCodec.encodeRequest(name, overflowPolicy.getId(), element);
    request.setPartitionId(partitionId);

    try {
      ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request).invoke();
      return new ClientDelegatingFuture<Long>(
          invocationFuture,
          getContext().getSerializationService(),
          ADD_ASYNC_ASYNC_RESPONSE_DECODER);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
  }
    private void handleClientMessage(ClientMessage clientMessage)
        throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
            InvocationTargetException, InstantiationException {
      int correlationId = clientMessage.getCorrelationId();

      final ClientInvocation future = deRegisterCallId(correlationId);
      if (future == null) {
        logger.warning("No call for callId: " + correlationId + ", response: " + clientMessage);
        return;
      }

      if (ExceptionResultParameters.TYPE == clientMessage.getMessageType()) {
        ExceptionResultParameters exceptionResultParameters =
            ExceptionResultParameters.decode(clientMessage);
        Throwable exception;
        if (exceptionResultParameters.causeClassName != null) {
          Class<?> causeClazz = Class.forName(exceptionResultParameters.causeClassName);
          Constructor<?> causeConstructor =
              causeClazz.getDeclaredConstructor(new Class[] {String.class});
          causeConstructor.setAccessible(true);
          Throwable cause =
              (Throwable) causeConstructor.newInstance(exceptionResultParameters.message);

          Class<?> clazz = Class.forName(exceptionResultParameters.className);
          Constructor<?> constructor =
              clazz.getDeclaredConstructor(new Class[] {String.class, Throwable.class});
          constructor.setAccessible(true);
          exception = (Throwable) constructor.newInstance(exceptionResultParameters.message, cause);
        } else {
          Class<?> clazz = Class.forName(exceptionResultParameters.className);
          Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[] {String.class});
          constructor.setAccessible(true);
          exception = (Throwable) constructor.newInstance(exceptionResultParameters.message);
        }
        future.notifyException(exception);
      } else {
        future.notify(clientMessage);
      }
    }
Ejemplo n.º 17
0
  @Test
  public void shouldEncodeAndDecodeClientMessageCorrectly() {
    SafeBuffer byteBuffer = new SafeBuffer(new byte[512]);

    ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0);

    cmEncode
        .setMessageType(7)
        .setVersion((short) 3)
        .addFlag(ClientMessage.BEGIN_AND_END_FLAGS)
        .setCorrelationId(66)
        .setPartitionId(77);

    ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0);

    assertEquals(7, cmDecode.getMessageType());
    assertEquals(3, cmDecode.getVersion());
    assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode.getFlags());
    assertEquals(66, cmDecode.getCorrelationId());
    assertEquals(77, cmDecode.getPartitionId());
    assertEquals(ClientMessage.HEADER_SIZE, cmDecode.getFrameLength());
  }
  protected void send(ClientInvocation invocation, ClientConnection connection) throws IOException {
    if (isShutdown) {
      throw new HazelcastClientNotActiveException("Client is shut down");
    }
    registerInvocation(invocation);

    ClientMessage clientMessage = invocation.getClientMessage();
    if (!isAllowedToSendRequest(connection, invocation)
        || !writeToConnection(connection, clientMessage)) {
      final int callId = clientMessage.getCorrelationId();
      ClientInvocation clientInvocation = deRegisterCallId(callId);
      deRegisterEventHandler(callId);
      if (clientInvocation != null) {
        throw new IOException("Packet not send to " + connection.getRemoteEndpoint());
      } else {
        if (logger.isFinestEnabled()) {
          logger.finest("Invocation not found to deregister for call id " + callId);
        }
      }
    }

    invocation.setSendConnection(connection);
  }
  @Override
  public ICompletableFuture<ReadResultSet<E>> readManyAsync(
      long startSequence, int minCount, int maxCount, IFunction<E, Boolean> filter) {

    checkSequence(startSequence);
    checkNotNegative(minCount, "minCount can't be smaller than 0");
    checkTrue(maxCount >= minCount, "maxCount should be equal or larger than minCount");
    checkTrue(
        minCount <= capacity(), "the minCount should be smaller than or equal to the capacity");
    checkTrue(maxCount <= MAX_BATCH_SIZE, "maxCount can't be larger than " + MAX_BATCH_SIZE);

    ClientMessage request =
        RingbufferReadManyAsyncCodec.encodeRequest(
            name, startSequence, minCount, maxCount, toData(filter));
    request.setPartitionId(partitionId);

    try {
      ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request).invoke();
      return new ClientDelegatingFuture<ReadResultSet<E>>(
          invocationFuture, getContext().getSerializationService(), readManyAsyncResponseDecoder);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
  }
Ejemplo n.º 20
0
    @Override
    public void run() {
      try {
        int correlationId = clientMessage.getCorrelationId();
        final EventHandler eventHandler = eventHandlerMap.get(correlationId);
        if (eventHandler == null) {
          logger.warning(
              "No eventHandler for callId: " + correlationId + ", event: " + clientMessage);
          return;
        }

        eventHandler.handle(clientMessage);
      } finally {
        connection.decrementPendingPacketCount();
      }
    }
  @Test
  public void shouldAccumulateClientMessageCorrectly() {
    ClientMessage accumulator = ClientMessage.create();
    final ByteBuffer inBuffer = ByteBuffer.wrap(BYTE_DATA);
    accumulator.readFrom(inBuffer);

    final ByteBuffer byteBuffer = accumulatedByteBuffer(accumulator.buffer(), accumulator.index());
    assertEquals(0, byteBuffer.position());
    assertEquals(accumulator.getFrameLength(), byteBuffer.limit());

    for (int i = 0; i < byteBuffer.limit(); i++) {
      assertEquals(BYTE_DATA[i], byteBuffer.get());
    }
    assertTrue(accumulator.isComplete());
  }
Ejemplo n.º 22
0
  public void notifyException(Throwable exception) {
    if (!lifecycleService.isRunning()) {
      clientInvocationFuture.complete(
          new HazelcastClientNotActiveException(exception.getMessage(), exception));
      return;
    }

    if (isRetryable(exception)) {
      if (handleRetry()) {
        return;
      }
    }
    if (exception instanceof RetryableHazelcastException) {
      if (clientMessage.isRetryable() || invocationService.isRedoOperation()) {
        if (handleRetry()) {
          return;
        }
      }
    }
    clientInvocationFuture.complete(exception);
  }
Ejemplo n.º 23
0
  public void notifyException(Throwable exception) {

    if (!lifecycleService.isRunning()) {
      clientInvocationFuture.setResponse(
          new HazelcastClientNotActiveException(exception.getMessage()));
      return;
    }

    if (exception instanceof IOException
        || exception instanceof HazelcastInstanceNotActiveException
        || exception instanceof AuthenticationException) {
      if (handleRetry()) {
        return;
      }
    }
    if (exception instanceof RetryableHazelcastException) {
      if (clientMessage.isRetryable() || invocationService.isRedoOperation()) {
        if (handleRetry()) {
          return;
        }
      }
    }
    clientInvocationFuture.setResponse(exception);
  }
 @Override
 public int getPartitionId() {
   return clientMessage.getPartitionId();
 }
 protected void sendClientMessage(Object key, ClientMessage resultClientMessage) {
   int partitionId = key == null ? -1 : nodeEngine.getPartitionService().getPartitionId(key);
   resultClientMessage.setPartitionId(partitionId);
   sendClientMessage(resultClientMessage);
 }
Ejemplo n.º 26
0
 @Test(expected = IndexOutOfBoundsException.class)
 public void test_wrapForDecode_withSmallBuffer() {
   ClientMessage.createForDecode(new SafeBuffer(new byte[10]), 1);
 }
Ejemplo n.º 27
0
 @Test
 public void test_wrapForDecode_withHeaderSizeBuffer() {
   ClientMessage.createForDecode(new SafeBuffer(new byte[ClientMessage.HEADER_SIZE]), 0);
 }
Ejemplo n.º 28
0
 @Test
 public void test_wrapForDecode_withLargeBuffer() {
   ClientMessage.createForDecode(new SafeBuffer(new byte[100]), 10);
 }
Ejemplo n.º 29
0
  @Test
  public void testUnsignedFields() throws IOException {
    ClientProtocolBuffer buffer = new SafeBuffer(new byte[18]);

    ClientMessage cmEncode = ClientMessage.createForEncode(buffer, 0);

    cmEncode.setVersion((short) (Byte.MAX_VALUE + 10));
    cmEncode.setMessageType(Short.MAX_VALUE + 10);
    cmEncode.setDataOffset((int) Short.MAX_VALUE + 10);
    cmEncode.setCorrelationId(Short.MAX_VALUE + 10);

    ClientMessage cmDecode = ClientMessage.createForDecode(buffer, 0);

    assertEquals(Byte.MAX_VALUE + 10, cmDecode.getVersion());
    assertEquals(Short.MAX_VALUE + 10, cmDecode.getMessageType());
    assertEquals((int) Short.MAX_VALUE + 10, cmDecode.getDataOffset());
  }
 private boolean writeToConnection(ClientConnection connection, ClientMessage clientMessage) {
   clientMessage.addFlag(ClientMessage.BEGIN_AND_END_FLAGS);
   return connection.write(clientMessage);
 }