@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);
    }
  }
  /** Retries sending packet maximum 5 times until connection to target becomes available. */
  @Override
  public boolean transmit(Packet packet, Address target) {
    checkNotNull(packet, "Packet can't be null");
    checkNotNull(target, "target can't be null");

    return send(packet, target, null);
  }
  @Override
  public Future invokeOnTarget(Object operation, Address address) {
    checkNotNull(operation, "operation cannot be null");
    checkNotNull(address, "address cannot be null");

    Operation op = (Operation) operation;
    return operationService.invokeOnTarget(MapService.SERVICE_NAME, op, address);
  }
  @Override
  public V put(K key, V value, long ttl, TimeUnit timeUnit) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);

    Data valueData = toData(value);
    Data keyData = toData(key);
    ClientMessage request =
        ReplicatedMapPutCodec.encodeRequest(getName(), keyData, valueData, timeUnit.toMillis(ttl));
    ClientMessage response = invoke(request);
    return toObject(response);
  }
  public boolean put(K key, V value) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    Data valueData = toData(value);
    ClientMessage request =
        MultiMapPutCodec.encodeRequest(name, keyData, valueData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapPutCodec.ResponseParameters resultParameters =
        MultiMapPutCodec.decodeResponse(response);
    return resultParameters.response;
  }
 @Override
 public String addEntryListener(EntryListener<K, V> listener, K key) {
   checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
   final Data keyData = toData(key);
   EventHandler<ClientMessage> handler = createHandler(listener);
   return registerListener(createEntryListenerToKeyCodec(keyData), handler);
 }
  @Override
  public V get(Object key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    initNearCache();
    if (nearCache != null) {
      Object cached = nearCache.get(key);
      if (cached != null) {
        if (cached.equals(ClientNearCache.NULL_OBJECT)) {
          return null;
        }
        return (V) cached;
      }
    }

    Data keyData = toData(key);
    ClientMessage request = ReplicatedMapGetCodec.encodeRequest(getName(), keyData);
    ClientMessage response = invoke(request);

    ReplicatedMapGetCodec.ResponseParameters result =
        ReplicatedMapGetCodec.decodeResponse(response);

    V value = (V) toObject(result.response);
    if (nearCache != null) {
      nearCache.put(key, value);
    }
    return value;
  }
  public void forceUnlock(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    ClientMessage request = MultiMapForceUnlockCodec.encodeRequest(name, keyData);
    invoke(request, keyData);
  }
  public void unlock(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    ClientMessage request =
        MultiMapUnlockCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    invoke(request, keyData);
  }
  @Override
  public Future invokeOnPartitionOwner(Object operation, int partitionId) {
    checkNotNull(operation, "operation cannot be null");
    checkNotNegative(partitionId, "partitionId");

    Operation op = (Operation) operation;
    return operationService.invokeOnPartition(MapService.SERVICE_NAME, op, partitionId);
  }
예제 #11
0
  private static InetAddress resolve(InetSocketAddress inetSocketAddress) {
    checkNotNull(inetSocketAddress, "inetSocketAddress can't be null");

    InetAddress address = inetSocketAddress.getAddress();
    if (address == null) {
      throw new IllegalArgumentException("Can't resolve address: " + inetSocketAddress);
    }
    return address;
  }
  public boolean tryLock(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    try {
      return tryLock(key, 0, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      return false;
    }
  }
 @Override
 public String addEntryListener(EntryListener<K, V> listener, K key) {
   checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
   Data keyData = toData(key);
   ClientMessage request =
       ReplicatedMapAddEntryListenerToKeyCodec.encodeRequest(getName(), keyData);
   EventHandler<ClientMessage> handler = createHandler(listener);
   return listen(request, keyData, handler);
 }
 @Override
 public V remove(Object key) {
   checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
   Data keyData = toData(key);
   ClientMessage request = ReplicatedMapRemoveCodec.encodeRequest(getName(), keyData);
   ClientMessage response = invoke(request);
   ReplicatedMapRemoveCodec.ResponseParameters result =
       ReplicatedMapRemoveCodec.decodeResponse(response);
   return toObject(result.response);
 }
 @Override
 public boolean containsValue(Object value) {
   checkNotNull(value, NULL_KEY_IS_NOT_ALLOWED);
   Data valueData = toData(value);
   ClientMessage request = ReplicatedMapContainsValueCodec.encodeRequest(getName(), valueData);
   ClientMessage response = invoke(request);
   ReplicatedMapContainsValueCodec.ResponseParameters result =
       ReplicatedMapContainsValueCodec.decodeResponse(response);
   return result.response;
 }
  @Override
  public boolean transmit(Packet packet, Connection connection) {
    checkNotNull(packet, "Packet can't be null");

    if (connection == null) {
      return false;
    }

    return connection.write(packet);
  }
 @Override
 public boolean containsKey(Object key) {
   checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
   Data keyData = toData(key);
   ClientMessage request = ReplicatedMapContainsKeyCodec.encodeRequest(name, keyData);
   ClientMessage response = invoke(request, keyData);
   ReplicatedMapContainsKeyCodec.ResponseParameters result =
       ReplicatedMapContainsKeyCodec.decodeResponse(response);
   return result.response;
 }
  @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);
    }
  }
  public boolean isLocked(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    ClientMessage request = MultiMapIsLockedCodec.encodeRequest(name, keyData);
    ClientMessage response = invoke(request, keyData);
    MultiMapIsLockedCodec.ResponseParameters resultParameters =
        MultiMapIsLockedCodec.decodeResponse(response);
    return resultParameters.response;
  }
  public void lock(K key, long leaseTime, TimeUnit timeUnit) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    checkPositive(leaseTime, "leaseTime should be positive");

    final Data keyData = toData(key);
    ClientMessage request =
        MultiMapLockCodec.encodeRequest(
            name, keyData, ThreadUtil.getThreadId(), getTimeInMillis(leaseTime, timeUnit));
    invoke(request, keyData);
  }
  public boolean containsValue(Object value) {
    checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);

    Data keyValue = toData(value);
    ClientMessage request = MultiMapContainsValueCodec.encodeRequest(name, keyValue);
    ClientMessage response = invoke(request);
    MultiMapContainsValueCodec.ResponseParameters resultParameters =
        MultiMapContainsValueCodec.decodeResponse(response);
    return resultParameters.response;
  }
  public int valueCount(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    ClientMessage request =
        MultiMapValueCountCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapValueCountCodec.ResponseParameters resultParameters =
        MultiMapValueCountCodec.decodeResponse(response);
    return resultParameters.response;
  }
  @Override
  public void registerEndpoint(ClientEndpoint endpoint) {
    checkNotNull(endpoint, "endpoint can't be null");

    final Connection conn = endpoint.getConnection();
    if (endpoints.putIfAbsent(conn, endpoint) != null) {
      logger.severe("An endpoint already exists for connection:" + conn);
    } else {
      totalRegistrations.inc();
    }
  }
