protected <T> InternalCompletableFuture<T> replaceAsyncInternal(
     K key,
     V oldValue,
     V newValue,
     ExpiryPolicy expiryPolicy,
     boolean hasOldValue,
     boolean isGet,
     boolean withCompletionEvent) {
   ensureOpen();
   if (hasOldValue) {
     validateNotNull(key, oldValue, newValue);
     validateConfiguredTypes(true, key, oldValue, newValue);
   } else {
     validateNotNull(key, newValue);
     validateConfiguredTypes(true, key, newValue);
   }
   final Data keyData = serializationService.toData(key);
   final Data oldValueData = oldValue != null ? serializationService.toData(oldValue) : null;
   final Data newValueData = serializationService.toData(newValue);
   final Operation operation;
   if (isGet) {
     operation =
         new CacheGetAndReplaceOperation(
             getDistributedObjectName(), keyData, newValueData, expiryPolicy);
   } else {
     operation =
         new CacheReplaceOperation(
             getDistributedObjectName(), keyData, oldValueData, newValueData, expiryPolicy);
   }
   return invoke(operation, keyData, withCompletionEvent);
 }
 private Result processQueryEventFilter(
     EventFilter filter,
     EntryEventType eventType,
     final Data dataKey,
     Data dataOldValue,
     Data dataValue) {
   final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
   final SerializationService serializationService = nodeEngine.getSerializationService();
   Object testValue;
   if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) {
     testValue = serializationService.toObject(dataOldValue);
   } else {
     testValue = serializationService.toObject(dataValue);
   }
   Object key = serializationService.toObject(dataKey);
   QueryEventFilter queryEventFilter = (QueryEventFilter) filter;
   QueryEntry entry = new QueryEntry(serializationService, dataKey, key, testValue);
   if (queryEventFilter.eval(entry)) {
     if (queryEventFilter.isIncludeValue()) {
       return Result.VALUE_INCLUDED;
     } else {
       return Result.NO_VALUE_INCLUDED;
     }
   }
   return Result.NONE;
 }
 @Override
 public void innerBeforeRun() {
   super.innerBeforeRun();
   final SerializationService serializationService = getNodeEngine().getSerializationService();
   final ManagedContext managedContext = serializationService.getManagedContext();
   managedContext.initialize(entryProcessor);
 }
 private Object authenticate(
     ClientConnection connection,
     Credentials credentials,
     ClientPrincipal principal,
     boolean firstConnection)
     throws IOException {
   final SerializationService ss = getSerializationService();
   AuthenticationRequest auth = new AuthenticationRequest(credentials, principal);
   connection.init();
   auth.setOwnerConnection(firstConnection);
   // contains remoteAddress and principal
   SerializableCollection collectionWrapper;
   try {
     collectionWrapper = (SerializableCollection) sendAndReceive(auth, connection);
   } catch (Exception e) {
     throw new RetryableIOException(e);
   }
   final Iterator<Data> iter = collectionWrapper.iterator();
   if (iter.hasNext()) {
     final Data addressData = iter.next();
     final Address address = ss.toObject(addressData);
     connection.setRemoteEndpoint(address);
     if (iter.hasNext()) {
       final Data principalData = iter.next();
       return ss.toObject(principalData);
     }
   }
   throw new AuthenticationException();
 }
 public <T> T sendAndReceiveFixedConnection(Connection conn, Object obj) throws IOException {
   final SerializationService serializationService = getSerializationService();
   final Data request = serializationService.toData(obj);
   conn.write(request);
   final Data response = conn.read();
   final Object result = serializationService.toObject(response);
   return ErrorHandler.returnResultOrThrowException(result);
 }
