Skip to content

Commit 8e41828

Browse files
committed
feat: Add a way to disable the indexer
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.
1 parent 40b0ac7 commit 8e41828

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

conf/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type Config struct {
2222
// blocks that the node doesn't have data for, such as by skipping them in checkpoint sync.
2323
// For sensible reasons, indexing may actually start at an even later block, such as if
2424
// this block is already indexed or the node indicates that it doesn't have this block.
25-
IndexingStart uint64 `koanf:"indexing_start"`
25+
IndexingStart uint64 `koanf:"indexing_start"`
26+
IndexingDisable bool `koanf:"indexing_disable"`
2627

2728
Log *LogConfig `koanf:"log"`
2829
Cache *CacheConfig `koanf:"cache"`

indexer/indexer.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ var ErrNotHealthy = errors.New("not healthy")
4343
type Service struct {
4444
service.BaseBackgroundService
4545

46-
runtimeID common.Namespace
47-
enablePruning bool
48-
pruningStep uint64
49-
indexingStart uint64
46+
runtimeID common.Namespace
47+
enablePruning bool
48+
pruningStep uint64
49+
indexingStart uint64
50+
indexingDisable bool
5051

5152
backend Backend
5253
client client.RuntimeClient
@@ -303,6 +304,14 @@ func (s *Service) indexingWorker() {
303304

304305
// Start starts service.
305306
func (s *Service) Start() {
307+
// TODO/NotYawning: Non-archive nodes that have the indexer disabled
308+
// likey want to use a different notion of healthy, and probably also
309+
// want to start a worker that monitors the database for changes.
310+
if s.indexingDisable {
311+
s.updateHealth(true)
312+
return
313+
}
314+
306315
go s.indexingWorker()
307316
go s.healthWorker()
308317

@@ -339,8 +348,20 @@ func New(
339348
enablePruning: cfg.EnablePruning,
340349
pruningStep: cfg.PruningStep,
341350
indexingStart: cfg.IndexingStart,
351+
indexingDisable: cfg.IndexingDisable,
342352
}
343353
s.Logger = s.Logger.With("runtime_id", s.runtimeID.String())
344354

355+
// TODO/NotYawning: Non-archive nodes probably want to do something
356+
// different here.
357+
if s.indexingDisable {
358+
if _, err := s.backend.QueryLastIndexedRound(ctx); err != nil {
359+
s.Logger.Error("indexer disabled and no rounds indexed, this will never work",
360+
"err", err,
361+
)
362+
return nil, nil, err
363+
}
364+
}
365+
345366
return s, cachingBackend, nil
346367
}

main.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func truncateExec(cmd *cobra.Command, args []string) error {
114114
}
115115

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

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

195+
// For now, "disable" write access to the DB in a kind of kludgy way
196+
// if the indexer is disabled. Yes this means that no migrations
197+
// can be done. Deal with it.
198+
dbReadOnly := cfg.IndexingDisable
199+
195200
// Initialize db for migrations (higher timeouts).
196-
db, err := psql.InitDB(ctx, cfg.Database, true)
201+
db, err := psql.InitDB(ctx, cfg.Database, true, dbReadOnly)
197202
if err != nil {
198203
logger.Error("failed to initialize db", "err", err)
199204
return err
@@ -208,7 +213,7 @@ func runRoot() error {
208213

209214
// Initialize db again, now with configured timeouts.
210215
var storage storage.Storage
211-
storage, err = psql.InitDB(ctx, cfg.Database, false)
216+
storage, err = psql.InitDB(ctx, cfg.Database, false, dbReadOnly)
212217
if err != nil {
213218
logger.Error("failed to initialize db", "err", err)
214219
return err

storage/psql/psql.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ type PostDB struct {
3030
}
3131

3232
// InitDB creates postgresql db instance.
33-
func InitDB(ctx context.Context, cfg *conf.DatabaseConfig, longTimeouts bool) (*PostDB, error) {
33+
func InitDB(
34+
ctx context.Context,
35+
cfg *conf.DatabaseConfig,
36+
longTimeouts bool,
37+
readOnly bool,
38+
) (*PostDB, error) {
3439
if cfg == nil {
3540
return nil, errors.New("nil configuration")
3641
}
@@ -63,6 +68,19 @@ func InitDB(ctx context.Context, cfg *conf.DatabaseConfig, longTimeouts bool) (*
6368
}
6469
}
6570

71+
// Set "read-only" mode by setting the default status of new
72+
// transactions.
73+
//
74+
// Note: This still allows txes to alter temporary tables, and is
75+
// advisory rather than something that is securely enforced.
76+
if readOnly {
77+
opts = append(opts, pgdriver.WithConnParams(
78+
map[string]interface{}{
79+
"default_transaction_read_only": "on",
80+
},
81+
))
82+
}
83+
6684
pgConn := pgdriver.NewConnector(opts...)
6785
sqlDB := sql.OpenDB(pgConn)
6886
maxOpenConns := cfg.MaxOpenConns

storage/psql/psql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
2121
var err error
2222
ctx := context.Background()
2323
tests.MustInitConfig()
24-
db, err = InitDB(ctx, tests.TestsConfig.Database, false)
24+
db, err = InitDB(ctx, tests.TestsConfig.Database, false, false)
2525
if err != nil {
2626
log.Println(`It seems database failed to initialize. Do you have PostgreSQL running? If not, you can run
2727
docker run -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p 5432:5432 -d postgres`)

tests/rpc/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func Setup() error {
130130

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

144144
// Initialize db again, now with configured timeouts.
145145
var storage storage.Storage
146-
storage, err = psql.InitDB(ctx, tests.TestsConfig.Database, false)
146+
storage, err = psql.InitDB(ctx, tests.TestsConfig.Database, false, false)
147147
if err != nil {
148148
return err
149149
}

0 commit comments

Comments
 (0)