Example #1
0
 def put(self, key, value, ttl=0):
     check_not_none(key, "key can't be None")
     check_not_none(key, "value can't be None")
     key_data = self._to_data(key)
     value_data = self._to_data(value)
     return self._encode_invoke_on_key(replicated_map_put_codec, key_data, key=key_data, value=value_data,
                                       ttl=to_millis(ttl))
    def try_remove(self, key, timeout=0):
        check_not_none(key, "key can't be None")

        key_data = self._to_data(key)

        return self._encode_invoke_on_key(map_try_remove_codec, key_data, key=key_data,
                                          thread_id=thread_id(), timeout=to_millis(timeout))
    def try_put(self, key, value, timeout=0):
        check_not_none(key, "key can't be None")
        check_not_none(value, "value can't be None")

        key_data = self._to_data(key)
        value_data = self._to_data(value)

        return self._encode_invoke_on_key(map_try_put_codec, key_data, key=key_data, value=value_data,
                                          thread_id=thread_id(), timeout=to_millis(timeout))
 def set(self, key, value, ttl=-1):
     """
     :param key:
     :param value:
     :param ttl:
     :return:
     """
     check_not_none(key, "key can't be None")
     check_not_none(value, "value can't be None")
     key_data = self._to_data(key)
     value_data = self._to_data(value)
     return self._encode_invoke_on_key(map_set_codec, key_data, key=key_data, value=value_data,
                                       thread_id=thread_id(),
                                       ttl=to_millis(ttl))
    def init(self, id):
        """
        This method does nothing and will simply tell if the next ID
        will be larger than the given ID.
        You don't need to call this method on cluster restart - uniqueness is preserved thanks to the
        timestamp component of the ID.
        This method exists to make :class:`~hazelcast.proxy.FlakeIdGenerator` drop-in replacement
        for the deprecated :class:`~hazelcast.proxy.IdGenerator`.

        :param id: (int), ID to compare.
        :return: (bool), True if the next ID will be larger than the supplied id, False otherwise.
        """

        # Add 1 hour worth of IDs as a reserve: due to long batch validity some clients might be still getting
        # older IDs. 1 hour is just a safe enough value, not a real guarantee: some clients might have longer
        # validity.
        # The init method should normally be called before any client generated IDs: in this case no reserve is
        # needed, so we don't want to increase the reserve excessively.
        reserve = to_millis(TimeUnit.HOUR) << (FlakeIdGenerator._BITS_NODE_ID +
                                               FlakeIdGenerator._BITS_SEQUENCE)
        return self.new_id().continue_with(lambda f: f.result() >=
                                           (id + reserve))
Example #6
0
    def poll(self, timeout: float = 0) -> Future[typing.Optional[ItemType]]:
        """Retrieves and removes the head of this queue.

        If this queue is empty:

        - If the timeout is provided, it waits until this timeout elapses
          and returns the result.
        - If the timeout is not provided, returns ``None``.

        Args:
            timeout: Maximum time in seconds to wait for addition.

        Returns:
            The head of this queue, or ``None`` if this queue is empty or
            specified timeout elapses before an item is added to the queue.
        """
        def handler(message):
            return self._to_object(queue_poll_codec.decode_response(message))

        request = queue_poll_codec.encode_request(self.name,
                                                  to_millis(timeout))
        return self._invoke(request, handler)
Example #7
0
    def await_latch(self, timeout):
        """Causes the current thread to wait until the latch has counted down to
        zero, or an exception is thrown, or the specified waiting time elapses.

        If the current count is zero then this method returns ``True``.

        If the current count is greater than zero, then the current
        thread becomes disabled for thread scheduling purposes and lies
        dormant until one of the following things happen:

        - The count reaches zero due to invocations of the ``count_down()`` method
        - This CountDownLatch instance is destroyed
        - The countdown owner becomes disconnected
        - The specified waiting time elapses

        If the count reaches zero, then the method returns with the
        value ``True``.

        If the specified waiting time elapses then the value ``False``
        is returned.  If the time is less than or equal to zero, the method
        will not wait at all.

        Args:
            timeout (int): The maximum time to wait in seconds

        Returns:
            hazelcast.future.Future[bool]: ``True`` if the count reached zero,
            ``False`` if the waiting time elapsed before the count reached zero
        Raises:
            IllegalStateError: If the Hazelcast instance was shut down while waiting.
        """
        check_is_number(timeout)
        timeout = max(0, timeout)
        invocation_uuid = uuid.uuid4()
        codec = count_down_latch_await_codec
        request = codec.encode_request(self._group_id, self._object_name,
                                       invocation_uuid, to_millis(timeout))
        return self._invoke(request, codec.decode_response)
Example #8
0
    def lock(self, key, lease_time=-1):
        """
        Acquires the lock for the specified key infinitely or for the specified lease time if provided.

        If the lock is not available, the current thread becomes disabled for thread scheduling purposes and lies
        dormant until the lock has been acquired.

        Scope of the lock is this map only. Acquired lock is only for the key in this map.

        Locks are re-entrant; so, if the key is locked N times, it should be unlocked N times before another thread can
        acquire it.

        **Warning: This method uses __hash__ and __eq__ methods of binary form of the key, not the actual implementations
        of __hash__ and __eq__ defined in key's class.**

        :param key: (object), the key to lock.
        :param lease_time: (int), time in seconds to wait before releasing the lock (optional).
        """
        check_not_none(key, "key can't be None")
        key_data = self._to_data(key)
        return self._encode_invoke_on_key(multi_map_lock_codec, key_data, key=key_data,
                                          thread_id=thread_id(), ttl=to_millis(lease_time),
                                          reference_id=self.reference_id_generator.get_and_increment())
    def try_acquire(self, permits=1, timeout=0):
        """
        Tries to acquire one or the given number of permits, if they are available, and returns immediately, with the
        value ``true``, reducing the number of available permits by the given amount.

        If there are insufficient permits and a timeout is provided, the current thread becomes disabled for thread
        scheduling purposes and lies dormant until one of following happens:
            * some other thread invokes the release() method for this semaphore and the current thread is next to be
            assigned a permit, or
            * some other thread interrupts the current thread, or
            * the specified waiting time elapses.

        If there are insufficient permits and no timeout is provided, this method will return immediately with the value
        ``false`` and the number of available permits is unchanged.

        :param permits: (int), the number of permits to acquire (optional).
        :param timeout: (long), the maximum time in seconds to wait for the permit(s) (optional).
        :return: (bool), ``true`` if desired amount of permits was acquired, ``false`` otherwise.
        """
        check_not_negative(permits, "Permits cannot be negative!")
        return self._encode_invoke(semaphore_try_acquire_codec,
                                   permits=permits,
                                   timeout=to_millis(timeout))
Example #10
0
    def await (self, timeout):
        """
        Causes the current thread to wait until the latch has counted down to zero, or the specified waiting time
        elapses.

        If the current count is zero then this method returns immediately with the value ``true``.

        If the current count is greater than zero, then the current thread becomes disabled for thread scheduling
        purposes and lies dormant until one of following happens:

            * the count reaches zero due to invocations of the countDown() method,
            * this CountDownLatch instance is destroyed,
            * the countdown owner becomes disconnected,
            * some other thread interrupts the current thread, or
            * the specified waiting time elapses.

        If the count reaches zero, then the method returns with the value ``true``.

        :param timeout: (long), the maximum time in seconds to wait.
        :return: (bool), ``true`` if the count reached zero, ``false`` if the waiting time elapsed before the count reached zero.
        """
        return self._encode_invoke(count_down_latch_await_codec,
                                   timeout=to_millis(timeout))
Example #11
0
    def lock(self, key: KeyType, lease_time: float = None) -> Future[None]:
        """Acquires the lock for the specified key infinitely or for the
        specified lease time if provided.

        If the lock is not available, the current thread becomes disabled for
        thread scheduling purposes and lies dormant until the lock has been
        acquired.

        Scope of the lock is this map only. Acquired lock is only for the key
        in this map.

        Locks are re-entrant; so, if the key is locked N times, it should be
        unlocked N times before another thread can acquire it.

        Warning:
            This method uses ``__hash__`` and ``__eq__`` methods of binary form
            of the key, not the actual implementations of ``__hash__`` and
            ``__eq__`` defined in key's class.

        Args:
            key: The key to lock.
            lease_time: Time in seconds to wait before releasing the lock.
        """
        check_not_none(key, "key can't be None")
        try:
            key_data = self._to_data(key)
        except SchemaNotReplicatedError as e:
            return self._send_schema_and_retry(e, self.lock, key, lease_time)

        request = multi_map_lock_codec.encode_request(
            self.name,
            key_data,
            thread_id(),
            to_millis(lease_time),
            self._reference_id_generator.get_and_increment(),
        )
        return self._invoke_on_key(request, key_data)
    def put(self,
            key: KeyType,
            value: ValueType,
            ttl: float = 0) -> Future[typing.Optional[ValueType]]:
        """Associates the specified value with the specified key in this map.

        If the map previously contained a mapping for the key, the old value
        is replaced by the specified value. If ttl is provided, entry will
        expire and get evicted after the ttl.

        Args:
            key: The specified key.
            value: The value to associate with the key.
            ttl: Maximum time in seconds for this entry to stay, if not
                provided, the value configured on server side configuration
                will be used.

        Returns:
            Previous value associated with key or ``None`` if there was no
            mapping for key.
        """
        check_not_none(key, "key can't be None")
        check_not_none(key, "value can't be None")
        try:
            key_data = self._to_data(key)
            value_data = self._to_data(value)
        except SchemaNotReplicatedError as e:
            return self._send_schema_and_retry(e, self.put, key, value, ttl)

        def handler(message):
            return self._to_object(
                replicated_map_put_codec.decode_response(message))

        request = replicated_map_put_codec.encode_request(
            self.name, key_data, value_data, to_millis(ttl))
        return self._invoke_on_key(request, key_data, handler)
    def put(self,
            key: KeyType,
            value: ValueType,
            ttl: float = None) -> typing.Optional[ValueType]:
        """Transactional implementation of
        :func:`Map.put(key, value, ttl) <hazelcast.proxy.map.Map.put>`

        The object to be put will be accessible only in the current transaction
        context till the transaction is committed.

        Args:
            key: The specified key.
            value: The value to associate with the key.
            ttl: Maximum time in seconds for this entry to stay.

        Returns:
            Previous value associated with key or ``None`` if there was no
            mapping for key.
        """
        check_not_none(key, "key can't be none")
        check_not_none(value, "value can't be none")
        try:
            key_data = self._to_data(key)
            value_data = self._to_data(value)
        except SchemaNotReplicatedError as e:
            self._send_schema(e)
            return self.put(key, value, ttl)

        def handler(message):
            return self._to_object(
                transactional_map_put_codec.decode_response(message))

        request = transactional_map_put_codec.encode_request(
            self.name, self.transaction.id, thread_id(), key_data, value_data,
            to_millis(ttl))
        return self._invoke(request, handler)
Example #14
0
    def offer(self, item: ItemType, timeout: float = 0) -> bool:
        """Transactional implementation of
        :func:`Queue.offer(item, timeout) <hazelcast.proxy.queue.Queue.offer>`

        Args:
            item: The item to be added.
            timeout: Maximum time in seconds to wait for addition.

        Returns:
            ``True`` if the element was added to this queue, ``False``
            otherwise.
        """
        check_not_none(item, "item can't be none")
        try:
            item_data = self._to_data(item)
        except SchemaNotReplicatedError as e:
            self._send_schema(e)
            return self.offer(item, timeout)

        request = transactional_queue_offer_codec.encode_request(
            self.name, self.transaction.id, thread_id(), item_data,
            to_millis(timeout))
        return self._invoke(request,
                            transactional_queue_offer_codec.decode_response)
    def put(self, key, value, ttl=0):
        """
        Associates the specified value with the specified key in this map. If the map previously contained a mapping for
        the key, the old value is replaced by the specified value. If ttl is provided, entry will expire and get evicted
        after the ttl.

        :param key: (object), the specified key.
        :param value: (object), the value to associate with the key.
        :param ttl: (int), maximum time in seconds for this entry to stay, if not provided, the value configured on
            server side configuration will be used(optional).
        :return: (object), previous value associated with key or None if there was no mapping for key.
        """
        check_not_none(key, "key can't be None")
        check_not_none(key, "value can't be None")

        def handler(message):
            return self._to_object(
                replicated_map_put_codec.decode_response(message))

        key_data = self._to_data(key)
        value_data = self._to_data(value)
        request = replicated_map_put_codec.encode_request(
            self.name, key_data, value_data, to_millis(ttl))
        return self._invoke_on_key(request, key_data, handler)
Example #16
0
 def lock(self, key, lease_time=-1):
     check_not_none(key, "key can't be None")
     key_data = self._to_data(key)
     return self._encode_invoke_on_key(multi_map_lock_codec, key_data, key=key_data,
                                       thread_id=thread_id(), ttl=to_millis(lease_time))
Example #17
0
 def _try_put_internal(self, key_data, value_data, timeout):
     return self._encode_invoke_on_key(map_try_put_codec, key_data, key=key_data, value=value_data,
                                       thread_id=thread_id(), timeout=to_millis(timeout))
Example #18
0
 def put(self, key, value, ttl=-1):
     check_not_none(key, "key can't be none")
     check_not_none(value, "value can't be none")
     return self._encode_invoke(transactional_map_put_codec, key=self._to_data(key),
                                value=self._to_data(value), ttl=to_millis(ttl))
Example #19
0
 def lock(self, lease_time=-1):
     return self._encode_invoke(lock_lock_codec,
                                lease_time=to_millis(lease_time),
                                thread_id=thread_id())
 def offer(self, item, timeout=0):
     check_not_none(item, "item can't be none")
     return self._encode_invoke(transactional_queue_offer_codec, item=self._to_data(item),
                                timeout=to_millis(timeout))
Example #21
0
File: map.py Project: OlyaT/FunFic
 def _try_remove_internal(self, key_data, timeout):
     return self._encode_invoke_on_key(map_try_remove_codec,
                                       key_data,
                                       key=key_data,
                                       thread_id=thread_id(),
                                       timeout=to_millis(timeout))
Example #22
0
    def execute(self, sql, params, cursor_buffer_size, timeout,
                expected_result_type, schema):
        """Constructs a statement and executes it.

        Args:
            sql (str): SQL string.
            params (tuple): Query parameters.
            cursor_buffer_size (int): Cursor buffer size.
            timeout (float): Timeout of the query.
            expected_result_type (SqlExpectedResultType): Expected result type
                of the query.
            schema (str or None): The schema name.

        Returns:
            hazelcast.future.Future[SqlResult]: The execution result.
        """
        statement = _SqlStatement(sql, params, cursor_buffer_size, timeout,
                                  expected_result_type, schema)

        connection = None
        try:
            try:
                # Serialize the passed parameters.
                serialized_params = [
                    self._serialization_service.to_data(param)
                    for param in statement.parameters
                ]
            except SchemaNotReplicatedError as e:
                return self._send_schema_and_retry_fn(
                    e,
                    self.execute,
                    sql,
                    params,
                    cursor_buffer_size,
                    timeout,
                    expected_result_type,
                    schema,
                )

            connection = self._get_query_connection()
            # Create a new, unique query id.
            query_id = _SqlQueryId.from_uuid(connection.remote_uuid)

            request = sql_execute_codec.encode_request(
                statement.sql,
                serialized_params,
                # to_millis expects None to produce -1
                to_millis(None if statement.timeout ==
                          -1 else statement.timeout),
                statement.cursor_buffer_size,
                statement.schema,
                statement.expected_result_type,
                query_id,
                False,
            )

            invocation = Invocation(
                request,
                connection=connection,
                response_handler=sql_execute_codec.decode_response)
            self._invocation_service.invoke(invocation)
            return invocation.future.continue_with(lambda future: SqlResult(
                self,
                connection,
                query_id,
                statement.cursor_buffer_size,
                self._handle_execute_response(future, connection),
            ))
        except Exception as e:
            return ImmediateExceptionFuture(self.re_raise(e, connection))
 def peek(self, timeout=0):
     return self._encode_invoke(transactional_queue_peek_codec, timeout=to_millis(timeout))
Example #24
0
 def configure_client(cls, config):
     flake_id_config = FlakeIdGeneratorConfig("short-term")
     flake_id_config.prefetch_count = SHORT_TERM_BATCH_SIZE
     flake_id_config.prefetch_validity_in_millis = to_millis(SHORT_TERM_VALIDITY_SECONDS)
     config.add_flake_id_generator_config(flake_id_config)
     return config
Example #25
0
    def _add_near_cache_metrics(self, attributes, compressor):
        for near_cache in self._near_cache_manager.list_near_caches():
            nc_name = near_cache.name
            nc_name_with_prefix = self._get_name_with_prefix(nc_name)
            nc_name_with_prefix.append(".")
            nc_name_with_prefix = "".join(nc_name_with_prefix)

            near_cache_stats = near_cache.get_statistics()
            self._add_near_cache_metric(
                attributes,
                compressor,
                "creationTime",
                to_millis(near_cache_stats["creation_time"]),
                ValueType.LONG,
                ProbeUnit.MS,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "evictions",
                near_cache_stats["evictions"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "hits",
                near_cache_stats["hits"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "misses",
                near_cache_stats["misses"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "ownedEntryCount",
                near_cache_stats["owned_entry_count"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "expirations",
                near_cache_stats["expirations"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "invalidations",
                near_cache_stats["invalidations"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "invalidationRequests",
                near_cache_stats["invalidation_requests"],
                ValueType.LONG,
                ProbeUnit.COUNT,
                nc_name,
                nc_name_with_prefix,
            )

            self._add_near_cache_metric(
                attributes,
                compressor,
                "ownedEntryMemoryCost",
                near_cache_stats["owned_entry_memory_cost"],
                ValueType.LONG,
                ProbeUnit.BYTES,
                nc_name,
                nc_name_with_prefix,
            )
Example #26
0
 def try_acquire(self, permits=1, timeout=0):
     check_not_negative(permits, "Permits cannot be negative!")
     return self._encode_invoke(semaphore_try_acquire_codec, permits=permits, timeout=to_millis(timeout))
Example #27
0
 def _put_if_absent_internal(self, key_data, value_data, ttl):
     return self._encode_invoke_on_key(map_put_if_absent_codec, key_data, key=key_data, value=value_data,
                                       thread_id=thread_id(), ttl=to_millis(ttl))
Example #28
0
 def write_data(self, object_data_output):
     object_data_output.write_long(to_millis(self.publish_time))
     object_data_output.write_object(self.publisher_address)
     _write_data_to(object_data_output, self.payload)
Example #29
0
    def put_transient(self, key, value, ttl=-1):
        check_not_none(key, "key can't be None")
        check_not_none(value, "value can't be None")

        key_data = self._to_data(key)
        value_data = self._to_data(value)
        return self._encode_invoke_on_key(map_put_transient_codec, key_data, key=key_data,
                                          value=value_data, thread_id=thread_id(), ttl=to_millis(ttl))
Example #30
0
 def await (self, timeout):
     return self._encode_invoke(count_down_latch_await_codec,
                                timeout=to_millis(timeout))
Example #31
0
 def offer(self, item, timeout=0):
     check_not_none(item, "Value can't be None")
     element_data = self._to_data(item)
     return self._encode_invoke(queue_offer_codec, value=element_data, timeout_millis=to_millis(timeout))
Example #32
0
 def try_lock(self, timeout=0, lease_time=-1):
     return self._encode_invoke(lock_try_lock_codec,
                                lease=to_millis(lease_time),
                                thread_id=thread_id(),
                                timeout=to_millis(timeout))
Example #33
0
 def peek(self, timeout=0):
     return self._encode_invoke(transactional_queue_peek_codec,
                                timeout=to_millis(timeout))
Example #34
0
 def offer(self, item, timeout=0):
     check_not_none(item, "item can't be none")
     return self._encode_invoke(transactional_queue_offer_codec,
                                item=self._to_data(item),
                                timeout=to_millis(timeout))
Example #35
0
 def poll(self, timeout=0):
     return self._encode_invoke(queue_poll_codec, timeout_millis=to_millis(timeout))
Example #36
0
 def _collect_process_uptime(self, psutil_stats, probe_name, process):
     return to_millis(current_time() - process.create_time())
Example #37
0
    def lock(self, lease_time=-1):
        """
        Acquires the lock. If a lease time is specified, lock will be released after this lease time.

        If the lock is not available, the current thread becomes disabled for thread scheduling purposes and lies
        dormant until the lock has been acquired.

        :param lease_time: (long), time to wait before releasing the lock (optional).
        """
        return self._encode_invoke(lock_lock_codec, invocation_timeout=MAX_SIZE, lease_time=to_millis(lease_time),
                                   thread_id=thread_id(), reference_id=self.reference_id_generator.get_and_increment())