-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
277 lines (226 loc) · 9.39 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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"
"log"
"net"
"os"
"strconv"
"github.com/apache/arrow-go/v18/arrow/flight"
"github.com/apache/arrow-go/v18/arrow/flight/flightsql"
"github.com/apecloud/myduckserver/backend"
"github.com/apecloud/myduckserver/catalog"
"github.com/apecloud/myduckserver/flightsqlserver"
"github.com/apecloud/myduckserver/myfunc"
"github.com/apecloud/myduckserver/pgserver"
"github.com/apecloud/myduckserver/pgserver/logrepl"
"github.com/apecloud/myduckserver/pgserver/pgconfig"
"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"
)
var (
initMode = false
address = "0.0.0.0"
port = 3306
socket string
defaultDb = "myduck"
dataDirectory = "."
dbFileName string
logLevel = int(logrus.InfoLevel)
replicaOptions replica.ReplicaOptions
postgresPort = 5432
// Shared between the MySQL and Postgres servers.
superuserPassword = ""
defaultTimeZone = ""
// for Restore
restoreFile = ""
restoreEndpoint = ""
restoreAccessKeyId = ""
restoreSecretAccessKey = ""
flightsqlHost = "localhost"
flightsqlPort = -1 // Disabled by default
)
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.StringVar(&defaultDb, "default-db", defaultDb, "The default database name to use.")
flag.IntVar(&logLevel, "loglevel", logLevel, "The log level to use.")
flag.StringVar(&superuserPassword, "superuser-password", superuserPassword, "The password for the superuser account.")
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.")
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.")
flag.StringVar(&replicaOptions.ReportUser, "report-user", replicaOptions.ReportUser, "The account user name of the replica to be reported to the source during replica registration.")
flag.StringVar(&replicaOptions.ReportPassword, "report-password", replicaOptions.ReportPassword, "The account password of the replica to be reported to the source during replica registration.")
flag.IntVar(&postgresPort, "pg-port", postgresPort, "The port to bind to for PostgreSQL wire protocol.")
flag.StringVar(&defaultTimeZone, "default-time-zone", defaultTimeZone, "The default time zone to use.")
flag.StringVar(&restoreFile, "restore-file", restoreFile, "The file to restore from.")
flag.StringVar(&restoreEndpoint, "restore-endpoint", restoreEndpoint, "The endpoint of object storage service to restore from.")
flag.StringVar(&restoreAccessKeyId, "restore-access-key-id", restoreAccessKeyId, "The access key ID to restore from.")
flag.StringVar(&restoreSecretAccessKey, "restore-secret-access-key", restoreSecretAccessKey, "The secret access key to restore from.")
flag.StringVar(&flightsqlHost, "flightsql-host", flightsqlHost, "hostname for the Flight SQL service")
flag.IntVar(&flightsqlPort, "flightsql-port", flightsqlPort, "port number for the Flight SQL service")
}
func ensureSQLTranslate() {
_, err := transpiler.TranslateWithSQLGlot("SELECT 1")
if err != nil {
panic(err)
}
}
func main() {
flag.Parse() // Parse all flags
dbFileName = defaultDb + ".db"
if replicaOptions.ReportPort == 0 {
replicaOptions.ReportPort = port
}
logrus.SetLevel(logrus.Level(logLevel))
ensureSQLTranslate()
executeRestoreIfNeeded()
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")
}
}
// Clear the pipes directory on startup.
backend.RemoveAllPipes(dataDirectory)
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, "root", superuserPassword); 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(
provider, pool,
address, postgresPort,
superuserPassword,
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.
err = logrepl.UpdateSubscriptions(pgServer.NewInternalCtx())
if err != nil {
logrus.WithError(err).Warnln("Failed to update subscriptions")
}
// Load the configuration for the Postgres server.
pgconfig.Init()
go pgServer.Start()
}
if flightsqlPort > 0 {
db := provider.Storage()
if err != nil {
log.Fatal(err)
}
defer db.Close()
srv, err := flightsqlserver.NewSQLiteFlightSQLServer(db)
if err != nil {
log.Fatal(err)
}
server := flight.NewServerWithMiddleware(nil)
server.RegisterFlightService(flightsql.NewFlightServer(srv))
server.Init(net.JoinHostPort(*&flightsqlHost, strconv.Itoa(*&flightsqlPort)))
server.SetShutdownOnSignals(os.Interrupt, os.Kill)
fmt.Println("Starting SQLite Flight SQL Server on", server.Addr(), "...")
go server.Serve()
}
if err = myServer.Start(); err != nil {
logrus.WithError(err).Fatalln("Failed to start MySQL-protocol server")
}
}
func executeRestoreIfNeeded() {
// If none of the restore parameters are set, return early.
if restoreFile == "" && restoreEndpoint == "" && restoreAccessKeyId == "" && restoreSecretAccessKey == "" {
return
}
// Map of required parameters to their names for validation.
required := map[string]string{
restoreFile: "restore file",
restoreEndpoint: "restore endpoint",
restoreAccessKeyId: "restore access key ID",
restoreSecretAccessKey: "restore secret access key",
}
// Validate that all required parameters are set.
for val, name := range required {
if val == "" {
logrus.Fatalf("The %s is required.", name)
}
}
msg, err := pgserver.ExecuteRestore(
defaultDb,
dataDirectory,
dbFileName,
restoreFile,
restoreEndpoint,
restoreAccessKeyId,
restoreSecretAccessKey,
)
if err != nil {
logrus.WithError(err).Fatalln("Failed to execute restore:", msg)
}
logrus.Infoln("Restore completed successfully:", msg)
}