apache-ignite
146 строк · 4.1 Кб
1# Licensed to the Apache Software Foundation (ASF) under one or more
2# contributor license agreements. See the NOTICE file distributed with
3# this work for additional information regarding copyright ownership.
4# The ASF licenses this file to You under the Apache License, Version 2.0
5# (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License
15
16"""
17Module contains classes and utility methods to create discovery configuration for ignite nodes.
18"""
19
20from abc import ABCMeta, abstractmethod21
22from ignitetest.services.utils.ignite_aware import IgniteAwareService23from ignitetest.services.zk.zookeeper import ZookeeperService24
25
26class DiscoverySpi(metaclass=ABCMeta):27"""28Abstract class for DiscoverySpi.
29"""
30@property31@abstractmethod32def type(self):33"""34Type of DiscoverySPI.
35"""
36
37@abstractmethod38def prepare_on_start(self, **kwargs):39"""40Call if update before start is needed.
41"""
42
43
44class ZookeeperDiscoverySpi(DiscoverySpi):45"""46ZookeeperDiscoverySpi.
47"""
48def __init__(self, zoo_service, root_path):49self.connection_string = zoo_service.connection_string()50self.port = zoo_service.settings.client_port51self.root_path = root_path52self.session_timeout = zoo_service.settings.min_session_timeout53
54@property55def type(self):56return "ZOOKEEPER"57
58def prepare_on_start(self, **kwargs):59pass60
61
62class TcpDiscoveryIpFinder(metaclass=ABCMeta):63"""64Abstract class for TcpDiscoveryIpFinder.
65"""
66@property67@abstractmethod68def type(self):69"""70Type of TcpDiscoveryIpFinder.
71"""
72
73@abstractmethod74def prepare_on_start(self, **kwargs):75"""76Call if update before start is needed.
77"""
78
79
80class TcpDiscoveryVmIpFinder(TcpDiscoveryIpFinder):81"""82IpFinder with static ips, obtained from cluster nodes.
83"""
84def __init__(self, nodes=None):85self.addresses = TcpDiscoveryVmIpFinder.__get_addresses(nodes) if nodes else None86
87@property88def type(self):89return 'VM'90
91def prepare_on_start(self, **kwargs):92if not self.addresses:93cluster = kwargs.get('cluster')94self.addresses = TcpDiscoveryVmIpFinder.__get_addresses(cluster.nodes)95
96@staticmethod97def __get_addresses(nodes):98return [node.account.externally_routable_ip for node in nodes]99
100
101class TcpDiscoverySpi(DiscoverySpi):102"""103TcpDiscoverySpi.
104"""
105def __init__(self, ip_finder=TcpDiscoveryVmIpFinder(), port=47500, port_range=100, local_address=None):106self.ip_finder = ip_finder107self.port = port108self.port_range = port_range109self.local_address = local_address110
111@property112def type(self):113return 'TCP'114
115def prepare_on_start(self, **kwargs):116self.ip_finder.prepare_on_start(**kwargs)117
118
119def from_ignite_cluster(cluster, subset=None):120"""121Form TcpDiscoverySpi from cluster or its subset.
122:param cluster: IgniteService cluster
123:param subset: slice object (optional).
124:return: TcpDiscoverySpi with static ip addresses.
125"""
126assert isinstance(cluster, IgniteAwareService)127
128if subset:129assert isinstance(subset, slice)130nodes = cluster.nodes[subset]131else:132nodes = cluster.nodes133
134return TcpDiscoverySpi(ip_finder=TcpDiscoveryVmIpFinder(nodes))135
136
137def from_zookeeper_cluster(cluster, root_path="/apacheIgnite"):138"""139Form ZookeeperDiscoverySpi from zookeeper service cluster.
140:param cluster: ZookeeperService cluster.
141:param root_path: root ZNode path.
142:return: ZookeeperDiscoverySpi.
143"""
144assert isinstance(cluster, ZookeeperService)145
146return ZookeeperDiscoverySpi(cluster, root_path=root_path)147