|
5 | 5 |
|
6 | 6 | package frontend_connectors |
7 | 7 |
|
8 | | -import ( |
9 | | - "context" |
10 | | - "errors" |
11 | | - "time" |
12 | | - "vitess.io/vitess/go/mysql" |
13 | | - "vitess.io/vitess/go/mysql/replication" |
14 | | - "vitess.io/vitess/go/sqltypes" |
15 | | - "vitess.io/vitess/go/vt/proto/query" |
16 | | - "vitess.io/vitess/go/vt/vtenv" |
17 | | - |
18 | | - "github.com/QuesmaOrg/quesma/platform/logger" |
19 | | - "github.com/QuesmaOrg/quesma/platform/recovery" |
20 | | - quesma_api "github.com/QuesmaOrg/quesma/platform/v2/core" |
21 | | -) |
22 | | - |
23 | | -type VitessMySqlConnector struct { |
24 | | - processors []quesma_api.Processor |
25 | | - listener *mysql.Listener |
26 | | - endpoint string |
27 | | -} |
28 | | - |
29 | | -func NewVitessMySqlConnector(endpoint string) (*VitessMySqlConnector, error) { |
30 | | - connector := VitessMySqlConnector{ |
31 | | - endpoint: endpoint, |
32 | | - } |
33 | | - |
34 | | - // FIXME: the parameter values below should be tweaked, in particular (list not exhaustive): |
35 | | - // - timeouts, delays are set to time.Second * 0 |
36 | | - // - authServer is set to mysql.NewAuthServerNone(), meaning no authentication |
37 | | - // - TLS is not set up |
38 | | - listener, err := mysql.NewListener("tcp", endpoint, mysql.NewAuthServerNone(), &connector, time.Second*0, time.Second*0, false, false, time.Second*0, time.Second*0) |
39 | | - if err != nil { |
40 | | - return nil, err |
41 | | - } |
42 | | - connector.listener = listener |
43 | | - |
44 | | - return &connector, nil |
45 | | -} |
46 | | - |
47 | | -func (t *VitessMySqlConnector) Listen() error { |
48 | | - go func() { |
49 | | - defer recovery.LogPanic() |
50 | | - t.listener.Accept() |
51 | | - }() |
52 | | - return nil |
53 | | -} |
54 | | - |
55 | | -// Implementation of Vitess mysql.Handler interface: |
56 | | - |
| 8 | +// FIXME: commented out because JUST IMPORTING |
| 9 | +// Vitess messes up logging in the whole application: |
| 10 | +// https://github.com/vitessio/vitess/blob/41a8d1dd323fb56e9dce88b29c64f9dc87b5a0fe/go/vt/logutil/logutil.go#L17-L18 |
| 11 | + |
| 12 | +// import ( |
| 13 | +// |
| 14 | +// "context" |
| 15 | +// "errors" |
| 16 | +// "time" |
| 17 | +// "vitess.io/vitess/go/mysql" |
| 18 | +// "vitess.io/vitess/go/mysql/replication" |
| 19 | +// "vitess.io/vitess/go/sqltypes" |
| 20 | +// "vitess.io/vitess/go/vt/proto/query" |
| 21 | +// "vitess.io/vitess/go/vt/vtenv" |
| 22 | +// |
| 23 | +// "github.com/QuesmaOrg/quesma/platform/logger" |
| 24 | +// "github.com/QuesmaOrg/quesma/platform/recovery" |
| 25 | +// quesma_api "github.com/QuesmaOrg/quesma/platform/v2/core" |
| 26 | +// |
| 27 | +// ) |
| 28 | +// |
| 29 | +// type VitessMySqlConnector struct { |
| 30 | +// processors []quesma_api.Processor |
| 31 | +// listener *mysql.Listener |
| 32 | +// endpoint string |
| 33 | +// } |
| 34 | +// |
| 35 | +// func NewVitessMySqlConnector(endpoint string) (*VitessMySqlConnector, error) { |
| 36 | +// connector := VitessMySqlConnector{ |
| 37 | +// endpoint: endpoint, |
| 38 | +// } |
| 39 | +// |
| 40 | +// // FIXME: the parameter values below should be tweaked, in particular (list not exhaustive): |
| 41 | +// // - timeouts, delays are set to time.Second * 0 |
| 42 | +// // - authServer is set to mysql.NewAuthServerNone(), meaning no authentication |
| 43 | +// // - TLS is not set up |
| 44 | +// listener, err := mysql.NewListener("tcp", endpoint, mysql.NewAuthServerNone(), &connector, time.Second*0, time.Second*0, false, false, time.Second*0, time.Second*0) |
| 45 | +// if err != nil { |
| 46 | +// return nil, err |
| 47 | +// } |
| 48 | +// connector.listener = listener |
| 49 | +// |
| 50 | +// return &connector, nil |
| 51 | +// } |
| 52 | +// |
| 53 | +// func (t *VitessMySqlConnector) Listen() error { |
| 54 | +// go func() { |
| 55 | +// defer recovery.LogPanic() |
| 56 | +// t.listener.Accept() |
| 57 | +// }() |
| 58 | +// return nil |
| 59 | +// } |
| 60 | +// |
| 61 | +// // Implementation of Vitess mysql.Handler interface: |
57 | 62 | type ComQueryMessage struct { |
58 | 63 | Query string |
59 | 64 | } |
60 | 65 |
|
61 | | -func (t *VitessMySqlConnector) NewConnection(c *mysql.Conn) { |
62 | | - // TODO: should we do something here? |
63 | | -} |
64 | | - |
65 | | -func (t *VitessMySqlConnector) ConnectionReady(c *mysql.Conn) { |
66 | | - // TODO: should we do something here? |
67 | | -} |
68 | | - |
69 | | -func (t *VitessMySqlConnector) ConnectionClosed(c *mysql.Conn) { |
70 | | - // TODO: should we do something here? |
71 | | -} |
72 | | - |
73 | | -func (t *VitessMySqlConnector) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error { |
74 | | - metadata := make(map[string]interface{}) |
75 | | - var message any = ComQueryMessage{ |
76 | | - Query: query, |
77 | | - } |
78 | | - |
79 | | - dispatcher := quesma_api.Dispatcher{} |
80 | | - _, result := dispatcher.Dispatch(t.processors, metadata, message) |
81 | | - switch result := result.(type) { |
82 | | - case *sqltypes.Result: |
83 | | - err := callback(result) |
84 | | - if err != nil { |
85 | | - return err |
86 | | - } |
87 | | - return nil |
88 | | - case error: |
89 | | - return result |
90 | | - default: |
91 | | - logger.Error().Msgf("Unexpected ComQuery result type received from the processor: %T", result) |
92 | | - return nil |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -func (t *VitessMySqlConnector) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*query.BindVariable) ([]*query.Field, error) { |
97 | | - // TODO implement ComPrepare |
98 | | - logger.Error().Msg("ComPrepare not implemented") |
99 | | - return nil, errors.New("ComPrepare not implemented") |
100 | | -} |
101 | | - |
102 | | -func (t *VitessMySqlConnector) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error { |
103 | | - // TODO implement ComStmtExecute |
104 | | - logger.Error().Msg("ComStmtExecute not implemented") |
105 | | - return errors.New("ComStmtExecute not implemented") |
106 | | -} |
107 | | - |
108 | | -func (t *VitessMySqlConnector) ComRegisterReplica(c *mysql.Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error { |
109 | | - // TODO implement ComRegisterReplica |
110 | | - logger.Error().Msg("ComRegisterReplica not implemented") |
111 | | - return errors.New("ComRegisterReplica not implemented") |
112 | | -} |
113 | | - |
114 | | -func (t *VitessMySqlConnector) ComBinlogDump(c *mysql.Conn, logFile string, binlogPos uint32) error { |
115 | | - // TODO implement ComBinlogDump |
116 | | - logger.Error().Msg("ComBinlogDump not implemented") |
117 | | - return errors.New("ComBinlogDump not implemented") |
118 | | -} |
119 | | - |
120 | | -func (t *VitessMySqlConnector) ComBinlogDumpGTID(c *mysql.Conn, logFile string, logPos uint64, gtidSet replication.GTIDSet) error { |
121 | | - // TODO implement ComBinlogDumpGTID |
122 | | - logger.Error().Msg("ComBinlogDumpGTID not implemented") |
123 | | - return errors.New("ComBinlogDumpGTID not implemented") |
124 | | -} |
125 | | - |
126 | | -func (t *VitessMySqlConnector) WarningCount(c *mysql.Conn) uint16 { |
127 | | - return 0 |
128 | | -} |
129 | | - |
130 | | -func (t *VitessMySqlConnector) ComResetConnection(c *mysql.Conn) { |
131 | | - // TODO implement ComResetConnection |
132 | | - logger.Error().Msg("ComResetConnection not implemented") |
133 | | -} |
134 | | - |
135 | | -func (t *VitessMySqlConnector) Env() *vtenv.Environment { |
136 | | - env, err := vtenv.New(vtenv.Options{ |
137 | | - MySQLServerVersion: "", // will use Vitess's default version |
138 | | - TruncateUILen: 512, |
139 | | - TruncateErrLen: 512, |
140 | | - }) |
141 | | - if err != nil { |
142 | | - logger.Error().Msgf("failed to create environment: %v", err) |
143 | | - return nil |
144 | | - } |
145 | | - |
146 | | - return env |
147 | | -} |
148 | | - |
149 | | -func (t *VitessMySqlConnector) InstanceName() string { |
150 | | - return "VitessMySqlConnector" |
151 | | -} |
152 | | - |
153 | | -func (t *VitessMySqlConnector) GetEndpoint() string { |
154 | | - return t.endpoint |
155 | | -} |
156 | | - |
157 | | -func (t *VitessMySqlConnector) Stop(ctx context.Context) error { |
158 | | - t.listener.Shutdown() |
159 | | - return nil |
160 | | -} |
161 | | - |
162 | | -func (t *VitessMySqlConnector) SetHandlers(processors []quesma_api.Processor) { |
163 | | - t.processors = processors |
164 | | -} |
| 66 | +// |
| 67 | +//func (t *VitessMySqlConnector) NewConnection(c *mysql.Conn) { |
| 68 | +// // TODO: should we do something here? |
| 69 | +//} |
| 70 | +// |
| 71 | +//func (t *VitessMySqlConnector) ConnectionReady(c *mysql.Conn) { |
| 72 | +// // TODO: should we do something here? |
| 73 | +//} |
| 74 | +// |
| 75 | +//func (t *VitessMySqlConnector) ConnectionClosed(c *mysql.Conn) { |
| 76 | +// // TODO: should we do something here? |
| 77 | +//} |
| 78 | +// |
| 79 | +//func (t *VitessMySqlConnector) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error { |
| 80 | +// metadata := make(map[string]interface{}) |
| 81 | +// var message any = ComQueryMessage{ |
| 82 | +// Query: query, |
| 83 | +// } |
| 84 | +// |
| 85 | +// dispatcher := quesma_api.Dispatcher{} |
| 86 | +// _, result := dispatcher.Dispatch(t.processors, metadata, message) |
| 87 | +// switch result := result.(type) { |
| 88 | +// case *sqltypes.Result: |
| 89 | +// err := callback(result) |
| 90 | +// if err != nil { |
| 91 | +// return err |
| 92 | +// } |
| 93 | +// return nil |
| 94 | +// case error: |
| 95 | +// return result |
| 96 | +// default: |
| 97 | +// logger.Error().Msgf("Unexpected ComQuery result type received from the processor: %T", result) |
| 98 | +// return nil |
| 99 | +// } |
| 100 | +//} |
| 101 | +// |
| 102 | +//func (t *VitessMySqlConnector) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*query.BindVariable) ([]*query.Field, error) { |
| 103 | +// // TODO implement ComPrepare |
| 104 | +// logger.Error().Msg("ComPrepare not implemented") |
| 105 | +// return nil, errors.New("ComPrepare not implemented") |
| 106 | +//} |
| 107 | +// |
| 108 | +//func (t *VitessMySqlConnector) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error { |
| 109 | +// // TODO implement ComStmtExecute |
| 110 | +// logger.Error().Msg("ComStmtExecute not implemented") |
| 111 | +// return errors.New("ComStmtExecute not implemented") |
| 112 | +//} |
| 113 | +// |
| 114 | +//func (t *VitessMySqlConnector) ComRegisterReplica(c *mysql.Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error { |
| 115 | +// // TODO implement ComRegisterReplica |
| 116 | +// logger.Error().Msg("ComRegisterReplica not implemented") |
| 117 | +// return errors.New("ComRegisterReplica not implemented") |
| 118 | +//} |
| 119 | +// |
| 120 | +//func (t *VitessMySqlConnector) ComBinlogDump(c *mysql.Conn, logFile string, binlogPos uint32) error { |
| 121 | +// // TODO implement ComBinlogDump |
| 122 | +// logger.Error().Msg("ComBinlogDump not implemented") |
| 123 | +// return errors.New("ComBinlogDump not implemented") |
| 124 | +//} |
| 125 | +// |
| 126 | +//func (t *VitessMySqlConnector) ComBinlogDumpGTID(c *mysql.Conn, logFile string, logPos uint64, gtidSet replication.GTIDSet) error { |
| 127 | +// // TODO implement ComBinlogDumpGTID |
| 128 | +// logger.Error().Msg("ComBinlogDumpGTID not implemented") |
| 129 | +// return errors.New("ComBinlogDumpGTID not implemented") |
| 130 | +//} |
| 131 | +// |
| 132 | +//func (t *VitessMySqlConnector) WarningCount(c *mysql.Conn) uint16 { |
| 133 | +// return 0 |
| 134 | +//} |
| 135 | +// |
| 136 | +//func (t *VitessMySqlConnector) ComResetConnection(c *mysql.Conn) { |
| 137 | +// // TODO implement ComResetConnection |
| 138 | +// logger.Error().Msg("ComResetConnection not implemented") |
| 139 | +//} |
| 140 | +// |
| 141 | +//func (t *VitessMySqlConnector) Env() *vtenv.Environment { |
| 142 | +// env, err := vtenv.New(vtenv.Options{ |
| 143 | +// MySQLServerVersion: "", // will use Vitess's default version |
| 144 | +// TruncateUILen: 512, |
| 145 | +// TruncateErrLen: 512, |
| 146 | +// }) |
| 147 | +// if err != nil { |
| 148 | +// logger.Error().Msgf("failed to create environment: %v", err) |
| 149 | +// return nil |
| 150 | +// } |
| 151 | +// |
| 152 | +// return env |
| 153 | +//} |
| 154 | +// |
| 155 | +//func (t *VitessMySqlConnector) InstanceName() string { |
| 156 | +// return "VitessMySqlConnector" |
| 157 | +//} |
| 158 | +// |
| 159 | +//func (t *VitessMySqlConnector) GetEndpoint() string { |
| 160 | +// return t.endpoint |
| 161 | +//} |
| 162 | +// |
| 163 | +//func (t *VitessMySqlConnector) Stop(ctx context.Context) error { |
| 164 | +// t.listener.Shutdown() |
| 165 | +// return nil |
| 166 | +//} |
| 167 | +// |
| 168 | +//func (t *VitessMySqlConnector) SetHandlers(processors []quesma_api.Processor) { |
| 169 | +// t.processors = processors |
| 170 | +//} |
0 commit comments