From 2b0ed21f083e3333bc4508b8c1d0266d7385eca6 Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Wed, 1 Mar 2017 07:35:52 +0530 Subject: [PATCH] tests: Fix test server init - cleanup (#3806) --- cmd/benchmark-utils_test.go | 20 ++------------ cmd/fs-v1-rwpool.go | 28 +++++++++++++++++-- cmd/test-utils_test.go | 55 +++++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/cmd/benchmark-utils_test.go b/cmd/benchmark-utils_test.go index bcd231568..0bd8f3c3d 100644 --- a/cmd/benchmark-utils_test.go +++ b/cmd/benchmark-utils_test.go @@ -28,25 +28,9 @@ import ( humanize "github.com/dustin/go-humanize" ) -// Prepare benchmark backend +// Prepare XL/FS backend for benchmark. func prepareBenchmarkBackend(instanceType string) (ObjectLayer, []string, error) { - switch instanceType { - // Total number of disks for FS backend is set to 1. - case FSTestStr: - obj, disk, err := prepareFS() - if err != nil { - return nil, nil, err - } - return obj, []string{disk}, nil - // Total number of disks for XL backend is set to 16. - case XLTestStr: - return prepareXL() - } - obj, disk, err := prepareFS() - if err != nil { - return nil, nil, err - } - return obj, []string{disk}, nil + return prepareTestBackend(instanceType) } // Benchmark utility functions for ObjectLayer.PutObject(). diff --git a/cmd/fs-v1-rwpool.go b/cmd/fs-v1-rwpool.go index b568c05d5..74a0f38b9 100644 --- a/cmd/fs-v1-rwpool.go +++ b/cmd/fs-v1-rwpool.go @@ -50,7 +50,6 @@ func (fsi *fsIOPool) Open(path string) (*lock.RLockedFile, error) { fsi.Lock() rlkFile, ok := fsi.readersMap[path] - // File reference exists on map, validate if its // really closed and we are safe to purge it. if ok && rlkFile != nil { @@ -76,8 +75,9 @@ func (fsi *fsIOPool) Open(path string) (*lock.RLockedFile, error) { // read lock mode. if !ok { var err error + var newRlkFile *lock.RLockedFile // Open file for reading. - rlkFile, err = lock.RLockedOpenFile(preparePath(path)) + newRlkFile, err = lock.RLockedOpenFile(preparePath(path)) if err != nil { if os.IsNotExist(err) { return nil, errFileNotFound @@ -95,6 +95,30 @@ func (fsi *fsIOPool) Open(path string) (*lock.RLockedFile, error) { // Save new reader on the map. fsi.Lock() + rlkFile, ok = fsi.readersMap[path] + if ok && rlkFile != nil { + // If the file is closed and not removed from map is a bug. + if rlkFile.IsClosed() { + // Log this as an error. + errorIf(errUnexpected, "Unexpected entry found on the map %s", path) + + // Purge the cached lock path from map. + delete(fsi.readersMap, path) + + // Save the newly acquired read locked file. + rlkFile = newRlkFile + } else { + // Increment the lock ref, since the file is not closed yet + // and caller requested to read the file again. + rlkFile.IncLockRef() + newRlkFile.Close() + } + } else { + // Save the newly acquired read locked file. + rlkFile = newRlkFile + } + + // Save the rlkFile back on the map. fsi.readersMap[path] = rlkFile fsi.Unlock() } diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index cb1d8d1e2..ef21fb881 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -75,19 +75,11 @@ func prepareFS() (ObjectLayer, string, error) { if err != nil { return nil, "", err } - endpoints, err := parseStorageEndpoints(fsDirs) + obj, err := newFSObjectLayer(fsDirs[0]) if err != nil { return nil, "", err } - fsPath, err := url.QueryUnescape(endpoints[0].String()) - if err != nil { - return nil, "", err - } - obj, err := newFSObjectLayer(fsPath) - if err != nil { - return nil, "", err - } - return obj, endpoints[0].Path, nil + return obj, fsDirs[0], nil } func prepareXL() (ObjectLayer, []string, error) { @@ -193,16 +185,18 @@ type TestServer struct { SrvCmdCfg serverCmdConfig } +// UnstartedTestServer - Configures a temp FS/XL backend, +// initializes the endpoints and configures the test server. +// The server should be started using the Start() method. func UnstartedTestServer(t TestErrHandler, instanceType string) TestServer { // create an instance of TestServer. testServer := TestServer{} - // create temporary backend for the test server. - nDisks := 16 - disks, err := getRandomDisks(nDisks) + // return FS/XL object layer and temp backend. + objLayer, disks, err := prepareTestBackend(instanceType) if err != nil { - t.Fatal("Failed to create disks for the backend") + t.Fatal(err) } - + // set the server configuration. root, err := newTestConfig(globalMinioDefaultRegion) if err != nil { t.Fatalf("%s", err) @@ -212,19 +206,15 @@ func UnstartedTestServer(t TestErrHandler, instanceType string) TestServer { // Get credential. credentials := serverConfig.GetCredential() - testServer.Root = root + testServer.Obj = objLayer testServer.Disks, err = parseStorageEndpoints(disks) if err != nil { - t.Fatalf("Unexpected error %s", err) + t.Fatalf("Unexpected error %v", err) } + testServer.Root = root testServer.AccessKey = credentials.AccessKey testServer.SecretKey = credentials.SecretKey - objLayer, _, err := initObjectLayer(testServer.Disks) - if err != nil { - t.Fatalf("Failed obtaining Temp Backend: %s", err) - } - srvCmdCfg := serverCmdConfig{ endpoints: testServer.Disks, } @@ -238,10 +228,9 @@ func UnstartedTestServer(t TestErrHandler, instanceType string) TestServer { // Run TestServer. testServer.Server = httptest.NewUnstartedServer(httpHandler) - + // obtain server address. srvCmdCfg.serverAddr = testServer.Server.Listener.Addr().String() - testServer.Obj = objLayer globalObjLayerMutex.Lock() globalObjectAPI = objLayer globalObjLayerMutex.Unlock() @@ -1774,6 +1763,24 @@ func initAPIHandlerTest(obj ObjectLayer, endpoints []string) (string, http.Handl return bucketName, f, nil } +// prepare test backend. +// create FS/XL bankend. +// return object layer, backend disks. +func prepareTestBackend(instanceType string) (ObjectLayer, []string, error) { + switch instanceType { + // Total number of disks for XL backend is set to 16. + case XLTestStr: + return prepareXL() + default: + // return FS backend by default. + obj, disk, err := prepareFS() + if err != nil { + return nil, nil, err + } + return obj, []string{disk}, nil + } +} + // ExecObjectLayerAPIAnonTest - Helper function to validate object Layer API handler // response for anonymous/unsigned and unknown signature type HTTP request.