/** * Fast check to see if a shard has messages to process * * @param shardName * @throws MessageQueueException */ private boolean hasMessages(String shardName) throws MessageQueueException { UUID currentTime = TimeUUIDUtils.getUniqueTimeUUIDinMicros(); try { ColumnList<MessageQueueEntry> result = keyspace .prepareQuery(queueColumnFamily) .setConsistencyLevel(consistencyLevel) .getKey(shardName) .withColumnRange( new RangeBuilder() .setLimit(1) // Read extra messages because of the lock column .setStart( entrySerializer .makeEndpoint( (byte) MessageQueueEntryType.Message.ordinal(), Equality.EQUAL) .toBytes()) .setEnd( entrySerializer .makeEndpoint( (byte) MessageQueueEntryType.Message.ordinal(), Equality.EQUAL) .append((byte) 0, Equality.EQUAL) .append(currentTime, Equality.LESS_THAN_EQUALS) .toBytes()) .build()) .execute() .getResult(); return !result.isEmpty(); } catch (ConnectionException e) { throw new MessageQueueException("Error checking shard for messages. " + shardName, e); } }
private SampleEntity createSampleEntity(String id) { Random prng = new Random(); SampleEntity entity = new SampleEntity(); entity.setId(id); entity.setBooleanPrimitive(prng.nextBoolean()); entity.setBooleanObject(prng.nextBoolean()); entity.setBytePrimitive((byte) prng.nextInt(Byte.MAX_VALUE)); entity.setByteObject((byte) prng.nextInt(Byte.MAX_VALUE)); entity.setShortPrimitive((short) prng.nextInt(Short.MAX_VALUE)); entity.setShortObject((short) prng.nextInt(Short.MAX_VALUE)); entity.setIntPrimitive(prng.nextInt()); entity.setIntObject(prng.nextInt()); entity.setLongPrimitive(prng.nextLong()); entity.setLongObject(prng.nextLong()); entity.setFloatPrimitive(prng.nextFloat()); entity.setFloatObject(prng.nextFloat()); entity.setDoublePrimitive(prng.nextDouble()); entity.setDoubleObject(prng.nextDouble()); entity.setString(RandomStringUtils.randomAlphanumeric(16)); entity.setByteArray(RandomStringUtils.randomAlphanumeric(16).getBytes(Charsets.UTF_8)); entity.setDate(new Date()); entity.setUuid(TimeUUIDUtils.getUniqueTimeUUIDinMicros()); Foo foo = new Foo(prng.nextInt(), RandomStringUtils.randomAlphanumeric(4)); entity.setFoo(foo); BarBar barbar = new BarBar(); barbar.i = prng.nextInt(); barbar.s = RandomStringUtils.randomAlphanumeric(4); Bar bar = new Bar(); bar.i = prng.nextInt(); bar.s = RandomStringUtils.randomAlphanumeric(4); bar.barbar = barbar; entity.setBar(bar); return entity; }
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; } catch (Exception e) { LOG.error("Error reading shard " + shardName, e); throw new MessageQueueException("Error", e); } finally { queue.lockManager.releaseLock(lock); } }
@Test public void testMultiRowUniqueness() { DedicatedMultiRowUniquenessConstraint<UUID> constraint = new DedicatedMultiRowUniquenessConstraint<UUID>( keyspace, TimeUUIDUtils.getUniqueTimeUUIDinMicros()) .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withRow(CF_USER_UNIQUE_UUID, "user1") .withRow(CF_EMAIL_UNIQUE_UUID, "*****@*****.**"); DedicatedMultiRowUniquenessConstraint<UUID> constraint2 = new DedicatedMultiRowUniquenessConstraint<UUID>( keyspace, TimeUUIDUtils.getUniqueTimeUUIDinMicros()) .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withRow(CF_USER_UNIQUE_UUID, "user1") .withRow(CF_EMAIL_UNIQUE_UUID, "*****@*****.**"); try { Column<UUID> c = constraint.getUniqueColumn(); Assert.fail(); } catch (Exception e) { LOG.info(e.getMessage()); } try { constraint.acquire(); Column<UUID> c = constraint.getUniqueColumn(); LOG.info("Unique column is " + c.getName()); try { constraint2.acquire(); Assert.fail("Should already be acquired"); } catch (NotUniqueException e) { } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { try { constraint2.release(); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { try { constraint.release(); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } try { constraint2.acquire(); Column<UUID> c = constraint.getUniqueColumn(); LOG.info("Unique column is " + c.getName()); } catch (NotUniqueException e) { Assert.fail("Should already be unique"); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { try { constraint2.release(); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } }