Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.execution;
 
 
 
 
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 
 public class QueryIdGenerator
 {
     // a-z, 0-9, except: l, o, 0, 1
     private static final char[] BASE_32 = {
             'a''b''c''d''e''f''g''h',
             'i''j''k''m''n''p''q''r',
             's''t''u''v''w''x''y''z',
             '2''3''4''5''6''7''8''9'};
 
     static {
         checkState(. == 32);
         checkState(ImmutableSet.copyOf(Chars.asList()).size() == 32);
     }
 
     private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormat.forPattern("YYYYMMdd_HHmmss").withZoneUTC();
     private static final long BASE_SYSTEM_TIME_MILLIS = System.currentTimeMillis();
     private static final long BASE_NANO_TIME = System.nanoTime();
 
     protected final String coordinatorId;
     @GuardedBy("this")
     private long lastTimeInDays;
     @GuardedBy("this")
     private long lastTimeInSeconds;
     @GuardedBy("this")
     private String lastTimestamp;
     @GuardedBy("this")
     private int counter;
 
     public QueryIdGenerator()
     {
         StringBuilder coordinatorId = new StringBuilder(5);
         for (int i = 0; i < 5; i++) {
             coordinatorId.append([ThreadLocalRandom.current().nextInt(32)]);
         }
         this. = coordinatorId.toString();
     }

    
Generate next queryId using the following format: YYYYMMdd_HHmmss_index_coordId

Index rolls at the start of every day or when it reaches 99,999, and the coordId is a randomly generated when this instance is created.

 
     public synchronized QueryId createNextQueryId()
     {
         // only generate 100,000 ids per day
         if ( > 99_999) {
             // wait for the second to rollover
             while (.toSeconds(nowInMillis()) == ) {
                 Uninterruptibles.sleepUninterruptibly(1, .);
             }
              = 0;
         }
 
         // if it has been a second since the last id was generated, generate a new timestamp
         long now = nowInMillis();
         if (.toSeconds(now) != ) {
             // generate new timestamp
              = .toSeconds(now);
              = .print(now);
 
             // if the day has rolled over, restart the counter
             if (.toDays(now) != ) {
                  = .toDays(now);
                  = 0;
             }
         }
        return new QueryId(String.format("%s_%05d_%s"++, ));
    }
    protected long nowInMillis()
    {
        // avoid problems with the clock moving backwards
    }
New to GrepCode? Check out our FAQ X