Skip to content

Commit

Permalink
indexserver: delete tmp dir on startup
Browse files Browse the repository at this point in the history
This probably caused our tmp-dirs to run full. In production we saw data
from years ago taking up disk space.

Co-authored-by: Petri-Johan Last <[email protected]>
Co-authored-by: Keegan Carruthers-Smith <[email protected]>
  • Loading branch information
3 people committed Sep 14, 2023
1 parent 2d1affd commit 4a6b66d
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions cmd/zoekt-sourcegraph-indexserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ import (
"time"

grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2"
"github.com/keegancsmith/tmpfriend"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
sglog "github.com/sourcegraph/log"
proto "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/protos/sourcegraph/zoekt/configuration/v1"
"github.com/sourcegraph/zoekt/grpc/internalerrs"
"github.com/sourcegraph/zoekt/grpc/messagesize"
"go.uber.org/automaxprocs/maxprocs"
"golang.org/x/net/trace"
"golang.org/x/sys/unix"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

proto "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/protos/sourcegraph/zoekt/configuration/v1"
"github.com/sourcegraph/zoekt/grpc/internalerrs"
"github.com/sourcegraph/zoekt/grpc/messagesize"

"github.com/sourcegraph/mountinfo"

"github.com/sourcegraph/zoekt"
Expand Down Expand Up @@ -981,23 +981,28 @@ func listIndexed(indexDir string) []uint32 {
//
// If main is true we will delete older temp directories left around. main is
// false when this is a debug command.
func setupTmpDir(main bool, index string) error {
// change the target tmp directory depending on if its our main daemon or a
func setupTmpDir(logger sglog.Logger, main bool, index string) error {
// change the target tmp directory depending on if it's our main daemon or a
// debug sub command.
dir := ".indexserver.debug.tmp"
if main {
dir = ".indexserver.tmp"
}

tmpRoot := filepath.Join(index, dir)
if err := os.MkdirAll(tmpRoot, 0755); err != nil {
return err

if main {
err := os.RemoveAll(tmpRoot)
if err != nil {
logger.Error("failed to remove tmp dir", sglog.String("tmpRoot", tmpRoot), sglog.Error(err))
}
}
if !tmpfriend.IsTmpFriendDir(tmpRoot) {
_, err := tmpfriend.RootTempDir(tmpRoot)

if err := os.MkdirAll(tmpRoot, 0755); err != nil {
return err
}
return nil

return os.Setenv("TMPDIR", tmpRoot)
}

func printMetaData(fn string) error {
Expand Down Expand Up @@ -1296,6 +1301,8 @@ func startServer(conf rootConfig) error {
}

func newServer(conf rootConfig) (*Server, error) {
logger := sglog.Scoped("server", "periodically reindexes enabled repositories on sourcegraph")

if conf.cpuFraction <= 0.0 || conf.cpuFraction > 1.0 {
return nil, fmt.Errorf("cpu_fraction must be between 0.0 and 1.0")
}
Expand Down Expand Up @@ -1331,7 +1338,7 @@ func newServer(conf rootConfig) (*Server, error) {
}
}

if err := setupTmpDir(conf.Main, conf.index); err != nil {
if err := setupTmpDir(logger, conf.Main, conf.index); err != nil {
return nil, fmt.Errorf("failed to setup TMPDIR under %s: %v", conf.index, err)
}

Expand Down Expand Up @@ -1401,8 +1408,6 @@ func newServer(conf rootConfig) (*Server, error) {
cpuCount = 1
}

logger := sglog.Scoped("server", "periodically reindexes enabled repositories on sourcegraph")

q := NewQueue(conf.backoffDuration, conf.maxBackoffDuration, logger)

return &Server{
Expand Down

0 comments on commit 4a6b66d

Please sign in to comment.