-
Notifications
You must be signed in to change notification settings - Fork 248
fix: delete cni statefile when unable to be parsed #3551
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package network | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
"sync" | ||
"time" | ||
|
@@ -192,13 +193,25 @@ func (nm *networkManager) restore(isRehydrationRequired bool) error { | |
// Read any persisted state. | ||
err := nm.store.Read(storeKey, nm) | ||
if err != nil { | ||
var syntaxErr *json.SyntaxError | ||
if err == store.ErrKeyNotFound { | ||
logger.Info("network store key not found") | ||
// Considered successful. | ||
return nil | ||
} else if err == store.ErrStoreEmpty { | ||
logger.Info("network store empty") | ||
return nil | ||
} else if errors.As(err, &syntaxErr) { | ||
// if null chars detected or failed to parse, state is unrecoverable; delete it | ||
logger.Error("Failed to parse corrupted state, deleting", zap.Error(err)) | ||
contents, readErr := nm.store.Dump() | ||
if readErr != nil { | ||
logger.Error("Could not read corrupted state", zap.Error(readErr)) | ||
} else { | ||
logger.Info("Logging state", zap.String("stateFile", contents)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: logging corrupted state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We log the corrupted state for debugging as per the suggestion by Michael above. Is there some drawback of doing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for debugging as per the above reviewer's comments-- what other issues could arise from logging the corrupted state? It should only be dumped once as the statefile will be deleted afterwards. |
||
} | ||
nm.store.Remove() | ||
QxBytes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errors.Wrap(err, "failed to parse corrupted state") | ||
} else { | ||
logger.Error("Failed to restore state", zap.Error(err)) | ||
return err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,14 @@ func TestKeyValuePairsAreWrittenAndReadCorrectly(t *testing.T) { | |
t.Fatalf("Failed to create KeyValueStore %v\n", err) | ||
} | ||
|
||
defer os.Remove(testFileName) | ||
|
||
// Dump empty store. | ||
_, err = kvs.Dump() | ||
if err == nil { | ||
t.Fatal("Expected store to not exist") | ||
Comment on lines
+135
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We expect the store to exist right ? why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The store struct does exist, but the actual json representation on disk doesn't exist yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the store struct is created, but it's not yet created on disk |
||
} | ||
|
||
// Write a key value pair. | ||
err = kvs.Write(testKey1, &writtenValue) | ||
if err != nil { | ||
|
@@ -153,8 +161,17 @@ func TestKeyValuePairsAreWrittenAndReadCorrectly(t *testing.T) { | |
testKey1, readValue, testKey1, writtenValue) | ||
} | ||
|
||
// Cleanup. | ||
os.Remove(testFileName) | ||
// Dump populated store. | ||
val, err := kvs.Dump() | ||
if err != nil { | ||
t.Fatalf("Failed to dump store %v", err) | ||
} | ||
val = strings.ReplaceAll(val, " ", "") | ||
val = strings.ReplaceAll(val, "\t", "") | ||
val = strings.ReplaceAll(val, "\n", "") | ||
if val != `{"key1":{"Field1":"test","Field2":42},"key2":{"Field1":"any","Field2":14}}` { | ||
t.Errorf("Dumped data not expected: %v", val) | ||
} | ||
} | ||
|
||
// test case for testing newjsonfilestore idempotent | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to log the contents invalid and all to help assist with troubleshooting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking into this and the store's file name and contents are not accessible-- currently leaning towards just adding a method to the interface to dump the contents of the store in string format.