Skip to content

Commit

Permalink
Provide Running call to see if the service is "up enough" (#340)
Browse files Browse the repository at this point in the history
* Provide Running call to see if the service is "up enough"

* Revert " fragile test case + race condition fixes (#337)"

This reverts commit c1243e7.

* only print an error if it fails on close

* Revert "Revert " fragile test case + race condition fixes (#337)""

This reverts commit 7e19d72.

* Be a lot more explicit when errors occur on New in database open
  • Loading branch information
marcopeereboom authored Dec 18, 2024
1 parent d6f1ba5 commit 06953a5
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 183 deletions.
17 changes: 14 additions & 3 deletions database/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ func New(ctx context.Context, home string, version int) (*Database, error) {
log.Tracef("New")
defer log.Tracef("New exit")

// Care must be taken to not shadow err and l in this function. The
// defer will overwrite those if an unwind condition occurs.

h, err := homedir.Expand(home)
if err != nil {
return nil, fmt.Errorf("home dir: %w", err)
Expand All @@ -181,7 +184,14 @@ func New(ctx context.Context, home string, version int) (*Database, error) {
unwind := true
defer func() {
if unwind {
log.Errorf("new unwind exited with: %v", l.Close())
cerr := l.Close()
if cerr != nil {
log.Debugf("new unwind exited with: %v", cerr)
err = errors.Join(err, cerr)
}
clear(l.pool)
clear(l.rawPool)
l = nil // Reset l
}
}()

Expand Down Expand Up @@ -240,7 +250,8 @@ func New(ctx context.Context, home string, version int) (*Database, error) {
dbVersion, version)
}

unwind = false
unwind = false // Everything is good, do not unwind.

return l, nil
// The defer above will set/reset these values.
return l, err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/sethvargo/go-retry v0.3.0
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/testcontainers/testcontainers-go v0.32.0
golang.org/x/sys v0.23.0
)

require (
Expand Down Expand Up @@ -90,6 +89,7 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.23.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
34 changes: 8 additions & 26 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ type Server struct {
// WebSockets
sessions map[string]*tbcWs
requestTimeout time.Duration

// ignoreUlimit will explicitly not check ulimit settings on the host
// machine, this is useful for very small datasets/chains
ignoreUlimit bool
}

func NewServer(cfg *Config) (*Server, error) {
Expand Down Expand Up @@ -579,7 +575,7 @@ func (s *Server) handlePeer(ctx context.Context, p *rawpeer.RawPeer) error {
}
}

func (s *Server) running() bool {
func (s *Server) Running() bool {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.isRunning
Expand All @@ -594,7 +590,7 @@ func (s *Server) testAndSetRunning(b bool) bool {
}

func (s *Server) promRunning() float64 {
r := s.running()
r := s.Running()
if r {
return 1
}
Expand Down Expand Up @@ -2228,28 +2224,9 @@ func (s *Server) Run(pctx context.Context) error {
return errors.New("run called but External Header mode is enabled")
}

if !s.testAndSetRunning(true) {
return errors.New("tbc already running")
}
defer s.testAndSetRunning(false)

// We need a lot of open files and memory for the indexes. Best effort
// to echo to the user what the ulimits are.
s.ignoreUlimit = true
if s.ignoreUlimit || s.cfg.Network == networkLocalnet {
log.Warningf("ignoring ulimit requirements")
} else if ulimitSupported {
if err := verifyUlimits(); err != nil {
return fmt.Errorf("verify ulimits: %w", err)
}
} else {
log.Errorf("This architecture does not supported ulimit verification. " +
"Consult the README for minimum values.")
}

// Rely on DBOpen failing if the database is already open.
ctx, cancel := context.WithCancel(pctx)
defer cancel()

err := s.DBOpen(ctx)
if err != nil {
return fmt.Errorf("open level database: %w", err)
Expand All @@ -2261,6 +2238,11 @@ func (s *Server) Run(pctx context.Context) error {
}
}()

if !s.testAndSetRunning(true) {
return errors.New("tbc already running")
}
defer s.testAndSetRunning(false)

// Find out where IBD is at
bhb, err := s.db.BlockHeaderBest(ctx)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions service/tbc/tbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,6 @@ func createTbcServer(ctx context.Context, t *testing.T, mappedPeerPort nat.Port)
t.Fatal(err)
}

tbcServer.ignoreUlimit = true

go func() {
err := tbcServer.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
Expand Down Expand Up @@ -1064,7 +1062,6 @@ func createTbcServerExternalHeaderMode(ctx context.Context, t *testing.T) *Serve
t.Fatal(err)
}

tbcServer.ignoreUlimit = true
tbcServer.ExternalHeaderSetup(ctx, defaultUpstreamStateId[:])
return tbcServer
}
Expand Down
3 changes: 0 additions & 3 deletions service/tbc/tbcfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,6 @@ func TestFork(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s.ignoreUlimit = true
go func() {
log.Infof("s run")
defer log.Infof("s run done")
Expand Down Expand Up @@ -1149,7 +1148,6 @@ func TestIndexNoFork(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s.ignoreUlimit = true

go func() {
err := s.Run(ctx)
Expand Down Expand Up @@ -1322,7 +1320,6 @@ func TestIndexFork(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s.ignoreUlimit = true
go func() {
err := s.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, rawpeer.ErrNoConn) {
Expand Down
65 changes: 0 additions & 65 deletions service/tbc/ulimit_darwin.go

This file was deleted.

69 changes: 0 additions & 69 deletions service/tbc/ulimit_linux.go

This file was deleted.

13 changes: 0 additions & 13 deletions service/tbc/ulimit_other.go

This file was deleted.

0 comments on commit 06953a5

Please sign in to comment.