Package org.apache.cassandra.db

Examples of org.apache.cassandra.db.RowMutation


                    continue;
                }

                /* deserialize the commit log entry */
                FastByteArrayInputStream bufIn = new FastByteArrayInputStream(buffer, 0, serializedSize);
                RowMutation rm;
                try
                {
                    // assuming version here. We've gone to lengths to make sure what gets written to the CL is in
                    // the current version. so do make sure the CL is drained prior to upgrading a node.
                    rm = RowMutation.serializer().deserialize(new DataInputStream(bufIn), MessagingService.version_, IColumnSerializer.Flag.LOCAL);
                }
                catch (UnknownColumnFamilyException ex)
                {
                    AtomicInteger i = invalidMutations.get(ex.cfId);
                    if (i == null)
                    {
                        i = new AtomicInteger(1);
                        invalidMutations.put(ex.cfId, i);
                    }
                    else
                        i.incrementAndGet();
                    continue;
                }

                if (logger.isDebugEnabled())
                    logger.debug(String.format("replaying mutation for %s.%s: %s", rm.getTable(), ByteBufferUtil.bytesToHex(rm.key()), "{" + StringUtils.join(rm.getColumnFamilies().iterator(), ", ")
                            + "}"));

                final long entryLocation = reader.getFilePointer();
                final RowMutation frm = rm;
                Runnable runnable = new WrappedRunnable()
                {
                    public void runMayThrow() throws IOException
                    {
                        if (Schema.instance.getKSMetaData(frm.getTable()) == null)
                            return;
                        if (pointInTimeExceeded(frm))
                            return;

                        final Table table = Table.open(frm.getTable());
                        RowMutation newRm = new RowMutation(frm.getTable(), frm.key());

                        // Rebuild the row mutation, omitting column families that
                        // a) have already been flushed,
                        // b) are part of a cf that was dropped. Keep in mind that the cf.name() is suspect. do every thing based on the cfid instead.
                        for (ColumnFamily columnFamily : frm.getColumnFamilies())
                        {
                            if (Schema.instance.getCF(columnFamily.id()) == null)
                                // null means the cf has been dropped
                                continue;

                            ReplayPosition rp = cfPositions.get(columnFamily.id());

                            // replay if current segment is newer than last flushed one or,
                            // if it is the last known segment, if we are after the replay position
                            if (segment > rp.segment || (segment == rp.segment && entryLocation > rp.position))
                            {
                                newRm.add(columnFamily);
                                replayedCount.incrementAndGet();
                            }
                        }
                        if (!newRm.isEmpty())
                        {
                            Table.open(newRm.getTable()).apply(newRm, false);
                            tablesRecovered.add(table);
                        }
                    }
                };
                futures.add(StageManager.getStage(Stage.MUTATION).submit(runnable));
View Full Code Here


            // since retRow is the resolved row it can be used as the super set
            Row diffRow = rowList.get(i).diff(retRow);
            if (diffRow == null) // no repair needs to happen
                continue;
            // create the row mutation message based on the diff and schedule a read repair
            RowMutation rowMutation = new RowMutation(table, key);
            for (ColumnFamily cf : diffRow.getColumnFamilies())
            {
                rowMutation.add(cf);
            }
            RowMutationMessage rowMutationMessage = new RowMutationMessage(rowMutation);
            ReadRepairManager.instance().schedule(endPoints.get(i), rowMutationMessage);
        }
        if (logger_.isDebugEnabled())
View Full Code Here

    }
    public static Message createMessage(String Keyspace, String Key, String CFName, List<ColumnFamily> ColumnFamiles)
    {
        ColumnFamily baseColumnFamily;
        DataOutputBuffer bufOut = new org.apache.cassandra.io.DataOutputBuffer();
        RowMutation rm;
        Message message;
        Column column;

        /* Get the first column family from list, this is just to get past validation */
        baseColumnFamily = new ColumnFamily(CFName, "Standard",DatabaseDescriptor.getComparator(Keyspace, CFName), DatabaseDescriptor.getSubComparator(Keyspace, CFName));
       
        for(ColumnFamily cf : ColumnFamiles) {
            bufOut.reset();
            try
            {
                ColumnFamily.serializer().serializeWithIndexes(cf, bufOut);
                byte[] data = new byte[bufOut.getLength()];
                System.arraycopy(bufOut.getData(), 0, data, 0, bufOut.getLength());

                column = new Column(cf.name().getBytes("UTF-8"), data, 0, false);
                baseColumnFamily.addColumn(column);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }
        rm = new RowMutation(Keyspace, Key);
        rm.add(baseColumnFamily);

        try
        {
            /* Make message */
            message = rm.makeRowMutationMessage(StorageService.binaryVerbHandler_);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
View Full Code Here

    }
   
    ByteBuffer coreNameBytes = ByteBufferUtil.bytes(coreName);
    ByteBuffer resourceNameBytes = ByteBufferUtil.bytes(resourceName);
   
    RowMutation rm = new RowMutation(CassandraUtils.keySpace, coreNameBytes);

    QueryPath queryPath = new QueryPath(
        CassandraUtils.schemaInfoColumnFamily,
        coreNameBytes,
        resourceNameBytes);

        rm.add(
            queryPath,
            ByteBufferUtil.bytes(resourceValue),
            System.currentTimeMillis());

        CassandraUtils.robustInsert(ConsistencyLevel.QUORUM, rm);
View Full Code Here

        // Permanently mark the id as taken
        ByteBuffer idKey = CassandraUtils.hashKeyBytes((indexName + "~" + shard).getBytes("UTF-8"),
                CassandraUtils.delimeterBytes, "ids".getBytes("UTF-8"));

        RowMutation rm = new RowMutation(CassandraUtils.keySpace, idKey);
        rm.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, idCol, ByteBuffer
                .wrap(getToken().getBytes("UTF-8"))), keyCol, System.currentTimeMillis());

        return rm;
    }
View Full Code Here

            // Permanently mark the id as taken
            ByteBuffer idKey = CassandraUtils.hashKeyBytes((indexName + "~" + shard).getBytes("UTF-8"),
                    CassandraUtils.delimeterBytes, "ids".getBytes("UTF-8"));

            RowMutation rm = new RowMutation(CassandraUtils.keySpace, idKey);
            rm.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, idCol, ByteBuffer.wrap(myToken
                    .getBytes("UTF-8"))), keyCol, System.currentTimeMillis());

            // Permanently link the key to the id
            ByteBuffer keyKey = CassandraUtils.hashKeyBytes((indexName + "~" + key).getBytes("UTF-8"),
                    CassandraUtils.delimeterBytes, "keys".getBytes("UTF-8"));

            ByteBuffer idVal = ByteBuffer.wrap(id.toString().getBytes("UTF-8"));

            RowMutation rm2 = new RowMutation(CassandraUtils.keySpace, keyKey);
            rm2.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, keyCol, idVal),
                    CassandraUtils.finalTokenBytes, System.currentTimeMillis());

            // Update last offset info for this shard
            RowMutation rm3 = updateNodeOffset(indexName, myToken, shard, shardedId);

            rowMutations[0] = rm;
            rowMutations[1] = rm2;
            rowMutations[2] = rm3;
View Full Code Here

                    ByteBuffer key = CassandraUtils.hashKeyBytes((indexName + "~" + node.shard).getBytes("UTF-8"),
                            CassandraUtils.delimeterBytes, "rsvp".getBytes("UTF-8"));

                    // Write the reserves
                    RowMutation rm = new RowMutation(CassandraUtils.keySpace, key);

                    ByteBuffer id = ByteBufferUtil.bytes(String.valueOf(nextOffset));
                    ByteBuffer off = id;

                    rm.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, id, ByteBuffer.wrap(myToken
                            .getBytes("UTF-8"))), off, System.currentTimeMillis(), expirationTime);

                    CassandraUtils.robustInsert(ConsistencyLevel.QUORUM, rm);

                    // Give it time to sink in, in-case clocks are off across
                    // nodes...
                    try
                    {
                        Thread.sleep(100);
                    } catch (InterruptedException e1)
                    {
                    }

                    // Read the columns back
                    IColumn supercol = null;
                    int attempts = 0;
                    while (supercol == null && attempts < CassandraUtils.retryAttempts)
                    {
                        try
                        {
                            List<Row> rows = CassandraUtils.robustRead(key, new QueryPath(
                                    CassandraUtils.schemaInfoColumnFamily), Arrays.asList(id), ConsistencyLevel.QUORUM);

                            if (rows == null || rows.size() == 0)
                            {
                                continue;
                            }

                            if (rows.size() == 1)
                            {
                                Row row = rows.get(0);

                                if (row.cf == null || row.cf.isMarkedForDelete())
                                {
                                    continue;
                                }

                                supercol = rows.get(0).cf.getColumn(id);
                            }
                        } catch (IOException e)
                        {
                            // let's try again...
                        }

                        attempts++;
                    }

                    if (supercol == null)
                        throw new IllegalStateException("just wrote " + offset + ", but didn't read it");

                    long minTtl = Long.MAX_VALUE;
                    ByteBuffer winningToken = null;

                    // See which ones we successfully reserved
                    for (IColumn c : supercol.getSubColumns())
                    {

                        // someone already took this id
                        if (!(c instanceof ExpiringColumn) && !(c instanceof DeletedColumn))
                        {
                            if (logger.isDebugEnabled())
                                logger.debug(offset + " was taken by " + ByteBufferUtil.string(c.name()));

                            winningToken = null;
                            break;
                        }

                        // expired reservation
                        if (c.isMarkedForDelete())
                        {
                            continue;
                        }

                        if (c.timestamp() < minTtl)
                        {
                            minTtl = c.timestamp();
                            winningToken = c.name();
                        }

                        // incase of a tie the token is the tiebreaker
                        if (c.timestamp() == minTtl && winningToken.compareTo(c.name()) <= 0)
                        {
                            winningToken = c.name();
                        }
                    }

                    String winningTokenStr;
                    try
                    {
                        winningTokenStr = winningToken == null ? "" : ByteBufferUtil.string(winningToken);
                    } catch (CharacterCodingException e)
                    {
                        throw new RuntimeException(e);
                    }

                    // we won!
                    if (winningTokenStr.equals(myToken))
                    {
                        // Mark this as permanently taken
                        rm = new RowMutation(CassandraUtils.keySpace, key);

                        rm.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, id, ByteBuffer.wrap(myToken
                                .getBytes("UTF-8"))), off, System.currentTimeMillis());

                        CassandraUtils.robustInsert(ConsistencyLevel.QUORUM, rm);

                        // Add to active rsvp list
View Full Code Here

    {

        // Update last offset info for this shard
        ByteBuffer shardKey = CassandraUtils.hashKeyBytes(indexName.getBytes("UTF-8"), CassandraUtils.delimeterBytes,
                "shards".getBytes("UTF-8"));
        RowMutation rm = new RowMutation(CassandraUtils.keySpace, shardKey);

        rm.add(new QueryPath(CassandraUtils.schemaInfoColumnFamily, ByteBuffer.wrap(String.valueOf(shard).getBytes(
                "UTF-8")), ByteBuffer.wrap(myToken.getBytes("UTF-8"))),
                ByteBuffer.wrap(String.valueOf(offset).getBytes("UTF-8")), System.currentTimeMillis());

        // update locally
        ShardInfo si = getShardInfo(indexName, false);
View Full Code Here

                if (expiredCallbackInfo.shouldHint())
                {
                    assert expiredCallbackInfo.message != null;
                    try
                    {
                        RowMutation rm = RowMutation.fromBytes(expiredCallbackInfo.message.getMessageBody(), expiredCallbackInfo.message.getVersion());
                        return StorageProxy.scheduleLocalHint(rm, expiredCallbackInfo.target, null, null);
                    }
                    catch (IOException e)
                    {
                        logger_.error("Unable to deserialize mutation when writting hint for: " + expiredCallbackInfo.target);
View Full Code Here

        ThriftValidation.validateKey(key);
        ThriftValidation.validateColumnParent(state().getKeyspace(), column_parent);
        ThriftValidation.validateColumn(state().getKeyspace(), column_parent, column);

        RowMutation rm = new RowMutation(state().getKeyspace(), key);
        try
        {
            rm.add(new QueryPath(column_parent.column_family, column_parent.super_column, column.name), column.value, column.timestamp, column.ttl);
        }
        catch (MarshalException e)
        {
            throw new InvalidRequestException(e.getMessage());
        }
View Full Code Here

TOP

Related Classes of org.apache.cassandra.db.RowMutation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.