From 375b79f11b7e92498152600b71c3d9f4d227b3d2 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 28 May 2020 16:17:42 +0100 Subject: [PATCH] storage: Implement GetDiskID request in REST server side (#9720) GetDiskID() in storage rest client does not really issue a REST request to the remote disk, but returns an in-memory value instead. However, GetDiskID() should return an error when format.json is not found or for other similar issues (unmounted disks, etc..) GetDiskID() is only called when formatting disks and getting storage informatio, hence this commit should not have a performance degradation. --- cmd/storage-interface.go | 2 +- cmd/storage-rest-client.go | 14 +++++++++++++- cmd/storage-rest-common.go | 1 + cmd/storage-rest-server.go | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/storage-interface.go b/cmd/storage-interface.go index 7cfe37f8d..e3a8d4a94 100644 --- a/cmd/storage-interface.go +++ b/cmd/storage-interface.go @@ -31,7 +31,7 @@ type StorageAPI interface { IsLocal() bool Hostname() string // Returns host name if remote host. Close() error - GetDiskID() (string, error) + GetDiskID() (string, error) // Could be expensive SetDiskID(id string) DiskInfo() (info DiskInfo, err error) diff --git a/cmd/storage-rest-client.go b/cmd/storage-rest-client.go index e57e33b94..5a81eb047 100644 --- a/cmd/storage-rest-client.go +++ b/cmd/storage-rest-client.go @@ -177,7 +177,19 @@ func (client *storageRESTClient) CrawlAndGetDataUsage(ctx context.Context, cache } func (client *storageRESTClient) GetDiskID() (string, error) { - return client.diskID, nil + respBody, err := client.call(storageRESTMethodGetDiskID, nil, nil, -1) + if err != nil { + // Ignore when other nodes does not support GetDiskID call, this check + // can be removed when the storage API version is bumped. + if strings.Contains(err.Error(), "404 page not found") { + return client.diskID, nil + } + return "", err + } + defer http.DrainBody(respBody) + var s string + err = gob.NewDecoder(respBody).Decode(&s) + return s, err } func (client *storageRESTClient) SetDiskID(id string) { diff --git a/cmd/storage-rest-common.go b/cmd/storage-rest-common.go index e9d8ed5c7..f4ab9c144 100644 --- a/cmd/storage-rest-common.go +++ b/cmd/storage-rest-common.go @@ -24,6 +24,7 @@ const ( const ( storageRESTMethodDiskInfo = "/diskinfo" + storageRESTMethodGetDiskID = "/getdiskid" storageRESTMethodCrawlAndGetDataUsage = "/crawlandgetdatausage" storageRESTMethodMakeVol = "/makevol" storageRESTMethodMakeVolBulk = "/makevolbulk" diff --git a/cmd/storage-rest-server.go b/cmd/storage-rest-server.go index 62abd6287..d170839a2 100644 --- a/cmd/storage-rest-server.go +++ b/cmd/storage-rest-server.go @@ -132,6 +132,22 @@ func (s *storageRESTServer) DiskInfoHandler(w http.ResponseWriter, r *http.Reque gob.NewEncoder(w).Encode(info) } +// GetDiskIDHandler - returns disk id. +func (s *storageRESTServer) GetDiskIDHandler(w http.ResponseWriter, r *http.Request) { + if err := storageServerRequestValidate(r); err != nil { + s.writeErrorResponse(w, err) + return + } + + info, err := s.storage.GetDiskID() + if err != nil { + s.writeErrorResponse(w, err) + return + } + defer w.(http.Flusher).Flush() + gob.NewEncoder(w).Encode(info) +} + func (s *storageRESTServer) CrawlAndGetDataUsageHandler(w http.ResponseWriter, r *http.Request) { if !s.IsValid(w, r) { return @@ -784,6 +800,7 @@ func registerStorageRESTHandlers(router *mux.Router, endpointZones EndpointZones subrouter := router.PathPrefix(path.Join(storageRESTPrefix, endpoint.Path)).Subrouter() subrouter.Methods(http.MethodPost).Path(storageRESTVersionPrefix + storageRESTMethodDiskInfo).HandlerFunc(httpTraceHdrs(server.DiskInfoHandler)) + subrouter.Methods(http.MethodPost).Path(storageRESTVersionPrefix + storageRESTMethodGetDiskID).HandlerFunc(httpTraceHdrs(server.GetDiskIDHandler)) subrouter.Methods(http.MethodPost).Path(storageRESTVersionPrefix + storageRESTMethodCrawlAndGetDataUsage).HandlerFunc(httpTraceHdrs(server.CrawlAndGetDataUsageHandler)) subrouter.Methods(http.MethodPost).Path(storageRESTVersionPrefix + storageRESTMethodMakeVol).HandlerFunc(httpTraceHdrs(server.MakeVolHandler)).Queries(restQueries(storageRESTVolume)...) subrouter.Methods(http.MethodPost).Path(storageRESTVersionPrefix + storageRESTMethodMakeVolBulk).HandlerFunc(httpTraceHdrs(server.MakeVolBulkHandler)).Queries(restQueries(storageRESTVolumes)...)