Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.

Comments

Implemented methods for checking if the NIL file exists and is valid and updating the NIL file.#66

Open
evanSpendlove wants to merge 9 commits intomasterfrom
evan/checkNil
Open

Implemented methods for checking if the NIL file exists and is valid and updating the NIL file.#66
evanSpendlove wants to merge 9 commits intomasterfrom
evan/checkNil

Conversation

@evanSpendlove
Copy link
Contributor

No description provided.

@evanSpendlove evanSpendlove added this to the MVP milestone Sep 16, 2020
@evanSpendlove evanSpendlove self-assigned this Sep 16, 2020
for i := firstID; i <= lastID; i++ {
var id string
if i <= 9 {
id = fmt.Sprintf("0%d", i)

Choose a reason for hiding this comment

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

replace this with fmt.Spintf("%02d", i)

return client
}

func TestDeleteRandomFile(t *testing.T) {

Choose a reason for hiding this comment

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

it'd be nice to test that we record some metrics (and maybe even that they're within some sane range). Fine to do this in a future PR though.


metricpb "github.com/google/cloudprober/metrics/proto"
monitorpb "github.com/googleinterns/step224-2020/config/proto"
m "github.com/googleinterns/step224-2020/hermes/probe/metrics"
Copy link

Choose a reason for hiding this comment

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

Don't rename for convenience.

// - name: pass the name for this probe instance.
// Returns:
// - hermesExtension: returns the HermesProbeDef extension.
func genTestConfig(name string) *monitorpb.HermesProbeDef {
Copy link

Choose a reason for hiding this comment

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

Nit: I would call this genHermesProbeDef.

"github.com/google/cloudprober/logger"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/googleinterns/step224-2020/hermes/probe"
"github.com/googleinterns/step224-2020/hermes/probe/mock"
Copy link

Choose a reason for hiding this comment

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

This package should be called fakegcs (as it's not a mock).

Name: "hermes",
TargetSystem: monitorpb.Target_GOOGLE_CLOUD_STORAGE,
TotalSpaceAllocatedMib: int64(100),
BucketName: bucketName,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you verify whether this test runs? It isn't clear were bucketName is being defined..

Comment on lines 78 to 84
const (
firstID = int32(1)
lastID = int32(50)
contents = "abc123"
hash = "6367c48dd193d56ea7b0baad25b19455e529f5ee"
bucketName = "test_bucket_5"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. I see it now. Always, place your constants towards the beginning of the file, right below your imports.

Copy link

Choose a reason for hiding this comment

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

FYI, this is a good rule for exported constants. For non-exported consts that are only used in a single function, it's better to declare them within the function.

Comment on lines 143 to 144
testProbeName := "testDelete1"
ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add more test scenarios? Such as: deleting a file that's not present, deleting multiple file, and deleting the NIL file.

probeErr = CheckNilFile(ctx, target, client, logger)
probeErr = CheckJournal(ctx, target, client, logger)
if probeErr.Status != m.Success {
t.Errorf("checkNilFile failed: %v", probeErr)
Copy link

Choose a reason for hiding this comment

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

Update the message ("checkNilFile").

@evanSpendlove evanSpendlove requested a review from leczb September 24, 2020 13:35
if err := RecordAPILatency(target.LatencyMetrics.APICallLatency, metrics.APICreateFile, func() *probe.ProbeError {
writer = client.Bucket(bucket).Object(file.name).NewWriter(ctx)
length, err := io.Copy(writer, file.reader())
if writerErr := writer.Close(); writerErr != nil {
Copy link

Choose a reason for hiding this comment

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

It's surprising that this is done before inspecting err.
I assume this is to ensure that writer.Close() is called in any case.

I would recommend using defer for this, even though this throws away the error. Rationale: it's unlikely that Close() returns an error. If something goes wrong, it's going to be Copy() that returns an error.

I would do

writer = client.Bucket(bucket).Object(file.name).NewWriter(ctx)
defer writer.Close()

var writer stiface.Writer
if err := RecordAPILatency(target.LatencyMetrics.APICallLatency, metrics.APICreateFile, func() *probe.ProbeError {
writer = client.Bucket(bucket).Object(file.name).NewWriter(ctx)
length, err := io.Copy(writer, file.reader())
Copy link

Choose a reason for hiding this comment

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

length is somewhat misleading. I would call this bytesWritten or just n (which is the customary name for "count" variables).

func findJournal(ctx context.Context, target *probe.Target, client stiface.Client, logger *logger.Logger) (string, *probe.ProbeError) {
bucket := target.Target.GetBucketName()
file := &journalFile{}
var fileName *string
Copy link

Choose a reason for hiding this comment

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

This doesn't have to be a pointer, just string.

return probe.NewProbeError(metrics.APICallFailed, fmt.Errorf("findJournal(): unable to list files: %w", err))
}
file.name = obj.Name
*fileName = obj.Name
Copy link

Choose a reason for hiding this comment

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

What if there are more than one results from the query?

}

fileID, err := strconv.Atoi(journal.Intent.Filename[len("Hermes_"):len("Hermes_ID")])
fileID, err := strconv.Atoi(journal.Intent.Filename[len(hermesPrefix):len(hermesIDPrefix)])
Copy link

Choose a reason for hiding this comment

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

What if the filename is corrupted and its length is shorter than hermesIDPrefix? Currently it would crash the process.
Add a unit test case to see if fail; fix the code; then run the test again to see it pass.

func (j *journalFile) checksumContents() error {
h := sha1.New()
if _, err := io.Copy(h, j.reader()); err != nil {
return fmt.Errorf("verifyNilFile(): unable to compute checksum of file contents: %w", err)
Copy link

Choose a reason for hiding this comment

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

Update annotation.

func (j *journalFile) unmarshalContents() (*pb.StateJournal, *probe.ProbeError) {
journal := &pb.StateJournal{}
if err := proto.Unmarshal(j.contents, journal); err != nil {
return nil, probe.NewProbeError(metrics.FileCorrupted, fmt.Errorf("verifyNilFile(): unable to unmarshal nil file proto: %w", err))
Copy link

Choose a reason for hiding this comment

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

Update the annotation.

// Author: Evan Spendlove, GitHub: evanSpendlove

// Package journal implements the probe operation for checking if the Journal file
// on the target storage system is present. It also verifies the contetns of
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
// on the target storage system is present. It also verifies the contetns of
// on the target storage system is present. It also verifies the contents of

return err
}

j := &journalFile{
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
j := &journalFile{
j := &journalFile {

bucket := target.Target.GetBucketName()
var fileName *string

err := RecordAPILatency(target.LatencyMetrics.APICallLatency, metrics.APIListFiles, func() *probe.ProbeError {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't seem do do anything with the err defined here. Can you add a check after this definition?

logger.Debug("UpdateJournal(): in-memory journal successfully marshalled, proceeding to upload to target storage system.")

var writer stiface.Writer
if err := RecordAPILatency(target.LatencyMetrics.APICallLatency, metrics.APICreateFile, func() *probe.ProbeError {
Copy link
Contributor

Choose a reason for hiding this comment

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

To make this look a bit cleaner, can you move the assignment of err outside the if statement? Something like:

err := RecordAPILatency(...)
if err != nil {...}


var reader stiface.Reader
err := RecordAPILatency(target.LatencyMetrics.APICallLatency, metrics.APIGetFile, func() *probe.ProbeError {
r, err := fileObject.NewReader(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not assign reader directly in here?

Comment on lines +78 to +85
const (
firstID = int32(1)
lastID = int32(50)
contents = "abc123"
hash = "6367c48dd193d56ea7b0baad25b19455e529f5ee"
bucketName = "test_bucket_5"
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Move constants towards the top of the file, below your imports.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants