Example #1
0
class IdGenerator(Proxy):
    def __init__(self, client, service_name, name, atomic_long):
        super(IdGenerator, self).__init__(client, service_name, name)
        self._atomic_long = atomic_long
        self._residue = AtomicInteger(BLOCK_SIZE)
        self._local = AtomicInteger(-1)
        self._lock = threading.RLock()

    def _on_destroy(self):
        self._atomic_long.destroy()

    def init(self, initial):
        if id <= 0:
            return False
        step = initial / BLOCK_SIZE
        with self._lock:
            init = self._atomic_long.compare_and_set(0, step + 1)
            if init:
                self._local.set(step)
                self._residue.set((initial % BLOCK_SIZE) + 1)
            return init

    def new_id(self):
        val = self._residue.get_and_increment()
        if val >= BLOCK_SIZE:
            with self._lock:
                val = self._residue.get()
                if val >= BLOCK_SIZE:
                    increment = self._atomic_long.get_and_increment()
                    self._local.set(increment)
                    self._residue.set(0)
                return self.new_id()
        get = self._local.get()
        return get * BLOCK_SIZE + val
Example #2
0
class _SessionState(object):
    __slots__ = ("id", "group_id", "ttl", "creation_time", "acquire_count")

    def __init__(self, state_id, group_id, ttl):
        self.id = state_id
        self.ttl = ttl
        self.group_id = group_id
        self.creation_time = time.time()
        self.acquire_count = AtomicInteger()

    def acquire(self, count):
        self.acquire_count.add(count)
        return self.id

    def release(self, count):
        self.acquire_count.add(-count)

    def is_valid(self):
        return self.is_in_use() or not self._is_expired(time.time())

    def is_in_use(self):
        return self.acquire_count.get() > 0

    def _is_expired(self, timestamp):
        expiration_time = self.creation_time + self.ttl
        if expiration_time < 0:
            expiration_time = six.MAXSIZE
        return timestamp > expiration_time

    def __eq__(self, other):
        return isinstance(other, _SessionState) and self.id == other.id

    def __ne__(self, other):
        return not self.__eq__(other)
Example #3
0
    def test_map_listener(self):
        config = {
            "cluster_name":
            self.cluster.id,
            "compact_serializers":
            [SomeFieldsSerializer([FieldDefinition(name="int32")])],
        }
        client = self.create_client(config)

        map_name = random_string()
        m = client.get_map(map_name).blocking()

        counter = AtomicInteger()

        def listener(_):
            counter.add(1)

        m.add_entry_listener(include_value=True, added_func=listener)

        # Put the entry from other client to not create a local
        # registry in the actual client. This will force it to
        # go the cluster to fetch the schema.
        other_client = self.create_client(config)
        other_client_map = other_client.get_map(map_name).blocking()
        other_client_map.put(1, SomeFields(int32=42))

        self.assertTrueEventually(lambda: self.assertEqual(1, counter.get()))
Example #4
0
    def test_client_state_is_sent_once_if_send_operation_is_successful(self):
        conn_manager = self.client._connection_manager

        counter = AtomicInteger()

        def send_state_to_cluster_fn():
            counter.add(1)
            return ImmediateFuture(None)

        conn_manager._send_state_to_cluster_fn = send_state_to_cluster_fn

        self._restart_cluster()

        self.assertEqual(1, counter.get())
Example #5
0
    def test_count_down_retry_on_timeout(self):
        latch = self._get_latch(1)

        original = latch._wrapped._request_count_down
        called_count = AtomicInteger()

        def mock(expected_round, invocation_uuid):
            if called_count.get_and_increment() < 2:
                return ImmediateExceptionFuture(OperationTimeoutError("xx"))
            return original(expected_round, invocation_uuid)

        latch._wrapped._request_count_down = mock

        latch.count_down()
        self.assertEqual(3, called_count.get(
        ))  # Will resolve on it's third call. First 2 throws timeout error
        self.assertEqual(0, latch.get_count())
Example #6
0
    def test_sending_client_state_is_retried_if_send_operation_is_failed_synchronously(self):
        conn_manager = self.client._connection_manager

        counter = AtomicInteger()

        def send_state_to_cluster_fn():
            counter.add(1)
            if counter.get() == 5:
                # Let's pretend it succeeds at some point
                return ImmediateFuture(None)

            raise RuntimeError("expected")

        conn_manager._send_state_to_cluster_fn = send_state_to_cluster_fn

        self._restart_cluster()

        self.assertEqual(5, counter.get())
    def test_timer_cleanup(self, _, cls):
        call_count = AtomicInteger()

        def callback():
            call_count.add(1)

        loop = cls({})
        loop.start()
        loop.add_timer(float('inf'), callback)  # never expired, must be cleaned up
        time.sleep(1)
        try:
            self.assertEqual(0, call_count.get())
        finally:
            loop.shutdown()

        def assertion():
            self.assertEqual(1, call_count.get())

        self.assertTrueEventually(assertion)