BatchMutations bug when updating same rows with CompositeKey multiple times #105

Closed
Aaronontheweb opened this Issue Jan 14, 2013 · 2 comments

Comments

Projects
None yet
2 participants
@Aaronontheweb
Contributor

Aaronontheweb commented Jan 14, 2013

I found an edge-case bug while I working with FluentCassandra earlier today - the problem is that if you try to execute multiple mutations against a row defined using the same CompositeType key (but with different column values associated with it), only one of the mutations is ever executed.

Reproducible unit test

Ran with the latest build of FluentCassandra - this test currently fails.

[Test(Description = "Magical tests")]
public void Should_Batch_Mutate_Multiple_Columns_with_LongType_ColumnNames_For_Composite_RowKey()
{
    var projectId = Guid.NewGuid().ToString();

    var logAppsCf = _db.GetColumnFamily("Logs_Index");

    var rowKey = CompositeType.Create<UTF8Type, UTF8Type>(projectId, "Logs");

    var sessionRow = logAppsCf.CreateRecord(rowKey);
    sessionRow.Columns.Add(new FluentColumn(new CassandraColumnSchema(){ NameType = CassandraType.LongType, ValueType = CassandraType.UTF8Type}) { ColumnName = DateTime.Today.Ticks, ColumnValue = "1" });

    var sessionRow2 = logAppsCf.CreateRecord(rowKey);
    sessionRow2.Columns.Add(new FluentColumn(new CassandraColumnSchema() { NameType = CassandraType.LongType, ValueType = CassandraType.UTF8Type }) { ColumnName = DateTime.UtcNow.Ticks, ColumnValue = "2" });

    _db.Attach(sessionRow);
    _db.Attach(sessionRow2);
    _db.SaveChanges();

    var columnCount = logAppsCf.Get(rowKey).CountColumns();
    Assert.AreEqual(2, columnCount);
}

However, if the test is changed to a column family that uses a simple type as its row key, like a LongType or UTF8Type, the test will pass.

Root Cause

It seems like this is an issue with the batch mutation tracker in FluentCassandra, and it appears to be isolated to CompositeType rowkeys given that I can't reproduce this with simpler data types.

@Aaronontheweb

This comment has been minimized.

Show comment Hide comment
@Aaronontheweb

Aaronontheweb Jan 14, 2013

Contributor

Found the issue looks like it's this:

https://github.com/managedfusion/fluentcassandra/blob/master/src/Types/CompositeType.cs#L138

When the CompositeType has its GetHashCode method called during BatchMutate's Mutations.GroupBy command on line 20, the hash code returned is different for each composite type instance despite having the same values.

This behavior needs to be changed so the hashcode is reflective of the sum of the parts, rather than per-instance. I'll experiment with changing this myself and would love feedback if this isn't the right approach or root cause of the problem.

Contributor

Aaronontheweb commented Jan 14, 2013

Found the issue looks like it's this:

https://github.com/managedfusion/fluentcassandra/blob/master/src/Types/CompositeType.cs#L138

When the CompositeType has its GetHashCode method called during BatchMutate's Mutations.GroupBy command on line 20, the hash code returned is different for each composite type instance despite having the same values.

This behavior needs to be changed so the hashcode is reflective of the sum of the parts, rather than per-instance. I'll experiment with changing this myself and would love feedback if this isn't the right approach or root cause of the problem.

nberardi added a commit that referenced this issue Jan 14, 2013

@nberardi

This comment has been minimized.

Show comment Hide comment
@nberardi

nberardi Jan 14, 2013

Contributor

Thanks this is a good addition. Do you think we should do it for the Dynamic Composite Type?

Contributor

nberardi commented Jan 14, 2013

Thanks this is a good addition. Do you think we should do it for the Dynamic Composite Type?

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