|
| 1 | +package trino_hoglake_smoke |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/rand" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "reflect" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +type statementResponse struct { |
| 20 | + Data [][]any `json:"data"` |
| 21 | + NextURI string `json:"nextUri"` |
| 22 | + Error *struct { |
| 23 | + Name string `json:"errorName"` |
| 24 | + } `json:"error"` |
| 25 | +} |
| 26 | + |
| 27 | +type smokeClient struct { |
| 28 | + http *http.Client |
| 29 | + server *url.URL |
| 30 | + user, password string |
| 31 | + routingGroup string |
| 32 | +} |
| 33 | + |
| 34 | +func newSmokeClient(server, user, password string) (*smokeClient, error) { |
| 35 | + u, err := url.Parse(server) |
| 36 | + if err != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" { |
| 37 | + return nil, fmt.Errorf("invalid Trino server URL") |
| 38 | + } |
| 39 | + if u.Scheme != "https" && (u.Scheme != "http" || (u.Hostname() != "localhost" && u.Hostname() != "127.0.0.1" && u.Hostname() != "::1")) { |
| 40 | + return nil, fmt.Errorf("Trino credentials require HTTPS outside loopback") |
| 41 | + } |
| 42 | + return &smokeClient{ |
| 43 | + server: u, user: user, password: password, |
| 44 | + http: &http.Client{Timeout: 30 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (c *smokeClient) query(ctx context.Context, sql string) ([][]any, error) { |
| 49 | + ctx, cancel := context.WithTimeout(ctx, time.Minute) |
| 50 | + defer cancel() |
| 51 | + next := strings.TrimRight(c.server.String(), "/") + "/v1/statement" |
| 52 | + method := http.MethodPost |
| 53 | + var body io.Reader = strings.NewReader(sql) |
| 54 | + var rows [][]any |
| 55 | + for page := 0; page < 1000; page++ { |
| 56 | + u, err := url.Parse(next) |
| 57 | + if err != nil || u.User != nil || u.Scheme != c.server.Scheme || u.Host != c.server.Host { |
| 58 | + return nil, fmt.Errorf("Trino continuation changed origin") |
| 59 | + } |
| 60 | + req, err := http.NewRequestWithContext(ctx, method, next, body) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + req.SetBasicAuth(c.user, c.password) |
| 65 | + req.Header.Set("X-Trino-User", c.user) |
| 66 | + req.Header.Set("X-Trino-Transaction-Id", "NONE") |
| 67 | + if c.routingGroup != "" { |
| 68 | + req.Header.Set("X-Trino-Routing-Group", c.routingGroup) |
| 69 | + } |
| 70 | + resp, err := c.http.Do(req) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + var result statementResponse |
| 75 | + decoder := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)) |
| 76 | + decoder.UseNumber() |
| 77 | + err = decoder.Decode(&result) |
| 78 | + _ = resp.Body.Close() |
| 79 | + if resp.StatusCode != http.StatusOK { |
| 80 | + return nil, fmt.Errorf("Trino returned HTTP %d", resp.StatusCode) |
| 81 | + } |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + if result.Error != nil { |
| 86 | + return nil, fmt.Errorf("Trino query failed: %s", result.Error.Name) |
| 87 | + } |
| 88 | + rows = append(rows, result.Data...) |
| 89 | + if result.NextURI == "" { |
| 90 | + return rows, nil |
| 91 | + } |
| 92 | + next, method, body = result.NextURI, http.MethodGet, nil |
| 93 | + } |
| 94 | + return nil, fmt.Errorf("Trino query exceeded its page bound") |
| 95 | +} |
| 96 | + |
| 97 | +func quote(name string) string { return `"` + strings.ReplaceAll(name, `"`, `""`) + `"` } |
| 98 | + |
| 99 | +// This opt-in test writes only generated tables, but compaction applies to the |
| 100 | +// whole catalog. Run it against a dedicated, provisioned Hoglake test tenant. |
| 101 | +func TestManagedHoglakeWritesAndCompaction(t *testing.T) { |
| 102 | + if os.Getenv("HOGLAKE_SMOKE_TEST") != "1" { |
| 103 | + t.Skip("set HOGLAKE_SMOKE_TEST=1 for the live managed-tenant smoke test") |
| 104 | + } |
| 105 | + env := func(key string) string { |
| 106 | + t.Helper() |
| 107 | + value := os.Getenv(key) |
| 108 | + if value == "" { |
| 109 | + t.Fatalf("%s is required", key) |
| 110 | + } |
| 111 | + return value |
| 112 | + } |
| 113 | + client, err := newSmokeClient(env("TRINO_SERVER"), env("TRINO_USER"), env("TRINO_PASSWORD")) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + client.routingGroup = os.Getenv("TRINO_ROUTING_GROUP") |
| 118 | + trinoCatalog, namespace := env("TRINO_CATALOG"), env("HOGLAKE_NAMESPACE") |
| 119 | + base, err := url.Parse(env("HOGLAKE_URI")) |
| 120 | + if err != nil || base.Host == "" || base.User != nil || base.RawQuery != "" || base.Fragment != "" || (base.Scheme != "http" && base.Scheme != "https") { |
| 121 | + t.Fatal("invalid Hoglake base URI") |
| 122 | + } |
| 123 | + catalogPath := strings.TrimRight(base.String(), "/") + "/v1/catalogs/" + url.PathEscape(env("HOGLAKE_CATALOG")) |
| 124 | + ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) |
| 125 | + defer cancel() |
| 126 | + query := func(sql string) [][]any { |
| 127 | + t.Helper() |
| 128 | + rows, err := client.query(ctx, sql) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + return rows |
| 133 | + } |
| 134 | + id := make([]byte, 8) |
| 135 | + if _, err := rand.Read(id); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + name := fmt.Sprintf("hoglake_smoke_%x", id) |
| 139 | + table := quote(trinoCatalog) + "." + quote(namespace) + "." + quote(name) |
| 140 | + copyTable := quote(trinoCatalog) + "." + quote(namespace) + "." + quote(name+"_copy") |
| 141 | + for _, target := range []string{name, name + "_copy"} { |
| 142 | + t.Cleanup(func() { |
| 143 | + cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 144 | + defer cleanupCancel() |
| 145 | + endpoint := catalogPath + "/namespaces/" + url.PathEscape(namespace) + "/tables/" + url.PathEscape(target) |
| 146 | + req, err := http.NewRequestWithContext(cleanupCtx, http.MethodDelete, endpoint, nil) |
| 147 | + if err != nil { |
| 148 | + t.Errorf("cleanup request failed: %v", err) |
| 149 | + return |
| 150 | + } |
| 151 | + resp, err := client.http.Do(req) |
| 152 | + if err != nil { |
| 153 | + t.Errorf("cleanup %s failed: %v", target, err) |
| 154 | + return |
| 155 | + } |
| 156 | + _ = resp.Body.Close() |
| 157 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { |
| 158 | + t.Errorf("cleanup %s returned HTTP %d", target, resp.StatusCode) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | + query("CREATE TABLE " + table + " (id bigint, amount decimal(38,2))") |
| 163 | + var expected [][]any |
| 164 | + for i := 1; i <= 8; i++ { |
| 165 | + query(fmt.Sprintf("INSERT INTO %s VALUES (%d, DECIMAL '123456789012345678901234567890.12')", table, i)) |
| 166 | + expected = append(expected, []any{json.Number(fmt.Sprint(i)), "123456789012345678901234567890.12"}) |
| 167 | + } |
| 168 | + read := func(target string) [][]any { |
| 169 | + return query("SELECT id, CAST(amount AS varchar) FROM " + target + " ORDER BY id") |
| 170 | + } |
| 171 | + if rows := read(table); !reflect.DeepEqual(rows, expected) { |
| 172 | + t.Fatal("INSERT did not preserve the expected rows and large decimals") |
| 173 | + } |
| 174 | + query("CREATE TABLE " + copyTable + " AS SELECT * FROM " + table) |
| 175 | + if rows := read(copyTable); !reflect.DeepEqual(rows, expected) { |
| 176 | + t.Fatal("CTAS did not preserve the expected rows and large decimals") |
| 177 | + } |
| 178 | + rest := func(method, endpoint string, result any) { |
| 179 | + t.Helper() |
| 180 | + req, err := http.NewRequestWithContext(ctx, method, endpoint, bytes.NewReader(nil)) |
| 181 | + if err != nil { |
| 182 | + t.Fatal(err) |
| 183 | + } |
| 184 | + resp, err := client.http.Do(req) |
| 185 | + if err != nil { |
| 186 | + t.Fatal(err) |
| 187 | + } |
| 188 | + defer func() { _ = resp.Body.Close() }() |
| 189 | + if resp.StatusCode != http.StatusOK { |
| 190 | + t.Fatalf("Hoglake returned HTTP %d", resp.StatusCode) |
| 191 | + } |
| 192 | + if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(result); err != nil { |
| 193 | + t.Fatal(err) |
| 194 | + } |
| 195 | + } |
| 196 | + scanPath := catalogPath + "/namespaces/" + url.PathEscape(namespace) + "/tables/" + url.PathEscape(name) + "/scan" |
| 197 | + var before, after []json.RawMessage |
| 198 | + rest(http.MethodGet, scanPath, &before) |
| 199 | + if len(before) < 2 { |
| 200 | + t.Fatal("compaction baseline already has fewer than two files; rerun with automatic maintenance paused for the test catalog") |
| 201 | + } |
| 202 | + var compact json.RawMessage |
| 203 | + rest(http.MethodPost, catalogPath+"/maintenance/compact?batch=100", &compact) |
| 204 | + rest(http.MethodGet, scanPath, &after) |
| 205 | + if len(after) >= len(before) { |
| 206 | + t.Fatalf("compaction did not reduce data files: before=%d after=%d", len(before), len(after)) |
| 207 | + } |
| 208 | + if rows := read(table); !reflect.DeepEqual(rows, expected) { |
| 209 | + t.Fatal("compaction changed rows or decimal values") |
| 210 | + } |
| 211 | + t.Logf("CREATE, INSERT, CTAS and compaction passed; files %d -> %d", len(before), len(after)) |
| 212 | +} |
0 commit comments