Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Feb 10, 2025
1 parent 4c9de0b commit aca4df2
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
11 changes: 10 additions & 1 deletion go/vt/vtorc/db/generate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ var TableNames = []string{
"global_recovery_disable",
"topology_recovery_steps",
"database_instance_stale_binlog_coordinates",
"vitess_tablet",
"vitess_cell",
"vitess_keyspace",
"vitess_shard",
"vitess_tablet",
}

// vtorcBackend is a list of SQL statements required to build the vtorc backend
Expand Down Expand Up @@ -307,6 +308,14 @@ CREATE TABLE vitess_shard (
PRIMARY KEY (keyspace, shard)
)`,
`
DROP TABLET IF EXISTS vitess_cell
`,
`
CREATE TABLE vitess_cell (
cell varchar(128) NOT NULL,
PRIMARY KEY (cell)
)`,
`
CREATE INDEX source_host_port_idx_database_instance_database_instance on database_instance (source_host, source_port)
`,
`
Expand Down
45 changes: 45 additions & 0 deletions go/vt/vtorc/inst/cell_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2025 The Vitess Authors.
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 inst

import (
"vitess.io/vitess/go/vt/external/golib/sqlutils"
"vitess.io/vitess/go/vt/vtorc/db"
)

// ReadCells reads all the vitess cell names.
func ReadCells() ([]string, error) {
cells := make([]string, 0)
query := `SELECT cell FROM vitess_cell`
err := db.QueryVTOrc(query, nil, func(row sqlutils.RowMap) error {
cells = append(cells, row.GetString("cell"))
return nil
})
return cells, err
}

// SaveCell saves the keyspace record against the keyspace name.
func SaveCell(cell string) error {
_, err := db.ExecVTOrc(`REPLACE INTO vitess_cell (cell) VALUES(?)`, cell)
return err
}

// DeleteCell deletes a cell.
func DeleteCell(cell string) (err error) {
_, err = db.ExecVTOrc(`DELETE FROM vitess_cell WHERE cell = ?`, cell)
return err
}
75 changes: 75 additions & 0 deletions go/vt/vtorc/logic/cell_discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2025 The Vitess Authors.
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 logic

import (
"context"
"sync"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtorc/inst"
)

var refreshCellsMu sync.Mutex

// RefreshCells refreshes the list of cells.
func RefreshCells(ctx context.Context) error {
cellsCtx, cellsCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer cellsCancel()
cells, err := ts.GetKnownCells(cellsCtx)
if err != nil {
return err
}
return refreshCells(cells)
}

// refreshCells saves a slice of cells and removes
// stale cells that were not updated.
func refreshCells(cells []string) (err error) {
refreshCellsMu.Lock()
defer refreshCellsMu.Unlock()

// save cells.
updated := make(map[string]bool)
for _, cell := range cells {
err = inst.SaveCell(cell)
if err != nil {
log.Error(err)
return err
}
updated[cell] = true
}

// read all saved cells. the values should not
// be changing because we are holding a lock.
cells, err = inst.ReadCells()
if err != nil {
return err
}

// delete cells that are stale.
for _, cell := range cells {
if updated[cell] {
continue
}
if err = inst.DeleteCell(cell); err != nil {
return err
}
}
return nil
}
9 changes: 5 additions & 4 deletions go/vt/vtorc/logic/tablet_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ func refreshAllTablets(ctx context.Context) error {

// refreshTabletsUsing refreshes tablets using a provided loader.
func refreshTabletsUsing(ctx context.Context, loader func(tabletAlias string), forceRefresh bool) error {
// Get all cells.
ctx, cancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer cancel()
cells, err := ts.GetKnownCells(ctx)
cells, err := inst.ReadCells()
if err != nil {
return err
}
if len(cells) == 0 {
log.Error("Found no cells")
return nil
}

// Get all tablets from all cells.
getTabletsCtx, getTabletsCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtorc/logic/vtorc.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ func refreshAllInformation(ctx context.Context) error {
// Create an errgroup
eg, ctx := errgroup.WithContext(ctx)

// Refresh all cells.
eg.Go(func() error {
return RefreshCells(ctx)
})

// Refresh all keyspace information.
eg.Go(func() error {
return RefreshAllKeyspacesAndShards(ctx)
Expand Down

0 comments on commit aca4df2

Please sign in to comment.