Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit 05364b8

Browse files
author
Alex Walker
authored
Rename RPC implementation classes for better organisation (typedb#182)
## What is the goal of this PR? Our RPC classes used the prefix "RPC", but for autocompletion purposes, it's better for that to be a suffix. So we made it a suffix. ## What are the changes implemented in this PR? Rename RPC* to *RPC across the rpc package.
1 parent 4f71b23 commit 05364b8

File tree

10 files changed

+206
-182
lines changed

10 files changed

+206
-182
lines changed

grakn/client.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
from grakn.common.exception import GraknClientException # noqa # pylint: disable=unused-import
2929
from grakn.concept.type.value_type import ValueType # noqa # pylint: disable=unused-import
3030
from grakn.options import GraknOptions
31-
from grakn.rpc.cluster.address import Address
32-
from grakn.rpc.cluster.database_manager import _RPCDatabaseManagerCluster
33-
from grakn.rpc.cluster.session import _RPCSessionCluster
34-
from grakn.rpc.database_manager import DatabaseManager, _RPCDatabaseManager
35-
from grakn.rpc.session import Session, SessionType, _RPCSession
31+
from grakn.rpc.cluster.server_address import ServerAddress
32+
from grakn.rpc.cluster.database_manager import _DatabaseManagerClusterRPC
33+
from grakn.rpc.cluster.session import _SessionClusterRPC
34+
from grakn.rpc.database_manager import DatabaseManager, _DatabaseManagerRPC
35+
from grakn.rpc.session import Session, SessionType, _SessionRPC
3636
from grakn.rpc.transaction import TransactionType # noqa # pylint: disable=unused-import
3737

3838

@@ -41,11 +41,11 @@ class GraknClient(ABC):
4141

4242
@staticmethod
4343
def core(address=DEFAULT_ADDRESS) -> "GraknClient":
44-
return _RPCGraknClient(address)
44+
return _ClientRPC(address)
4545

4646
@staticmethod
4747
def cluster(addresses: List[str]) -> "GraknClient":
48-
return _RPCGraknClientCluster(addresses)
48+
return _ClientClusterRPC(addresses)
4949

5050
@abstractmethod
5151
def session(self, database: str, session_type: SessionType, options: GraknOptions = None) -> Session:
@@ -72,18 +72,18 @@ def __exit__(self, exc_type, exc_val, exc_tb):
7272
pass
7373

7474

75-
class _RPCGraknClient(GraknClient):
75+
class _ClientRPC(GraknClient):
7676

7777
def __init__(self, address: str):
7878
self._address = address
7979
self._channel = grpc.insecure_channel(self._address)
80-
self._databases = _RPCDatabaseManager(self._channel)
80+
self._databases = _DatabaseManagerRPC(self._channel)
8181
self._is_open = True
8282

8383
def session(self, database: str, session_type: SessionType, options=None) -> Session:
8484
if not options:
8585
options = GraknOptions.core()
86-
return _RPCSession(self, database, session_type, options)
86+
return _SessionRPC(self, database, session_type, options)
8787

8888
def databases(self):
8989
return self._databases
@@ -110,18 +110,18 @@ def channel(self):
110110

111111

112112
# _RPCGraknClientCluster must live in this package because of circular ref with GraknClient
113-
class _RPCGraknClientCluster(GraknClient):
113+
class _ClientClusterRPC(GraknClient):
114114

115115
def __init__(self, addresses: List[str]):
116-
self._core_clients: Dict[Address.Server, _RPCGraknClient] = {addr: _RPCGraknClient(addr.client()) for addr in self._discover_cluster(addresses)}
116+
self._core_clients: Dict[ServerAddress, _ClientRPC] = {addr: _ClientRPC(addr.client()) for addr in self._discover_cluster(addresses)}
117117
self._grakn_cluster_grpc_stubs = {addr: GraknClusterStub(client.channel()) for (addr, client) in self._core_clients.items()}
118-
self._databases = _RPCDatabaseManagerCluster({addr: client.databases() for (addr, client) in self._core_clients.items()})
118+
self._databases = _DatabaseManagerClusterRPC({addr: client.databases() for (addr, client) in self._core_clients.items()})
119119
self._is_open = True
120120

121121
def session(self, database: str, session_type: SessionType, options=None) -> Session:
122122
if not options:
123123
options = GraknOptions.cluster()
124-
return _RPCSessionCluster(self, database, session_type, options)
124+
return _SessionClusterRPC(self, database, session_type, options)
125125

126126
def databases(self) -> DatabaseManager:
127127
return self._databases
@@ -140,23 +140,23 @@ def __enter__(self):
140140
def __exit__(self, exc_type, exc_val, exc_tb):
141141
self.close()
142142

143-
def cluster_members(self) -> Set[Address.Server]:
143+
def cluster_members(self) -> Set[ServerAddress]:
144144
return set(self._core_clients.keys())
145145

146-
def core_client(self, address: Address.Server) -> _RPCGraknClient:
146+
def core_client(self, address: ServerAddress) -> _ClientRPC:
147147
return self._core_clients.get(address)
148148

149-
def grakn_cluster_grpc_stub(self, address: Address.Server) -> GraknClusterStub:
149+
def grakn_cluster_grpc_stub(self, address: ServerAddress) -> GraknClusterStub:
150150
return self._grakn_cluster_grpc_stubs.get(address)
151151

152-
def _discover_cluster(self, addresses: List[str]) -> Set[Address.Server]:
152+
def _discover_cluster(self, addresses: List[str]) -> Set[ServerAddress]:
153153
for address in addresses:
154154
try:
155-
with _RPCGraknClient(address) as client:
155+
with _ClientRPC(address) as client:
156156
print("Performing cluster discovery to %s..." % address)
157157
grakn_cluster_stub = GraknClusterStub(client.channel())
158158
res = grakn_cluster_stub.cluster_discover(cluster_proto.Cluster.Discover.Req())
159-
members = set([Address.Server.parse(srv) for srv in res.servers])
159+
members = set([ServerAddress.parse(srv) for srv in res.servers])
160160
print("Discovered %s" % [str(member) for member in members])
161161
return members
162162
except RpcError:

grakn/rpc/cluster/address.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

grakn/rpc/cluster/database_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from typing import Dict, List
2121

2222
from grakn.common.exception import GraknClientException
23-
from grakn.rpc.cluster.address import Address
24-
from grakn.rpc.database_manager import DatabaseManager, _RPCDatabaseManager
23+
from grakn.rpc.cluster.server_address import ServerAddress
24+
from grakn.rpc.database_manager import DatabaseManager, _DatabaseManagerRPC
2525

2626

27-
class _RPCDatabaseManagerCluster(DatabaseManager):
27+
class _DatabaseManagerClusterRPC(DatabaseManager):
2828

29-
def __init__(self, database_managers: Dict[Address.Server, "_RPCDatabaseManager"]):
29+
def __init__(self, database_managers: Dict[ServerAddress, "_DatabaseManagerRPC"]):
3030
assert database_managers
3131
self._database_managers = database_managers
3232

grakn/rpc/cluster/replica_info.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
from typing import Dict, Optional
21+
import grakn_protocol.protobuf.cluster.database_pb2 as database_proto
22+
23+
from grakn.rpc.cluster.server_address import ServerAddress
24+
25+
26+
class ReplicaInfo:
27+
28+
def __init__(self, replicas: Dict["ReplicaInfo.Replica.Id", "ReplicaInfo.Replica"]):
29+
assert replicas
30+
self._replicas = replicas
31+
32+
@staticmethod
33+
def of_proto(res: database_proto.Database.Discover.Res) -> "ReplicaInfo":
34+
replica_map: Dict["ReplicaInfo.Replica.Id", "ReplicaInfo.Replica"] = {}
35+
for replica_proto in res.replicas:
36+
replica_id = ReplicaInfo.Replica.Id(ServerAddress.parse(replica_proto.address), replica_proto.database)
37+
replica_map[replica_id] = ReplicaInfo.Replica.of_proto(replica_proto)
38+
return ReplicaInfo(replica_map)
39+
40+
def primary_replica(self) -> Optional["ReplicaInfo.Replica"]:
41+
primaries = [replica for replica in self._replicas.values() if replica.is_primary()]
42+
return max(primaries, key=lambda r: r.term) if primaries else None
43+
44+
def replicas(self):
45+
return self._replicas.values()
46+
47+
class Replica:
48+
49+
def __init__(self, replica_id: "ReplicaInfo.Replica.Id", term: int, is_primary: bool):
50+
self._replica_id = replica_id
51+
self._term = term
52+
self._is_primary = is_primary
53+
54+
@staticmethod
55+
def of_proto(replica_proto: database_proto.Database.Discover.Res.Replica) -> "ReplicaInfo.Replica":
56+
return ReplicaInfo.Replica(
57+
replica_id=ReplicaInfo.Replica.Id(ServerAddress.parse(replica_proto.address), replica_proto.database),
58+
term=replica_proto.term,
59+
is_primary=replica_proto.is_primary
60+
)
61+
62+
def replica_id(self) -> "ReplicaInfo.Replica.Id":
63+
return self._replica_id
64+
65+
def term(self) -> int:
66+
return self._term
67+
68+
def is_primary(self) -> bool:
69+
return self._is_primary
70+
71+
def __eq__(self, other):
72+
if self is other:
73+
return True
74+
if not other or type(self) != type(other):
75+
return False
76+
return self._term == other.term() and self._is_primary == other.is_primary()
77+
78+
def __hash__(self):
79+
return hash((self._is_primary, self._term))
80+
81+
def __str__(self):
82+
return "%s:%s:%d" % (str(self._replica_id), "P" if self._is_primary else "S", self._term)
83+
84+
class Id:
85+
86+
def __init__(self, address: ServerAddress, database: str):
87+
self._address = address
88+
self._database = database
89+
90+
def address(self) -> ServerAddress:
91+
return self._address
92+
93+
def database(self) -> str:
94+
return self._database
95+
96+
def __eq__(self, other):
97+
if self is other:
98+
return True
99+
if not other or type(self) != type(other):
100+
return False
101+
return self._address == other.address() and self._database == other.database()
102+
103+
def __hash__(self):
104+
return hash((self._address, self._database))
105+
106+
def __str__(self):
107+
return "%s/%s" % (str(self._address), self._database)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
21+
class ServerAddress:
22+
23+
def __init__(self, host: str, client_port: int, server_port: int):
24+
self._host = host
25+
self._client_port = client_port
26+
self._server_port = server_port
27+
28+
def host(self) -> str:
29+
return self._host
30+
31+
def client_port(self) -> int:
32+
return self._client_port
33+
34+
def server_port(self) -> int:
35+
return self._server_port
36+
37+
def server(self) -> str:
38+
return "%s:%d" % (self._host, self._server_port)
39+
40+
def client(self) -> str:
41+
return "%s:%d" % (self._host, self._client_port)
42+
43+
def __eq__(self, other):
44+
if other is self:
45+
return True
46+
if not other or type(self) != type(other):
47+
return False
48+
return self._client_port == other.client_port() and self._server_port == other.server_port() and self._host == other.host()
49+
50+
def __hash__(self):
51+
return hash((self._host, self._client_port, self._server_port))
52+
53+
def __str__(self):
54+
return "%s:%d:%d" % (self._host, self._client_port, self._server_port)
55+
56+
@staticmethod
57+
def parse(address: str) -> "ServerAddress":
58+
s = address.split(":")
59+
return ServerAddress(host=s[0], client_port=int(s[1]), server_port=int(s[2]))

0 commit comments

Comments
 (0)