Java 类org.apache.hadoop.util.Timer 实例源码

项目:aliyun-oss-hadoop-fs    文件:TestAccessTokenTimer.java   
@Test
public void shouldRefreshIsCorrect() {
  Timer mockTimer = mock(Timer.class);
  when(mockTimer.now())
      .thenReturn(500l)
      .thenReturn(1000000l + 500l);

  AccessTokenTimer timer = new AccessTokenTimer(mockTimer);

  timer.setExpiresInMSSinceEpoch("1000000");

  assertFalse(timer.shouldRefresh());
  assertTrue(timer.shouldRefresh());

  verify(mockTimer, times(2)).now();
}
项目:hadoop-oss    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:hadoop    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  staticUserToGroupsMap = parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:aliyun-oss-hadoop-fs    文件:TestAccessTokenTimer.java   
@Test
public void expireConversionWorks() {
  Timer mockTimer = mock(Timer.class);
  when(mockTimer.now())
      .thenReturn(5l);

  AccessTokenTimer timer = new AccessTokenTimer(mockTimer);

  timer.setExpiresIn("3");
  assertEquals(3005, timer.getNextRefreshMSSinceEpoch());

  assertTrue(timer.shouldRefresh());
}
项目:aliyun-oss-hadoop-fs    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:big-c    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:hadoop-2.6.0-cdh5.4.3    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:hadoop-oss    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:hadoop-oss    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}
项目:hadoop    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:hadoop    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}
项目:aliyun-oss-hadoop-fs    文件:ConfRefreshTokenBasedAccessTokenProvider.java   
public ConfRefreshTokenBasedAccessTokenProvider(Timer timer) {
  this.accessTokenTimer = new AccessTokenTimer(timer);
}
项目:aliyun-oss-hadoop-fs    文件:ConfCredentialBasedAccessTokenProvider.java   
public ConfCredentialBasedAccessTokenProvider(Timer timer) {
  super(timer);
}
项目:aliyun-oss-hadoop-fs    文件:CredentialBasedAccessTokenProvider.java   
CredentialBasedAccessTokenProvider(Timer timer) {
  this.timer = new AccessTokenTimer(timer);
}
项目:aliyun-oss-hadoop-fs    文件:AccessTokenTimer.java   
public AccessTokenTimer() {
  this(new Timer());
}
项目:aliyun-oss-hadoop-fs    文件:AccessTokenTimer.java   
/**
 *
 * @param timer Timer instance for unit testing
 */
public AccessTokenTimer(Timer timer) {
  this.timer = timer;
  this.nextRefreshMSSinceEpoch = 0;
}
项目:aliyun-oss-hadoop-fs    文件:AccessTokenTimer.java   
/**
 * The expires_in param from OAuth is in seconds-from-now.  Convert to
 * milliseconds-from-epoch
 */
