|
5 | 5 | "encoding/hex" |
6 | 6 | "errors" |
7 | 7 | "fmt" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
8 | 11 |
|
9 | 12 | "github.com/TykTechnologies/tyk-pump/analytics" |
10 | 13 | "github.com/mitchellh/mapstructure" |
@@ -45,6 +48,9 @@ type SQLPump struct { |
45 | 48 | db *gorm.DB |
46 | 49 | dbType string |
47 | 50 | dialect gorm.Dialector |
| 51 | + |
| 52 | + // this channel is used to signal that the background index creation has finished - this is used for testing |
| 53 | + backgroundIndexCreated chan bool |
48 | 54 | } |
49 | 55 |
|
50 | 56 | // @PumpConf SQL |
@@ -107,6 +113,18 @@ var ( |
107 | 113 | SQLPrefix = "SQL-pump" |
108 | 114 | SQLDefaultENV = PUMPS_ENV_PREFIX + "_SQL" + PUMPS_ENV_META_PREFIX |
109 | 115 | SQLDefaultQueryBatchSize = 1000 |
| 116 | + |
| 117 | + indexes = []struct { |
| 118 | + baseName string |
| 119 | + column string |
| 120 | + }{ |
| 121 | + {"idx_responsecode", "responsecode"}, |
| 122 | + {"idx_apikey", "apikey"}, |
| 123 | + {"idx_timestamp", "timestamp"}, |
| 124 | + {"idx_apiid", "apiid"}, |
| 125 | + {"idx_orgid", "orgid"}, |
| 126 | + {"idx_oauthid", "oauthid"}, |
| 127 | + } |
110 | 128 | ) |
111 | 129 |
|
112 | 130 | func (c *SQLPump) New() Pump { |
@@ -231,8 +249,8 @@ func (c *SQLPump) WriteData(ctx context.Context, data []interface{}) error { |
231 | 249 |
|
232 | 250 | table := analytics.SQLTable + "_" + recDate |
233 | 251 | c.db = c.db.Table(table) |
234 | | - if !c.db.Migrator().HasTable(table) { |
235 | | - c.db.AutoMigrate(&analytics.AnalyticsRecord{}) |
| 252 | + if errTable := c.ensureTable(table); errTable != nil { |
| 253 | + return errTable |
236 | 254 | } |
237 | 255 | } else { |
238 | 256 | i = dataLen // write all records at once for non-sharded case, stop for loop after 1 iteration |
@@ -354,3 +372,96 @@ func (c *SQLPump) WriteUptimeData(data []interface{}) { |
354 | 372 |
|
355 | 373 | c.log.Debug("Purged ", len(data), " records...") |
356 | 374 | } |
| 375 | + |
| 376 | +func (c *SQLPump) buildIndexName(indexBaseName, tableName string) string { |
| 377 | + return fmt.Sprintf("%s_%s", tableName, indexBaseName) |
| 378 | +} |
| 379 | + |
| 380 | +func (c *SQLPump) createIndex(indexBaseName, tableName, column string) error { |
| 381 | + indexName := c.buildIndexName(indexBaseName, tableName) |
| 382 | + option := "" |
| 383 | + if c.dbType == "postgres" { |
| 384 | + option = "CONCURRENTLY" |
| 385 | + } |
| 386 | + |
| 387 | + columnExist := c.db.Migrator().HasColumn(&analytics.AnalyticsRecord{}, column) |
| 388 | + if !columnExist { |
| 389 | + return errors.New("cannot create index for non existent column " + column) |
| 390 | + } |
| 391 | + |
| 392 | + query := fmt.Sprintf("CREATE INDEX %s IF NOT EXISTS %s ON %s (%s)", option, indexName, tableName, column) |
| 393 | + err := c.db.Exec(query).Error |
| 394 | + if err != nil { |
| 395 | + c.log.WithFields(logrus.Fields{ |
| 396 | + "index": indexName, |
| 397 | + "table": tableName, |
| 398 | + }).WithError(err).Error("Error creating index") |
| 399 | + return err |
| 400 | + } |
| 401 | + |
| 402 | + c.log.Infof("Index %s created for table %s", indexName, tableName) |
| 403 | + c.log.WithFields(logrus.Fields{ |
| 404 | + "index": indexName, |
| 405 | + "table": tableName, |
| 406 | + }).Info("Index created") |
| 407 | + return nil |
| 408 | +} |
| 409 | + |
| 410 | +// ensureIndex check that all indexes for the analytics SQL table are in place |
| 411 | +func (c *SQLPump) ensureIndex(tableName string, background bool) error { |
| 412 | + if !c.db.Migrator().HasTable(tableName) { |
| 413 | + return errors.New("cannot create indexes as table doesn't exist: " + tableName) |
| 414 | + } |
| 415 | + |
| 416 | + // waitgroup to facilitate testing and track when all indexes are created |
| 417 | + var wg sync.WaitGroup |
| 418 | + if background { |
| 419 | + wg.Add(len(indexes)) |
| 420 | + } |
| 421 | + |
| 422 | + for _, idx := range indexes { |
| 423 | + indexName := tableName + idx.baseName |
| 424 | + |
| 425 | + if c.db.Migrator().HasIndex(tableName, indexName) { |
| 426 | + c.log.WithFields(logrus.Fields{ |
| 427 | + "index": indexName, |
| 428 | + "table": tableName, |
| 429 | + }).Info("Index already exists") |
| 430 | + continue |
| 431 | + } |
| 432 | + |
| 433 | + if background { |
| 434 | + go func(baseName, cols string) { |
| 435 | + defer wg.Done() |
| 436 | + if err := c.createIndex(baseName, tableName, cols); err != nil { |
| 437 | + c.log.Error(err) |
| 438 | + } |
| 439 | + }(idx.baseName, idx.column) |
| 440 | + } else { |
| 441 | + if err := c.createIndex(idx.baseName, tableName, idx.column); err != nil { |
| 442 | + return err |
| 443 | + } |
| 444 | + } |
| 445 | + } |
| 446 | + |
| 447 | + if background { |
| 448 | + wg.Wait() |
| 449 | + c.backgroundIndexCreated <- true |
| 450 | + } |
| 451 | + return nil |
| 452 | +} |
| 453 | + |
| 454 | +// ensureTable creates the table if it doesn't exist |
| 455 | +func (c *SQLPump) ensureTable(tableName string) error { |
| 456 | + if !c.db.Migrator().HasTable(tableName) { |
| 457 | + c.db = c.db.Table(tableName) |
| 458 | + if err := c.db.Migrator().CreateTable(&analytics.AnalyticsRecord{}); err != nil { |
| 459 | + c.log.Error("error creating table", err) |
| 460 | + return err |
| 461 | + } |
| 462 | + if err := c.ensureIndex(tableName, false); err != nil { |
| 463 | + return err |
| 464 | + } |
| 465 | + } |
| 466 | + return nil |
| 467 | +} |
0 commit comments