Search in sources :

Example 31 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class UFTest method testFunctionExecutionExceptionNet.

@Test
public void testFunctionExecutionExceptionNet() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, dval double)");
    execute("INSERT INTO %s (key, dval) VALUES (?, ?)", 1, 1d);
    String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE JAVA\n" + "AS 'throw new RuntimeException();'");
    for (ProtocolVersion version : PROTOCOL_VERSIONS) {
        try {
            assertRowsNet(version, executeNet(version, "SELECT " + fName + "(dval) FROM %s WHERE key = 1"));
            Assert.fail();
        } catch (com.datastax.driver.core.exceptions.FunctionExecutionException fee) {
            // Java driver neither throws FunctionExecutionException nor does it set the exception code correctly
            Assert.assertTrue(version.isGreaterOrEqualTo(ProtocolVersion.V4));
        } catch (InvalidQueryException e) {
            Assert.assertTrue(version.isSmallerThan(ProtocolVersion.V4));
        }
    }
}
Also used : ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Test(org.junit.Test)

Example 32 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class UFTypesTest method testComplexNullValues.

@Test
public void testComplexNullValues() throws Throwable {
    String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
    createTable("CREATE TABLE %s (key int primary key, lst list<double>, st set<text>, mp map<int, boolean>," + "tup frozen<tuple<double, text, int, boolean>>, udt frozen<" + type + ">)");
    String fList = createFunction(KEYSPACE, "list<double>", "CREATE FUNCTION %s( coll list<double> ) " + "CALLED ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fSet = createFunction(KEYSPACE, "set<text>", "CREATE FUNCTION %s( coll set<text> ) " + "CALLED ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fMap = createFunction(KEYSPACE, "map<int, boolean>", "CREATE FUNCTION %s( coll map<int, boolean> ) " + "CALLED ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fTup = createFunction(KEYSPACE, "tuple<double, text, int, boolean>", "CREATE FUNCTION %s( val tuple<double, text, int, boolean> ) " + "CALLED ON NULL INPUT " + "RETURNS tuple<double, text, int, boolean> " + "LANGUAGE java\n" + "AS $$return val;$$;");
    String fUdt = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( val " + type + " ) " + "CALLED ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java\n" + "AS $$return val;$$;");
    List<Double> list = Arrays.asList(1d, 2d, 3d);
    Set<String> set = new TreeSet<>(Arrays.asList("one", "three", "two"));
    Map<Integer, Boolean> map = new TreeMap<>();
    map.put(1, true);
    map.put(2, false);
    map.put(3, true);
    Object t = tuple(1d, "one", 42, false);
    execute("INSERT INTO %s (key, lst, st, mp, tup, udt) VALUES (1, ?, ?, ?, ?, {txt: 'one', i:1})", list, set, map, t);
    execute("INSERT INTO %s (key, lst, st, mp, tup, udt) VALUES (2, ?, ?, ?, ?, null)", null, null, null, null);
    execute("SELECT " + fList + "(lst), " + fSet + "(st), " + fMap + "(mp), " + fTup + "(tup), " + fUdt + "(udt) FROM %s WHERE key = 1");
    UntypedResultSet.Row row = execute("SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 1").one();
    Assert.assertNotNull(row.getBytes("l"));
    Assert.assertNotNull(row.getBytes("s"));
    Assert.assertNotNull(row.getBytes("m"));
    Assert.assertNotNull(row.getBytes("t"));
    Assert.assertNotNull(row.getBytes("u"));
    row = execute("SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 2").one();
    Assert.assertNull(row.getBytes("l"));
    Assert.assertNull(row.getBytes("s"));
    Assert.assertNull(row.getBytes("m"));
    Assert.assertNull(row.getBytes("t"));
    Assert.assertNull(row.getBytes("u"));
    for (ProtocolVersion version : PROTOCOL_VERSIONS) {
        Row r = executeNet(version, "SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 1").one();
        Assert.assertNotNull(r.getBytesUnsafe("l"));
        Assert.assertNotNull(r.getBytesUnsafe("s"));
        Assert.assertNotNull(r.getBytesUnsafe("m"));
        Assert.assertNotNull(r.getBytesUnsafe("t"));
        Assert.assertNotNull(r.getBytesUnsafe("u"));
        r = executeNet(version, "SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 2").one();
        Assert.assertNull(r.getBytesUnsafe("l"));
        Assert.assertNull(r.getBytesUnsafe("s"));
        Assert.assertNull(r.getBytesUnsafe("m"));
        Assert.assertNull(r.getBytesUnsafe("t"));
        Assert.assertNull(r.getBytesUnsafe("u"));
    }
}
Also used : TreeMap(java.util.TreeMap) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) TreeSet(java.util.TreeSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 33 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class Dump method dump.

public static void dump(List<String> arguments, String rollCycle, boolean follow) {
    StringBuilder sb = new StringBuilder();
    ReadMarshallable reader = wireIn -> {
        sb.setLength(0);
        int version = wireIn.read(BinLog.VERSION).int16();
        if (version > FullQueryLogger.CURRENT_VERSION) {
            throw new IORuntimeException("Unsupported record version [" + version + "] - highest supported version is [" + FullQueryLogger.CURRENT_VERSION + ']');
        }
        String type = wireIn.read(BinLog.TYPE).text();
        if (!FullQueryLogger.SINGLE_QUERY.equals((type)) && !FullQueryLogger.BATCH.equals((type))) {
            throw new IORuntimeException("Unsupported record type field [" + type + "] - supported record types are [" + FullQueryLogger.SINGLE_QUERY + ", " + FullQueryLogger.BATCH + ']');
        }
        sb.append("Type: ").append(type).append(System.lineSeparator());
        long queryStartTime = wireIn.read(FullQueryLogger.QUERY_START_TIME).int64();
        sb.append("Query start time: ").append(queryStartTime).append(System.lineSeparator());
        int protocolVersion = wireIn.read(FullQueryLogger.PROTOCOL_VERSION).int32();
        sb.append("Protocol version: ").append(protocolVersion).append(System.lineSeparator());
        QueryOptions options = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wireIn.read(FullQueryLogger.QUERY_OPTIONS).bytes()), ProtocolVersion.decode(protocolVersion, true));
        long generatedTimestamp = wireIn.read(FullQueryLogger.GENERATED_TIMESTAMP).int64();
        sb.append("Generated timestamp:").append(generatedTimestamp).append(System.lineSeparator());
        int generatedNowInSeconds = wireIn.read(FullQueryLogger.GENERATED_NOW_IN_SECONDS).int32();
        sb.append("Generated nowInSeconds:").append(generatedNowInSeconds).append(System.lineSeparator());
        switch(type) {
            case (FullQueryLogger.SINGLE_QUERY):
                dumpQuery(options, wireIn, sb);
                break;
            case (FullQueryLogger.BATCH):
                dumpBatch(options, wireIn, sb);
                break;
            default:
                throw new IORuntimeException("Log entry of unsupported type " + type);
        }
        System.out.print(sb.toString());
        System.out.flush();
    };
    // Backoff strategy for spinning on the queue, not aggressive at all as this doesn't need to be low latency
    Pauser pauser = Pauser.millis(100);
    List<ChronicleQueue> queues = arguments.stream().distinct().map(path -> SingleChronicleQueueBuilder.single(new File(path)).readOnly(true).rollCycle(RollCycles.valueOf(rollCycle)).build()).collect(Collectors.toList());
    List<ExcerptTailer> tailers = queues.stream().map(ChronicleQueue::createTailer).collect(Collectors.toList());
    boolean hadWork = true;
    while (hadWork) {
        hadWork = false;
        for (ExcerptTailer tailer : tailers) {
            while (tailer.readDocument(reader)) {
                hadWork = true;
            }
        }
        if (follow) {
            if (!hadWork) {
                // Chronicle queue doesn't support blocking so use this backoff strategy
                pauser.pause();
            }
            // Don't terminate the loop even if there wasn't work
            hadWork = true;
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Unpooled(io.netty.buffer.Unpooled) Bytes(net.openhft.chronicle.bytes.Bytes) Option(io.airlift.airline.Option) ReadMarshallable(net.openhft.chronicle.wire.ReadMarshallable) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Pauser(net.openhft.chronicle.threads.Pauser) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) BinLog(org.apache.cassandra.utils.binlog.BinLog) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Collectors(java.util.stream.Collectors) File(java.io.File) BufferUnderflowException(java.nio.BufferUnderflowException) WireIn(net.openhft.chronicle.wire.WireIn) Command(io.airlift.airline.Command) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) ValueIn(net.openhft.chronicle.wire.ValueIn) FullQueryLogger(org.apache.cassandra.fql.FullQueryLogger) RollCycles(net.openhft.chronicle.queue.RollCycles) Arguments(io.airlift.airline.Arguments) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Pauser(net.openhft.chronicle.threads.Pauser) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) ReadMarshallable(net.openhft.chronicle.wire.ReadMarshallable) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) File(java.io.File)

Aggregations

ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)33 Test (org.junit.Test)22 ByteBuffer (java.nio.ByteBuffer)11 List (java.util.List)6 TreeMap (java.util.TreeMap)5 TreeSet (java.util.TreeSet)5 ArrayList (java.util.ArrayList)4 Row (com.datastax.driver.core.Row)3 Unpooled (io.netty.buffer.Unpooled)3 Collections (java.util.Collections)3 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)3 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)3 RollCycles (net.openhft.chronicle.queue.RollCycles)3 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)3 ValueIn (net.openhft.chronicle.wire.ValueIn)3 QueryOptions (org.apache.cassandra.cql3.QueryOptions)3 TupleType (com.datastax.driver.core.TupleType)2 TupleValue (com.datastax.driver.core.TupleValue)2 UDTValue (com.datastax.driver.core.UDTValue)2 Path (java.nio.file.Path)2