-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuroraMySqlDialect.cs
More file actions
102 lines (85 loc) · 4.21 KB
/
AuroraMySqlDialect.cs
File metadata and controls
102 lines (85 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed 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.
using System.Data.Common;
using AwsWrapperDataProvider.Driver.HostListProviders;
using AwsWrapperDataProvider.Driver.HostListProviders.Monitoring;
using AwsWrapperDataProvider.Driver.Utils;
using AwsWrapperDataProvider.Properties;
using Microsoft.Extensions.Logging;
namespace AwsWrapperDataProvider.Driver.Dialects;
public class AuroraMySqlDialect : MySqlDialect, IBlueGreenDialect
{
private static readonly ILogger<AuroraMySqlDialect> Logger = LoggerUtils.GetLogger<AuroraMySqlDialect>();
private static readonly string TopologyQuery = "SELECT SERVER_ID, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END, "
+ "CPU, REPLICA_LAG_IN_MILLISECONDS, LAST_UPDATE_TIMESTAMP "
+ "FROM information_schema.replica_host_status "
+ "WHERE time_to_sec(timediff(now(), LAST_UPDATE_TIMESTAMP)) <= 300 OR SESSION_ID = 'MASTER_SESSION_ID' ";
private static readonly string IsReaderQuery = "SELECT @@innodb_read_only";
private static readonly string NodeIdQuery = "SELECT @@aurora_server_id, @@aurora_server_id";
internal static readonly string IsDialectQuery = "SHOW VARIABLES LIKE 'aurora_version'";
private static readonly string IsWriterQuery = "SELECT SERVER_ID FROM information_schema.replica_host_status "
+ "WHERE SESSION_ID = 'MASTER_SESSION_ID' AND SERVER_ID = @@aurora_server_id";
private static readonly string AuroraMySqlBgTopologyExistsQuery = "SELECT 1 AS tmp FROM information_schema.tables WHERE table_schema = 'mysql' AND table_name = 'rds_topology'";
private static readonly string AuroraMySqlBgStatusQuery = "SELECT * FROM mysql.rds_topology";
public override IList<Type> DialectUpdateCandidates { get; } = [
typeof(RdsMultiAzDbClusterMySqlDialect),
];
public override async Task<bool> IsDialect(DbConnection connection)
{
try
{
await using var command = connection.CreateCommand();
command.CommandText = IsDialectQuery;
await using var reader = await command.ExecuteReaderAsync();
return await reader.ReadAsync();
}
catch (Exception ex) when (this.ExceptionHandler.IsSyntaxError(ex))
{
Logger.LogTrace(ex, Resources.Error_CantCheckDialect_Syntax, nameof(AuroraMySqlDialect));
}
catch (Exception ex)
{
Logger.LogTrace(ex, Resources.Error_CantCheckDialect, nameof(AuroraMySqlDialect));
}
return false;
}
public override HostListProviderSupplier HostListProviderSupplier => this.GetHostListProviderSupplier();
private HostListProviderSupplier GetHostListProviderSupplier()
{
return (props, hostListProviderService, pluginService) =>
(PropertyDefinition.Plugins.GetString(props) ?? DefaultPluginCodes).Contains("failover") ?
new MonitoringRdsHostListProvider(
props,
hostListProviderService,
TopologyQuery,
NodeIdQuery,
IsReaderQuery,
IsWriterQuery,
pluginService) :
new RdsHostListProvider(
props,
hostListProviderService,
TopologyQuery,
NodeIdQuery,
IsReaderQuery);
}
public async Task<bool> IsBlueGreenStatusAvailable(DbConnection connection)
{
return await DialectUtils.CheckExistenceQueries(connection, this.ExceptionHandler, Logger, AuroraMySqlBgTopologyExistsQuery);
}
public string GetBlueGreenStatusQuery()
{
return AuroraMySqlBgStatusQuery;
}
}