def _connect_to_cluster(self): current_attempt = 1 attempt_limit = self._config.network_config.connection_attempt_limit retry_delay = self._config.network_config.connection_attempt_period while current_attempt <= attempt_limit: provider_addresses = get_provider_addresses(self._address_providers) addresses = get_possible_addresses(provider_addresses, self.members) for address in addresses: try: self.logger.info("Connecting to %s", address) self._connect_to_address(address) return except: self.logger.warning("Error connecting to %s ", address, exc_info=True) if current_attempt >= attempt_limit: self.logger.warning( "Unable to get alive cluster connection, attempt %d of %d", current_attempt, attempt_limit) break self.logger.warning( "Unable to get alive cluster connection, attempt %d of %d, trying again in %d seconds", current_attempt, attempt_limit, retry_delay) current_attempt += 1 time.sleep(retry_delay) error_msg = "Could not connect to any of %s after %d tries" % (addresses, attempt_limit) raise HazelcastError(error_msg)
def test_no_given_address(self): self.network_config.addresses = [] provider_addresses = get_provider_addresses([self.address_provider]) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual(self, addresses, [ Address("127.0.0.1", 5701), Address("127.0.0.1", 5702), Address("127.0.0.1", 5703) ])
def test_multiple_providers_with_empty_network_config_addresses(self): default_address_provider = DefaultAddressProvider(self.network_config) providers = [default_address_provider, self.cloud_address_provider] provider_addresses = get_provider_addresses(providers) six.assertCountEqual(self, provider_addresses, [Address("10.0.0.1", 5701)]) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual(self, addresses, [Address("10.0.0.1", 5701)])
def test_addresses_and_members(self): self.network_config.addresses = ["127.0.0.1:5701"] member_list = [ Member(Address("10.0.0.1", 5703), "uuid1"), Member(Address("10.0.0.2", 5701), "uuid2") ] provider_addresses = get_provider_addresses([self.address_provider]) addresses = get_possible_addresses(provider_addresses, member_list) six.assertCountEqual(self, addresses, [ Address("127.0.0.1", 5701), Address("10.0.0.1", 5703), Address("10.0.0.2", 5701) ])
def test_multiple_providers_with_empty_load_addresses(self): default_address_provider = DefaultAddressProvider(self.network_config) self.cloud_address_provider.load_addresses = lambda: [] providers = [default_address_provider, self.cloud_address_provider] provider_addresses = get_provider_addresses(providers) six.assertCountEqual(self, provider_addresses, []) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual(self, addresses, [ Address("127.0.0.1", 5701), Address("127.0.0.1", 5702), Address("127.0.0.1", 5703) ])
def test_multiple_providers_with_duplicate_network_config_addresses(self): self.network_config.addresses.append("127.0.0.1:5701") self.network_config.addresses.append("127.0.0.1:5701") default_address_provider = DefaultAddressProvider(self.network_config) providers = [default_address_provider, self.cloud_address_provider] provider_addresses = get_provider_addresses(providers) six.assertCountEqual(self, provider_addresses, [ Address("10.0.0.1", 5701), Address("127.0.0.1", 5701), Address("127.0.0.1", 5701) ]) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual( self, addresses, [Address("10.0.0.1", 5701), Address("127.0.0.1", 5701)])
def test_multiple_addresses_non_unique(self): self.network_config.addresses = ["127.0.0.1:5701", "127.0.0.1:5701"] provider_addresses = get_provider_addresses([self.address_provider]) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual(self, addresses, [Address("127.0.0.1", 5701)])
def test_single_address_and_port(self): self.network_config.addresses = ["127.0.0.1:5701"] provider_addresses = get_provider_addresses([self.address_provider]) addresses = get_possible_addresses(provider_addresses) six.assertCountEqual(self, addresses, [Address("127.0.0.1", 5701)])