本頁說明 Spanner 中的領袖感知路由,以及如何使用。Spanner 會使用領袖感知路由,在雙區域和多區域執行個體設定中動態路由讀寫交易,藉此降低延遲並提升資料庫效能。預設會啟用領袖感知路由。
讀寫交易的 Spanner 路由
Spanner 會複製資料,提供額外的資料可用性和地理區域。在 Spanner 雙區域和多區域執行個體設定中,雙區域和多區域執行個體設定中的一個區域會指定為主要區域,並包含資料庫的主要備用資源。當您使用雙地區或多地區執行個體設定,且您的用戶端從非主要區域向資料庫發出讀寫交易時,寫入作業一律會在主要區域中處理,然後傳回至非主要區域。因此,從非領導區域提交的讀寫交易需要多次往返領導備用資源,才能成功提交。
支援領袖的路由是一種機制,可透過智慧型路由方式改善讀寫交易的延遲時間。如果啟用支援領導者的路由,即使寫入作業並非來自領導區域,工作階段建立要求也會路由至領導區域,以便將 Spanner API 前端與領導區域對齊。這個轉送機制可減少非領導區域 (用戶端應用程式所在位置) 和領導區域之間的網路往返次數,進而縮短讀寫交易的延遲時間。
圖 1. 啟用領袖感知路由功能的 Spanner 路由範例。
如果停用支援領袖的轉送,用戶端應用程式會先將要求轉送至用戶端應用程式區域 (非領袖區域) 中的 Spanner API 前端服務。接著,從用戶端應用程式區域的 Spanner API 前端,會向領導區域中的 Spanner 伺服器 (SpanServer) 進行三次以上的往返作業,以便將寫入作業提交,這會增加延遲時間。這些額外的往返作業是為了支援次要索引、限制檢查和讀取寫入資料。
圖 2. 停用領袖感知路由功能的 Spanner 路由範例。
用途
由於使用了可偵測領袖的路由,以下用途可從較低的延遲中受益:
- 大量更新:從非領導區域執行 Dataflow 匯入作業或背景變更 (例如 批次 DML)。
- 災難復原能力和更高的可用性:在領導區域和非領導區域中部署用戶端應用程式,以便在從非領導區域啟動寫入作業時,容許區域性服務中斷。
- 全球應用程式:在全球部署用戶端應用程式,並在多個區域位置提交資料。
限制
如果用戶端應用程式部署在領袖區域之外,且您在未讀取資料的情況下寫入值 (「盲寫」),則在啟用領袖感知路由時,您可能會觀察到延遲回歸。這是因為啟用支援領袖的路由時,在非領袖區域中的用戶端應用程式和領袖區域中的 Spanner API 前端之間,會有兩個區域間往返傳輸 (beginTransaction
和 commit
要求)。不過,如果停用支援領導者的路由,則只需要為 commit
要求進行一次區域間往返作業,即可完成寫入作業 (beginTransaction
會在本機 Spanner API 前端處理)。舉例來說,如果您將檔案資料大量載入新建立的資料表,交易可能不會從資料表讀取資料。如果您經常在應用程式中提交寫入作業,但不讀取該作業,建議您考慮停用支援領袖的路由。詳情請參閱「停用可偵測領袖的路由功能」。
使用可辨識領袖的路由
根據預設,Spanner 用戶端程式庫會啟用領袖感知路由。
建議您在啟用可辨識領袖的路由功能時,處理讀寫要求。您可以停用這項功能,比較成效差異。
啟用可偵測領袖的路由
您可以使用 Spanner 用戶端程式庫,手動啟用領導人感知路由。
C++
使用 RouteToLeaderOption
結構體設定客戶端應用程式,並啟用可偵測領袖的路由功能:
void RouteToLeaderOption(std::string const& project_id, std::string const& instance_id,
std::string const& database_id) {
namespace spanner = ::google::cloud::spanner;
// Create a client with RouteToLeaderOption enabled.
auto client = spanner::Client(
spanner::MakeConnection(
spanner::Database(project_id, instance_id, database_id)),
google::cloud::Options{}.set<spanner::RouteToLeaderOption>(
spanner::true));
C#
使用 EnableLeaderRouting
設定用戶端應用程式,啟用可偵測領袖的路由功能:
// Create a client with leader-aware routing enabled.
SpannerConnectionStringBuilder builder = new
SpannerConnectionStringBuilder();
Builder.EnableLeaderRouting = true;
Go
使用 ClientConfig
設定用戶端應用程式,啟用可偵測領袖的路由功能:
type ClientConfig struct {
// DisableRouteToLeader specifies if all the requests of type read-write
// and PDML need to be routed to the leader region.
// Default: false
DisableRouteToLeader false
}
Java
使用 SpannerOptions.Builder
設定用戶端應用程式,啟用可偵測領袖的路由功能:
SpannerOptions options = SpannerOptions.newBuilder().enableLeaderAwareRouting.build();
Spanner spanner = options.getService();
String instance = "my-instance";
String database = "my-database";
Node.js
使用 SpannerOptions
設定用戶端應用程式,啟用可偵測領袖的路由功能:
// Instantiates a client with routeToLeaderEnabled enabled
const spanner = new Spanner({
projectId: projectId,
routeToLeaderEnabled: true;
});
PHP
使用 routeToLeader
設定用戶端應用程式,並啟用可偵測領袖的路由功能:
// Instantiates a client with leader-aware routing enabled
use Google\Cloud\Spanner\SpannerClient;
$routeToLeader = true;
$spanner = new SpannerClient($routeToLeader);
Python
使用 route_to_leader_enabled
設定用戶端應用程式,啟用可偵測領袖的路由功能:
spanner_client = spanner.Client(
route_to_leader_enabled=true
)
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
Ruby
使用 self.new
設定用戶端應用程式,啟用可偵測領袖的路由功能:
def self.new(project_id: nil, credentials: nil, scope: nil, timeout: nil,
endpoint: nil, project: nil, keyfile: nil, emulator_host: nil,
lib_name: nil, lib_version: nil, enable_leader_aware_routing: true) ->
Google::Cloud::Spanner::Project
停用領導者感知路由
您可以使用 Spanner 用戶端程式庫停用支援領袖的路由。
C++
使用 RouteToLeaderOption
結構設定客戶端應用程式,並停用可偵測領袖的路由功能:
void RouteToLeaderOption(std::string const& project_id, std::string const& instance_id,
std::string const& database_id) {
namespace spanner = ::google::cloud::spanner;
// Create a client with RouteToLeaderOption disabled.
auto client = spanner::Client(
spanner::MakeConnection(
spanner::Database(project_id, instance_id, database_id)),
google::cloud::Options{}.set<spanner::RouteToLeaderOption>(
spanner::false));
C#
使用 EnableLeaderRouting
設定用戶端應用程式,並停用可偵測領袖的路由功能:
// Create a client with leader-aware routing disabled.
SpannerConnectionStringBuilder builder = new
SpannerConnectionStringBuilder();
Builder.EnableLeaderRouting = false;
Go
使用 ClientConfig
設定用戶端應用程式,並停用可偵測領袖的路由功能:
type ClientConfig struct {
// DisableRouteToLeader specifies if all the requests of type read-write
// and PDML need to be routed to the leader region.
// Default: false
DisableRouteToLeader true
}
Java
使用 SpannerOptions.Builder
建立連線,連線至已停用領袖感知路由的 Spanner 資料庫:
SpannerOptions options = SpannerOptions.newBuilder().disableLeaderAwareRouting.build();
Spanner spanner = options.getService();
String instance = "my-instance";
String database = "my-database";
Node.js
使用 SpannerOptions
設定用戶端應用程式,並停用可偵測領袖的路由功能:
// Instantiates a client with routeToLeaderEnabled disabled
const spanner = new Spanner({
projectId: projectId,
routeToLeaderEnabled: false;
});
PHP
使用 routeToLeader
設定用戶端應用程式,並停用可偵測領袖的路由功能:
// Instantiates a client with leader-aware routing disabled
use Google\Cloud\Spanner\SpannerClient;
$routeToLeader = false;
$spanner = new SpannerClient($routeToLeader);
Python
使用 route_to_leader_enabled
設定用戶端應用程式,並停用可偵測領袖的路由功能:
spanner_client = spanner.Client(
route_to_leader_enabled=false
)
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
Ruby
使用 self.new
設定用戶端應用程式,並停用可偵測領袖的路由功能:
def self.new(project_id: nil, credentials: nil, scope: nil, timeout: nil,
endpoint: nil, project: nil, keyfile: nil, emulator_host: nil,
lib_name: nil, lib_version: nil, enable_leader_aware_routing: false) ->
Google::Cloud::Spanner::Project
後續步驟
- 瞭解地區、雙區域和多區域設定。
- 瞭解複製功能。
- 瞭解如何修改資料庫的主要區域。