forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tim Vaillancourt <[email protected]>
- Loading branch information
1 parent
4c9de0b
commit aca4df2
Showing
5 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters