Package com.netflix.astyanax

Examples of com.netflix.astyanax.MutationBatch


    private MutationBatch getMutationBatch() {
        if (autoCommit) {
            return newMutationBatch();
        }
        else {
            MutationBatch mb = tlMutation.get();
            if (mb == null) {
                mb = newMutationBatch();
                tlMutation.set(mb);
            }
            return mb;
View Full Code Here


        }
    }

    @Override
    public void commit() throws PersistenceException {
        MutationBatch mb = tlMutation.get();
        if (mb != null) {
            try {
                mb.execute();
            } catch (ConnectionException e) {
                throw new PersistenceException("Failed to commit mutation batch", e);
            }
        }
    }
View Full Code Here

    List<MessageContext> readMessagesFromShardUsingLockManager(String shardName, int itemToPop) throws MessageQueueException, BusyLockException {
        ShardLock lock = null;
        try {
            lock = queue.lockManager.acquireLock(shardName);
            MutationBatch m = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
            ColumnListMutation<MessageQueueEntry> rowMutation = m.withRow(queue.queueColumnFamily, shardName);
            long curTimeMicros = TimeUUIDUtils.getMicrosTimeFromUUID(TimeUUIDUtils.getUniqueTimeUUIDinMicros());
            return readMessagesInternal(shardName, itemToPop, 0, null, rowMutation, m, curTimeMicros);
        } catch (BusyLockException e) {
            queue.stats.incLockContentionCount();
            throw e;
View Full Code Here

            queue.lockManager.releaseLock(lock);
        }
    }

    List<MessageContext> readMessagesFromShardUsingDefaultLock(String shardName, int itemsToPop) throws MessageQueueException, BusyLockException {
        MutationBatch m = null;
        MessageQueueEntry lockColumn = null;
        ColumnListMutation<MessageQueueEntry> rowMutation = null;
        int lockColumnCount = 0;
        // Try locking first
        try {
            // 1. Write the lock column
            lockColumn = MessageQueueEntry.newLockEntry(MessageQueueEntryState.None);
            long curTimeMicros = TimeUUIDUtils.getTimeFromUUID(lockColumn.getTimestamp());
            m = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
            m.withRow(queue.queueColumnFamily, shardName).putColumn(lockColumn, curTimeMicros + queue.lockTimeout, queue.lockTtl);
            m.execute();
            // 2. Read back lock columns and entries
            ColumnList<MessageQueueEntry> result = queue.keyspace.prepareQuery(queue.queueColumnFamily).setConsistencyLevel(queue.consistencyLevel).getKey(shardName)
                    .withColumnRange(ShardedDistributedMessageQueue.entrySerializer
                                                                                   .buildRange()
                                                                                   .greaterThanEquals((byte) MessageQueueEntryType.Lock.ordinal())
                                                                                   .lessThanEquals((byte) MessageQueueEntryType.Lock.ordinal())
                                                                                   .build()
                                                                   )
                    .execute()
                    .getResult();
            m = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
            rowMutation = m.withRow(queue.queueColumnFamily, shardName);
            rowMutation.deleteColumn(lockColumn);
            int lockCount = 0;
            boolean lockAcquired = false;
            lockColumnCount = result.size();
            for (Column<MessageQueueEntry> column : result) {
                MessageQueueEntry lock = column.getName();
                if (lock.getType() == MessageQueueEntryType.Lock) {
                    lockColumnCount++;
                    // Stale lock so we can discard it
                    if (column.getLongValue() < curTimeMicros) {
                        queue.stats.incExpiredLockCount();
                        rowMutation.deleteColumn(lock);
                    } else if (lock.getState() == MessageQueueEntryState.Acquired) {
                        throw new BusyLockException("Not first lock");
                    } else {
                        lockCount++;
                        if (lockCount == 1 && lock.getTimestamp().equals(lockColumn.getTimestamp())) {
                            lockAcquired = true;
                        }
                    }
                    if (!lockAcquired) {
                        throw new BusyLockException("Not first lock");
                    }
                    // Write the acquired lock column
                    lockColumn = MessageQueueEntry.newLockEntry(lockColumn.getTimestamp(), MessageQueueEntryState.Acquired);
                    rowMutation.putColumn(lockColumn, curTimeMicros + queue.lockTimeout, queue.lockTtl);
                }
            }
        } catch (BusyLockException e) {
            queue.stats.incLockContentionCount();
            throw e;
        } catch (ConnectionException e) {
            LOG.error("Error reading shard " + shardName, e);
            throw new MessageQueueException("Error", e);
        } finally {
            try {
                m.execute();
            } catch (Exception e) {
                throw new MessageQueueException("Error committing lock", e);
            }
        }
        long curTimeMicros = TimeUUIDUtils.getMicrosTimeFromUUID(lockColumn.getTimestamp());
        m = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
        // First, release the lock column
        rowMutation = m.withRow(queue.queueColumnFamily, shardName);
        rowMutation.deleteColumn(lockColumn);
        return readMessagesInternal(shardName, itemsToPop, lockColumnCount, lockColumn, rowMutation, m, curTimeMicros);
    }
View Full Code Here

        return readMessagesInternal(shardName, itemsToPop, lockColumnCount, lockColumn, rowMutation, m, curTimeMicros);
    }

    @Override
    public void ackMessage(MessageContext context) throws MessageQueueException {
        MutationBatch mb = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
        fillAckMutation(context, mb);
        try {
            mb.execute();
        } catch (ConnectionException e) {
            throw new MessageQueueException("Failed to ack message", e);
        }
    }
View Full Code Here

        }
    }

    @Override
    public void ackMessages(Collection<MessageContext> messages) throws MessageQueueException {
        MutationBatch mb = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
        for (MessageContext context : messages) {
            fillAckMutation(context, mb);
        }
        try {
            mb.execute();
        } catch (ConnectionException e) {
            throw new MessageQueueException("Failed to ack messages", e);
        }
    }
View Full Code Here

    }

    @Override
    public void ackPoisonMessage(MessageContext context) throws MessageQueueException {
        // TODO: Remove bad message and add to poison queue
        MutationBatch mb = queue.keyspace.prepareMutationBatch().setConsistencyLevel(queue.consistencyLevel);
        fillAckMutation(context, mb);
        try {
            mb.execute();
        } catch (ConnectionException e) {
            queue.stats.incPersistError();
            throw new MessageQueueException("Failed to ack messages", e);
        }
    }
View Full Code Here

    testReadRowRangeColumnRangeWithColumnCount(rowDeleted);
  }
 
  private void populateRowsForCFPopulation() throws Exception {
   
    MutationBatch m = keyspace.prepareMutationBatch();
   
    Random random = new Random();
   
    for (int year = 2001; year <= 2014; year++) {
     
      m.withRow(CF_POPULATION, year)
        .putColumn(NewYork.clone(), random.nextInt(25000))
        .putColumn(SanDiego.clone(), random.nextInt(25000))
        .putColumn(SanFrancisco.clone(), random.nextInt(25000))
        .putColumn(Seattle.clone(), random.nextInt(25000));
    }
   
    m.execute();
  }
View Full Code Here

    m.execute();
  }

  private void deleteRowsForCFPopulation() throws Exception {
   
    MutationBatch m = keyspace.prepareMutationBatch();
   
    for (int year = 2001; year <= 2014; year ++) {
      m.withRow(CF_POPULATION, year).delete();
    }
   
    m.execute();
  }
View Full Code Here

  }

    @Test
    public void testTimeUUID() throws Exception {
     
      MutationBatch m = keyspace.prepareMutationBatch();

        UUID columnName = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
        long columnTime = TimeUUIDUtils.getTimeFromUUID(columnName);
        String rowKey = "Key1";

        m.withRow(CF_TIME_UUID, rowKey).delete();
        m.execute();
        m.discardMutations();
       
        int startTime = 100;
        int endTime = 200;

        m.withRow(CF_TIME_UUID, rowKey).putColumn(columnName, 42, null);
        for (int i = startTime; i < endTime; i++) {
            // UUID c = TimeUUIDUtils.getTimeUUID(i);
            LOG.info(TimeUUIDUtils.getTimeUUID(columnTime + i).toString());

            m.withRow(CF_TIME_UUID, rowKey).putColumn(
                    TimeUUIDUtils.getTimeUUID(columnTime + i), i, null);
        }

        m.execute();

        OperationResult<Column<UUID>> result = keyspace
            .prepareQuery(CF_TIME_UUID).getKey(rowKey)
            .getColumn(columnName).execute();
View Full Code Here

TOP

Related Classes of com.netflix.astyanax.MutationBatch

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.