Skip to content

Commit

Permalink
[ozone] Fix case when volume name start with service_id in path
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshg999 committed Nov 27, 2024
1 parent a8a8585 commit 7140c73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
29 changes: 22 additions & 7 deletions desktop/core/src/desktop/lib/fs/ozone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from hadoop.fs import normpath as fs_normpath


OFS_ROOT = 'ofs://'
OFS_PATH_RE = re.compile('^/*[oO][fF][sS]?://([^/]+)(/(.*?([^/]+)?/?))?$')

Expand All @@ -40,7 +39,7 @@ def normpath(path):
if is_root(path):
normalized = path
else:
normalized = '%s%s' % (OFS_ROOT, fs_normpath(path[len(OFS_ROOT):]))
normalized = '%s%s' % (OFS_ROOT, fs_normpath(path[len(OFS_ROOT) :]))
else:
normalized = fs_normpath(path)
return normalized
Expand All @@ -66,17 +65,33 @@ def _prep(uri):
return '/%s/%s' % parse_uri(uri)[:2]
except ValueError:
return '/' if is_root(uri) else uri

joined = posixpath.join(*list(map(_prep, comp_list)))
return joined

def _serviceid_join(path, ofs_serviceid):
if path and (path == '/' or path.startswith('/' + ofs_serviceid)):

def _serviceid_join(path, ofs_serviceid, is_vol_serviceid_equal=False):
"""
Modify the provided path based on the service ID and a flag indicating
if the volume service ID is equal to the service ID.
Args:
path (str): The path to be joined with ofs_serviceid.
ofs_serviceid (str): The ofs_serviceid to be joined with the path.
is_vol_serviceid_equal (bool, optional): Flag to indicate if ofs_serviceid is equal to the volume service ID. Defaults to False.
Returns:
str: The joined path.
"""

if path and (path == '/' or path.startswith('/' + ofs_serviceid + '/') or path == '/' + ofs_serviceid) and not is_vol_serviceid_equal:
path = 'ofs:/' + path
elif path and not path.startswith(OFS_ROOT + ofs_serviceid + '/'):
path = OFS_ROOT + ofs_serviceid + '/' + path.lstrip('/')

return path


def _append_separator(path):
if path and not path.endswith('/'):
path += '/'
Expand All @@ -87,8 +102,8 @@ def parse_uri(uri):
"""
Returns tuple (service_id, key_name, key_basename).
Raises ValueError if invalid OFS URI is passed.
ofs://ozone1/volume1/bucket1/key1/key2 ->
ofs://ozone1/volume1/bucket1/key1/key2 ->
group1 -> ozone1
group2 -> /volume1/bucket1/key1/key2
group3 -> volume1/bucket1/key1/key2
Expand Down
11 changes: 6 additions & 5 deletions desktop/core/src/desktop/lib/fs/ozone/ofsstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
# limitations under the License.

from builtins import oct
import math
import stat

from django.utils.encoding import smart_str

from hadoop.fs.hadoopfs import decode_fs_path
from desktop.lib.fs.ozone import _serviceid_join, join as ofs_join
from hadoop.fs.webhdfs_types import WebHdfsStat

from desktop.lib.fs.ozone import _serviceid_join, join as ofs_join

class OzoneFSStat(WebHdfsStat):
"""
Expand All @@ -35,7 +32,11 @@ class OzoneFSStat(WebHdfsStat):

def __init__(self, file_status, parent_path, ofs_serviceid=''):
super(OzoneFSStat, self).__init__(file_status, parent_path)
self.path = _serviceid_join(ofs_join(parent_path, self.name), ofs_serviceid)

# Check for edge case when volume name is equal to service_id,
# then forcefully append ofs://<service_id> in current path so that further directory level paths are consistent.
is_vol_serviceid_equal = parent_path.startswith(f'/{ofs_serviceid}')
self.path = _serviceid_join(ofs_join(parent_path, self.name), ofs_serviceid, is_vol_serviceid_equal)

def __unicode__(self):
return "[OzoneFSStat] %7s %8s %8s %12s %s%s" % (oct(self.mode), self.user, self.group, self.size, self.path, self.isDir and '/' or "")
Expand Down

0 comments on commit 7140c73

Please sign in to comment.