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

fix: implement batches on top of mysql #85

Merged
merged 1 commit into from
Nov 15, 2018
Merged

fix: implement batches on top of mysql #85

merged 1 commit into from
Nov 15, 2018

Conversation

pjenvey
Copy link
Member

@pjenvey pjenvey commented Nov 13, 2018

like go-syncstorage, we just store the bsos' json in a text column,
concatenating batches together w/ newlines

also:

  • fix load_collection_names handling of cache hits
  • get_bsos limit handling after b066fba
  • kill the old schema files I forgot to remove

Closes #51, #18

Copy link
Contributor

@rfk rfk left a comment

Choose a reason for hiding this comment

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

I think this needs a few more guards against bad behaviours (appending to a non-existing batch, commiting a batch twice, etc) but otherwise it's looking good!

// when we convert one to a bigint in milliseconds, the final digit is
// always zero. But we want to use the lower digits of the batchid for
// sharding writes via (batchid % num_tables), and leaving it as zero would
// skew the sharding distribution.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you in fact do this sharding across multiple "batches" tables?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, so I removed this comment and the associated code

.filter(batches::collection_id.eq(&collection_id))
.filter(batches::id.eq(&params.id))
.get_result::<i32>(&db.conn)
.optional()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like it should also check whether the batch has expired.

}

pub fn append(db: &MysqlDb, params: params::AppendToBatch) -> Result<()> {
// XXX: this is possibly an upsert
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I agree about an upsert here, you should always know whether you have a new batch or an existing batch.

.filter(batches::collection_id.eq(&collection_id))
.filter(batches::id.eq(&params.id))
.set(batches::bsos.eq(batches::bsos.concat(&bsos)))
.execute(&db.conn)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

If the batch has already expired, should this update it or return an error? Also if the specified batch id does not exist, this should likely return an error rather updating zero rows.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, I've added expiry checks here and other places

user_id: params.user_id,
collection: params.collection,
bsos,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we should atomically delete the batch after commiting it, to ensure that it can't be committed twice.


let batch = get(&db, gb(uid, coll, id))?.unwrap();
assert_ne!(batch.bsos, "".to_owned());
// XXX: expiry updated?
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have strong opinions about this, but my first instance is that expiry should not be updated. Otherwise there's no finite bound on the ultimate lifetime of a batch, a misbehaving client could slow-drip add items to a batch and keep it alive indefinitely.


pub fn commit(db: &MysqlDb, params: params::CommitBatch) -> Result<results::CommitBatch> {
let bsos = batch_string_to_bsos(&params.batch.bsos)?;
db.post_bsos_sync(params::PostBsos {
Copy link
Contributor

Choose a reason for hiding this comment

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

Defering the work here to an existing post_bsos_sync method should give you a lot of edge-cases for free, such as proper handling of partial updates and ttl bumping. I like it! Handling those was a source of some complexity in the python code.

like go-syncstorage, we just store the bsos' json in a text column,
concatenating batches together w/ newlines

also:

- fix load_collection_names handling of cache hits
- get_bsos limit handling after b066fba
- kill the old schema files I forgot to remove

Closes #51, #18
Copy link
Contributor

@rfk rfk left a comment

Choose a reason for hiding this comment

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

Looks good!

pub fn timestamp(&self) -> SyncTimestamp {
self.session.borrow().timestamp
}

#[cfg(test)]
pub(super) fn set_timestamp(&self, timestamp: i64) {
self.session.borrow_mut().timestamp = SyncTimestamp::from_i64(timestamp).unwrap();
pub(super) fn with_delta<T, E, F>(&self, delta: i64, f: F) -> std::result::Result<T, E>
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a neat little abstraction!

@pjenvey pjenvey merged commit 4dbb916 into master Nov 15, 2018
@pjenvey pjenvey deleted the feat/51 branch November 15, 2018 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement batch db calls
2 participants