Skip to content

Commit

Permalink
Pull sefl-test folder out from the director tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haoming29 committed May 14, 2024
1 parent 8e37b46 commit de19136
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 58 deletions.
45 changes: 0 additions & 45 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ package cache

import (
"context"
"errors"
"os"
"path/filepath"
"time"

"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"

"github.com/pelicanplatform/pelican/param"
"github.com/pelicanplatform/pelican/server_utils"
)

Expand All @@ -45,43 +40,3 @@ func RegisterCacheAPI(router *gin.Engine, ctx context.Context, egrp *errgroup.Gr
group.POST("/directorTest", func(ginCtx *gin.Context) { server_utils.HandleDirectorTestResponse(ginCtx, notificationChan) })
}
}

// Periodically scan the /<Cache.LocalRoot>/pelican/monitoring directory to clean up test files
func LaunchDirectorTestFileCleanup(ctx context.Context) {
server_utils.LaunchWatcherMaintenance(ctx,
[]string{filepath.Join(param.Cache_LocalRoot.GetString(), "pelican", "monitoring")},
"cache director-based health test clean up",
time.Minute,
func(notifyEvent bool) error {
// We run this function regardless of notifyEvent to do the cleanup
dirPath := filepath.Join(param.Cache_LocalRoot.GetString(), "pelican", "monitoring")
dirInfo, err := os.Stat(dirPath)
if err != nil {
return err
} else {
if !dirInfo.IsDir() {
return errors.New("monitoring path is not a directory: " + dirPath)
}
}
dirItems, err := os.ReadDir(dirPath)
if err != nil {
return err
}
if len(dirItems) <= 2 { // At mininum there are the test file and .cinfo file, and we don't want to remove the last two
return nil
}
for idx, item := range dirItems {
// For all but the latest two files (test file and its .cinfo file)
// os.ReadDir sorts dirEntries in order of file names. Since our test file names are timestamped and is string comparable,
// the last two files should be the latest test files, which we want to keep
if idx < len(dirItems)-2 {
err := os.Remove(filepath.Join(dirPath, item.Name()))
if err != nil {
return err
}
}
}
return nil
},
)
}
61 changes: 60 additions & 1 deletion cache/cache_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
package cache

import (
"context"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/pelicanplatform/pelican/param"
"github.com/pkg/errors"

"github.com/pelicanplatform/pelican/param"
"github.com/pelicanplatform/pelican/server_utils"
)

// Check for the sentinel file
Expand All @@ -44,3 +50,56 @@ func CheckCacheSentinelLocation() error {
}
return nil
}

// Periodically scan the /<Cache.LocalRoot>/pelican/monitoring directory to clean up test files
// TODO: Director test files should be under /pelican/monitoring/directorTest and the file names
// should have director-test- as the prefix
func LaunchDirectorTestFileCleanup(ctx context.Context) {
server_utils.LaunchWatcherMaintenance(ctx,
[]string{filepath.Join(param.Cache_LocalRoot.GetString(), "pelican", "monitoring")},
"cache director-based health test clean up",
time.Minute,
func(notifyEvent bool) error {
// We run this function regardless of notifyEvent to do the cleanup
dirPath := filepath.Join(param.Cache_LocalRoot.GetString(), "pelican", "monitoring")
dirInfo, err := os.Stat(dirPath)
if err != nil {
return err
} else {
if !dirInfo.IsDir() {
return errors.New("monitoring path is not a directory: " + dirPath)
}
}
dirItems, err := os.ReadDir(dirPath)
if err != nil {
return err
}
directorItems := []fs.DirEntry{}
for _, item := range dirItems {
if item.IsDir() {
continue
}
// Ignore self tests. They should be handled automatically by self test logic
if strings.HasPrefix(item.Name(), selfTestPrefix) {
continue
}
directorItems = append(directorItems, item)
}
if len(directorItems) <= 2 { // At mininum there are the test file and .cinfo file, and we don't want to remove the last two
return nil
}
for idx, item := range directorItems {
// For all but the latest two files (test file and its .cinfo file)
// os.ReadDir sorts dirEntries in order of file names. Since our test file names are timestamped and is string comparable,
// the last two files should be the latest test files, which we want to keep
if idx < len(dirItems)-2 {
err := os.Remove(filepath.Join(dirPath, item.Name()))
if err != nil {
return err
}
}
}
return nil
},
)
}
29 changes: 17 additions & 12 deletions cache/self_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ import (
)