Exemplo n.º 6
0
 public void sendResponse(ClientEndpoint endpoint, Object response) {
   if (response instanceof Throwable) {
     response =
         ClientExceptionConverters.get(endpoint.getClientType()).convert((Throwable) response);
   }
   final Data resultData = response != null ? serializationService.toData(response) : NULL;
   Connection conn = endpoint.getConnection();
   conn.write(new DataAdapter(resultData, serializationService.getSerializationContext()));
 }
 protected InternalCompletableFuture<Boolean> putIfAbsentAsyncInternal(
     K key, V value, ExpiryPolicy expiryPolicy, boolean withCompletionEvent) {
   ensureOpen();
   validateNotNull(key, value);
   validateConfiguredTypes(true, key, value);
   final Data keyData = serializationService.toData(key);
   final Data valueData = serializationService.toData(value);
   final Operation op =
       new CachePutIfAbsentOperation(getDistributedObjectName(), keyData, valueData, expiryPolicy);
   return invoke(op, keyData, withCompletionEvent);
 }
 @Test
 public void testAddAll() throws IOException {
   List<Data> list = new ArrayList<Data>();
   list.add(ss.toData("item1"));
   list.add(ss.toData("item2"));
   list.add(ss.toData("item3"));
   list.add(ss.toData("item4"));
   final SimpleClient client = getClient();
   client.send(new AddAllRequest(queueName, list));
   Object result = client.receive();
   assertTrue((Boolean) result);
   int size = getQueue().size();
   assertEquals(size, list.size());
 }
  private void listenMembershipEvents() throws IOException {
    final SerializationService serializationService = clusterService.getSerializationService();
    while (!Thread.currentThread().isInterrupted()) {
      final Data clientResponseData = conn.read();
      final ClientResponse clientResponse = serializationService.toObject(clientResponseData);
      final Object eventObject = serializationService.toObject(clientResponse.getResponse());
      final ClientMembershipEvent event = (ClientMembershipEvent) eventObject;
      final MemberImpl member = (MemberImpl) event.getMember();
      boolean membersUpdated = false;
      if (event.getEventType() == MembershipEvent.MEMBER_ADDED) {
        members.add(member);
        membersUpdated = true;
      } else if (event.getEventType() == ClientMembershipEvent.MEMBER_REMOVED) {
        members.remove(member);
        membersUpdated = true;
        //                    getConnectionManager().removeConnectionPool(member.getAddress());
        // //TODO
      } else if (event.getEventType() == ClientMembershipEvent.MEMBER_ATTRIBUTE_CHANGED) {
        MemberAttributeChange memberAttributeChange = event.getMemberAttributeChange();
        Map<Address, MemberImpl> memberMap = clusterService.getMembersRef();
        if (memberMap != null) {
          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;
            }
          }
        }
      }

      if (membersUpdated) {
        ((ClientPartitionServiceImpl) client.getClientPartitionService()).refreshPartitions();
        updateMembersRef();
        LOGGER.info(clusterService.membersString());
        clusterService.fireMembershipEvent(
            new MembershipEvent(
                client.getCluster(),
                member,
                event.getEventType(),
                Collections.unmodifiableSet(new LinkedHashSet<Member>(members))));
      }
    }
  }
Exemplo n.º 10
0
 @Override
 public Object sendAndReceive(ClientRequest request, ClientConnection connection)
     throws Exception {
   final SerializationService ss = client.getSerializationService();
   connection.write(ss.toData(request));
   final Data data = connection.read();
   ClientResponse clientResponse = ss.toObject(data);
   Object response = ss.toObject(clientResponse.getResponse());
   if (response instanceof Throwable) {
     Throwable t = (Throwable) response;
     ExceptionUtil.fixRemoteStackTrace(t, Thread.currentThread().getStackTrace());
     throw new Exception(t);
   }
   return response;
 }
Exemplo n.º 11
0
    @Override
    public void handle(Data dataItem, String uuid, int eventType) {
      SerializationService serializationService = getContext().getSerializationService();
      ClientClusterService clusterService = getContext().getClusterService();

      E item = includeValue ? (E) serializationService.toObject(dataItem) : null;
      Member member = clusterService.getMember(uuid);
      ItemEvent<E> itemEvent =
          new ItemEvent<E>(name, ItemEventType.getByType(eventType), item, member);
      if (eventType == ItemEventType.ADDED.getType()) {
        listener.itemAdded(itemEvent);
      } else {
        listener.itemRemoved(itemEvent);
      }
    }
Exemplo n.º 12
0
 @Override
 public Data getKeyData() {
   if (keyData == null && serializationService != null) {
     keyData = serializationService.toData(keyObject);
   }
   return keyData;
 }
Exemplo n.º 13
0
 public CacheRecord newRecordWithExpiry(Data key, Object value, long expiryTime) {
   final CacheRecord record;
   switch (inMemoryFormat) {
     case BINARY:
       Data dataValue = serializationService.toData(value);
       record = new CacheDataRecord(key, dataValue, expiryTime);
       break;
     case OBJECT:
       Object objectValue = serializationService.toObject(value);
       record = new CacheObjectRecord(key, objectValue, expiryTime);
       break;
     default:
       throw new IllegalArgumentException("Invalid storage format: " + inMemoryFormat);
   }
   return record;
 }
Exemplo n.º 14
0
 @Override
 public Data getValueData() {
   if (valueData == null && serializationService != null) {
     valueData = serializationService.toData(valueObject);
   }
   return valueData;
 }
Exemplo n.º 15
0
  private void _sendAndHandle(
      ConnectionFactory connectionFactory, Object obj, ResponseHandler handler) throws IOException {
    ResponseStream stream = null;
    while (stream == null) {
      if (!active) {
        throw new HazelcastInstanceNotActiveException();
      }
      Connection conn = null;
      try {
        conn = connectionFactory.create();
        final SerializationService serializationService = getSerializationService();
        final Data request = serializationService.toData(obj);
        conn.write(request);
        stream = new ResponseStreamImpl(serializationService, conn);
      } catch (Exception e) {
        if (e instanceof IOException) {
          if (logger.isFinestEnabled()) {
            logger.finest("Error on connection... conn: " + conn + ", error: " + e);
          }
        }
        if (conn != null) {
          IOUtil.closeResource(conn);
        }
        if (ErrorHandler.isRetryable(e)) {
          if (redoOperation || obj instanceof RetryableRequest) {
            if (logger.isFinestEnabled()) {
              logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
            }
            beforeRetry();
            continue;
          }
        }
        if (e instanceof IOException && !active) {
          continue;
        }
        throw ExceptionUtil.rethrow(e, IOException.class);
      }
    }

    try {
      handler.handle(stream);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e, IOException.class);
    } finally {
      stream.end();
    }
  }
Exemplo n.º 16
0
 @Override
 public Object getKey() {
   // TODO: What is serialization service is null??
   if (keyObject == null && serializationService != null) {
     keyObject = serializationService.toObject(keyData);
   }
   return keyObject;
 }
Exemplo n.º 17
0
 @Override
 public void init(NodeEngine nodeEngine, Properties properties) {
   ClassDefinitionBuilder builder =
       new ClassDefinitionBuilder(ClientPortableHook.ID, ClientPortableHook.PRINCIPAL);
   builder.addUTFField("uuid").addUTFField("ownerUuid");
   serializationService.getSerializationContext().registerClassDefinition(builder.build());
   node.getConnectionManager().addConnectionListener(this);
 }
Exemplo n.º 18
0
 @Override
 public Object getValue() {
   // TODO: What is serialization service is null??
   if (valueObject == null && serializationService != null) {
     valueObject = serializationService.toObject(valueData);
   }
   return valueObject;
 }
Exemplo n.º 19
0
  @Test
  public void testRemove() throws IOException {

    final IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");

    final SimpleClient client = getClient();
    client.send(new RemoveRequest(queueName, ss.toData("item2")));
    Boolean result = (Boolean) client.receive();
    assertTrue(result);
    assertEquals(2, q.size());

    client.send(new RemoveRequest(queueName, ss.toData("item2")));
    result = (Boolean) client.receive();
    assertFalse(result);
    assertEquals(2, q.size());
  }
Exemplo n.º 20
0
 private <T> T _sendAndReceive(ConnectionFactory connectionFactory, Object obj)
     throws IOException {
   while (active) {
     Connection conn = null;
     boolean release = true;
     try {
       conn = connectionFactory.create();
       final SerializationService serializationService = getSerializationService();
       final Data request = serializationService.toData(obj);
       conn.write(request);
       final Data response = conn.read();
       final Object result = serializationService.toObject(response);
       return ErrorHandler.returnResultOrThrowException(result);
     } catch (Exception e) {
       if (e instanceof IOException) {
         if (logger.isFinestEnabled()) {
           logger.finest("Error on connection... conn: " + conn + ", error: " + e);
         }
         IOUtil.closeResource(conn);
         release = false;
       }
       if (ErrorHandler.isRetryable(e)) {
         if (redoOperation || obj instanceof RetryableRequest) {
           if (logger.isFinestEnabled()) {
             logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
           }
           beforeRetry();
           continue;
         }
       }
       if (e instanceof IOException && !active) {
         continue;
       }
       throw ExceptionUtil.rethrow(e, IOException.class);
     } finally {
       if (release && conn != null) {
         conn.release();
       }
     }
   }
   throw new HazelcastInstanceNotActiveException();
 }
Exemplo n.º 21
0
  @Test
  public void testAddListener() throws IOException {
    final SimpleClient client = getClient();
    client.send(new AddListenerRequest(queueName, true));
    client.receive();
    getQueue().offer("item");

    PortableItemEvent result = (PortableItemEvent) client.receive();
    assertEquals("item", ss.toObject(result.getItem()));
    assertEquals(ItemEventType.ADDED, result.getEventType());
  }
Exemplo n.º 22
0
  private Object authenticate(
      Connection connection,
      Credentials credentials,
      ClientPrincipal principal,
      boolean reAuth,
      boolean firstConnection)
      throws IOException {
    AuthenticationRequest auth = new AuthenticationRequest(credentials, principal);
    auth.setReAuth(reAuth);
    auth.setFirstConnection(firstConnection);
    final SerializationService serializationService = getSerializationService();
    connection.write(serializationService.toData(auth));
    final Data addressData = connection.read();
    Address address =
        ErrorHandler.returnResultOrThrowException(serializationService.toObject(addressData));
    connection.setRemoteEndpoint(address);

    final Data data = connection.read();
    return ErrorHandler.returnResultOrThrowException(serializationService.toObject(data));
  }
Exemplo n.º 23
0
    private void loadInitialMemberList() throws IOException {
      final SerializationService serializationService = getSerializationService();
      final Data request = serializationService.toData(new AddMembershipListenerRequest());
      conn.write(request);
      final Data response = conn.read();
      SerializableCollection coll =
          ErrorHandler.returnResultOrThrowException(serializationService.toObject(response));

      Map<String, MemberImpl> prevMembers = Collections.emptyMap();
      if (!members.isEmpty()) {
        prevMembers = new HashMap<String, MemberImpl>(members.size());
        for (MemberImpl member : members) {
          prevMembers.put(member.getUuid(), member);
        }
        members.clear();
      }
      for (Data d : coll.getCollection()) {
        members.add((MemberImpl) serializationService.toObject(d));
      }
      updateMembersRef();
      logger.info(membersString());
      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));
      }
      for (MembershipEvent event : events) {
        fireMembershipEvent(event);
      }
    }
  private void loadInitialMemberList() throws Exception {
    final SerializationService serializationService = clusterService.getSerializationService();
    final AddMembershipListenerRequest request = new AddMembershipListenerRequest();
    final SerializableCollection coll =
        (SerializableCollection) connectionManager.sendAndReceive(request, conn);

    Map<String, MemberImpl> prevMembers = Collections.emptyMap();
    if (!members.isEmpty()) {
      prevMembers = new HashMap<String, MemberImpl>(members.size());
      for (MemberImpl member : members) {
        prevMembers.put(member.getUuid(), member);
      }
      members.clear();
    }
    for (Data data : coll) {
      members.add((MemberImpl) serializationService.toObject(data));
    }
    updateMembersRef();
    LOGGER.info(clusterService.membersString());
    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));
    }
    for (MembershipEvent event : events) {
      clusterService.fireMembershipEvent(event);
    }
    latch.countDown();
  }
Exemplo n.º 25
0
  @Test
  public void testOffer() throws IOException, InterruptedException {
    final IQueue q = getQueue();

    final SimpleClient client = getClient();
    client.send(new OfferRequest(queueName, ss.toData("item1")));
    Object result = client.receive();
    assertTrue((Boolean) result);
    Object item = q.peek();
    assertEquals(item, "item1");

    q.offer("item2");
    q.offer("item3");
    q.offer("item4");
    q.offer("item5");
    q.offer("item6");

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          latch.await(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        q.poll();
      }
    }.start();

    client.send(new OfferRequest(queueName, 500, ss.toData("item7")));
    result = client.receive();
    assertFalse((Boolean) result);

    client.send(new OfferRequest(queueName, 10 * 1000, ss.toData("item7")));
    Thread.sleep(1000);
    latch.countDown();
    result = client.receive();
    assertTrue((Boolean) result);
  }
