public QueueIndexUpdate( Mutator<ByteBuffer> batch, String queuePath, UUID queueId, String entryName, Object entryValue, UUID timestampUuid ) { this.batch = batch; this.queuePath = queuePath; this.queueId = queueId; this.entryName = entryName; this.entryValue = entryValue; timestamp = getTimestampInMicros( timestampUuid ); this.timestampUuid = timestampUuid; }
/** * Write the updated client pointer * * @param lastReturnedId This is a null safe parameter. If it's null, this won't be written since it means we didn't * read any messages */ protected void writeClientPointer( UUID queueId, UUID consumerId, UUID lastReturnedId ) { // nothing to do if ( lastReturnedId == null ) { return; } // we want to set the timestamp to the value from the time uuid. If this is // not the max time uuid to ever be written // for this consumer, we want this to be discarded to avoid internode race // conditions with clock drift. long colTimestamp = UUIDUtils.getTimestampInMicros( lastReturnedId ); Mutator<UUID> mutator = CountingMutator.createFlushingMutator( ko, ue ); if ( logger.isDebugEnabled() ) { logger.debug( "Writing last client id pointer of '{}' for queue '{}' and consumer '{}' with timestamp '{}", lastReturnedId, queueId, consumerId, colTimestamp ); } mutator.addInsertion( consumerId, CONSUMERS.getColumnFamily(), createColumn( queueId, lastReturnedId, colTimestamp, ue, ue ) ); mutator.execute(); }
long timestamp = UUIDUtils.getTimestampInMicros( timestampUuid );
@Test @Category(ExperimentalTest.class) @SuppressWarnings("unchecked") public void verifyOrderingTsOnlyAndUnique() { int count = 500; long ts = System.currentTimeMillis(); List<UUID> uuids = new ArrayList<UUID>( count ); HashSet times = new HashSet(); UUID lastSeen; for ( int i = 0; i < count; i++ ) { lastSeen = newTimeUUID( ts ); uuids.add( lastSeen ); times.add( UUIDUtils.getTimestampInMicros( lastSeen ) ); } assertEquals( 500, times.size() ); for ( int i = 0; i < count - 1; i++ ) { assertEquals( -1, uuids.get( i ).compareTo( uuids.get( i + 1 ) ) ); } }
@Test public void testUUIDUtils() { UUID uuid = UUIDUtils.newTimeUUID(); logger.info("" + uuid); logger.info("" + uuid.timestamp()); logger.info("" + UUIDUtils.getTimestampInMillis(uuid)); logger.info("" + UUIDUtils.getTimestampInMillis(UUIDUtils.newTimeUUID())); logger.info("" + System.currentTimeMillis()); logger.info("" + UUIDUtils.getTimestampInMicros(UUIDUtils.newTimeUUID())); logger.info("" + (System.currentTimeMillis() * 1000)); logger.info("" + UUIDUtils.MIN_TIME_UUID); logger.info("" + UUIDUtils.MIN_TIME_UUID.variant()); logger.info("" + UUIDUtils.MIN_TIME_UUID.version()); logger.info("" + UUIDUtils.MIN_TIME_UUID.clockSequence()); logger.info("" + UUIDUtils.MIN_TIME_UUID.timestamp()); logger.info("" + UUIDUtils.MAX_TIME_UUID); logger.info("" + UUIDUtils.MAX_TIME_UUID.variant()); logger.info("" + UUIDUtils.MAX_TIME_UUID.version()); logger.info("" + UUIDUtils.MAX_TIME_UUID.clockSequence()); logger.info("" + UUIDUtils.MAX_TIME_UUID.timestamp()); }
long timestamp = UUIDUtils.getTimestampInMicros( UUIDUtils.newTimeUUID() ); long timestampFromImport = -1L; if ( UUIDUtils.isTimeBased( importId ) ) { timestampFromImport = UUIDUtils.getTimestampInMicros( importId );
throws Exception { long timestamp = UUIDUtils.getTimestampInMicros( timestampUuid );
throws Exception { long timestamp = getTimestampInMicros( timestampUuid );
@Override public QueueSet subscribeToQueue( String publisherQueuePath, String subscriberQueuePath ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); batchSubscribeToQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); if ( queue != null ) { batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, queue.getProperties(), timestampUuid ); } } catch ( Exception e ) { logger.error( "Unable to update index", e ); } batchExecute( batch, RETRY_COUNT ); return new QueueSet().addQueue( subscriberQueuePath, subscriberQueueId ); }
createColumn( messageUuid, ByteBuffer.allocate( 0 ), timestamp, ue, be ) ); long oldest_ts = Long.MAX_VALUE - getTimestampInMicros( messageUuid ); batch.addInsertion( bytebuffer( queueId ), QUEUE_PROPERTIES.getColumnFamily(), createColumn( QUEUE_OLDEST, messageUuid, oldest_ts, se, ue ) ); long newest_ts = getTimestampInMicros( messageUuid ); batch.addInsertion( bytebuffer( queueId ), QUEUE_PROPERTIES.getColumnFamily(), createColumn( QUEUE_NEWEST, messageUuid, newest_ts, se, ue ) );
@Override public QueueSet unsubscribeFromQueue( String publisherQueuePath, String subscriberQueuePath ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); batchUnsubscribeFromQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, emptyMapWithKeys( queue.getProperties() ), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update index", e ); } batchExecute( batch, RETRY_COUNT ); return new QueueSet().addQueue( subscriberQueuePath, subscriberQueueId ); }
@Override public QueueSet addSubscribersToQueue( String publisherQueuePath, List<String> subscriberQueuePaths ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); QueueSet queues = new QueueSet(); for ( String subscriberQueuePath : subscriberQueuePaths ) { subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); batchSubscribeToQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, queue.getProperties(), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update index", e ); } queues.addQueue( subscriberQueuePath, subscriberQueueId ); } batchExecute( batch, RETRY_COUNT ); return queues; }
@Override public QueueSet subscribeToQueues( String subscriberQueuePath, List<String> publisherQueuePaths ) { subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); QueueSet queues = new QueueSet(); for ( String publisherQueuePath : publisherQueuePaths ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); batchSubscribeToQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, queue.getProperties(), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update index", e ); } queues.addQueue( publisherQueuePath, publisherQueueId ); } batchExecute( batch, RETRY_COUNT ); return queues; }
@Override public QueueSet removeSubscribersFromQueue( String publisherQueuePath, List<String> subscriberQueuePaths ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); QueueSet queues = new QueueSet(); for ( String subscriberQueuePath : subscriberQueuePaths ) { subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); batchUnsubscribeFromQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, emptyMapWithKeys( queue.getProperties() ), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update index", e ); } queues.addQueue( subscriberQueuePath, subscriberQueueId ); } batchExecute( batch, RETRY_COUNT ); return queues; }
@Override public QueueSet unsubscribeFromQueues( String subscriberQueuePath, List<String> publisherQueuePaths ) { subscriberQueuePath = normalizeQueuePath( subscriberQueuePath ); UUID subscriberQueueId = getQueueId( subscriberQueuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); QueueSet queues = new QueueSet(); for ( String publisherQueuePath : publisherQueuePaths ) { publisherQueuePath = normalizeQueuePath( publisherQueuePath ); UUID publisherQueueId = getQueueId( publisherQueuePath ); batchUnsubscribeFromQueue( batch, publisherQueuePath, publisherQueueId, subscriberQueuePath, subscriberQueueId, timestamp ); try { Queue queue = getQueue( subscriberQueuePath, subscriberQueueId ); batchUpdateQueuePropertiesIndexes( batch, publisherQueueId, subscriberQueuePath, subscriberQueueId, emptyMapWithKeys( queue.getProperties() ), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update index", e ); } queues.addQueue( publisherQueuePath, publisherQueueId ); } batchExecute( batch, RETRY_COUNT ); return queues; }
@Override public Queue updateQueue( String queuePath, Queue queue ) { queue.setPath( queuePath ); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros( timestampUuid ); Mutator<ByteBuffer> batch = CountingMutator.createFlushingMutator( cass.getApplicationKeyspace( applicationId ), be ); addQueueToMutator( batch, queue, timestamp ); try { batchUpdateQueuePropertiesIndexes( batch, queuePath, queue.getUuid(), queue.getProperties(), timestampUuid ); } catch ( Exception e ) { logger.error( "Unable to update queue", e ); } batch.addInsertion( bytebuffer( queue.getUuid() ), QUEUE_PROPERTIES.getColumnFamily(), createColumn( QUEUE_CREATED, timestamp / 1000, Long.MAX_VALUE - timestamp, se, le ) ); batch.addInsertion( bytebuffer( queue.getUuid() ), QUEUE_PROPERTIES.getColumnFamily(), createColumn( QUEUE_MODIFIED, timestamp / 1000, timestamp, se, le ) ); batchExecute( batch, RETRY_COUNT ); return queue; }