static Long convertExpiresIn(Timer timer, String expiresInSecs) {
  long expiresSecs = Long.parseLong(expiresInSecs);
  long expiresMs = expiresSecs * 1000;
  return timer.now() + expiresMs;
}
项目:aliyun-oss-hadoop-fs    文件:TestRefreshTokenTimeBasedTokenRefresher.java   
@Test
public void refreshUrlIsCorrect() throws IOException {
  final int PORT = 7552;
  final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";

  long tokenExpires = 0;

  Configuration conf = buildConf("refresh token key",
      Long.toString(tokenExpires),
      "joebob",
      REFRESH_ADDRESS);

  Timer mockTimer = mock(Timer.class);
  when(mockTimer.now()).thenReturn(tokenExpires + 1000l);

  AccessTokenProvider tokenProvider =
      new ConfRefreshTokenBasedAccessTokenProvider(mockTimer);
  tokenProvider.setConf(conf);

  // Build mock server to receive refresh request

  ClientAndServer mockServer  = startClientAndServer(PORT);

  HttpRequest expectedRequest = request()
      .withMethod("POST")
      .withPath("/refresh")
      // Note, OkHttp does not sort the param values, so we need to
      // do it ourselves via the ordering provided to ParameterBody...
      .withBody(
          ParameterBody.params(
              Parameter.param(CLIENT_ID, "joebob"),
              Parameter.param(GRANT_TYPE, REFRESH_TOKEN),
              Parameter.param(REFRESH_TOKEN, "refresh token key")));

  MockServerClient mockServerClient = new MockServerClient("localhost", PORT);

  // https://tools.ietf.org/html/rfc6749#section-5.1
  Map<String, Object> map = new TreeMap<>();

  map.put(EXPIRES_IN, "0987654321");
  map.put(TOKEN_TYPE, BEARER);
  map.put(ACCESS_TOKEN, "new access token");

  ObjectMapper mapper = new ObjectMapper();

  HttpResponse resp = response()
      .withStatusCode(HttpStatus.SC_OK)
      .withHeaders(
          CONTENT_TYPE_APPLICATION_JSON
      )
      .withBody(mapper.writeValueAsString(map));

  mockServerClient
      .when(expectedRequest, exactly(1))
      .respond(resp);

  assertEquals("new access token", tokenProvider.getAccessToken());

  mockServerClient.verify(expectedRequest);

  mockServerClient.clear(expectedRequest);
  mockServer.stop();
}
项目:aliyun-oss-hadoop-fs    文件:TestClientCredentialTimeBasedTokenRefresher.java   
@Test
public void refreshUrlIsCorrect() throws IOException {
  final int PORT = ServerSocketUtil.getPort(0, 20);
  final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";

  long tokenExpires = 0;

  Configuration conf = buildConf("myreallycoolcredential",
      Long.toString(tokenExpires),
      CLIENT_ID_FOR_TESTING,
      REFRESH_ADDRESS);

  Timer mockTimer = mock(Timer.class);
  when(mockTimer.now()).thenReturn(tokenExpires + 1000l);

  AccessTokenProvider credProvider =
      new ConfCredentialBasedAccessTokenProvider(mockTimer);
  credProvider.setConf(conf);

  // Build mock server to receive refresh request
  ClientAndServer mockServer  = startClientAndServer(PORT);

  HttpRequest expectedRequest = request()
      .withMethod("POST")
      .withPath("/refresh")
      .withBody( 
      // Note, OkHttp does not sort the param values, so we need to do
      // it ourselves via the ordering provided to ParameterBody...
          ParameterBody.params(
              Parameter.param(CLIENT_SECRET, "myreallycoolcredential"),
              Parameter.param(GRANT_TYPE, CLIENT_CREDENTIALS),
              Parameter.param(CLIENT_ID, CLIENT_ID_FOR_TESTING)
              ));

  MockServerClient mockServerClient = new MockServerClient("localhost", PORT);

  // https://tools.ietf.org/html/rfc6749#section-5.1
  Map<String, Object> map = new TreeMap<>();

  map.put(EXPIRES_IN, "0987654321");
  map.put(TOKEN_TYPE, "bearer");
  map.put(ACCESS_TOKEN, "new access token");

  ObjectMapper mapper = new ObjectMapper();

  HttpResponse resp = response()
      .withStatusCode(HttpStatus.SC_OK)
      .withHeaders(
          CONTENT_TYPE_APPLICATION_JSON
      )
      .withBody(mapper.writeValueAsString(map));

  mockServerClient
      .when(expectedRequest, exactly(1))
      .respond(resp);

  assertEquals("new access token", credProvider.getAccessToken());

  mockServerClient.verify(expectedRequest);

  mockServerClient.clear(expectedRequest);
  mockServer.stop();
}
项目:aliyun-oss-hadoop-fs    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:aliyun-oss-hadoop-fs    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}
项目:big-c    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:big-c    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}
项目:hadoop-2.6.0-cdh5.4.3    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:hadoop-2.6.0-cdh5.4.3    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}
项目:hops    文件:Groups.java   
public Groups(Configuration conf) {
  this(conf, new Timer());
}
项目:hops    文件:Groups.java   
public Groups(Configuration conf, final Timer timer) {
  impl = 
    ReflectionUtils.newInstance(
        conf.getClass(CommonConfigurationKeys.HADOOP_SECURITY_GROUP_MAPPING, 
                      ShellBasedUnixGroupsMapping.class, 
                      GroupMappingServiceProvider.class), 
        conf);

  cacheTimeout = 
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS, 
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_SECS_DEFAULT) * 1000;
  negativeCacheTimeout =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS,
        CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_NEGATIVE_CACHE_SECS_DEFAULT) * 1000;
  warningDeltaMs =
    conf.getLong(CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS,
      CommonConfigurationKeys.HADOOP_SECURITY_GROUPS_CACHE_WARN_AFTER_MS_DEFAULT);
  reloadGroupsInBackground =
    conf.getBoolean(
        CommonConfigurationKeys.
            HADOOP_SECURITY_GROUPS_CACHE_BACKGROUND_RELOAD,
        CommonConfigurationKeys.
            HADOOP_SECURITY_GROUPS_CACHE_BACKGROUND_RELOAD_DEFAULT);
  reloadGroupsThreadCount  =
    conf.getInt(
        CommonConfigurationKeys.
            HADOOP_SECURITY_GROUPS_CACHE_BACKGROUND_RELOAD_THREADS,
        CommonConfigurationKeys.
            HADOOP_SECURITY_GROUPS_CACHE_BACKGROUND_RELOAD_THREADS_DEFAULT);
  parseStaticMapping(conf);

  this.timer = timer;
  this.cache = CacheBuilder.newBuilder()
    .refreshAfterWrite(cacheTimeout, TimeUnit.MILLISECONDS)
    .ticker(new TimerToTickerAdapter(timer))
    .expireAfterWrite(10 * cacheTimeout, TimeUnit.MILLISECONDS)
    .build(new GroupCacheLoader());

  if(negativeCacheTimeout > 0) {
    Cache<String, Boolean> tempMap = CacheBuilder.newBuilder()
      .expireAfterWrite(negativeCacheTimeout, TimeUnit.MILLISECONDS)
      .ticker(new TimerToTickerAdapter(timer))
      .build();
    negativeCache = Collections.newSetFromMap(tempMap.asMap());
  }

  if(LOG.isDebugEnabled())
    LOG.debug("Group mapping impl=" + impl.getClass().getName() + 
        "; cacheTimeout=" + cacheTimeout + "; warningDeltaMs=" +
        warningDeltaMs);
}
项目:hops    文件:Groups.java   
public TimerToTickerAdapter(Timer timer) {
  this.timer = timer;
}