github.com/Cloud-Foundations/Dominator@v0.3.4/lib/objectserver/filesystem/new.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Cloud-Foundations/Dominator/lib/hash"
     7  	"github.com/Cloud-Foundations/Dominator/lib/lockwatcher"
     8  	"github.com/Cloud-Foundations/Dominator/lib/log/prefixlogger"
     9  	"github.com/Cloud-Foundations/Dominator/lib/objectserver/filesystem/scan"
    10  	"github.com/Cloud-Foundations/Dominator/lib/wsyscall"
    11  )
    12  
    13  func newObjectServer(config Config, params Params) (*ObjectServer, error) {
    14  	objSrv := &ObjectServer{
    15  		Config:                config,
    16  		Params:                params,
    17  		lastGarbageCollection: time.Now(),
    18  		objects:               make(map[hash.Hash]*objectType),
    19  	}
    20  	startTime := time.Now()
    21  	var rusageStart, rusageStop wsyscall.Rusage
    22  	wsyscall.Getrusage(wsyscall.RUSAGE_SELF, &rusageStart)
    23  	err := scan.ScanTree(config.BaseDirectory, func(hashVal hash.Hash,
    24  		size uint64) {
    25  		objSrv.rwLock.Lock()
    26  		objSrv.add(&objectType{hash: hashVal, size: size})
    27  		objSrv.rwLock.Unlock()
    28  	})
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	plural := ""
    33  	if len(objSrv.objects) != 1 {
    34  		plural = "s"
    35  	}
    36  	err = wsyscall.Getrusage(wsyscall.RUSAGE_SELF, &rusageStop)
    37  	if err != nil {
    38  		params.Logger.Printf("Scanned %d object%s in %s\n",
    39  			len(objSrv.objects), plural, time.Since(startTime))
    40  	} else {
    41  		userTime := time.Duration(rusageStop.Utime.Sec)*time.Second +
    42  			time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
    43  			time.Duration(rusageStart.Utime.Sec)*time.Second -
    44  			time.Duration(rusageStart.Utime.Usec)*time.Microsecond
    45  		params.Logger.Printf("Scanned %d object%s in %s (%s user CPUtime)\n",
    46  			len(objSrv.objects), plural, time.Since(startTime), userTime)
    47  	}
    48  	go objSrv.garbageCollectorLoop()
    49  	objSrv.lockWatcher = lockwatcher.New(&objSrv.rwLock,
    50  		lockwatcher.LockWatcherOptions{
    51  			CheckInterval: config.LockCheckInterval,
    52  			Logger:        prefixlogger.New("ObjectServer: ", params.Logger),
    53  			LogTimeout:    config.LockLogTimeout,
    54  		})
    55  	return objSrv, nil
    56  }