Search in sources :

Example 1 with Batch

use of org.apache.cassandra.batchlog.Batch in project cassandra by apache.

the class StorageProxy method syncWriteToBatchlog.

private static void syncWriteToBatchlog(Collection<Mutation> mutations, Collection<InetAddress> endpoints, UUID uuid, long queryStartNanoTime) throws WriteTimeoutException, WriteFailureException {
    WriteResponseHandler<?> handler = new WriteResponseHandler<>(endpoints, Collections.<InetAddress>emptyList(), endpoints.size() == 1 ? ConsistencyLevel.ONE : ConsistencyLevel.TWO, Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME), null, WriteType.BATCH_LOG, queryStartNanoTime);
    Batch batch = Batch.createLocal(uuid, FBUtilities.timestampMicros(), mutations);
    MessageOut<Batch> message = new MessageOut<>(MessagingService.Verb.BATCH_STORE, batch, Batch.serializer);
    for (InetAddress target : endpoints) {
        logger.trace("Sending batchlog store request {} to {} for {} mutations", batch.id, target, batch.size());
        if (canDoLocalRequest(target))
            performLocally(Stage.MUTATION, Optional.empty(), () -> BatchlogManager.store(batch), handler);
        else
            MessagingService.instance().sendRR(message, target, handler);
    }
    handler.get();
}
Also used : Batch(org.apache.cassandra.batchlog.Batch) InetAddress(java.net.InetAddress)

Example 2 with Batch

use of org.apache.cassandra.batchlog.Batch in project cassandra by apache.

the class Instance method serializeMessage.

public static IMessage serializeMessage(InetAddressAndPort from, InetAddressAndPort to, Message<?> messageOut) {
    int fromVersion = MessagingService.instance().versions.get(from);
    int toVersion = MessagingService.instance().versions.get(to);
    // See CASSANDRA-16157 for details.
    if (fromVersion < MessagingService.current_version && ((messageOut.verb().serializer() == ((IVersionedAsymmetricSerializer) NoPayload.serializer) || messageOut.payload == null))) {
        return new MessageImpl(messageOut.verb().id, ByteArrayUtil.EMPTY_BYTE_ARRAY, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
    }
    try (DataOutputBuffer out = new DataOutputBuffer(1024)) {
        // we use a special procedure here that "re-serializes" a "remote" batch to build the message.
        if (fromVersion >= MessagingService.VERSION_40 && messageOut.verb().id == BATCH_STORE_REQ.id) {
            Object maybeBatch = messageOut.payload;
            if (maybeBatch instanceof Batch) {
                Batch batch = (Batch) maybeBatch;
                // If the batch is local, it can be serialized along the normal path.
                if (!batch.isLocal()) {
                    reserialize(batch, out, toVersion);
                    byte[] bytes = out.toByteArray();
                    return new MessageImpl(messageOut.verb().id, bytes, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
                }
            }
        }
        Message.serializer.serialize(messageOut, out, toVersion);
        byte[] bytes = out.toByteArray();
        if (messageOut.serializedSize(toVersion) != bytes.length)
            throw new AssertionError(String.format("Message serializedSize(%s) does not match what was written with serialize(out, %s) for verb %s and serializer %s; " + "expected %s, actual %s", toVersion, toVersion, messageOut.verb(), Message.serializer.getClass(), messageOut.serializedSize(toVersion), bytes.length));
        return new MessageImpl(messageOut.verb().id, bytes, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Batch(org.apache.cassandra.batchlog.Batch) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IVersionedAsymmetricSerializer(org.apache.cassandra.io.IVersionedAsymmetricSerializer) IOException(java.io.IOException)

Example 3 with Batch

use of org.apache.cassandra.batchlog.Batch in project cassandra by apache.

the class VerifyTest method testVerifyLocalPartitioner.

@Test
public void testVerifyLocalPartitioner() throws UnknownHostException {
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    byte[] tk1 = new byte[1], tk2 = new byte[1];
    tk1[0] = 2;
    tk2[0] = 1;
    tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk1), InetAddressAndPort.getByName("127.0.0.1"));
    tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk2), InetAddressAndPort.getByName("127.0.0.2"));
    // write some bogus to a localpartitioner table
    Batch bogus = Batch.createLocal(UUID.randomUUID(), 0, Collections.emptyList());
    BatchlogManager.store(bogus);
    ColumnFamilyStore cfs = Keyspace.open("system").getColumnFamilyStore("batches");
    cfs.forceBlockingFlush();
    for (SSTableReader sstable : cfs.getLiveSSTables()) {
        try (Verifier verifier = new Verifier(cfs, sstable, false, Verifier.options().checkOwnsTokens(true).build())) {
            verifier.verify();
        }
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Batch(org.apache.cassandra.batchlog.Batch) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Verifier(org.apache.cassandra.db.compaction.Verifier) ByteOrderedPartitioner(org.apache.cassandra.dht.ByteOrderedPartitioner) Test(org.junit.Test)

Example 4 with Batch

use of org.apache.cassandra.batchlog.Batch in project cassandra by apache.

the class StorageProxy method syncWriteToBatchlog.

private static void syncWriteToBatchlog(Collection<Mutation> mutations, ReplicaPlan.ForTokenWrite replicaPlan, UUID uuid, long queryStartNanoTime) throws WriteTimeoutException, WriteFailureException {
    WriteResponseHandler<?> handler = new WriteResponseHandler(replicaPlan, WriteType.BATCH_LOG, queryStartNanoTime);
    Batch batch = Batch.createLocal(uuid, FBUtilities.timestampMicros(), mutations);
    Message<Batch> message = Message.out(BATCH_STORE_REQ, batch);
    for (Replica replica : replicaPlan.liveAndDown()) {
        logger.trace("Sending batchlog store request {} to {} for {} mutations", batch.id, replica, batch.size());
        if (replica.isSelf())
            performLocally(Stage.MUTATION, replica, () -> BatchlogManager.store(batch), handler);
        else
            MessagingService.instance().sendWithCallback(message, replica.endpoint(), handler);
    }
    handler.get();
}
Also used : Batch(org.apache.cassandra.batchlog.Batch) Replica(org.apache.cassandra.locator.Replica)

Aggregations

Batch (org.apache.cassandra.batchlog.Batch)4 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 Verifier (org.apache.cassandra.db.compaction.Verifier)1 ByteOrderedPartitioner (org.apache.cassandra.dht.ByteOrderedPartitioner)1 IVersionedAsymmetricSerializer (org.apache.cassandra.io.IVersionedAsymmetricSerializer)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)1 Replica (org.apache.cassandra.locator.Replica)1 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)1 Test (org.junit.Test)1