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

corefile uploader: Updates per review comments offline #3915

Merged
merged 5 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions files/image_config/corefile_uploader/core_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@

HOURS_4 = (4 * 60 * 60)
PAUSE_ON_FAIL = (60 * 60)
WAIT_FILE_WRITE1 = (10 * 60)
WAIT_FILE_WRITE2= (5 * 60)
POLL_SLEEP = (60 * 60)
MAX_RETRIES = 5
UPLOAD_PREFIX = "UPLOADED_"

log_level = syslog.LOG_DEBUG

Expand Down Expand Up @@ -116,7 +120,7 @@ def run(self):
self.observer.start()
try:
while True:
time.sleep(5)
time.sleep(POLL_SLEEP)
except:
self.observer.stop()
log_err("Error in watcher")
Expand Down Expand Up @@ -179,29 +183,33 @@ def on_any_event(event):
elif event.event_type == 'created':
# Take any action here when a file is first created.
log_debug("Received create event - " + event.src_path)
Handler.wait_for_file_write_complete(event.src_path)
Handler.handle_file(event.src_path)


@staticmethod
def wait_for_file_write_complete(path):
ct_size = -1
mtime = 0

while ct_size != os.path.getsize(path):
ct_size = os.path.getsize(path)
time.sleep(2)
# Sleep for ample time enough for file dump to complete.
time.sleep(WAIT_FILE_WRITE1)

time.sleep(2)
if ct_size != os.path.getsize(path):
# Give another chance & poll until mtime stabilizes
while mtime != os.stat(path).st_mtime:
mtime = os.stat(path).st_mtime
time.sleep(10)

# A safety pause for double confirmation
time.sleep(WAIT_FILE_WRITE2)
if mtime != os.stat(path).st_mtime:
raise Exception("Dump file creation is too slow: " + path)
# Give up as something is terribly wrong with this file.

log_debug("File write complete - " + path)


@staticmethod
def handle_file(path):

Handler.wait_for_file_write_complete(path)

lpath = "/".join(cwd)
make_new_dir(lpath)
os.chdir(lpath)
Expand All @@ -221,18 +229,18 @@ def handle_file(path):
tar.close()
log_debug("Tar file for upload created: " + tarf_name)

Handler.upload_file(tarf_name, tarf_name)
Handler.upload_file(tarf_name, tarf_name, path)

log_debug("File uploaded - " + path)
os.chdir(INIT_CWD)

@staticmethod
def upload_file(fname, fpath):
def upload_file(fname, fpath, coref):
daemonname = fname.split(".")[0]
i = 0
fail_msg = ""

while i <= MAX_RETRIES:
while True:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do retry in the big loop. if it fails, retry in the big loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess no. The bigger loop is either scan/wait-for-core. In either case, the next one would suffer the same fate. So I rather spew log & retry, until either I succeed or service is restarted, by someone alerted by these log messages.

try:
svc = FileService(account_name=acctname, account_key=acctkey)

Expand All @@ -246,22 +254,23 @@ def upload_file(fname, fpath):

svc.create_file_from_path(sharename, "/".join(l), fname, fpath)
log_debug("Remote file created: name{} path{}".format(fname, fpath))
newcoref = os.path.dirname(coref) + "/" + UPLOAD_PREFIX + os.path.basename(coref)
os.rename(coref, newcoref)
break

except Exception as e:
log_err("core uploader failed: Failed during upload (" + str(e) +")")
fail_msg = str(e)
except Exception as ex:
log_err("core uploader failed: Failed during upload (" + coref + ") err: ("+ str(ex) +") retry:" + str(i))
if not os.path.exists(fpath):
break
i += 1
if i >= MAX_RETRIES:
raise Exception("Failed while uploading. msg(" + fail_msg + ") after " + str(i) + " retries")
time.sleep(PAUSE_ON_FAIL)


@staticmethod
def scan():
for e in os.listdir(CORE_FILE_PATH):
fl = CORE_FILE_PATH + e
if os.path.isfile(fl):
if os.path.isfile(fl) and not e.startswith(UPLOAD_PREFIX):
Handler.handle_file(fl)


Expand Down
6 changes: 4 additions & 2 deletions files/image_config/corefile_uploader/core_uploader.service
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[Unit]
Description=Host core file uploader daemon
Requires=updategraph.service
After=updategraph.service
Requires=syslog.service
After=syslog.service

[Service]
Type=simple
ExecStart=/usr/bin/core_uploader.py
StandardOutput=null
Restart=on-failure
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need back off here? if core_uploader is constantly restarting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. The only failure that crashes the service, is some fatal things like, sonic_version.yaml does not have expected attributes or some fatal system related failure (which should crash many more ...), and running out of disk space.

The one thing, I would need to take care of "running out of disk space". This I can take care of inside the script.


[Install]
WantedBy=multi-user.target
55 changes: 0 additions & 55 deletions files/image_config/corefile_uploader/update_json.py

This file was deleted.