forked from apecloud/myduckserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (164 loc) · 7.37 KB
/
main.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2024-2025 ApeCloud, Ltd.
//
// 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.
package main
import (
"context"
"flag"
"fmt"
"github.com/apecloud/myduckserver/backend"
"github.com/apecloud/myduckserver/catalog"
"github.com/apecloud/myduckserver/myfunc"
"github.com/apecloud/myduckserver/pgserver"
"github.com/apecloud/myduckserver/pgserver/config"
"github.com/apecloud/myduckserver/pgserver/logrepl"
"github.com/apecloud/myduckserver/plugin"
"github.com/apecloud/myduckserver/replica"
"github.com/apecloud/myduckserver/transpiler"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/vitess/go/mysql"
_ "github.com/marcboeker/go-duckdb"
"github.com/sirupsen/logrus"
)
// After running the executable, you may connect to it using the following:
//
// > mysql --host=localhost --port=3306 --user=root
//
// The included MySQL client is used in this example, however any MySQL-compatible client will work.
var (
initMode = false
address = "0.0.0.0"
port = 3306
socket string
dataDirectory = "."
dbFileName = "mysql.db"
logLevel = int(logrus.InfoLevel)
replicaOptions replica.ReplicaOptions
postgresPort = 5432
defaultTimeZone = ""
)
func init() {
flag.BoolVar(&initMode, "init", initMode, "Initialize the program and exit. The necessary extensions will be installed.")
flag.StringVar(&address, "address", address, "The address to bind to.")
flag.IntVar(&port, "port", port, "The port to bind to.")
flag.StringVar(&socket, "socket", socket, "The Unix domain socket to bind to.")
flag.StringVar(&dataDirectory, "datadir", dataDirectory, "The directory to store the database.")
flag.IntVar(&logLevel, "loglevel", logLevel, "The log level to use.")
// The following options need to be set for MySQL Shell's utilities to work properly.
// https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html#sysvar_report_host
flag.StringVar(&replicaOptions.ReportHost, "report-host", replicaOptions.ReportHost, "The host name or IP address of the replica to be reported to the source during replica registration.")
// https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html#sysvar_report_port
flag.IntVar(&replicaOptions.ReportPort, "report-port", replicaOptions.ReportPort, "The TCP/IP port number for connecting to the replica, to be reported to the source during replica registration.")
// https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html#sysvar_report_user
flag.StringVar(&replicaOptions.ReportUser, "report-user", replicaOptions.ReportUser, "The account user name of the replica to be reported to the source during replica registration.")
// https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html#sysvar_report_password
flag.StringVar(&replicaOptions.ReportPassword, "report-password", replicaOptions.ReportPassword, "The account password of the replica to be reported to the source during replica registration.")
// The following options are used to configure the Postgres server.
flag.IntVar(&postgresPort, "pg-port", postgresPort, "The port to bind to for PostgreSQL wire protocol.")
// The following options configure default DuckDB settings.
flag.StringVar(&defaultTimeZone, "default-time-zone", defaultTimeZone, "The default time zone to use.")
}
func ensureSQLTranslate() {
_, err := transpiler.TranslateWithSQLGlot("SELECT 1")
if err != nil {
panic(err)
}
}
func main() {
flag.Parse()
if replicaOptions.ReportPort == 0 {
replicaOptions.ReportPort = port
}
logrus.SetLevel(logrus.Level(logLevel))
ensureSQLTranslate()
if initMode {
provider := catalog.NewInMemoryDBProvider()
provider.Close()
return
}
provider, err := catalog.NewDBProvider(dataDirectory, dbFileName)
if err != nil {
logrus.Fatalln("Failed to open the database:", err)
}
defer provider.Close()
pool := backend.NewConnectionPool(provider.CatalogName(), provider.Connector(), provider.Storage())
if _, err := pool.ExecContext(context.Background(), "PRAGMA enable_checkpoint_on_shutdown"); err != nil {
logrus.WithError(err).Fatalln("Failed to enable checkpoint on shutdown")
}
if defaultTimeZone != "" {
_, err := pool.ExecContext(context.Background(), fmt.Sprintf(`SET TimeZone = '%s'`, defaultTimeZone))
if err != nil {
logrus.WithError(err).Fatalln("Failed to set the default time zone")
}
}
engine := sqle.NewDefault(provider)
builder := backend.NewDuckBuilder(engine.Analyzer.ExecBuilder, pool, provider)
engine.Analyzer.ExecBuilder = builder
engine.Analyzer.Catalog.RegisterFunction(sql.NewContext(context.Background()), myfunc.ExtraBuiltIns...)
engine.Analyzer.Catalog.MySQLDb.SetPlugins(plugin.AuthPlugins)
if err := setPersister(provider, engine); err != nil {
logrus.Fatalln("Failed to set the persister:", err)
}
replica.RegisterReplicaOptions(&replicaOptions)
replica.RegisterReplicaController(provider, engine, pool, builder)
serverConfig := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("%s:%d", address, port),
Socket: socket,
}
myServer, err := server.NewServerWithHandler(serverConfig, engine, backend.NewSessionBuilder(provider, pool), nil, backend.WrapHandler(pool))
if err != nil {
logrus.WithError(err).Fatalln("Failed to create MySQL-protocol server")
}
if postgresPort > 0 {
// Postgres tables are created in the `public` schema by default.
// Create the `public` schema if it doesn't exist.
_, err := pool.ExecContext(context.Background(), "CREATE SCHEMA IF NOT EXISTS public")
if err != nil {
logrus.WithError(err).Fatalln("Failed to create the `public` schema")
}
pgServer, err := pgserver.NewServer(
address, postgresPort,
func() *sql.Context {
session := backend.NewSession(memory.NewSession(sql.NewBaseSession(), provider), provider, pool)
return sql.NewContext(context.Background(), sql.WithSession(session))
},
pgserver.WithEngine(myServer.Engine),
pgserver.WithSessionManager(myServer.SessionManager()),
pgserver.WithConnID(&myServer.Listener.(*mysql.Listener).ConnectionID), // Shared connection ID counter
)
if err != nil {
logrus.WithError(err).Fatalln("Failed to create Postgres-protocol server")
}
// Check if there is a replication subscription and start replication if there is.
_, conn, pub, ok, err := logrepl.FindReplication(pool.DB)
if err != nil {
logrus.WithError(err).Warnln("Failed to find replication")
} else if ok {
replicator, err := logrepl.NewLogicalReplicator(conn)
if err != nil {
logrus.WithError(err).Fatalln("Failed to create logical replicator")
}
go replicator.StartReplication(pgServer.NewInternalCtx(), pub)
}
// Load the configuration for the Postgres server.
config.Init()
go pgServer.Start()
}
if err = myServer.Start(); err != nil {
logrus.WithError(err).Fatalln("Failed to start MySQL-protocol server")
}
}