Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DATA] Don't acquire lock for whole duration of syncDataCaptureFiles. #1481

Merged
merged 5 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions services/datamanager/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,10 @@ func (svc *builtIn) Sync(_ context.Context) error {

func (svc *builtIn) syncDataCaptureFiles() error {
svc.lock.Lock()
defer svc.lock.Unlock()
oldFiles := make([]string, 0, len(svc.collectors))
for _, collector := range svc.collectors {
currCollectors := svc.collectors
AaronCasas marked this conversation as resolved.
Show resolved Hide resolved
svc.lock.Unlock()
for _, collector := range currCollectors {
// Create new target and set it.
attributes := collector.Attributes
captureMetadata, err := datacapture.BuildCaptureMetadata(attributes.Type, attributes.Name,
Expand All @@ -378,7 +379,7 @@ func (svc *builtIn) syncDataCaptureFiles() error {

nextTarget, err := datacapture.CreateDataCaptureFile(svc.captureDir, captureMetadata)
if err != nil {
svc.logger.Errorw("failed to create new data capture file", "error", err)
return err
AaronCasas marked this conversation as resolved.
Show resolved Hide resolved
}
oldFiles = append(oldFiles, collector.Collector.GetTarget().Name())
collector.Collector.SetTarget(nextTarget)
Expand All @@ -389,10 +390,13 @@ func (svc *builtIn) syncDataCaptureFiles() error {

func (svc *builtIn) buildAdditionalSyncPaths() []string {
svc.lock.Lock()
defer svc.lock.Unlock()
currAdditionalSyncPaths := svc.additionalSyncPaths
waitAfterLastModified := svc.waitAfterLastModifiedSecs
svc.lock.Unlock()

var filepathsToSync []string
// Loop through additional sync paths and add files from each to the syncer.
for _, asp := range svc.additionalSyncPaths {
for _, asp := range currAdditionalSyncPaths {
// Check that additional sync paths directories exist. If not, create directories accordingly.
if _, err := os.Stat(asp); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(asp, os.ModePerm)
Expand All @@ -410,9 +414,9 @@ func (svc *builtIn) buildAdditionalSyncPaths() []string {
if info.IsDir() {
return nil
}
// If a file was modified within the past svc.waitAfterLastModifiedSecs seconds, do not sync it (data
// If a file was modified within the past waitAfterLastModifiedSecs seconds, do not sync it (data
// may still be being written).
if diff := now.Sub(info.ModTime()); diff < (time.Duration(svc.waitAfterLastModifiedSecs) * time.Second) {
if diff := now.Sub(info.ModTime()); diff < (time.Duration(waitAfterLastModified) * time.Second) {
return nil
}
filepathsToSync = append(filepathsToSync, path)
Expand Down
1 change: 1 addition & 0 deletions services/datamanager/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestNewDataManager(t *testing.T) {
// Empty config at initialization.
captureDir := "/tmp/capture"
defer resetFolder(t, captureDir)
resetFolder(t, captureDir)
err := dmsvc.Update(context.Background(), testCfg)
test.That(t, err, test.ShouldBeNil)
captureTime := time.Millisecond * 100
Expand Down