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

AMBARI-24670. Directory creation sometimes fails with parallel_execution=1 #2379

Merged
merged 2 commits into from
Sep 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestLinkResource(TestCase):

@patch.object(os.path, "realpath")
@patch("resource_management.core.sudo.path_lexists")
@patch("resource_management.core.sudo.path_lexists")
@patch("resource_management.core.sudo.path_islink")
@patch("resource_management.core.sudo.unlink")
@patch("resource_management.core.sudo.symlink")
def test_action_create_relink(self, symlink_mock, unlink_mock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def action_create(self):
if self.resource.follow:
# Follow symlink until the tail.
followed_links = set()
while sudo.path_lexists(path):
while sudo.path_islink(path):
if path in followed_links:
raise Fail("Applying %s failed, looped symbolic links found while resolving %s" % (self.resource, path))
followed_links.add(path)
Expand All @@ -188,8 +188,15 @@ def action_create(self):
if not sudo.path_isdir(dirname):
raise Fail("Applying %s failed, parent directory %s doesn't exist" % (self.resource, dirname))

sudo.makedir(path, self.resource.mode or 0755)

try:
sudo.makedir(path, self.resource.mode or 0755)
except Exception as ex:
# race condition (somebody created the file before us)
if "File exists" in str(ex):
sudo.makedirs(path, self.resource.mode or 0755)
else:
raise

if not sudo.path_isdir(path):
raise Fail("Applying %s failed, file %s already exists" % (self.resource, path))

Expand All @@ -216,7 +223,7 @@ def action_create(self):
oldpath = os.path.realpath(path)
if oldpath == self.resource.to:
return
if not sudo.path_lexists(path):
if not sudo.path_islink(path):
raise Fail(
"%s trying to create a symlink with the same name as an existing file or directory" % self.resource)
Logger.info("%s replacing old symlink to %s" % (self.resource, oldpath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_content(self):
basedir = self.env.config.basedir
path = os.path.join(basedir, "files", self.name)

if not sudo.path_isfile(path) and not sudo.path_lexists(path):
if not sudo.path_isfile(path):
raise Fail("{0} Source file {1} is not found".format(repr(self), path))

return self.read_file(path)
Expand Down
11 changes: 9 additions & 2 deletions ambari-common/src/main/python/resource_management/core/sudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ def path_exists(path):

def path_isdir(path):
return os.path.isdir(path)


def path_islink(path):
return os.path.islink(path)

def path_lexists(path):
return os.path.lexists(path)

Expand Down Expand Up @@ -267,10 +270,14 @@ def path_exists(path):
# os.path.isdir
def path_isdir(path):
return (shell.call(["test", "-d", path], sudo=True)[0] == 0)

# os.path.islink
def path_islink(path):
return (shell.call(["test", "-L", path], sudo=True)[0] == 0)

# os.path.lexists
def path_lexists(path):
return (shell.call(["test", "-L", path], sudo=True)[0] == 0)
return (shell.call(["test", "-e", path], sudo=True)[0] == 0)

# os.readlink
def readlink(path):
Expand Down