Search in sources :

Example 31 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class BatchTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    TableMetadata cfm = Keyspace.open(KEYSPACE).getColumnFamilyStore(CF_STANDARD).metadata();
    long now = FBUtilities.timestampMicros();
    int version = MessagingService.current_version;
    UUID uuid = UUIDGen.getTimeUUID();
    List<Mutation> mutations = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), bytes(i)).clustering("name" + i).add("val", "val" + i).build());
    }
    Batch batch1 = Batch.createLocal(uuid, now, mutations);
    assertEquals(uuid, batch1.id);
    assertEquals(now, batch1.creationTime);
    assertEquals(mutations, batch1.decodedMutations);
    DataOutputBuffer out = new DataOutputBuffer();
    Batch.serializer.serialize(batch1, out, version);
    assertEquals(out.getLength(), Batch.serializer.serializedSize(batch1, version));
    DataInputPlus dis = new DataInputBuffer(out.getData());
    Batch batch2 = Batch.serializer.deserialize(dis, version);
    assertEquals(batch1.id, batch2.id);
    assertEquals(batch1.creationTime, batch2.creationTime);
    assertEquals(batch1.decodedMutations.size(), batch2.encodedMutations.size());
    Iterator<Mutation> it1 = batch1.decodedMutations.iterator();
    Iterator<ByteBuffer> it2 = batch2.encodedMutations.iterator();
    while (it1.hasNext()) {
        try (DataInputBuffer in = new DataInputBuffer(it2.next().array())) {
            assertEquals(it1.next().toString(), Mutation.serializer.deserialize(in, version).toString());
        }
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 32 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class BatchlogManagerTest method testAddBatch.

@Test
public void testAddBatch() throws IOException {
    long initialAllBatches = BatchlogManager.instance.countAllBatches();
    TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
    long timestamp = (System.currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout() * 2) * 1000;
    UUID uuid = UUIDGen.getTimeUUID();
    // Add a batch with 10 mutations
    List<Mutation> mutations = new ArrayList<>(10);
    for (int j = 0; j < 10; j++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
    }
    BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
    Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
    String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
    UntypedResultSet result = executeInternal(query);
    assertNotNull(result);
    assertEquals(1L, result.one().getLong("count"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 33 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class BatchlogManagerTest method testRemoveBatch.

@Test
public void testRemoveBatch() {
    long initialAllBatches = BatchlogManager.instance.countAllBatches();
    TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
    long timestamp = (System.currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout() * 2) * 1000;
    UUID uuid = UUIDGen.getTimeUUID();
    // Add a batch with 10 mutations
    List<Mutation> mutations = new ArrayList<>(10);
    for (int j = 0; j < 10; j++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
    }
    // Store the batch
    BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
    Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
    // Remove the batch
    BatchlogManager.remove(uuid);
    assertEquals(initialAllBatches, BatchlogManager.instance.countAllBatches());
    String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
    UntypedResultSet result = executeInternal(query);
    assertNotNull(result);
    assertEquals(0L, result.one().getLong("count"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 34 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class BatchlogManagerTest method testReplayWithNoPeers.

// CASSANRDA-9223
@Test
public void testReplayWithNoPeers() throws Exception {
    StorageService.instance.getTokenMetadata().removeEndpoint(InetAddress.getByName("127.0.0.1"));
    long initialAllBatches = BatchlogManager.instance.countAllBatches();
    long initialReplayedBatches = BatchlogManager.instance.getTotalBatchesReplayed();
    TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD1).metadata();
    long timestamp = (System.currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout() * 2) * 1000;
    UUID uuid = UUIDGen.getTimeUUID();
    // Add a batch with 10 mutations
    List<Mutation> mutations = new ArrayList<>(10);
    for (int j = 0; j < 10; j++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
    }
    BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
    assertEquals(1, BatchlogManager.instance.countAllBatches() - initialAllBatches);
    // Flush the batchlog to disk (see CASSANDRA-6822).
    Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(SystemKeyspace.BATCHES).forceBlockingFlush();
    assertEquals(1, BatchlogManager.instance.countAllBatches() - initialAllBatches);
    assertEquals(0, BatchlogManager.instance.getTotalBatchesReplayed() - initialReplayedBatches);
    // Force batchlog replay and wait for it to complete.
    BatchlogManager.instance.startBatchlogReplay().get();
    // Replay should be cancelled as there are no peers in the ring.
    assertEquals(1, BatchlogManager.instance.countAllBatches() - initialAllBatches);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Aggregations

RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)34 Test (org.junit.Test)22 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)19 Keyspace (org.apache.cassandra.db.Keyspace)17 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)14 ByteBuffer (java.nio.ByteBuffer)12 TableMetadata (org.apache.cassandra.schema.TableMetadata)12 DecoratedKey (org.apache.cassandra.db.DecoratedKey)10 Mutation (org.apache.cassandra.db.Mutation)9 ArrayList (java.util.ArrayList)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 File (java.io.File)3 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)3 UUID (java.util.UUID)2 CompactionController (org.apache.cassandra.db.compaction.CompactionController)2 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)2 Row (org.apache.cassandra.db.rows.Row)2 WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)2 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)2 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)2