Exemplo n.º 26
0
 private void listenMembershipEvents() throws IOException {
   final SerializationService serializationService = getSerializationService();
   while (!Thread.currentThread().isInterrupted()) {
     final Data eventData = conn.read();
     final ClientMembershipEvent event =
         (ClientMembershipEvent) serializationService.toObject(eventData);
     final MemberImpl member = (MemberImpl) event.getMember();
     if (event.getEventType() == MembershipEvent.MEMBER_ADDED) {
       members.add(member);
     } else {
       members.remove(member);
       getConnectionManager().removeConnectionPool(member.getAddress());
     }
     updateMembersRef();
     logger.info(membersString());
     fireMembershipEvent(
         new MembershipEvent(
             client.getCluster(),
             member,
             event.getEventType(),
             Collections.unmodifiableSet(new LinkedHashSet<Member>(members))));
   }
 }
  protected void putAndGetFromCacheRecordStore(
      ICacheRecordStore cacheRecordStore, InMemoryFormat inMemoryFormat) {
    SerializationService serializationService = new DefaultSerializationServiceBuilder().build();

    for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
      cacheRecordStore.put(serializationService.toData(i), "value-" + i, null, null, -1);
    }

    if (inMemoryFormat == InMemoryFormat.BINARY || inMemoryFormat == InMemoryFormat.NATIVE) {
      for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
        assertTrue(
            Data.class.isAssignableFrom(
                cacheRecordStore.get(serializationService.toData(i), null).getClass()));
      }
    } else if (inMemoryFormat == InMemoryFormat.OBJECT) {
      for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
        assertTrue(
            String.class.isAssignableFrom(
                cacheRecordStore.get(serializationService.toData(i), null).getClass()));
      }
    } else {
      throw new IllegalArgumentException("Unsupported in-memory format: " + inMemoryFormat);
    }
  }
Exemplo n.º 28
0
  @Test
  public void testContains() throws IOException {
    IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");
    q.offer("item4");
    q.offer("item5");

    List<Data> list = new ArrayList<Data>();
    list.add(ss.toData("item1"));
    list.add(ss.toData("item2"));

    final SimpleClient client = getClient();
    client.send(new ContainsRequest(queueName, list));
    Boolean result = (Boolean) client.receive();
    assertTrue(result);

    list.add(ss.toData("item0"));

    client.send(new ContainsRequest(queueName, list));
    result = (Boolean) client.receive();
    assertFalse(result);
  }
Exemplo n.º 29
0
  @Test
  public void testDrain() throws IOException {
    IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");
    q.offer("item4");
    q.offer("item5");

    final SimpleClient client = getClient();
    client.send(new DrainRequest(queueName, 1));
    PortableCollection result = (PortableCollection) client.receive();
    Collection<Data> coll = result.getCollection();
    assertEquals(1, coll.size());
    assertEquals("item1", ss.toObject(coll.iterator().next()));
    assertEquals(4, q.size());
  }
Exemplo n.º 30
0
  @Override
  public AttributeType getAttributeType(String attributeName) {
    if (KEY_ATTRIBUTE_NAME.equals(attributeName)) {
      return ReflectionHelper.getAttributeType(getKey().getClass());
    } else if (THIS_ATTRIBUTE_NAME.equals(attributeName)) {
      return ReflectionHelper.getAttributeType(getValue().getClass());
    }

    boolean isKey = isKey(attributeName);
    attributeName = getAttributeName(isKey, attributeName);
    Data data = getOptionalTargetData(isKey);

    if (data != null && data.isPortable()) {
      PortableContext portableContext = serializationService.getPortableContext();
      return PortableExtractor.getAttributeType(portableContext, data, attributeName);
    }
    return ReflectionHelper.getAttributeType(isKey ? getKey() : getValue(), attributeName);
  }