Skip to content

Commit

Permalink
feat: Add a way to disable the indexer
Browse files Browse the repository at this point in the history
This is only for archive support at the moment, and configuring a node
with the indexer disabled will also prevent it from writing to the db.
  • Loading branch information
Yawning committed Jun 1, 2022
1 parent 40b0ac7 commit 8e41828
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
3 changes: 2 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Config struct {
// blocks that the node doesn't have data for, such as by skipping them in checkpoint sync.
// For sensible reasons, indexing may actually start at an even later block, such as if
// this block is already indexed or the node indicates that it doesn't have this block.
IndexingStart uint64 `koanf:"indexing_start"`
IndexingStart uint64 `koanf:"indexing_start"`
IndexingDisable bool `koanf:"indexing_disable"`

Log *LogConfig `koanf:"log"`
Cache *CacheConfig `koanf:"cache"`
Expand Down
29 changes: 25 additions & 4 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ var ErrNotHealthy = errors.New("not healthy")
type Service struct {
service.BaseBackgroundService

runtimeID common.Namespace
enablePruning bool
pruningStep uint64
indexingStart uint64
runtimeID common.Namespace
enablePruning bool
pruningStep uint64
indexingStart uint64
indexingDisable bool

backend Backend
client client.RuntimeClient
Expand Down Expand Up @@ -303,6 +304,14 @@ func (s *Service) indexingWorker() {

// Start starts service.
func (s *Service) Start() {
// TODO/NotYawning: Non-archive nodes that have the indexer disabled
// likey want to use a different notion of healthy, and probably also
// want to start a worker that monitors the database for changes.
if s.indexingDisable {
s.updateHealth(true)
return
}

go s.indexingWorker()
go s.healthWorker()

Expand Down Expand Up @@ -339,8 +348,20 @@ func New(
enablePruning: cfg.EnablePruning,
pruningStep: cfg.PruningStep,
indexingStart: cfg.IndexingStart,
indexingDisable: cfg.IndexingDisable,
}
s.Logger = s.Logger.With("runtime_id", s.runtimeID.String())

// TODO/NotYawning: Non-archive nodes probably want to do something
// different here.
if s.indexingDisable {
if _, err := s.backend.QueryLastIndexedRound(ctx); err != nil {
s.Logger.Error("indexer disabled and no rounds indexed, this will never work",
"err", err,
)
return nil, nil, err
}
}

return s, cachingBackend, nil
}
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func truncateExec(cmd *cobra.Command, args []string) error {
}

// Initialize db.
db, err := psql.InitDB(ctx, cfg.Database, true)
db, err := psql.InitDB(ctx, cfg.Database, true, false)
if err != nil {
logger.Error("failed to initialize db", "err", err)
return err
Expand Down Expand Up @@ -145,7 +145,7 @@ func migrateExec(cmd *cobra.Command, args []string) error {
logger := logging.GetLogger("migrate-db")

// Initialize db.
db, err := psql.InitDB(ctx, cfg.Database, true)
db, err := psql.InitDB(ctx, cfg.Database, true, false)
if err != nil {
logger.Error("failed to initialize db", "err", err)
return err
Expand Down Expand Up @@ -192,8 +192,13 @@ func runRoot() error {
// Create the runtime client with account module query helpers.
rc := client.New(conn, runtimeID)

// For now, "disable" write access to the DB in a kind of kludgy way
// if the indexer is disabled. Yes this means that no migrations
// can be done. Deal with it.
dbReadOnly := cfg.IndexingDisable

// Initialize db for migrations (higher timeouts).
db, err := psql.InitDB(ctx, cfg.Database, true)
db, err := psql.InitDB(ctx, cfg.Database, true, dbReadOnly)
if err != nil {
logger.Error("failed to initialize db", "err", err)
return err
Expand All @@ -208,7 +213,7 @@ func runRoot() error {

// Initialize db again, now with configured timeouts.
var storage storage.Storage
storage, err = psql.InitDB(ctx, cfg.Database, false)
storage, err = psql.InitDB(ctx, cfg.Database, false, dbReadOnly)
if err != nil {
logger.Error("failed to initialize db", "err", err)
return err
Expand Down
20 changes: 19 additions & 1 deletion storage/psql/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ type PostDB struct {
}

// InitDB creates postgresql db instance.
func InitDB(ctx context.Context, cfg *conf.DatabaseConfig, longTimeouts bool) (*PostDB, error) {
func InitDB(
ctx context.Context,
cfg *conf.DatabaseConfig,
longTimeouts bool,
readOnly bool,
) (*PostDB, error) {
if cfg == nil {
return nil, errors.New("nil configuration")
}
Expand Down Expand Up @@ -63,6 +68,19 @@ func InitDB(ctx context.Context, cfg *conf.DatabaseConfig, longTimeouts bool) (*
}
}

// Set "read-only" mode by setting the default status of new
// transactions.
//
// Note: This still allows txes to alter temporary tables, and is
// advisory rather than something that is securely enforced.
if readOnly {
opts = append(opts, pgdriver.WithConnParams(
map[string]interface{}{
"default_transaction_read_only": "on",
},
))
}

pgConn := pgdriver.NewConnector(opts...)
sqlDB := sql.OpenDB(pgConn)
maxOpenConns := cfg.MaxOpenConns
Expand Down
2 changes: 1 addition & 1 deletion storage/psql/psql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
var err error
ctx := context.Background()
tests.MustInitConfig()
db, err = InitDB(ctx, tests.TestsConfig.Database, false)
db, err = InitDB(ctx, tests.TestsConfig.Database, false, false)
if err != nil {
log.Println(`It seems database failed to initialize. Do you have PostgreSQL running? If not, you can run
docker run -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p 5432:5432 -d postgres`)
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Setup() error {

// Initialize db.
ctx := context.Background()
db, err = psql.InitDB(ctx, tests.TestsConfig.Database, true)
db, err = psql.InitDB(ctx, tests.TestsConfig.Database, true, false)
if err != nil {
return fmt.Errorf("failed to initialize DB: %w", err)
}
Expand All @@ -143,7 +143,7 @@ func Setup() error {

// Initialize db again, now with configured timeouts.
var storage storage.Storage
storage, err = psql.InitDB(ctx, tests.TestsConfig.Database, false)
storage, err = psql.InitDB(ctx, tests.TestsConfig.Database, false, false)
if err != nil {
return err
}
Expand Down

0 comments on commit 8e41828

Please sign in to comment.