const (
selfTestBody string = "This object was created by the Pelican self-test functionality"
selfTestDir string = "/pelican/monitoring"
selfTestBody string = "This object was created by the Pelican self-test functionality"
selfTestDir string = "/pelican/monitoring/selfTest"
selfTestPrefix string = "self-test-"
)

// Add self-test directories to xrootd data location of the cache
Expand All @@ -59,7 +60,8 @@ func InitSelfTestDir() error {
basePath := param.Cache_LocalRoot.GetString()
pelicanMonPath := filepath.Join(basePath, "/pelican")
monitoringPath := filepath.Join(pelicanMonPath, "/monitoring")
err = os.MkdirAll(monitoringPath, 0700)
selfTestPath := filepath.Join(monitoringPath, "/selfTest")
err = os.MkdirAll(selfTestPath, 0700)
if err != nil {
return errors.Wrap(err, "Fail to create directory for the self-test")
}
Expand All @@ -69,6 +71,9 @@ func InitSelfTestDir() error {
if err = os.Chown(monitoringPath, uid, gid); err != nil {
return errors.Wrapf(err, "Unable to change ownership of self-test /pelican/monitoring directory %v to desired daemon gid %v", monitoringPath, gid)
}
if err = os.Chown(selfTestPath, uid, gid); err != nil {
return errors.Wrapf(err, "Unable to change ownership of self-test /pelican/monitoring directory %v to desired daemon gid %v", monitoringPath, gid)
}
return nil
}

Expand All @@ -77,10 +82,10 @@ func generateTestFile() (string, error) {
if basePath == "" {
return "", errors.New("failed to generate self-test file for cache: Cache.LocalRoot is not set.")
}
monitoringPath := filepath.Join(basePath, selfTestDir)
_, err := os.Stat(monitoringPath)
selfTestPath := filepath.Join(basePath, selfTestDir)
_, err := os.Stat(selfTestPath)
if err != nil {
return "", errors.Wrap(err, "self-test directory does not exist at "+monitoringPath)
return "", errors.Wrap(err, "self-test directory does not exist at "+selfTestPath)
}
uid, err := config.GetDaemonUID()
if err != nil {
Expand All @@ -107,13 +112,13 @@ func generateTestFile() (string, error) {
return "", err
}

testFileName := "self-test-" + now.Format(time.RFC3339) + ".txt"
testFileCinfoName := "self-test-" + now.Format(time.RFC3339) + ".txt.cinfo"
testFileName := selfTestPrefix + now.Format(time.RFC3339) + ".txt"
testFileCinfoName := selfTestPrefix + now.Format(time.RFC3339) + ".txt.cinfo"

finalFilePath := filepath.Join(monitoringPath, testFileName)
finalFilePath := filepath.Join(selfTestPath, testFileName)

tmpFileCinfoPath := filepath.Join(monitoringPath, testFileCinfoName+".tmp")
finalFileCinfoPath := filepath.Join(monitoringPath, testFileCinfoName)
tmpFileCinfoPath := filepath.Join(selfTestPath, testFileCinfoName+".tmp")
finalFileCinfoPath := filepath.Join(selfTestPath, testFileCinfoName)

// This is for web URL path, do not use filepath
extFilePath := path.Join(selfTestDir, testFileName)
Expand Down Expand Up @@ -169,7 +174,7 @@ func generateFileTestScitoken() (string, error) {
fTestTokenCfg.Lifetime = time.Minute
fTestTokenCfg.Issuer = issuerUrl
fTestTokenCfg.Subject = "cache"
fTestTokenCfg.Claims = map[string]string{"scope": "storage.read:/pelican/monitoring"}
fTestTokenCfg.Claims = map[string]string{"scope": "storage.read:/pelican/monitoring/selfTest"}
// For self-tests, the audience is the server itself
fTestTokenCfg.AddAudienceAny()

Expand Down

0 comments on commit de19136

Please sign in to comment.