/** * The start time of this operation as represented by the timestamp embedded in the type-1 UUID of the opTag * property */ public long getStart() { return UUIDUtils.getTimestampInMillis( opTag ); }
/** Apply tagName only if not already applied */ public void stopAndApply( String tName, boolean opStatus ) { if ( elapsed == 0 ) { // extract from uuid and calculate elapsed = System.currentTimeMillis() - UUIDUtils.getTimestampInMillis( opTag ); } if ( tName != null ) { this.tagName = tName; this.status = opStatus; } }
public void setUuid( UUID uuid ) { properties.put(MESSAGE_ID, uuid); properties.put(MESSAGE_TIMESTAMP, UUIDUtils.getTimestampInMillis(uuid)); }
@JsonIgnore public synchronized long getTimestamp() { if ( properties.containsKey( MESSAGE_TIMESTAMP ) ) { long ts = getLongValue( properties, MESSAGE_TIMESTAMP ); if ( ts != 0 ) { return ts; } } long timestamp = getTimestampInMillis( getUuid() ); properties.put( MESSAGE_TIMESTAMP, timestamp ); return timestamp; }
@Override public long getMaxTokenAge( String token ) { TokenCategory tokenCategory = TokenCategory.getFromBase64String( token ); byte[] bytes = decodeBase64( token.substring( TokenCategory.BASE64_PREFIX_LENGTH ) ); UUID uuid = uuid( bytes ); long timestamp = getTimestampInMillis( uuid ); int i = 16; if ( tokenCategory.getExpires() ) { long expires = ByteBuffer.wrap( bytes, i, 8 ).getLong(); return expires - timestamp; } return Long.MAX_VALUE; }
@JsonIgnore public synchronized UUID getUuid() { UUID uuid = uuid( properties.get( MESSAGE_ID ), null ); if ( uuid == null ) { if ( properties.containsKey( MESSAGE_TIMESTAMP ) ) { long ts = getLongValue( properties, MESSAGE_TIMESTAMP ); uuid = newTimeUUID( ts ); } else { uuid = newTimeUUID(); } properties.put( MESSAGE_ID, uuid ); properties.put( MESSAGE_TIMESTAMP, getTimestampInMillis( uuid ) ); } return uuid; }
private String getTokenForUUID( TokenInfo tokenInfo, TokenCategory tokenCategory, UUID uuid ) { int l = 36; if ( tokenCategory.getExpires() ) { l += 8; } ByteBuffer bytes = ByteBuffer.allocate( l ); bytes.put( bytes( uuid ) ); long expires = Long.MAX_VALUE; if ( tokenCategory.getExpires() ) { expires = ( tokenInfo.getDuration() > 0 ) ? UUIDUtils.getTimestampInMillis( uuid ) + ( tokenInfo.getDuration() ) : UUIDUtils.getTimestampInMillis( uuid ) + getExpirationForTokenType( tokenCategory ); bytes.putLong( expires ); } bytes.put( sha( tokenCategory.getPrefix() + uuid + tokenSecretSalt + expires ) ); return tokenCategory.getBase64Prefix() + encodeBase64URLSafeString( bytes.array() ); }
@Override public void importToken(String token, TokenCategory tokenCategory, String type, AuthPrincipalInfo principal, Map<String, Object> state, long duration, UUID workflowOrgId) throws Exception { // same logic as create token long maxTokenTtl = getMaxTtl( tokenCategory, principal ); if ( duration > maxTokenTtl ) { throw new IllegalArgumentException( String.format( "Your token age cannot be more than the maximum age of %d milliseconds", maxTokenTtl ) ); } if ( duration == 0 ) { duration = maxTokenTtl; } if ( principal != null ) { Assert.notNull( principal.getType() ); Assert.notNull( principal.getApplicationId() ); Assert.notNull( principal.getUuid() ); } // except that we generate the UUID based on the token UUID uuid = getUUIDForToken(token); long timestamp = getTimestampInMillis( uuid ); if ( type == null ) { type = TOKEN_TYPE_ACCESS; } TokenInfo tokenInfo = new TokenInfo( uuid, type, timestamp, timestamp, 0, duration, principal, state, workflowOrgId ); putTokenInfo( tokenInfo ); }
/** Exposed for testing purposes. The interface does not allow creation timestamp checking */ public String createToken( TokenCategory tokenCategory, String type, AuthPrincipalInfo principal, Map<String, Object> state, long duration, UUID workflowOrgId, long creationTimestamp ) throws Exception { long maxTokenTtl = getMaxTtl( tokenCategory, principal ); if ( duration > maxTokenTtl ) { throw new IllegalArgumentException( String.format( "Your token age cannot be more than the maximum age of %d milliseconds", maxTokenTtl ) ); } if ( duration == 0 ) { duration = maxTokenTtl; } if ( principal != null ) { Assert.notNull( principal.getType() ); Assert.notNull( principal.getApplicationId() ); Assert.notNull( principal.getUuid() ); } // create UUID that we will use to store token info in our database UUID uuid = UUIDUtils.newTimeUUID( creationTimestamp ); long timestamp = getTimestampInMillis( uuid ); if ( type == null ) { type = TOKEN_TYPE_ACCESS; } TokenInfo tokenInfo = new TokenInfo( uuid, type, timestamp, timestamp, 0, duration, principal, state, workflowOrgId ); putTokenInfo( tokenInfo ); // generate token from the UUID that we created return getTokenForUUID(tokenInfo, tokenCategory, uuid); }
@Test public void testAppProvidedTimestamp() { logger.info("UUIDUtilsTest.testAppProvidedTimestamp"); long ts = System.currentTimeMillis(); System.out.println( ts ); Set<UUID> uuids = new HashSet<UUID>(); int count = 1000000; logger.info("Generating " + count + " UUIDs..."); for ( int i = 0; i < count; i++ ) { UUID uuid = newTimeUUID( ts ); assertFalse( "UUID already generated", uuids.contains( uuid ) ); uuids.add( uuid ); assertEquals( "Incorrect UUID timestamp value", ts, getTimestampInMillis( uuid ) ); if ( i % 1000 == 0 ) { logger.info("testAppProvidedTimestamp processed " + i); } } logger.info("UUIDs checked"); }
@Test public void timeUUIDOrderingRolls() { long ts = System.currentTimeMillis(); UUID first = newTimeUUID( ts, 0 ); assertEquals( ts, getTimestampInMillis( first ) ); UUID second = newTimeUUID( ts, 10001 ); assertEquals( ts + 1, getTimestampInMillis( second ) ); }
long start_ts_shard = roundLong( getTimestampInMillis( start ), QUEUE_SHARD_INTERVAL ); long finish_ts_shard = roundLong( getTimestampInMillis( finish_uuid ), QUEUE_SHARD_INTERVAL );
@Override public void setProperty( EntityRef entityRef, String propertyName, Object propertyValue, boolean override ) throws Exception { if ( ( propertyValue instanceof String ) && ( ( String ) propertyValue ).equals( "" ) ) { propertyValue = null; } Entity entity = get( entityRef ); propertyValue = Schema.getDefaultSchema().validateEntityPropertyValue( entity.getType(), propertyName, propertyValue ); entity.setProperty( propertyName, propertyValue ); entity.setProperty( PROPERTY_MODIFIED, UUIDUtils.getTimestampInMillis( UUIDUtils.newTimeUUID() ) ); update(entity); }
@Test public void timeUUIDOrdering() { int count = 10000; long ts = System.currentTimeMillis(); List<UUID> uuids = new ArrayList<UUID>( count ); logger.info("Generating " + count + " UUIDs..."); for ( int i = 0; i < count; i++ ) { UUID uuid = newTimeUUID( ts, i ); uuids.add( uuid ); assertEquals( "Incorrect UUID timestamp value", ts, getTimestampInMillis( uuid ) ); if ( i % 1000 == 0 ) { logger.info("timeUUIDOrdering processed " + i); } } 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()); }
properties.put(PROPERTY_MODIFIED, UUIDUtils.getTimestampInMillis(UUIDUtils.newTimeUUID()));
long start_ts_shard = roundLong( getTimestampInMillis( start_uuid ), QUEUE_SHARD_INTERVAL ); long finish_ts_shard = roundLong( getTimestampInMillis( finish_uuid ), QUEUE_SHARD_INTERVAL );