This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Bind Cluster sessions to single server nodes, and add naive load balancing #183
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # | ||
| import time | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| import grakn_protocol.protobuf.cluster.database_pb2 as database_proto | ||
| from grpc import RpcError, StatusCode | ||
|
|
||
| from grakn.common.exception import GraknClientException | ||
| from grakn.rpc.cluster.replica_info import ReplicaInfo | ||
|
|
||
|
|
||
| class FailsafeTask(ABC): | ||
|
|
||
| PRIMARY_REPLICA_TASK_MAX_RETRIES = 10 | ||
| FETCH_REPLICAS_MAX_RETRIES = 10 | ||
| WAIT_FOR_PRIMARY_REPLICA_SELECTION_SECONDS: float = 2 | ||
|
|
||
| def __init__(self, client, database: str): | ||
| self.client = client | ||
| self.database = database | ||
|
|
||
| @abstractmethod | ||
| def run(self, replica: ReplicaInfo.Replica): | ||
| pass | ||
|
|
||
| def rerun(self, replica: ReplicaInfo.Replica): | ||
| return self.run(replica) | ||
|
|
||
| def run_primary_replica(self): | ||
| if self.database not in self.client.replica_info_map() or not self.client.replica_info_map()[self.database].primary_replica(): | ||
| self._seek_primary_replica() | ||
| replica = self.client.replica_info_map()[self.database].primary_replica() | ||
| retries = 0 | ||
| while True: | ||
| try: | ||
| return self.run(replica) if retries == 0 else self.rerun(replica) | ||
| except GraknClientException as e: | ||
| # TODO: propagate exception from the server in a less brittle way | ||
| if "[RPL01]" in str(e): # The contacted replica reported that it was not the primary replica | ||
| print("Unable to open a session or transaction, retrying in 2s... %s" % str(e)) | ||
| time.sleep(self.WAIT_FOR_PRIMARY_REPLICA_SELECTION_SECONDS) | ||
| replica = self._seek_primary_replica() | ||
| else: | ||
| raise e | ||
| # TODO: introduce a special type that extends RpcError and Call | ||
| except RpcError as e: | ||
| # TODO: this logic should be extracted into GraknClientException | ||
| # TODO: error message should be checked in a less brittle way | ||
| if e.code() == StatusCode.UNAVAILABLE or "[INT07]" in str(e) or "Received RST_STREAM" in str(e): | ||
| print("Unable to open a session or transaction, retrying in 2s... %s" % str(e)) | ||
| time.sleep(self.WAIT_FOR_PRIMARY_REPLICA_SELECTION_SECONDS) | ||
| replica = self._seek_primary_replica() | ||
| else: | ||
| raise e | ||
| retries += 1 | ||
| if retries > self.PRIMARY_REPLICA_TASK_MAX_RETRIES: | ||
| raise self._cluster_not_available_exception() | ||
|
|
||
| def run_any_replica(self): | ||
| if self.database in self.client.replica_info_map(): | ||
| replica_info = self.client.replica_info_map()[self.database] | ||
| else: | ||
| replica_info = self._fetch_database_replicas() | ||
|
|
||
| replicas = [replica_info.preferred_secondary_replica()] + [replica for replica in replica_info.replicas() if not replica.is_preferred_secondary()] | ||
vmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| retries = 0 | ||
| for replica in replicas: | ||
| try: | ||
| return self.run(replica) if retries == 0 else self.rerun(replica) | ||
| except RpcError as e: | ||
| if e.code() == StatusCode.UNAVAILABLE or "[INT07]" in str(e) or "Received RST_STREAM" in str(e): | ||
| print("Unable to open a session or transaction to %s. Attempting next replica. %s" % (str(replica.replica_id()), str(e))) | ||
| else: | ||
| raise e | ||
| retries += 1 | ||
| raise self._cluster_not_available_exception() | ||
|
|
||
| def _seek_primary_replica(self) -> ReplicaInfo.Replica: | ||
| retries = 0 | ||
| while retries < self.FETCH_REPLICAS_MAX_RETRIES: | ||
| replica_info = self._fetch_database_replicas() | ||
| if replica_info.primary_replica(): | ||
| return replica_info.primary_replica() | ||
| else: | ||
| time.sleep(self.WAIT_FOR_PRIMARY_REPLICA_SELECTION_SECONDS) | ||
| retries += 1 | ||
| raise self._cluster_not_available_exception() | ||
|
|
||
| def _fetch_database_replicas(self) -> ReplicaInfo: | ||
| for server_address in self.client.cluster_members(): | ||
| try: | ||
| print("Fetching replica info from %s" % server_address) | ||
| db_replicas_req = database_proto.Database.Replicas.Req() | ||
| db_replicas_req.database = self.database | ||
| res = self.client.grakn_cluster_grpc_stub(server_address).database_replicas(db_replicas_req) | ||
| replica_info = ReplicaInfo.of_proto(res) | ||
| print("Requested database discovery from peer %s, and got response: %s" % (str(server_address), str([str(replica) for replica in replica_info.replicas()]))) | ||
| self.client.replica_info_map()[self.database] = replica_info | ||
| return replica_info | ||
| except RpcError as e: | ||
| print("Unable to perform database discovery to %s. Attempting next address. %s" % (str(server_address), str(e))) | ||
| raise self._cluster_not_available_exception() | ||
|
|
||
| def _cluster_not_available_exception(self) -> GraknClientException: | ||
| addresses = str([str(addr) for addr in self.client.cluster_members()]) | ||
| return GraknClientException("Unable to connect to Grakn Cluster. Attempted connecting to the cluster members, but none are available: '%s'" % addresses) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FailsafeTaskis a new class that holds the failover/retry logic. It is an abstract base class, requiring thatrunis implemented, whererunrepresents a unit of work that can fail and should be retried / failed over.