예제 #24
0
  public Address(String hostname, InetAddress inetAddress, int port) {
    checkNotNull(inetAddress, "inetAddress can't be null");

    type = (inetAddress instanceof Inet4Address) ? IPV4 : IPV6;
    String[] addressArgs = inetAddress.getHostAddress().split("\\%");
    host = hostname != null ? hostname : addressArgs[0];
    if (addressArgs.length == 2) {
      scopeId = addressArgs[1];
    }
    this.port = port;
    hostSet = !AddressUtil.isIpAddress(host);
  }
  @Override
  public Set<ClientEndpoint> getEndpoints(String clientUuid) {
    checkNotNull(clientUuid, "clientUuid can't be null");

    Set<ClientEndpoint> endpointSet = new HashSet<ClientEndpoint>();
    for (ClientEndpoint endpoint : endpoints.values()) {
      if (clientUuid.equals(endpoint.getUuid())) {
        endpointSet.add(endpoint);
      }
    }
    return endpointSet;
  }
  @Override
  public long add(E item) {
    checkNotNull(item, "item can't be null");

    Data element = toData(item);
    ClientMessage request =
        RingbufferAddCodec.encodeRequest(name, OverflowPolicy.OVERWRITE.getId(), element);
    ClientMessage response = invoke(request, partitionId);
    RingbufferAddCodec.ResponseParameters resultParameters =
        RingbufferAddCodec.decodeResponse(response);
    return resultParameters.response;
  }
  public Collection<V> get(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    ClientMessage request = MultiMapGetCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapGetCodec.ResponseParameters resultParameters =
        MultiMapGetCodec.decodeResponse(response);
    Collection<Data> result = resultParameters.list;
    Collection<V> resultCollection = new ArrayList<V>(result.size());
    for (Data data : result) {
      final V value = toObject(data);
      resultCollection.add(value);
    }
    return resultCollection;
  }
  @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);
  }
  public boolean tryLock(K key, long time, TimeUnit timeunit, long leaseTime, TimeUnit leaseUnit)
      throws InterruptedException {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    long timeoutInMillis = getTimeInMillis(time, timeunit);
    long leaseTimeInMillis = getTimeInMillis(leaseTime, leaseUnit);

    long threadId = ThreadUtil.getThreadId();
    ClientMessage request =
        MultiMapTryLockCodec.encodeRequest(
            name, keyData, threadId, leaseTimeInMillis, timeoutInMillis);
    ClientMessage response = invoke(request, keyData);
    MultiMapTryLockCodec.ResponseParameters resultParameters =
        MultiMapTryLockCodec.decodeResponse(response);
    return resultParameters.response;
  }
예제 #30
0
  /**
   * Registers all the metrics in this metric pack.
   *
   * @param metricsRegistry the MetricsRegistry upon which the metrics are registered.
   */
  public static void register(MetricsRegistry metricsRegistry) {
    checkNotNull(metricsRegistry, "metricsRegistry");

    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();

    metricsRegistry.register(
        mxBean,
        "thread.threadCount",
        new LongProbeFunction<ThreadMXBean>() {
          @Override
          public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getThreadCount();
          }
        });

    metricsRegistry.register(
        mxBean,
        "thread.peakThreadCount",
        new LongProbeFunction<ThreadMXBean>() {
          @Override
          public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getPeakThreadCount();
          }
        });

    metricsRegistry.register(
        mxBean,
        "thread.daemonThreadCount",
        new LongProbeFunction<ThreadMXBean>() {
          @Override
          public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getDaemonThreadCount();
          }
        });

    metricsRegistry.register(
        mxBean,
        "thread.totalStartedThreadCount",
        new LongProbeFunction<ThreadMXBean>() {
          @Override
          public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getTotalStartedThreadCount();
          }
        });
  }